RuntimeError: Expected object of type torch.LongTensor but found type torch.FloatTensor

fc = nn.Linear(128, 128)
lin = fc1(torch.tensor([16,128]))

Can anyone please tell me why this error appears?.. it works when editing the last line to be
lin = fc1(torch.Tensor(16,128)) … what is the difference between torch.tensor() and torch.Tensor()

Thanks a lot

In the first example you are creating a tensor containing two values: 16 and 128.
As the type is inferred from the input, it’s a torch.LongTensor, which yields the error message (besides having a wrong shape, too).
In your second example you are creating an un-initialized tensor of shape [16, 128].
Your code won’t throw an error, but since the tensor is uninitialized, you might get strange output values.

I assume you would like to create a random tensor of shape [16, 128].
You can do it with x = torch.randn(16, 128).

1 Like

@ptrblck … Thanks for the clarification i missed… but can you tell me here why the error still exists:
loss = criterion(output, target)
although output shape and target are the same (torch.Size([16, 1]))

What kind of error do you get and what criterion are you using?

This is the error:
RuntimeError: Expected object of type torch.cuda.FloatTensor but found type torch.cuda.DoubleTensor for argument #2 ‘target’

and the criterion is:
criterion = nn.MSELoss(size_average=True).cuda()

and those are my output shapes and values:
output tensor([[ 0.4398],
[ 0.4516],
[ 0.4457],
[ 0.4424],
[ 0.4521],
[ 0.4275],
[ 0.4483],
[ 0.4403],
[ 0.4385],
[ 0.4634],
[ 0.4566],
[ 0.4427],
[ 0.4404],
[ 0.4565],
[ 0.4455],
[ 0.4476]], device=‘cuda:0’)
target tensor(1.00000e-02 *
[[ 1.7727],
[ 4.0564],
[ 4.9746],
[ 4.9140],
[ 5.3685],
[ 5.9247],
[ 5.2389],
[ 5.3715],
[ 5.4332],
[ 5.2338],
[ 5.4343],
[ 3.9557],
[ 4.1932],
[ 4.2754],
[ 3.8148],
[ 3.9546]], dtype=torch.float64, device=‘cuda:0’)

The types of output and target should be the same.
Try to cast your target to float32 using target = target.float().

1 Like

That’s really helped! … Thank you a lot, but can you tell me how output and targets did not match in type although both of them is float and how can i avoid it again because i believe that part of this issue happened while importing my dataset using dataloader.

this is the class for importing my dataframe:

class My_dataset(Dataset):

def __init__(self, csv_path):
    self.data_info = pd.read_csv(csv_path,header=None) = np.asarray(self.data_info.iloc[:,1:7], dtype=np.float64)
    self.label = np.asarray(self.data_info.iloc[:,7])
    self.data_len = len(self.data_info.index)
def __getitem__(self, index):
    single_row =[index]
    row = torch.FloatTensor(single_row)
    target = torch.from_numpy(np.array(self.label[index]) )
    return (row, target)
def __len__(self):
    return self.data_len

and here’s a snippet of the main function while iterating over my_dataset:

for data, target in train_loader:

    target = target.float()
    target = target.unsqueeze_(0)
    data = Variable(data.permute(1, 0)).contiguous()
    target = Variable(target.permute(1,0))

where do you think this error aroused from?

If I’m not mistaken np.asarray casts the data usually to np.float64. Also pd.read_csv might already return float64.
You also cast your to float64 which is unnecessary as you create torch.FloatTensors in __getitem__.

Try to load your data as: = np.asarray(self.data_info.iloc[:, 1:7], dtype=np.float32)
self.label = np.asarray(self.data_info.iloc[:,7], dtype=np.float32)

RuntimeError: Expected object of type torch.FloatTensor but found type torch.LongTensor

def TernarizeWeights(self):
for index in range(self.num_of_params):
self.target_modules[index].data = self.Ternarize(self.target_modules[index].data)

def Ternarize(self,tensor):
    tensor = tensor.cpu()
    output = torch.zeros(tensor.size())
    delta = self.Delta(tensor)
    alpha = self.Alpha(tensor,delta)
    for i in range(tensor.size()[0]):
        for w in tensor[i].view(1,-1):
            pos_one = (w > delta[i]).type(torch.FloatTensor)
            neg_one = torch.mul((w < -delta[i]).type(torch.FloatTensor),-1)
        out = torch.add(pos_one,neg_one).view(tensor.size()[1:])
        output[i] = torch.add(output[i],torch.mul(out,alpha[i]))
    return output.cuda()

can anyone help me , how to solve this error? thanks in adavance