How to create/modify values of a Tensor of shape [1,32,32,1] (4-dimensional array) ?

151 views
Skip to first unread message

Kahlil Abreu

unread,
Jan 12, 2020, 1:24:46 PM1/12/20
to Rust for TensorFlow
I have the following code. I have tried a lot to try and modify the created Tensor, but it gives me the error (pasted below)

What strikes me as odd is that it is able to create the Tensor and (default behaviour) initialize all the values of the Tensor to 0, but if I want to modify the Tensor (OR Create a Tensor with some values other than 0), it does not allow me to do that.

```
fn getFakeImgArray () -> [[[ [f32;1]; 32]; 32];1] {...}

let mut fakeImgTensor = Tensor::new(&[1,32,32,1]);
fakeImgTensor = fakeImgTensor.with_values(&[getFakeImgArray()]).unwrap();
```

Error message :

```
error[E0277]: the trait bound `[[[[f32; 1]; 32]; 32]; 1]: tensorflow::TensorType` is not satisfied
   --> src/main.rs:173:47
    |
173 |     fakeImgTensor = fakeImgTensor.with_values(&[getFakeImgArray()]).unwrap();
    |                                               ^^^^^^^^^^^^^^^^^^^^ the trait `tensorflow::TensorType` is not implemented for `[[[[f32; 1]; 32]; 32]; 1]`
    |
    = note: required by `tensorflow::Tensor::<T>::new`
```

David Holtz

unread,
Jan 12, 2020, 8:39:22 PM1/12/20
to Kahlil Abreu, Rust for TensorFlow
Hey Kahlil,

I am no expert but I think that the issue is due to the shape of the slice that is output from "getFakeImgArray" 

When I've used the Rust Tensorflow API, I passed a flattened version of the array to the Tensor "with_values" function and created the expected value-filled Tensor. 

In order to best explain, I've made a small Github repo that has an example of creating and populating a Tensor in Rust, and then a comparable Numpy array in Python. The README has a better description: https://github.com/drbh/rs-tf-tensors

Hope this helps! Good luck

--
You received this message because you are subscribed to the Google Groups "Rust for TensorFlow" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rust+uns...@tensorflow.org.
To view this discussion on the web visit https://groups.google.com/a/tensorflow.org/d/msgid/rust/e003118d-3626-4a75-af0f-e66b0ae98984%40tensorflow.org.


--
David Holtz

Daniël de Kok

unread,
Jan 13, 2020, 2:27:56 AM1/13/20
to David Holtz, Kahlil Abreu, Rust for TensorFlow
On Mon, Jan 13, 2020, at 02:39, David Holtz wrote:
> When I've used the Rust Tensorflow API, I passed a flattened version
> of the array to the Tensor "with_values" function and created the
> expected value-filled Tensor.

Also, if you find the Tensor API too limiting. I have made a small crate
that provides a wrapper for Tensor, that exposes it as an ndarray
ArrayView:

https://crates.io/crates/ndarray-tensorflow
https://docs.rs/ndarray-tensorflow/0.2.0/ndarray_tensorflow/struct.NdTensor.html

Kind regards,
Daniël

Kahlil Abreu

unread,
Jan 13, 2020, 6:17:52 AM1/13/20
to Rust for TensorFlow, kahlil...@holmusk.com
Hey David

It was really kind of you to take the time to create a repo and explain the solution so well!
It solved my problem and after struggling a bit with the padding part of things, I managed to get my inference to work!

Thank you so much! :)

Sincerely,
Kahlil

On Monday, January 13, 2020 at 9:39:22 AM UTC+8, David Holtz wrote:
Hey Kahlil,

I am no expert but I think that the issue is due to the shape of the slice that is output from "getFakeImgArray" 

When I've used the Rust Tensorflow API, I passed a flattened version of the array to the Tensor "with_values" function and created the expected value-filled Tensor. 

In order to best explain, I've made a small Github repo that has an example of creating and populating a Tensor in Rust, and then a comparable Numpy array in Python. The README has a better description: https://github.com/drbh/rs-tf-tensors

Hope this helps! Good luck

On Sun, Jan 12, 2020 at 1:24 PM Kahlil Abreu <kahli...@holmusk.com> wrote:
I have the following code. I have tried a lot to try and modify the created Tensor, but it gives me the error (pasted below)

What strikes me as odd is that it is able to create the Tensor and (default behaviour) initialize all the values of the Tensor to 0, but if I want to modify the Tensor (OR Create a Tensor with some values other than 0), it does not allow me to do that.

```
fn getFakeImgArray () -> [[[ [f32;1]; 32]; 32];1] {...}

let mut fakeImgTensor = Tensor::new(&[1,32,32,1]);
fakeImgTensor = fakeImgTensor.with_values(&[getFakeImgArray()]).unwrap();
```

Error message :

```
error[E0277]: the trait bound `[[[[f32; 1]; 32]; 32]; 1]: tensorflow::TensorType` is not satisfied
   --> src/main.rs:173:47
    |
173 |     fakeImgTensor = fakeImgTensor.with_values(&[getFakeImgArray()]).unwrap();
    |                                               ^^^^^^^^^^^^^^^^^^^^ the trait `tensorflow::TensorType` is not implemented for `[[[[f32; 1]; 32]; 32]; 1]`
    |
    = note: required by `tensorflow::Tensor::<T>::new`
```

--
You received this message because you are subscribed to the Google Groups "Rust for TensorFlow" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ru...@tensorflow.org.


--
David Holtz

Kahlil Abreu

unread,
Jan 13, 2020, 6:18:33 AM1/13/20
to Rust for TensorFlow, david.ric...@gmail.com, kahlil...@holmusk.com
Thanks Daniel, I'll check it out :)
Reply all
Reply to author
Forward
0 new messages