创建神经网络,Numpy 点函数给出形状错误

问题描述 投票:0回答:1

我正在按照本教程制作一个神经网络。 https://www.youtube.com/watch?v=TEWy9vZcxW4&list=PLQVvvaa0QuDcjD5BAw2DxE6OF2tius3V3&index=4

在 14:10 他用这段代码运行他的程序并且没有错误

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]

output = np.dot(inputs, weights) + biases
print(output)

我的错误如下: 回溯(最近一次调用最后一次): 文件“g:\My Drive\Coding\Python\ml.py”,第 13 行,位于 输出 = np.dot(输入、权重) + 偏差 ^^^^^^^^^^^^^^^^^^^^^^^^ ValueError:使用序列设置数组元素。请求的数组在 1 维之后具有不均匀的形状。检测到的形状为 (3,) + 不均匀部分。

我正在运行 Python 版本 3.12.3 和 numpy 版本 1.26.4

我已经通过运行确保转置正常工作

print(np.array(weights).T)

这给出了预期的输出,所以我不确定问题是什么。

python numpy shapes
1个回答
0
投票

我可以看到以下内容:

  • 你必须构建numpy数组

  • 对于有效的矩阵乘法,您必须:

    • 转置权重(使用 np.dot() 时)
    • 或转置偏差(当使用输入*权重的元素乘法时)
  • 输入矩阵中缺少逗号(5.0 和 -1.0 之间)

--

import numpy as np

inputs = np.array([                                   # numpy array
                   [ 1.0, 2.0,  3.0,  2.5],
                   [ 2.0, 5.0, -1.0,  2.0],
                   [-1.5, 2.7,  3.3, -0.8]
                    ])

weights = np.array([[ 0.2,  0.8,  -0.5,   1.0],       # numpy array
                    [ 0.5, -0.91,  0.26, -0.5],
                    [-0.26, -0.27, 0.17,  0.87]])

biases = np.array([2, 3, 0.5])                        # numpy array

output = np.dot(inputs, weights.T) + biases           # <<< weights.T
print(output)


[[ 4.8    1.21   2.385]
 [ 8.9   -1.81   0.2  ]
 [ 1.41   1.051  0.026]]    
© www.soinside.com 2019 - 2024. All rights reserved.