array-broadcasting

PyTorch nn.module won't unbatch operations

PyTorch nn.module won't unbatch operations Question: I have a nn.Module whose forward function takes in two inputs. Inside the function, I multiply one of the inputs x1 by a set of trainable parameters, and then concatenate them with the other input x2. class ConcatMe(nn.Module): def __init__(self, pad_len, emb_size): super(ConcatMe, self).__init__() self.W = nn.Parameter(torch.randn(pad_len, emb_size).to(DEVICE), requires_grad=True) …

Total answers: 1

Numpy einsum fails for matrix product with unspecified leading and trailing axes

Numpy einsum fails for matrix product with unspecified leading and trailing axes Question: I want to use dot product between the leading and trailing axes of arrays with unspecified dimensions. einsum doesn’t seem to accommodate this use case. User error? With prnd=np.random.RandomState(), np.einsum(‘…i,i…’, prnd.rand(5,2), prnd.rand(2,3)) Traceback (most recent call last): File "/tmp/ipykernel_1106807/647899105.py", line 1, in …

Total answers: 1

Vectorization of Product Over

Vectorization of Product Over Question: I am seeking a vectorized form of the following computation: import numpy as np D = 100 N = 1000 K = 10 X = np.random.uniform(0, 1, (K, N)) T = np.random.uniform(0, 1000, (D, N)) out = np.zeros((D, K)) for i in range(D): for j in range(K): out[i, j] = …

Total answers: 1

Element-wise multiplication of a 3D array with a 2D array

Element-wise multiplication of a 3D array with a 2D array Question: I have a portion of a RGB image as numpy array, the shape of which is (height, width, channel) = (5, 5, 3). What I want to do with this is to get the sum of element-wise multiplication with 5×5 kernel matrix, channel by …

Total answers: 1

Numpy 3D array max and min value

Numpy 3D array max and min value Question: I have Numpy 3d array that is just a list of gray images: images = np.zeros((xlen, height, width), dtype=int) for i in range (5): images[i] = cv2.imread(filename[i], cv2.IMREAD_GRAYSCALE) All the images are pretty the same but they all have some random noise pixels. My idea is that …

Total answers: 2

What is the numpy equivalent of expand in pytorch?

What is the numpy equivalent of expand in pytorch? Question: Suppose I have a numpy array x of shape [1,5]. I want to expand it along axis 0 such that the resulting array y has shape [10,5] and y[i:i+1,:] is equal to x for each i. If x were a pytorch tensor I could simply …

Total answers: 3

PyTorch: How to multiply via broadcasting of two tensors with different shapes

PyTorch: How to multiply via broadcasting of two tensors with different shapes Question: I have the following two PyTorch tensors A and B. A = torch.tensor(np.array([40, 42, 38]), dtype = torch.float64) tensor([40., 42., 38.], dtype=torch.float64) B = torch.tensor(np.array([[[1,2,3,4,5],[1,2,3,4,5],[1,2,3,4,5],[1,2,3,4,5],[1,2,3,4,5]], [[4,5,6,7,8],[4,5,6,7,8],[4,5,6,7,8],[4,5,6,7,8],[4,5,6,7,8]], [[7,8,9,10,11],[7,8,9,10,11],[7,8,9,10,11],[7,8,9,10,11],[7,8,9,10,11]]]), dtype = torch.float64) tensor([[[ 1., 2., 3., 4., 5.], [ 1., 2., 3., 4., 5.], …

Total answers: 1

Add multiple np.newaxis as needed?

Add multiple np.newaxis as needed? Question: I would like to pairwise compare (with <=) all elements of two NumPy ndarrays A and B, where both arrays can have arbitrary dimensions m and n, such that the result is an array of dimension m + n. I know how to do it for given dimension of …

Total answers: 2