I have a matrix M of shape (N, L) and a 3D tensor P of shape (N, L, K). I want to get matrix V of shape (N, K) where V[i] = M[i] # P[i]. I can do it with for loop but that's inefficient, I want to do it with a single or few operations so that it would run in parallel on CUDA.
I tried just multiplying it like so
V = M # P
but that results in a 3D tensor where V[i, j] = M[j] # P[i].
np.diagonal(M # P).T is basically what I want, but calculating it like that wastes a lot of computation.
You could use np.einsum:
>>> M = np.random.rand(5, 2)
>>> P = np.random.rand(5, 2, 3)
>>> V = np.einsum('nl,nlk->nk', M, P)
>>> V.shape
(5, 3)
Related
I have a L x L matrix A, which I currently fill in using the following code:
A = np.zeros((L, L))
for J in range(X):
for a in range(L):
for b in range(L):
A[a][b] += alpha[J, a] * O[b, J] * A_old[a, b] * betas[J+2, b]
Where X is an integer defined elsewhere, alpha and betas is of shape (X, L), O is of shape (L, X) and A_old is of shape (L, L). I'm concerned about the speed of this code, and am trying to find a more numpythonic way to approach filling in this matrix. My instinct is to do something like:
for J in range(X):
A += alpha[J, :] * O[:, J] * A_old[:, :] * betas[J+2, :]
But this doesn't broadcast the operations correctly because of the A_old matrix (the resulting shape is right, but the values are not). What's a good way to condense this loop using numpy?
I need to speed up a python code, I would like to avoid the use of the following for cycle, where "data" matrix has dimension [dim1xdim2]:
for i in range(int(dim1)):
data_process = data[i,:].reshape((dim2, 1))
rxx = data_process * np.matrix.getH(np.asmatrix(data_process)) / dim2
Using the 'for cycle' the dimension of the rxx matrix is [dim2xdim2], I would get a 3D "rxx" matrix [dim1xdim2xdim2]. I tried to use the following solution:
data_new = repeat(data_process0[:, :, newaxis], dim2, axis=2)
N_2 = data_new.shape[2]
m1 = data_new - data_new.sum(2, keepdims=1) / N_2
y_out = einsum('ijk,ilk->ijl', m1, m1) / (N_2 - 1)
In this case I got 3D "y_out" matrix [dim1xdim2xdim2] but this doesn't work in my case.
Thanks
representative sample data:
from numpy import matrix, random, asmatrix, linalg, empty
B = random.random((156, 48))
A = B.shape
eig_val = empty(A, dtype=complex)
eig_vec = empty((A[0], A[1], A[1]), dtype=complex)
for i in range(int(A[0])):
data_process = B[i, :].reshape((A[1], 1))
rxx = data_process * matrix.getH(asmatrix(data_process)) / A[1]
eig_val[i:, ...], eig_vec[i:, ...] = linalg.eig(rxx)
Roughly I want to convert this (non-numpy) for-loop:
N = len(left)
M = len(right)
matrix = np.zeros(N, M)
for i in range(N):
for j in range(M):
matrix[i][j] = scipy.stats.binom.pmf(left[i], C, right[j])
It's sort of like a dot product but of course mathematically not a dot product. How would I normally vectorize or make something like this pythonic/numpythonic?
scipy.stats.binom.pmf already is vectorized. However, you have to broadcast your inputs in order to get your desired result.
broadcast_out = scipy.stats.binom.pmf(left[:, None], C, right)
Validation
np.random.seed(314)
left = np.arange(5, dtype=float)
right = np.random.rand(5)
C = 5
broadcast_out = scipy.stats.binom.pmf(left[:, None], C, right)
N = len(left)
M = len(right)
matrix = np.zeros((N, M))
for i in range(N):
for j in range(M):
matrix[i][j] = scipy.stats.binom.pmf(left[i], C, right[j])
print(np.array_equal(matrix, broadcast_out))
True
I have two numpy arrays, x of shape (m, 2) and y of shape (n, 2). I would like compute the (m, n, 2) array where at position (i, j) one finds the sum of x[i] and y[j] at out[i, j]. List comprehension works
import numpy
x = numpy.random.rand(13, 2)
y = numpy.random.rand(5, 2)
xy = numpy.array([
[xx + yy for yy in y]
for xx in x
])
but I was wondering if there is a more efficient solution via numpy.add.outer or something along those lines.
You can use numpys broadcasting rules to cast the first array to the shape (13, 1, 2) and the second to the shape (1, 5, 2):
numpy.all(x[:, None, :] + y[None, :, :] == xy)
# True
The array is repeated across the dimension where None is added (since it has length 1).
Therefore the shape of the output becomes (13, 5, 2).
xy = x[:, None]+y
should do the trick.
I'd like to transform a tensor T of size (n x n x m x m) into a tensor U of size (n x m x m) while only retreiving the diagonal elements of T over the (NxN) chunks (i.e. Uikl=Tiikl). torch.diag() only works with 2-D tensors and I really fail to see how to do this without looping on the indexes of the elements (which I'd like to avoid given that I think that it is inefficient computationnally). In clear, I'd like to vectorize the following code:
U = torch.zeros(n, m, m)
for i in range(n):
for k in range(m):
for l in range(m):
U[i][k][l] = T[i][i][k][l]
I'm totally new to pytorch and I tried many combination of functions but none of them gives me a satisfying result. Has anyone an idea?
You can generate the indexes using np.meshgrid
i, k, l = np.meshgrid(range(n), range(m), range(m))
U[i, k, l] = T[i, i, k, l]
for completeness I did:
n = 3
m = 5
T = torch.arange(n * n * m * m).view(n, n, m, m)
U = torch.zeros(n, m, m)
U_ = torch.zeros(n, m, m)
i, k, l = np.meshgrid(range(n), range(m), range(m))
U_[i, k, l] = T[i, i, k, l]
for i in range(n):
for k in range(m):
for l in range(m):
U[i][k][l] = T[i][i][k][l]
U = U.view(-1)
U_ = U_.view(-1)
print ((U == U_).all())
The output is True so I assume it is correct.
When applied to 2d matrices, torch.diag() is an alias for torch.diagonal().
diagonal itself allows you to specify which two dimensions of an arbitrary rank tensor the diagonal is taken from, by default these are 0 and 1:
U = T.diagonal()