np.sum维度降维问题:cs231n assignment2 报错"ValueError: non-broadcastable output operand "

报错:

ValueError: non-broadcastable output operand with shape (100,1) doesn't match the broadcast shape (100,100)

原因:

在fc_net.py、TwoLayerNet类中:

self.params['b1'] = np.zeros(hidden_dim)  b1是一维的
self.params['b1'] = np.zeros((hidden_dim,1))  b1是二维的

 

而在layers.py、affine_backward函数中,使用np.sum时,不指定keepdims=True面临降维问题:

db=np.sum(dout,axis=0)  db降维,db变成一维的
db=np.sum(dout,axis=0,keepdims=True)  db仍为二维

而python broadcast方法要求维度必须一致

 

之后解决方法已经很显然了,令self.params['b1']与db的维度一致即可。

 

 

其中关于np.sum维度问题,可参考:https://blog.csdn.net/m0_37390405/article/details/79175273

你可能感兴趣的:(cs231n,深度学习,broadcast,python,numpy)