Adding matrices with different sizes

Matrices (or tensors) of different dimensions can be added to numpy and tensorflow if the shape of the smaller matrix is ​​the suffix of the larger matrix. This is an example:

x = np.ndarray(shape=(10, 7, 5), dtype = float) y = np.ndarray(shape=(7, 5), dtype = float) 

For these two matrices, the operation x+y is a shortcut for:

 for a in range(10): for b in range(7): for b in range(5): result[a,b,c] = x[a,b,c] + y[b,c] 

In my case, however, I have form matrices (10,7,5) and (10,5) , and I would also like to perform the + operation using the same logic:

 for a in range(10): for b in range(7): for b in range(5): result[a,b,c] = x[a,b,c] + y[a,c] ^ 

In this case, however, the operation x+y fails because neither numpy nor tensorflow understand what I want to do. Is there a way I can efficiently perform this operation (don't write the python loop itself)?

So far, I realized that I can introduce a temporal matrix z form (10,7,5) using einsum as follows:

 z = np.einsum('ij,k->ikj', y, np.ones(7)) x + z 

but this creates an explicit three-dimensional matrix (or tensor), and, if possible, I would rather avoid this.

+5
source share
2 answers

The NumPy you can expand y to 3D , and then add

 x + y[:,None,:] 

They didn’t really consider tensorflow , but looking at his docs, we can use tf.expand_dims -

 x + tf.expand_dims(y, 1) 

The extended version will still display in y and, as such, will no longer occupy memory, as shown below -

 In [512]: np.may_share_memory(y, y[:,None,:]) Out[512]: True 
+3
source

As correctly indicated in the accepted answer, the solution is to expand the dimensions with the available design.

The goal is to understand how numpy translates matrices when matrices are added if their sizes do not match. The rule is that the two matrices must have exactly the same dimensions with the exception that some sizes in any of the matrices can be replaced by 1.

eg.

 A (4d array): 8 x 1 x 6 x 1 B (3d array): 7 x 1 x 5 Result (4d array): 8 x 7 x 6 x 5 

This example and detailed explanation can be found in scipy docs https://docs.scipy.org/doc/numpy-1.10.0/user/basics.broadcasting.html

+1
source

Source: https://habr.com/ru/post/1264185/


All Articles