简体   繁体   中英

Add lists of numpy arrays element-wise

I've been working on an algorithm for backpropagation in neural networks. My program calculates the partial derivative of each weight with respect to the loss function, and stores it in an array. The weights at each layer are stored in a single 2d numpy array, and so the partial derivatives are stored as an array of numpy arrays, where each numpy array has a different size depending on the number of neurons in each layer.

When I want to average the array of partial derivatives after a number of training data has been used, I want to add each array together and divide by the number of arrays. Currently, I just iterate through each array and add each element together, but is there a quicker way? I could use ndarray with dtype=object but apparently, this has been deprecated.

For example, if I have the arrays:

arr1 = [ndarray([[1,1],[1,1],[1,1]]), ndarray([[2,2],[2,2]])]
arr2 = [ndarray([[3,3],[3,3],[3,3]]), ndarray([[4,4],[4,4]])]

How can I add these together to get the array:

arr3 = [ndarray([[4,4],[4,4],[4,4]]), ndarray([[6,6],[6,6]])]

You can use zip / map / sum :

import numpy as np
arr1 = [np.array([[1,1],[1,1],[1,1]]), np.array([[2,2],[2,2]])]
arr2 = [np.array([[3,3],[3,3],[3,3]]), np.array([[4,4],[4,4]])]

arr3 = list(map(sum, zip(arr1, arr2)))

output:

>>> arr3
[array([[4, 4],
        [4, 4],
        [4, 4]]),
 array([[6, 6],
        [6, 6]])]

In NumPy, you can add two arrays element-wise by adding two NumPy arrays. NB: if your array shape varies then reshape the array and fill with 0.

arr1 = np.array([np.array([[1,1],[1,1],[1,1]]), np.array([[2,2],[2,2]])])
arr2 = np.array([np.array([[3,3],[3,3],[3,3]]), np.array([[4,4],[4,4]])])
arr3 = arr2 + arr1

You don't need to add the numbers in the array element-wise, make use of numpy's parallel computations by usingnumpy.add

Here's some code to do just that:

import numpy as np

arr1 = np.asarray([[[1,1],[1,1],[1,1]], [[2,2],[2,2]]])
arr2 = np.asarray([[[3,3],[3,3],[3,3]], [[4,4],[6,6]]])

ans = []

for first, second in zip(arr1, arr2):
    ans.append(np.add(first,second))

Outputs:

>>> [array([[4, 4], [4, 4], [4, 4]]), array([[6, 6], [8, 8]])]

PS Could use a one-liner list-comprehension as well

ans = [np.add(first, second) for first, second in zip(arr1, arr2)]

您可以使用列表理解:

[x + y for x, y in zip(arr1, arr2)]

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