Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/281.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 具有(Nx1)矩阵和长度为N的向量的Numpy行加法_Python_Numpy - Fatal编程技术网

Python 具有(Nx1)矩阵和长度为N的向量的Numpy行加法

Python 具有(Nx1)矩阵和长度为N的向量的Numpy行加法,python,numpy,Python,Numpy,我试图用这行代码更新神经网络中的权重: self.l1weights[0] = self.l1weights[0] + self.learning_rate * l1error 这会导致值错误: ValueError: could not broadcast input array from shape (7,7) into shape (7) 打印learning_rate*错误和权重返回如下内容: [[-0.00657573] [-0.01430752] [-0.01739463]

我试图用这行代码更新神经网络中的权重:

self.l1weights[0] = self.l1weights[0] + self.learning_rate * l1error
这会导致值错误:

ValueError: could not broadcast input array from shape (7,7) into shape (7)
打印learning_rate*错误和权重返回如下内容:

[[-0.00657573]
 [-0.01430752]
 [-0.01739463]
 [-0.00038115]
 [-0.01563393]
 [-0.02060908]
 [-0.01559269]]
[  4.17022005e-01   7.20324493e-01   1.14374817e-04   3.02332573e-01
   1.46755891e-01   9.23385948e-02   1.86260211e-01]
[[  4.10446271e-01   7.13748760e-01  -6.46135890e-03   2.95756839e-01
    1.40180157e-01   8.57628611e-02   1.79684478e-01]
 [  4.02714481e-01   7.06016970e-01  -1.41931487e-02   2.88025049e-01
    1.32448367e-01   7.80310713e-02   1.71952688e-01]
 [  3.99627379e-01   7.02929868e-01  -1.72802505e-02   2.84937947e-01
    1.29361266e-01   7.49439695e-02   1.68865586e-01]
 [  4.16640855e-01   7.19943343e-01  -2.66775370e-04   3.01951422e-01
    1.46374741e-01   9.19574446e-02   1.85879061e-01]
 [  4.01388075e-01   7.04690564e-01  -1.55195551e-02   2.86698643e-01
    1.31121961e-01   7.67046648e-02   1.70626281e-01]
 [  3.96412924e-01   6.99715412e-01  -2.04947062e-02   2.81723492e-01
    1.26146810e-01   7.17295137e-02   1.65651130e-01]
 [  4.01429313e-01   7.04731801e-01  -1.54783174e-02   2.86739880e-01
    1.31163199e-01   7.67459026e-02   1.70667519e-01]]
很明显,在本例中,权重初始化为长度为7的向量,误差初始化为7x1矩阵。我希望加法也会返回一个7x1矩阵或向量,但它会生成一个7x7矩阵,如下所示:

[[-0.00657573]
 [-0.01430752]
 [-0.01739463]
 [-0.00038115]
 [-0.01563393]
 [-0.02060908]
 [-0.01559269]]
[  4.17022005e-01   7.20324493e-01   1.14374817e-04   3.02332573e-01
   1.46755891e-01   9.23385948e-02   1.86260211e-01]
[[  4.10446271e-01   7.13748760e-01  -6.46135890e-03   2.95756839e-01
    1.40180157e-01   8.57628611e-02   1.79684478e-01]
 [  4.02714481e-01   7.06016970e-01  -1.41931487e-02   2.88025049e-01
    1.32448367e-01   7.80310713e-02   1.71952688e-01]
 [  3.99627379e-01   7.02929868e-01  -1.72802505e-02   2.84937947e-01
    1.29361266e-01   7.49439695e-02   1.68865586e-01]
 [  4.16640855e-01   7.19943343e-01  -2.66775370e-04   3.01951422e-01
    1.46374741e-01   9.19574446e-02   1.85879061e-01]
 [  4.01388075e-01   7.04690564e-01  -1.55195551e-02   2.86698643e-01
    1.31121961e-01   7.67046648e-02   1.70626281e-01]
 [  3.96412924e-01   6.99715412e-01  -2.04947062e-02   2.81723492e-01
    1.26146810e-01   7.17295137e-02   1.65651130e-01]
 [  4.01429313e-01   7.04731801e-01  -1.54783174e-02   2.86739880e-01
    1.31163199e-01   7.67459026e-02   1.70667519e-01]]
Numpy.sum还返回相同的7x7矩阵。有没有一种方法可以解决这个问题,而无需显式重塑?输出大小是可变的,这是一个特定于输出大小为1时的问题。

将(7,)数组(命名为
a
)与(1,7)数组(命名为
b
)相加时,会发生广播并生成(7,7)数组。如果您只想逐元素添加,请将它们保持在相同的形状。
a+b.flant()
给出了(7,)<代码>展平将所有维度折叠为一个维度。这会将结果保留为一行。
a.重塑(-1,1)+b
给出(1,7)<代码>-1中的
重塑
需要
numpy
来确定给定其他维度的元素数量。这会将结果保留为一列

a = np.arange(7)                          # row
b = a.reshape(-1, 1)                      # column

print((a + b).shape)                      # (7, 7)
print((a + b.flatten()).shape)            # (7,)
print((a.reshape(-1, 1) + b).shape)       # (7, 1)
在您的情况下,
a
b
将分别是
self.l1weights[0]
self.learning\u rate*l1error

。如果您只想逐元素添加,请将它们保持在相同的形状。
a+b.flant()
给出了(7,)<代码>展平将所有维度折叠为一个维度。这会将结果保留为一行。
a.重塑(-1,1)+b
给出(1,7)<代码>-1中的
重塑
需要
numpy
来确定给定其他维度的元素数量。这会将结果保留为一列

a = np.arange(7)                          # row
b = a.reshape(-1, 1)                      # column

print((a + b).shape)                      # (7, 7)
print((a + b.flatten()).shape)            # (7,)
print((a.reshape(-1, 1) + b).shape)       # (7, 1)
在您的情况下,
a
b
将分别是
self.l1权重[0]
self.learning\u rate*l1错误

a(7,)可以广播到(1,7)和(7,7)。您已经明确地将其设置为(7,1)。也就是说,前导
None
是自动的,尾随则不是。A(7,)可以广播到(1,7)并继续广播到(7,7)。您已经明确地将其设置为(7,1)。也就是说,前导
None
是自动的,尾随不是。