Pytorch 1D tensors expected but got 2D tensors

Viewed 3519

I have been working on making neural network from scratch in python. The input tensor is of shape [400,3] and target_tensor has the shape [400]. I am getting error during taking derivative of weights. Below are functions

    def sigmoid(z):
           return 1 / (1 + torch.exp(-z))

    def nueral_net(data,weights,bias):
           return sigmoid( ( data @ weights ) + bias )

    def loss_function(prediction,actual,m):
           return (-1/m) * (torch.sum(actual * torch.log(prediction) + (1-actual) 
           * torch.log(1- prediction)))

    w = torch.randn(input_tensor.shape[1],1)
    b = torch.randn(1,1)

    predictions = nueral_net(input_tensor.float() , w, b) #Applying model
    loss = loss_function(predictions,target_tensor.unsqueeze(1),400)
    dw = (1/400) * torch.dot(input_tensor,(predictions - target_tensor).T)

Running this throws an error.

    RuntimeError                              Traceback (most recent call last)
    <ipython-input-26-632338d8fd16> in <module>
          1 predictions = nueral_net(input_tensor.float() , w, b) #Applying model
          2 loss = loss_function(predictions,target_tensor.unsqueeze(1),400)
    ----> 3 dw = (1/400) * torch.dot(input_tensor,(predictions - target_tensor).T)
          4 db = (1/400) * torch.sum(predictions - target_tensor)
          5 #m = input_tensor.shape[0]
    
    RuntimeError: 1D tensors expected, but got 2D and 2D tensor

2 Answers

To do matrix multiplation 2D tensor use:

torch.matmul(input, other, *, out=None) → Tensor

torch.dot is for 1D tensors

Check Official PyTorch Doc

If we see the doc of torch.dot :
torch.dot(input, other, *, out=None) → Tensor Computes the dot product of two 1D tensors.

NOTE : Unlike NumPy’s dot, torch.dot intentionally only supports computing the dot product of two 1D tensors with the same number of elements.

Parameters
input (Tensor) – first tensor in the dot product, must be 1D.
other (Tensor) – second tensor in the dot product, must be 1D.

Coming to your question....both input_tensor & (predictions - target_tensor).T in 2D.
Please make it 1D

Related