简体   繁体   中英

Numpy addition to an element of an array

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:

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.

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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