Home > Blockchain >  Add lists of numpy arrays element-wise
Add lists of numpy arrays element-wise

Time:11-07

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]])]

CodePudding user response:

You don't need to add the numbers in the array element-wise, make use of numpy's parallel computations by using numpy.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]])]

P.S Could use a one-liner list-comprehension as well

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

CodePudding user response:

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]])]

CodePudding user response:

In NumPy, you can add two arrays element-wise by adding two NumPy arrays. N.B: 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

CodePudding user response:

You can use a list comprehension:

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