np.dot(): 类型错误: 不支持的操作数类型 *: for 循环中的'float'和'NoneType'



在从头开始实现前向传播算法时,我在尝试从N输入和权重更新加权和z时出现此错误:

File "root/StandardFF/standard_feed_forward.py", line 119, in forward_propagation
Z = np.dot(self.params['weight_' + str(layer + 1)], activation_Z) + self.params['bias_' + str(layer + 1)]
File "<__array_function__ internals>", line 5, in dot
TypeError: unsupported operand type(s) for *: 'float' and 'NoneType'

在我的forward_propagation函数中,我定义了for-循环之外的第一个加权和参数:

def forward_propagation(self, features_train):
cache = {}
# For weighted sum
activation_Z = np.array(features_train.T)
# Exclude the final layer
for layer in range(self.L - 1):
Z = np.dot(self.params['weight_' + str(layer + 1)], activation_Z) + self.params['bias_' + str(layer + 1)]
activation_Z = self.activation(Z, self.activation_seq[layer])
# Rest of implementation...

我打印了type(activation_Z),它实际上是一个包含所有浮点的NumPy数组,self.params['weight_1']也包含浮点。看起来for循环一执行,activation_Z就以某种方式重新初始化为None。为什么会发生这种情况,我如何修复此代码?

您的features_train包含None值。您可以打印它或在调试模式下观察它,以查看出现了什么问题。

最新更新