# DataLoader gives double instead of float?

**URL:** <https://discuss.pytorch.org/t/dataloader-gives-double-instead-of-float/717>\
**Category:** Uncategorized\
**Created:** [February 25, 2017, 6:06am UTC](https://discuss.pytorch.org/t/dataloader-gives-double-instead-of-float/717 "2017-02-25T06:06:12Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Veril](https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/v/77aa72/32.png) [@Veril](https://discuss.pytorch.org/u/Veril)\
**Post date:** [February 25, 2017, 6:06am UTC](https://discuss.pytorch.org/t/dataloader-gives-double-instead-of-float/717/1 "2017-02-25T06:06:13Z")

</div>

```
X_train = torch.from_numpy(X_train)
Y_train = torch.from_numpy(Y_train)
X_test = torch.from_numpy(X_test)
Y_test = torch.from_numpy(Y_test)

[all dtypes torch.FloatTensor confirmed]

train = torch.utils.data.TensorDataset(X_train, Y_train)
trainloader = torch.utils.data.DataLoader(train, batch_size=BATCH_SIZE, shuffle=True)

train_iter = iter(trainloader)
data = train_iter.next()
x, y = data		
print y

```

And I get torch.DoubleTensor

x is torch.FloatTensor

---

<div class="post-metadata">

**Author:** ![apaszke](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/apaszke/32/21_2.png) [@apaszke](https://discuss.pytorch.org/u/apaszke)\
**Post date:** [February 25, 2017, 9:49am UTC](https://discuss.pytorch.org/t/dataloader-gives-double-instead-of-float/717/2 "2017-02-25T09:49:57Z")

</div>

I can’t reproduce your issue. To resolve a problem we need a self-contained snippet that we can run.

---

<div class="post-metadata">

**Author:** ![Veril](https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/v/77aa72/32.png) [@Veril](https://discuss.pytorch.org/u/Veril)\
**Post date:** [February 25, 2017, 3:24pm UTC](https://discuss.pytorch.org/t/dataloader-gives-double-instead-of-float/717/3 "2017-02-25T15:24:20Z")

</div>

The following reproduces this.

```
import numpy as np

import torch
import torch.utils.data

X_train = np.random.uniform(-1, 1, (1000,11)).astype(np.float32)
Y_train = np.hstack((np.zeros(500), np.ones(500))).astype(np.float32)

X_train = torch.from_numpy(X_train)
Y_train = torch.from_numpy(Y_train)

print X_train
print Y_train

train = torch.utils.data.TensorDataset(X_train, Y_train)
trainloader = torch.utils.data.DataLoader(train, batch_size=128, shuffle=True)

train_iter = iter(trainloader)
data = train_iter.next()
x, y = data		
print x
print y

```

[torch.FloatTensor of size 1000x11]  
[torch.FloatTensor of size 1000]  
[torch.FloatTensor of size 128x11]  
[torch.DoubleTensor of size 128]

Maybe this is intentional but I can’t figure out why make a float into a double in that context, and why only the second one.

---

<div class="post-metadata">

**Author:** ![albanD](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/alband/32/215_2.png) [@albanD](https://discuss.pytorch.org/u/albanD)\
**Post date:** [February 25, 2017, 3:50pm UTC](https://discuss.pytorch.org/t/dataloader-gives-double-instead-of-float/717/4 "2017-02-25T15:50:00Z")

</div>

Hi,  
The problem comes from the fact that your `Y_train` is a 1D tensor and thus when the batch is created, its stacking plain numbers (creating a double tensor to have best precision)  
Reshaping your `Y_train` to a 2D tensor solved the problem:

```auto
Y_train = torch.from_numpy(Y_train).view(-1, 1)

```

@apaszke changing [this](https://github.com/pytorch/pytorch/blob/master/torch/utils/data/dataset.py#L34) line with:

```auto
 return self.data_tensor.narrow(0, index, 1), self.target_tensor.narrow(0, index, 1)

```

should solve the issue by always returning Tensor and not numbers. Would this break something I’m not aware of? (Let me know if you want me to send a PR for that)

---

<div class="post-metadata">

**Author:** ![Veril](https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/v/77aa72/32.png) [@Veril](https://discuss.pytorch.org/u/Veril)\
**Post date:** [February 25, 2017, 3:55pm UTC](https://discuss.pytorch.org/t/dataloader-gives-double-instead-of-float/717/5 "2017-02-25T15:55:15Z")

</div>

copy\_ doesn’t care if it gets data in shape (batch) or (batch,) ? A quick test shows no changes in loss behavior.

```
input = Variable(torch.FloatTensor(BATCH_SIZE, dims).cuda())
label = Variable(torch.FloatTensor(BATCH_SIZE).cuda())

x, y = train_iter.next()
input.data.resize_(x.size()).copy_(x)
label.data.resize_(x.size(0)).copy_(y)
```

---

<div class="post-metadata">

**Author:** ![albanD](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/alband/32/215_2.png) [@albanD](https://discuss.pytorch.org/u/albanD)\
**Post date:** [February 25, 2017, 4:00pm UTC](https://discuss.pytorch.org/t/dataloader-gives-double-instead-of-float/717/6 "2017-02-25T16:00:36Z")

</div>

No `copy_` won’t care: [http://pytorch.org/docs/tensors.html#torch.Tensor.copy\_](http://pytorch.org/docs/tensors.html#torch.Tensor.copy_) it juste requires the two tensor to have the same number of elements !
