def affine_forward(x, w, b): out = None N = x.shape[0] x_row = x.reshape(N, -1) w = np.random.randn(784, 100) out = np.dot(x_row, w) + b.reshape(1, -1) cache = (x, w, b) return out, cache mnist识别shapes (40,6272) and (784,100) not aligned: 6272 (dim 1) != 784 (dim 0)如何解决
时间: 2023-11-16 11:06:53 浏览: 86
王桂林老师cocos2d-x第五期课件(pdf)
你需要将第一个矩阵的列数与第二个矩阵的行数相等,才能进行矩阵乘法。在这里,你需要将第一个矩阵的形状改为(40, 784),可以通过x.reshape(N, -1)实现。修改后的代码如下:
```
def affine_forward(x, w, b):
out = None
N = x.shape[0]
x_row = x.reshape(N, -1)
out = np.dot(x_row, w) + b.reshape(1, -1)
cache = (x, w, b)
return out, cache
```
注意,这里将w的值重新赋值为np.random.randn(784, 100)是没有必要的,因为w是作为函数参数传入的,所以不需要重新赋值。
阅读全文