我一直致力于用Python从头开始制作神经网络。输入张量的形状为
[400,3]
,target_tensor 的形状为 [400]
。我在求权重导数时遇到错误。以下是功能
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)
运行此程序会引发错误。
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
如果我们看到
torch.dot
的文档:torch.dot(input, other, *, out=None)
→ Tensor
计算两个一维张量的点积。
注意:与 NumPy 的 dot 不同,torch.dot 有意仅支持计算具有相同元素数量的两个一维张量的点积。
参数
input
(张量)- 点积中的第一个张量,必须是 1D。other
(张量)- 点积中的第二个张量,必须是 1D。
回答你的问题....
input_tensor
和 (predictions - target_tensor).T
都是二维的。1D
我在下面遇到了同样的错误:
运行时错误:预期为 1D 张量,但得到了 2D 和 2D 张量
因为我尝试将 2D 张量与 dot() 相乘,如下所示。 *
dot()
只能通过点乘来乘一维张量:
import torch
tensor1 = torch.tensor([[2, 7, 4], [8, 3, 2]])
tensor2 = torch.tensor([[5, 0, 8], [3, 6, 1]])
torch.dot(tensor1, tensor2)
但是,我可以通过逐元素乘法将 2D 张量与
*
或 mul() 相乘,如下所示:
import torch
tensor1 = torch.tensor([[2, 7, 4], [8, 3, 2]])
tensor2 = torch.tensor([[5, 0, 8], [3, 6, 1]])
tensor1 * tensor2 # tensor([[10, 0, 32], [24, 18, 2]])
torch.mul(tensor1, tensor2) # tensor([[10, 0, 32], [24, 18, 2]])
请小心,尝试通过矩阵乘法将 2D(2x3 和 2x3)张量与
@
、matmul() 或 mm() 相乘会得到如下所示的错误,因为 tensor1
的列数不与 tensor2
的行数不匹配进行矩阵乘法:
import torch
tensor1 = torch.tensor([[2, 7, 4], # 2 rows x (3) columns
[8, 3, 2]]) # Doesn't match!
tensor2 = torch.tensor([[5, 0, 8], # (2) rows x 3 columns
[3, 6, 1]])
tensor1 @ tensor2 # Error
torch.matmul(tensor1, tensor2) # Error
torch.mm(tensor1, tensor2) # Error
运行时错误:mat1 和 mat2 形状无法相乘(2x3 和 2x3)
*备注:
@
或 matmul()
可以通过点乘或矩阵乘法来乘以 1D 或更多 D 张量。mm()
可以通过矩阵乘法来乘以 2D 张量。因此,要进行矩阵乘法,
tensor1
的列数必须与tensor2
的行数相匹配,然后您可以将二维张量与@
、matmul()
或mm()
相乘,如下所示:
import torch
tensor1 = torch.tensor([[2, 7], # 2 rows x (2) columns
[8, 3]]) # Does match!
tensor2 = torch.tensor([[5, 0, 8], # (2) rows x 3 columns
[3, 6, 1]])
tensor1 @ tensor2 # tensor([[31, 42, 23], [49, 18, 67]])
torch.matmul(tensor1, tensor2) # tensor([[31, 42, 23], [49, 18, 67]])
torch.mm(tensor1, tensor2) # tensor([[31, 42, 23], [49, 18, 67]])
import torch
tensor1 = torch.tensor([[2, 7, 4], # 2 rows x (3) columns
[8, 3, 2]]) # Does match!
tensor2 = torch.tensor([[5, 0, 8], # (3) rows x 3 columns
[3, 6, 1],
[7, 4, 9]])
tensor1 @ tensor2 # tensor([[59, 58, 59], [63, 26, 85]])
torch.matmul(tensor1, tensor2) # tensor([[59, 58, 59], [63, 26, 85]])
torch.mm(tensor1, tensor2) # tensor([[59, 58, 59], [63, 26, 85]])