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