RuntimeError: Expected object of type torch.cuda.LongTensor but found type torch.cuda.FloatTensor for argument #2 'mat2'

I am facing this error:

python classfn3daj3.py
118060
tensor([[6.0167e+05, 4.4948e+06, 3.9800e+00,  ..., 0.0000e+00, 0.0000e+00,
         0.0000e+00],
        [6.0591e+05, 4.4951e+06, 1.2120e+01,  ..., 0.0000e+00, 0.0000e+00,
         0.0000e+00],
        [5.9716e+05, 4.4925e+06, 5.9400e+00,  ..., 0.0000e+00, 0.0000e+00,
         0.0000e+00],
        ...,
        [6.0279e+05, 4.5158e+06, 2.0560e+01,  ..., 0.0000e+00, 0.0000e+00,
         0.0000e+00],
        [6.0283e+05, 4.5159e+06, 2.5710e+01,  ..., 0.0000e+00, 0.0000e+00,
         0.0000e+00],
        [5.8590e+05, 4.5146e+06, 2.6260e+01,  ..., 0.0000e+00, 0.0000e+00,
         0.0000e+00]])
torch.Size([118060, 7673])
-tensor label points**********************************-
tensor([0, 1, 0,  ..., 0, 0, 0])
size of tensor label torch.Size([118060])
**--------------------------------------
<torch.utils.data.dataloader.DataLoader object at 0x7f6d1da22c18>
--------------------------------------
/home/raman/anaconda2/envs/pyto3/lib/python3.6/site-packages/torch/cuda/__init__.py:116: UserWarning: 
    Found GPU1 NVS 315 which is of cuda capability 2.1.
    PyTorch no longer supports this GPU because it is too old.
    
  warnings.warn(old_gpu_warn % (d, name, major, capability[1]))
NeuralNet(
  (fc1): Linear(in_features=118060, out_features=10, bias=True)
  (relu): ReLU()
  (fc2): Linear(in_features=10, out_features=3, bias=True)
)
Traceback (most recent call last):
  File "classfn3daj3.py", line 117, in <module>
    outputs = model(dpoints).to(device)
  File "/home/raman/anaconda2/envs/pyto3/lib/python3.6/site-packages/torch/nn/modules/module.py", line 477, in __call__
    result = self.forward(*input, **kwargs)
  File "/home/raman/recon/nn_3d.py", line 14, in forward
    out = self.fc1(x)
  File "/home/raman/anaconda2/envs/pyto3/lib/python3.6/site-packages/torch/nn/modules/module.py", line 477, in __call__
    result = self.forward(*input, **kwargs)
  File "/home/raman/anaconda2/envs/pyto3/lib/python3.6/site-packages/torch/nn/modules/linear.py", line 55, in forward
    return F.linear(input, self.weight, self.bias)
  File "/home/raman/anaconda2/envs/pyto3/lib/python3.6/site-packages/torch/nn/functional.py", line 1026, in linear
    output = input.matmul(weight.t())
RuntimeError: Expected object of type torch.cuda.LongTensor but found type torch.cuda.FloatTensor for argument #2 'mat2'

My code is the following:

input_size = 7673
hidden_size = 10
num_classes = 3
num_epochs = 5
batch_size = 1
learning_rate = 0.01
torch.cuda.device(0)
def np_fill(data):
    lens = [len(data[i]) for i in range(0,len(data))]
    max_len = np.asarray(lens).max()
    # print(lens)

    for k in range(0,len(data)):
        num_append = max_len - len(data[k])
        to_append = np.ndarray.tolist(np.zeros(num_append))
        data[k].extend(to_append)
    return (data,max_len)


def pad(A, length):
    arr = np.zeros(length)
    arr[:len(A)] = A
    return arr

def to_tensor(data1,label1):
    
    points = data1.values
    labels = label1.values
    
    points=np.delete(points,(9989,16027,16688,18957,43657,48915,50115,57132, 71211,73804,93884,104406,116125),0);
    labels=np.delete(labels,(9989,16027,16688,18957,43657,48915,50115,57132, 71211,73804,93884,104406,116125),0);

    points_list = []

    for kk in range(0,points.shape[0]):
        points[kk] = points[kk].replace(',',' ')
        points[kk] = np.core.defchararray.split(points[kk]," ")
        points_list.append(np.ndarray.tolist(points[kk]))
    print(len(points_list))
    fpoints,maxl =  np_fill(points_list) 
    p_len = len(points_list)
    max_len = maxl
    fpoints =  np.asarray(fpoints)
    arpoints=fpoints.astype('float64')
    ten_p = []
    l = []
    ten_p = torch.from_numpy(arpoints).float()
    print(ten_p)
    l = ten_p.size()
    print(l)
    #ten_l = torch.from_numpy(labels).float()
    ten_l = torch.from_numpy(labels)
    print("-tensor label points**********************************-")
    print(ten_l)
    print("size of tensor label", ten_l.size())
    return ten_p,ten_l

device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')

class BDatasetFromCSV(Dataset):

    def __init__(self,csv_path):
        self.data = pd.read_csv(csv_path)
        data_labels = self.data.iloc[:, 0]
        data_points = self.data.iloc[:, 1]
        tens,lb = to_tensor(data_points,data_labels)
        self.points = tens
        self.labels = lb
        
    def __getitem__(self, index):
        d_labels = self.labels[index]
        d_points = self.points[index]
        return d_points, d_labels  
   
    def __len__(self):
        return len(self.data.index)

if __name__ == "__main__":
    #Load the training set
	custom_b_from_csv = BDatasetFromCSV('broof.csv')  
	#Create a loader for the training set
	b_dataset_loader = torch.utils.data.DataLoader(dataset= custom_b_from_csv,batch_size=10,shuffle=False)
	#total_step = len(b_dataset_loader)
	print("**--------------------------------------")
	print(b_dataset_loader)
	#print(total_step)
	print("--------------------------------------")
	#model = NeuralNet(input_size, hidden_size, num_classes)
	model = NeuralNet(input_size, hidden_size, num_classes).to(device)
	print(model)
	criterion = torch.nn.CrossEntropyLoss()
	#criterion = torch.nn.MSELoss()
	optimizer = torch.optim.SGD(model.parameters(), lr=0.01)
	#optimizer = torch.optim.Adam(model.parameters(), lr=learning_rate) 	
	for i, (d_labels,d_points) in enumerate(b_dataset_loader):
	   
	    dpoints = d_points.to(device)
	    #dlabels = Variable(d_labels).to(device)
	    dlabels = d_labels.to(device)
	    # Clear gradients
	    optimizer.zero_grad()
	    # Forward pass
	    outputs = model(dpoints).to(device)
        # Calculate loss
	    loss = criterion(outputs, dlabels)
	    # Backward pass
	    loss.backward()
	    # Update weights
	    optimizer.step()
	    break

Could you tell me the major mistake i have done inthis and hepl me to rectify it.

1 Like

Hi,

You can use 3 backticks ``` to format you code properly.

I can’t really read your code at the moment but I would say the problem is that your label tensor is of type float while losses likes CrossEntropyLoss expect a type long: It expect the index of the ground truth (not a one-hot encoding of it).

how can i convert float tensor into long as .long() doesnot work .

You should use .long() why doesn’t it work?

same error showing…
RuntimeError: Expected object of type torch.cuda.LongTensor but found type torch.cuda.FloatTensor for argument #2 ‘mat2’

How do you do this conversion?
Remember that it is not inplace, so you need to do dlabels = dlabels.long() or call your criterion as loss = criterion(outputs, dlabels.long())

2 Likes

i didnt get the idea of using backticks for formatting code. Could u tell me what exactly meant?

I edited your post adding them, you can check where I added ``` and the resulting formating :slight_smile:

are you sure . i couldnot find it in the edited code by u

You can’t see them in the displayed version, but if you try to edit the code again, you will see them.
Can you see the difference it makes on your original post?

Thanks albanD. i dont understand why giving dlabels = dlabels.long() is not resolving the issue

Hi,

Looking at the stack trace above, the problem seem to be that the input of your model is of type long while it expects a float. Are you sure dpoints is of type float? And that the input to your fc1 Module is of type float?

Yes, the input points to the model are of float tensor format, so the model will output also float value. Then regarding labels , i have converted to float tensor. i am not sure am doing the right thing. pls help

class NeuralNet(nn.Module):
def init(self, input_size, hidden_size, num_classes):
super(NeuralNet, self).init()
self.fc1 = nn.Linear(input_size, hidden_size)
self.relu = nn.ReLU()
self.fc2 = nn.Linear(hidden_size, num_classes)

def forward(self, x):
    out = self.fc1(x)
    out = self.relu(out)
    out = self.fc2(out)
    return out

In you NeuralNet, forward can you change it to:

def forward(self, x):
    print(x.type())
    out = self.fc1(x)
    out = self.relu(out)
    out = self.fc2(out)
    return out

i printed it …Type of data inside model torch.cuda.LongTensor.
should i convert it here itself ?

The problem is with dataloader. It is returing tensor([0] for the d_points. Why is it so? How can i correct it?

Now it is working to some extent. But loss calculated for only one iteration, dont know what is wrong here.

python classfn3daj3.py
15
[[6.01671520e+05 4.49484606e+06 3.98000000e+00 … 0.00000000e+00
0.00000000e+00 0.00000000e+00]
[6.05909290e+05 4.49509237e+06 1.21200000e+01 … 0.00000000e+00
0.00000000e+00 0.00000000e+00]
[5.97163600e+05 4.49254968e+06 5.94000000e+00 … 0.00000000e+00
0.00000000e+00 0.00000000e+00]

[6.01516880e+05 4.49428028e+06 8.25000000e+00 … 0.00000000e+00
0.00000000e+00 0.00000000e+00]
[5.92138930e+05 4.49045314e+06 5.42000000e+00 … 0.00000000e+00
0.00000000e+00 0.00000000e+00]
[6.04355840e+05 4.49610921e+06 1.15700000e+01 … 0.00000000e+00
0.00000000e+00 0.00000000e+00]]
tensor([[6.0167e+05, 4.4948e+06, 3.9800e+00, …, 0.0000e+00, 0.0000e+00,
0.0000e+00],
[6.0591e+05, 4.4951e+06, 1.2120e+01, …, 0.0000e+00, 0.0000e+00,
0.0000e+00],
[5.9716e+05, 4.4925e+06, 5.9400e+00, …, 0.0000e+00, 0.0000e+00,
0.0000e+00],
…,
[6.0152e+05, 4.4943e+06, 8.2500e+00, …, 0.0000e+00, 0.0000e+00,
0.0000e+00],
[5.9214e+05, 4.4905e+06, 5.4200e+00, …, 0.0000e+00, 0.0000e+00,
0.0000e+00],
[6.0436e+05, 4.4961e+06, 1.1570e+01, …, 0.0000e+00, 0.0000e+00,
0.0000e+00]])
size of tensor points torch.Size([15, 3480])
-tensor label points**********************************-
tensor([0, 1, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 1, 0, 1])
size of tensor label torch.Size([15])
the points after returing to class… tensor([[6.0167e+05, 4.4948e+06, 3.9800e+00, …, 0.0000e+00, 0.0000e+00,
0.0000e+00],
[6.0591e+05, 4.4951e+06, 1.2120e+01, …, 0.0000e+00, 0.0000e+00,
0.0000e+00],
[5.9716e+05, 4.4925e+06, 5.9400e+00, …, 0.0000e+00, 0.0000e+00,
0.0000e+00],
…,
[6.0152e+05, 4.4943e+06, 8.2500e+00, …, 0.0000e+00, 0.0000e+00,
0.0000e+00],
[5.9214e+05, 4.4905e+06, 5.4200e+00, …, 0.0000e+00, 0.0000e+00,
0.0000e+00],
[6.0436e+05, 4.4961e+06, 1.1570e+01, …, 0.0000e+00, 0.0000e+00,
0.0000e+00]])
Custom-b_from_csv… <main.BDatasetFromCSV object at 0x7f7d8f54fa90>
**--------------------------------------
<torch.utils.data.dataloader.DataLoader object at 0x7f7d7f7a9160>
size of dataloader… 15

/home/raman/anaconda2/envs/pyto3/lib/python3.6/site-packages/torch/cuda/init.py:116: UserWarning:
Found GPU1 NVS 315 which is of cuda capability 2.1.
PyTorch no longer supports this GPU because it is too old.

warnings.warn(old_gpu_warn % (d, name, major, capability[1]))
the model is … NeuralNet(
(fc1): Linear(in_features=3480, out_features=10, bias=True)
(relu): ReLU()
(fc2): Linear(in_features=10, out_features=3, bias=True)
)
the points after getting to class… tensor([6.0167e+05, 4.4948e+06, 3.9800e+00, …, 0.0000e+00, 0.0000e+00,
0.0000e+00])
i is … 0
The points tensor from dataloader… tensor([[6.0167e+05, 4.4948e+06, 3.9800e+00, …, 0.0000e+00, 0.0000e+00,
0.0000e+00]])
The labels tensor from dataloader … tensor([0])
Type of data before giving to model torch.cuda.FloatTensor
Type of data inside model torch.cuda.FloatTensor
Loss is … tensor(394650.5000, device=‘cuda:0’, grad_fn=)

It helped me. Thank you for saving my time