numpy Python Array .dot product [已关闭]

k0pti3hp  于 12个月前  发布在  Python
关注(0)|答案(1)|浏览(116)

已关闭。此问题需要details or clarity。目前不接受回答。
**要改进此问题吗?**通过editing this post添加详细信息并阐明问题。

4天前关闭。
Improve this question

ValueError                                Traceback (most recent call last)
Cell In[159], line 19
     16 iters = 10 
     18 for i in range(iters):
---> 19     w1, b1, w2, b2 = backpropagatealgorithm(w1, w2, b1, b2, p, t, alpha)

Cell In[154], line 5, in backpropagatealgorithm(w1, w2, b1, b2, p, t, alpha)
      3 def backpropagatealgorithm(w1, w2, b1, b2, p, t, alpha):
----> 5     a0, a1, a2 = forwardpropagation(w1, w2, b1, b2)
      7     #Calculate derivatives
      8     n1 = a1  

Cell In[150], line 6, in forwardpropagation(w1, b1, w2, b2)
      3 def forwardpropagation(w1, b1, w2, b2):
      5     a0 = p
----> 6     a1 = logsiglayer(np.dot(w1, p, b1))
      7     a2 = linearlayer(np.dot(w2, a1, b2))
      9     return a0 ,a1, a2

File <__array_function__ internals>:200, in dot(*args, **kwargs)

# **ValueError: shapes (3,1) and (3,) not aligned: 1 (dim 1) != 3 (dim 0)**

的一个字符串
当它应该工作的时候,什么都不工作,我检查了它们是否是数组,它们都是,我改变了维度,但无论如何,什么都不工作。

6ioyuze2

6ioyuze21#

让我们使用一个可重复的示例:

import numpy as np

a = np.array([[1], [2], [3]])   # shape is (3, 1)
b = np.array([1, 2, 3])         # shape is (3,)

print(np.dot(a, b))
# ValueError: shapes (3,1) and (3,) not aligned: 1 (dim 1) != 3 (dim 0)

字符串
让我们重新审视什么是矩阵乘法以及它是如何定义的:
在数学中,特别是在线性代数中,矩阵乘法是一种二元运算,它从两个矩阵产生一个矩阵。对于矩阵乘法,第一个矩阵的列数必须等于第二个矩阵的行数。-Wikipedia
因此,让我们实现它:

import numpy as np

a = np.array([[1], [2], [3]])   # shape is (3, 1)
b = np.array([1, 2, 3])         # shape is (3,)

# Add a dimension to the left (it's now a row-matrix)
b = b[None, :]                  # shape is (1, 3)

print(np.dot(a, b))
# [[1 2 3]
#  [2 4 6]
#  [3 6 9]]

相关问题