Runtime Error : both arguments to matmul need to be at least 1d but they are 0d and 2d

Viewed 2421

This is the code I have written, I have tried modifying here and there and have always gotten the same error. As I am a beginner to PyTorch, I am just trying things out to see if machine learning will work on a linear dataset. So, with random, I initialized a dataset. Then, made a single linear neural network. Then, train the neural network on the linear data. Nevertheless, I have an error stating that matmul expects a 1d data, but then my data is at least 1d but is read as 0d instead.

import torch 
import torch.nn as nn
import torch.nn.functional as F
import torch.optim as optim
import random

class LinearNetwork(nn.Module):
    def __init__(self):
        super(LinearNetwork, self).__init__()
        self.fc1 = nn.Linear(1, 1)

    def forward(self, input):
        x = self.fc1(input)
        return x; 

#Generate a Dataset to train on. Just a linear function, and we wanna see if this thing is working 
DataSet = []
grad = random.randint(0,100)
const = random.randint(0,100)
for i in range(0, 1000):
    DataSet.append([i, grad*i + const])

DataSet = torch.tensor(DataSet,dtype = torch.double, requires_grad = True)

#Declare a Linear Network
Net = LinearNetwork()
criterion = nn.MSELoss()
optimizer = optim.SGD(Net.parameters(), lr = 0.01)
print(DataSet)
#Train Model 
for i, data in enumerate(DataSet, 0):
    Input, Target = data
    optimizer.zero_grad()
    Output = Net(Input)
    loss = criterion(Output, Target)
    loss.backward()
    optimizer.step()

print("Gradient of the Function is : " + str(grad))
print("Constant Value of the Function is : " + str(const))
print("Learned Gradient and Constant : " + list(Net.parameters()))
1 Answers

There are two errors in your code, first regarding shapes, second regarding dtypes. BTW. Please use snake_case for variables (e.g. my_dataset, net) and CamelCase for classes as it's a common Python convention.

Shape error

This one lies here:

for i, data in enumerate(DataSet, 0):
    input, target = data
    optimizer.zero_grad()
    output = net(input)
    loss = criterion(output, target)
    loss.backward()
    optimizer.step()

When you print input.shape you get torch.Size([]) which is a 0d tensor. Matrix multiplication needs 1d tensor so you should unsqueeze it so it has this dimension. Change above output = net(input) to:

output = net(input.unsqueeze(dim=0))

dtype error

Here:

DataSet = torch.tensor(DataSet,dtype = torch.double, requires_grad = True)

You create tensor of torch.double type while your neural network is (by default) of type torch.float. The latter is usually used as that's enough numerical precision and saves memory. So instead of the above you should do:

DataSet = torch.tensor(DataSet,dtype = torch.float, requires_grad = True)

Shape again

Coming back to shapes, neural networks should operate on at least two dimensions: (batch, features). In your case batch=1 and features=1, so it should be unsqueezed once more (and i advise you to try your code using batches).

Related