Skip to content

Commit 3664c6a

Browse files
committed
Fix tensor data elem type conversion in book (#2211)
1 parent 9a0b8ed commit 3664c6a

File tree

2 files changed

+9
-8
lines changed

2 files changed

+9
-8
lines changed

burn-book/src/basic-workflow/data.md

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,8 @@ impl<B: Backend> Batcher<MnistItem, MnistBatch<B>> for MnistBatcher<B> {
6868
fn batch(&self, items: Vec<MnistItem>) -> MnistBatch<B> {
6969
let images = items
7070
.iter()
71-
.map(|item| TensorData::from(item.image))
72-
.map(|data| Tensor::<B, 2>::from_data(data.convert(), &self.device))
71+
.map(|item| TensorData::from(item.image).convert::<B::FloatElem>())
72+
.map(|data| Tensor::<B, 2>::from_data(data, &self.device))
7373
.map(|tensor| tensor.reshape([1, 28, 28]))
7474
// Normalize: make between [0,1] and make the mean=0 and std=1
7575
// values mean=0.1307,std=0.3081 are from the PyTorch MNIST example
@@ -119,8 +119,8 @@ images.
119119
```rust, ignore
120120
let images = items // take items Vec<MnistItem>
121121
.iter() // create an iterator over it
122-
.map(|item| TensorData::from(item.image)) // for each item, convert the image to float32 data struct
123-
.map(|data| Tensor::<B, 2>::from_data(data.convert(), &self.device)) // for each data struct, create a tensor on the device
122+
.map(|item| TensorData::from(item.image).convert::<B::FloatElem>()) // for each item, convert the image to float data struct
123+
.map(|data| Tensor::<B, 2>::from_data(data, &self.device)) // for each data struct, create a tensor on the device
124124
.map(|tensor| tensor.reshape([1, 28, 28])) // for each tensor, reshape to the image dimensions [C, H, W]
125125
.map(|tensor| ((tensor / 255) - 0.1307) / 0.3081) // for each image tensor, apply normalization
126126
.collect(); // consume the resulting iterator & collect the values into a new vector
@@ -138,5 +138,6 @@ a targets tensor that contains the indexes of the correct digit class. The first
138138
the image array into a `TensorData` struct. Burn provides the `TensorData` struct to encapsulate
139139
tensor storage information without being specific for a backend. When creating a tensor from data,
140140
we often need to convert the data precision to the current backend in use. This can be done with the
141-
`.convert()` method. While importing the `burn::tensor::ElementConversion` trait, you can call
142-
`.elem()` on a specific number to convert it to the current backend element type in use.
141+
`.convert()` method (in this example, the data is converted backend's float element type
142+
`B::FloatElem`). While importing the `burn::tensor::ElementConversion` trait, you can call `.elem()`
143+
on a specific number to convert it to the current backend element type in use.

examples/guide/src/data.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@ impl<B: Backend> Batcher<MnistItem, MnistBatch<B>> for MnistBatcher<B> {
2424
fn batch(&self, items: Vec<MnistItem>) -> MnistBatch<B> {
2525
let images = items
2626
.iter()
27-
.map(|item| TensorData::from(item.image))
28-
.map(|data| Tensor::<B, 2>::from_data(data.convert::<B::FloatElem>(), &self.device))
27+
.map(|item| TensorData::from(item.image).convert::<B::FloatElem>())
28+
.map(|data| Tensor::<B, 2>::from_data(data, &self.device))
2929
.map(|tensor| tensor.reshape([1, 28, 28]))
3030
// normalize: make between [0,1] and make the mean = 0 and std = 1
3131
// values mean=0.1307,std=0.3081 were copied from Pytorch Mist Example

0 commit comments

Comments
 (0)