Numpy添加到数组的元素

时间:2021-10-04 21:22:04

I am coding a neural network in python, and need to adjust my weights. In order to do so, I need to add my change variable to an element of my weights array. However, I don't know how to do this. The code would look like:

我在python中编写一个神经网络,需要调整我的权重。为了做到这一点,我需要将我的变量变量添加到我的权重数组的元素中。但是,我不知道该怎么做。代码看起来像:

weights = numpy.array([1, 2, 3])
change = 1
weights[0]+= change
print(weights)
-- [2, 2, 3]

I have tried this, but it does not seem to work. Thanks in advance for any answers.

我试过这个,但似乎没有用。提前感谢您的任何答案。

1 个解决方案

#1


1  

If you're trying to add your variable 'change' to just the first element of the weights array, then your code works fine. if you are trying to add 'change' to all elements of the weights array, simply put

如果您尝试将变量'change'添加到权重数组的第一个元素,那么您的代码可以正常工作。如果您尝试将“更改”添加到权重数组的所有元素,只需添加

weights=numpy.array([1,2,3])
change=1
weights+=change
print(weights)

this code will add the change to all the elements. I'm assuming that this is what you're trying to do because that would make the most sense in the context of a neural network. if this is not your problem, be more specific on what you are trying to do.

此代码将更改添加到所有元素。我假设这是你正在尝试做的事情,因为这将在神经网络的背景下最有意义。如果这不是您的问题,请更具体地说明您要做的事情。

#1


1  

If you're trying to add your variable 'change' to just the first element of the weights array, then your code works fine. if you are trying to add 'change' to all elements of the weights array, simply put

如果您尝试将变量'change'添加到权重数组的第一个元素,那么您的代码可以正常工作。如果您尝试将“更改”添加到权重数组的所有元素,只需添加

weights=numpy.array([1,2,3])
change=1
weights+=change
print(weights)

this code will add the change to all the elements. I'm assuming that this is what you're trying to do because that would make the most sense in the context of a neural network. if this is not your problem, be more specific on what you are trying to do.

此代码将更改添加到所有元素。我假设这是你正在尝试做的事情,因为这将在神经网络的背景下最有意义。如果这不是您的问题,请更具体地说明您要做的事情。