numpy-einsum

cross products with einsums

喜欢而已 提交于 2019-12-11 06:54:17
问题 I'm trying to compute the cross-products of many 3x1 vector pairs as fast as possible. This n = 10000 a = np.random.rand(n, 3) b = np.random.rand(n, 3) numpy.cross(a, b) gives the correct answer, but motivated by this answer to a similar question, I thought that einsum would get me somewhere. I found that both eijk = np.zeros((3, 3, 3)) eijk[0, 1, 2] = eijk[1, 2, 0] = eijk[2, 0, 1] = 1 eijk[0, 2, 1] = eijk[2, 1, 0] = eijk[1, 0, 2] = -1 np.einsum('ijk,aj,ak->ai', eijk, a, b) np.einsum('iak,ak-

Generating np.einsum evaluation graph

我的梦境 提交于 2019-12-11 06:22:48
问题 I was planning to teach np.einsum to colleagues, by hoping to show how it would be reduced to multiplications and summations. So, instead of numerical data, I thought to use alphabet chars. in the arrays. Say, we have A (2X2) as [['a', 'b'], ['c', 'd']] and B (2X1) as [['e'], ['f']] We could use einsum to create a matrix C, say like: np.einsum('ab , bc -> ac', A, B) . What I'd like to see is: it return the computation graph: something like: a*c + ..., etc. Ofcourse, np.einsum expects

Einsum optimize fails for basic operation

廉价感情. 提交于 2019-12-11 00:57:36
问题 With the recent update to Numpy (1.14), I found that it breaks my entire codebase. This is based on changing the default numpy einsum optimize argument from False to True. As a result, the following basic operation now fails: a = np.random.random([50, 2, 2]) b = np.random.random([50, 2]) np.einsum('bdc, ac -> ab', a, b, optimize=True) with the following error trace: ValueError Traceback (most recent call last) <ipython-input-71-b0f9ce3c71a3> in <module>() ----> 1 np.einsum('bdc, ac -> ab', a,

Theano version of a numpy einsum for two 3dim matrices

笑着哭i 提交于 2019-12-11 00:00:52
问题 I have two 3dim numpy matrices and I want to do a dot product according to one axis without using a loop in theano. a numpy solution with sample data would be like: a=[ [[ 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0], [ 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0], [ 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1], [ 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 1, 0, 0]], [[ 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0], [ 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0], [ 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0,

Can I use more than 26 letters in `numpy.einsum`?

谁说我不能喝 提交于 2019-12-08 17:43:37
问题 I am using np.einsum to multiply probability tables like: np.einsum('ijk,jklm->ijklm', A, B) The issue is that I am dealing with more than 26 random variables (axes) overall, so if I assign each random variable a letter I run out of letters. Is there another way I can specify the above operation to avoid this issue, without resorting to a mess of np.sum and np.dot operations? 回答1: The short answer is, you can use any of the 52 letters (upper and lower). That's all the letters in the English

Batch convolution 2d in numpy without scipy?

风格不统一 提交于 2019-12-07 18:41:18
问题 I have a batch of b m x n images stored in an array x , and a convolutional filter f of size p x q that I'd like to apply to each image (then use sum pooling and store in an array y ) in the batch, i.e. all(np.allclose(y[i][j][k], (x[i, j:j+p, k:k+q] * f).sum()) for i in range(b) for j in range(m-p+1) for k in range(n-q+1)) is true. Adapting this answer, I could write the following: b, m, n, p, q = 6, 5, 4, 3, 2 x = np.arange(b*m*n).reshape((b, m, n)) f = np.arange(p*q).reshape((p, q)) y = []

Batch convolution 2d in numpy without scipy?

久未见 提交于 2019-12-06 05:48:16
I have a batch of b m x n images stored in an array x , and a convolutional filter f of size p x q that I'd like to apply to each image (then use sum pooling and store in an array y ) in the batch, i.e. all(np.allclose(y[i][j][k], (x[i, j:j+p, k:k+q] * f).sum()) for i in range(b) for j in range(m-p+1) for k in range(n-q+1)) is true. Adapting this answer , I could write the following: b, m, n, p, q = 6, 5, 4, 3, 2 x = np.arange(b*m*n).reshape((b, m, n)) f = np.arange(p*q).reshape((p, q)) y = [] for i in range(b): shape = f.shape + tuple(np.subtract(x[i].shape, f.shape) + 1) strides = x[i]

How do I calculate all pairs of vector differences in numpy?

China☆狼群 提交于 2019-12-04 03:54:35
问题 I know I can do np.subtract.outer(x, x) . If x has shape (n,) , then I end up with an array with shape (n, n) . However, I have an x with shape (n, 3) . I want to output something with shape (n, n, 3) . How do I do this? Maybe np.einsum ? 回答1: You can use broadcasting after extending the dimensions with None/np.newaxis to form a 3D array version of x and subtracting the original 2D array version from it, like so - x[:, np.newaxis, :] - x Sample run - In [6]: x Out[6]: array([[6, 5, 3], [4, 3,

numpy einsum: nested dot products

北城余情 提交于 2019-12-02 11:31:05
问题 I have two n -by- k -by- 3 arrays a and b , e.g., import numpy as np a = np.array([ [ [1, 2, 3], [3, 4, 5] ], [ [4, 2, 4], [1, 4, 5] ] ]) b = np.array([ [ [3, 1, 5], [0, 2, 3] ], [ [2, 4, 5], [1, 2, 4] ] ]) and it like to compute the dot-product of all pairs of "triplets", i.e., np.sum(a*b, axis=2) A better way to do that is perhaps einsum, but I can't seem to get the indices straight. Any hints here? 回答1: You are loosing the third axis on those two 3D input arrays with that sum-reduction,

Using numpy einsum to compute inner product of column-vectors of a matrix

跟風遠走 提交于 2019-12-02 03:19:41
问题 Suppose I have a numpy matrix like this: [[ 1 2 3] [ 10 100 1000]] I would like to compute the inner product of each column with itself, so the result would be: [1*1 + 10*10 2*2 + 100*100 3*3 + 1000*1000] == [101, 10004, 1000009] I would like to know if this is possible using the einsum function (and to better understand it). So far, the closest result I could have is: import numpy as np arr = np.array([[1, 2, 3], [10, 100, 1000]]) res = np.einsum('ij,ik->jk', arr, arr) # [[ 101 1002 10003] #