'datatype in pytorch makes my program fail

The following program is a subset of a classification program using Pytorch. For simplicity I have just used the initial lines of the csv file which I am going to attach below.

I have 2 questions I cannot answer:

  • in my implementation of the class Dataset(), using or not using the following code does not change the data type which is always tensor dtype=torch.float64

def init(self,X):

or

def init(self,X, transform=transform):

  • when doing the forward pass, unless I change the datatype as :

model = model.float()

log_ps = model(DATA.float())

I receive the following exception:

RuntimeError: expected scalar type Float but found Double

Here is the code:

import numpy as np
import torch
import torch.nn.functional as F
from torch import nn, optim
from torch.utils.data import Dataset
from torch.utils.data import DataLoader
from torchvision import transforms
import pandas as pd
X = np.asarray(pd.read_csv('./test.csv', header = None))
transform =transforms.Compose([
    transforms.ToTensor()])
#
class Wine(Dataset):
    def __init__(self,X):
#    def __init__(self,X, transform=transform):
        self.x = X[:, 1:]
        self.y = X[:, 0]
        self.num_samples = X.shape[0]

    def __len__(self):
        return(self.num_samples)

    def __getitem__(self, index):
        return (self.x[index] , self.y[index] )
#        
WineDS = Wine(X)
#
#convert data to a dataloder suitable for mini-batch processing when training
train_dataloader = DataLoader(WineDS, batch_size=2, shuffle=False)
#
train_features , train_labels = next(iter(train_dataloader))
print(train_features)
print('----')
print(train_labels)
#
class ThreeClassClassification(nn.Module):
    def __init__(self):
        super().__init__()
        self.fc1 = nn.Linear(13, 6)
        self.fc2 = nn.Linear(6,4)
    def forward(self, x):
        x = x.view(x.shape[0], -1)
        x = F.sigmoid(self.fc1(x))
        x = F.log_softmax(self.fc2(x), dim=1)
        return x

model = ThreeClassClassification()
loss = nn.NLLLoss()
optimizer = optim.Adam(model.parameters(), lr=0.001)
epochs = 2
model = model.float()

for i in range(epochs):
    running_loss = 0
    for DATA, LAB in train_dataloader:
        print(type(DATA))
        log_ps = model(DATA.float())
#        log_ps = model(DATA)
print (log_ps.shape,LAB.shape)

and here is the input file:

1,13.16,2.36,2.67,18.6,101,2.8,3.24,0.3,2.81,5.68,1.03,3.17,1185 1,14.37,1.95,2.5,16.8,113,3.85,3.49,0.24,2.18,7.8,0.86,3.45,1480 1,13.24,2.59,2.87,21,118,2.8,2.69,0.39,1.82,4.32,1.04,2.93,735



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source