【问题标题】:"TypeError: can't multiply sequence by non-int of type 'float'" when doing a numpy dot product执行 numpy 点积时,“TypeError:不能将序列乘以‘float’类型的非整数”
【发布时间】:2021-10-26 01:26:50
【问题描述】:

我目前正在尝试学习在 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'**

【问题讨论】:

  • 您在weights2 中有错字:[0.1, -0,14, 0.5]。有一个 , 你的意思是 . 所以你有参差不齐的数组(因此警告)。
  • 这个警告很重要。它告诉我们您没有从weights2 制作二维数组。我建议在尝试进行计算之前从所有列表中制作数组。 np.dot 确实会为您将它们转换为数组,但您会更早发现这样的错误。

标签: python numpy neural-network


【解决方案1】:

-0,14 在 weights2 中应该是 -0.14

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

【讨论】:

  • 非常感谢!不可能我这么傻。非常感谢!!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2010-12-30
  • 2012-10-09
  • 1970-01-01
  • 2017-02-04
  • 2019-03-21
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多