You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Python手写神经网络执行numpy点积时报TypeError: can't multiply sequence by non-int of type 'float'

问题排查与解决方案

核心错误为weights2定义时的笔误:你将第一行的-0.14误写为-0,14,逗号替代了小数点,导致weights2第一个子列表长度为4,后两个子列表长度为3,生成了长度不一致的嵌套列表,触发了ragged nested sequences警告,后续矩阵运算维度不匹配最终抛出类型错误。

修正后的完整可运行代码如下:

import numpy as np

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)

运行后输出结果为:

[[ 0.5031  -1.04185 -2.03875]
 [ 0.2434  -2.7332  -5.7633 ]
 [-0.99314  1.41254 -0.35655]]

内容的提问来源于stack exchange,提问作者banachtarski

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.06 06:48:03