首页 > 解决方案 > “TypeError:在进行 numpy 点积时,不能将序列乘以‘float’类型的非整数”

问题描述

我目前正在尝试学习在 python 中从头开始编写神经网络,但在尝试编写矩阵“layer2_outputs”时遇到以下错误。到目前为止,这是我的代码:

inputs = [[1, 2, 3, 2.5],
         [2.0, 5.0, -1.0, 2.0],
         [-1.5, 2.7, 3.3, -0.8]]

weights = [[0.2, 0.8, -0.5, 1.0],
          [0.5, -0.91, 0.26, -0.5],
          [-0.26, -0.27, 0.17, 0.87]]
biases = [2, 3, 0.5]

weights2 = [[0.1, -0,14, 0.5],
           [-0.5, 0.12, -0.33],
           [-0.44, 0.73, -0.13]]
biases2 = [-1, 2, -0.5] 

layer1_outputs = np.dot(inputs, np.array(weights).T) + biases

layer2_outputs = np.dot(layer1_outputs, np.array(weights2)) + biases2

print(layer2_outputs)

我已经尝试查找错误消息,但我无法找到解决我的问题的方法,所以如果你们中的任何一个可以帮助我,我会非常高兴,如果你需要任何其他信息,请问我,我是 Stack Overflow 的新手,所以不要太苛刻哈哈 :)

这是确切的错误消息,以防万一:

> VisibleDeprecationWarning: Creating an ndarray from ragged nested sequences (which is a 
list-or-tuple of lists-or-tuples-or ndarrays with different lengths or shapes) is deprecated. 
If you meant to do this, you must specify 'dtype=object' when creating the ndarray
layer2_outputs = np.dot(layer1_outputs, np.array(weights2)) + biases2
Traceback (most recent call last):
File "c:\Users\[myName]\Desktop\firstneuralnetwork.py", line 20, in <module>
**layer2_outputs = np.dot(layer1_outputs, np.array(weights2)) + biases2**
File "<__array_function__ internals>", line 5, in dot
**TypeError: can't multiply sequence by non-int of type 'float'**

标签: pythonnumpyneural-network

解决方案


-0,14 应为 -0.14 权重2

错误消息的第一行表示您正在尝试构建一个不是矩形的 np 数组,这是不允许的


推荐阅读