繁体   English   中英

ValueError:形状 (3,) 和 (4,) 未对齐:3 (dim 0) != 4

[英]ValueError: shapes (3,) and (4,) not aligned: 3 (dim 0) != 4

import numpy as np

inputs = [1, 2, 3, 2.5]
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(weights, inputs) + biases
print(output)

我是 numpy 的新手,使用以下“输入”向量、“权重”矩阵和“偏差”向量编写了一个点积。 output 给我一个形状错误:

ValueError:形状 (3,) 和 (4,) 未对齐:3 (dim 0) != 4

我发现了你的问题,这是忘记逗号和“融合在一起”数组元素的常见问题,在这里我将np.array添加到你的代码中:

import numpy as np

inputs = np.array([1, 2, 3, 2.5])
weights = np.array([
    [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(weights, inputs) + biases
print(output)

现在我收到弃用警告:

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.
  weights = np.array([

您必须添加逗号,来分隔此处的项目:

[0.5 -0.91, 0.26, -0.5], ->     [0.5, -0.91, 0.26, -0.5],

最终代码:

import numpy as np

inputs = np.array([1, 2, 3, 2.5])
weights = np.array([
    [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(weights, inputs) + biases
print(output)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM