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, cacheshapes (40,6272) and (784,100) not aligned: 6272 (dim 1) != 784 (dim 0)
时间: 2023-09-14 08:08:15 浏览: 64
affine_fit(X):仿射表面-matlab开发
这个错误发生在第3行的 np.dot(x_row, w) 上,因为x和w的维度不兼容。x的形状是(40,6272),w的形状是(784,100),两个矩阵的第二个维度不相等,所以无法进行矩阵乘法。要解决这个问题,需要将w转置成(100,784)的形状,然后再进行矩阵乘法。修改代码如下:
```
def affine_forward(x, w, b):
out = None
N = x.shape[0]
x_row = x.reshape(N, -1)
w_row = w.reshape(w.shape[0], -1).T
out = np.dot(x_row, w_row) + b.reshape(1, -1)
cache = (x, w, b)
return out, cache
```
阅读全文