matrix-multiplication

Applying linear transformation to whole surface of weird shape

Applying linear transformation to whole surface of weird shape Question: So, I have this torus that is moved from its usual position given by the function def toro(u,v,R,r,t): n = unitary_normal_t(t) n0, n1, n2 = [n[k][0] for k in [0,1,2]] b = binormal_t(t) b0, b1, b2 = [b[k][0] for k in [0,1,2]] tang = tangente(t) …

Total answers: 1

Why am I getting an incorrect result from multiplying an inverted matrix by a vector?

Why am I getting an incorrect result from multiplying an inverted matrix by a vector? Question: I’m trying to learn Python for basic work in linear algebra. I’m running into the following problem with a simple system of linear equations: import scipy.linalg as la import numpy as np A = np.array([[186/450, 54/21, 30/60], [12/450, 6/21 …

Total answers: 1

Matrix multiplication in TensorFlow model

Matrix multiplication in TensorFlow model Question: I want to use matrix multiplication inside TF model. My model is a NN with input shape = (1,9). And I want to get a product of this vectors by themself (i.e. I want to get a matrix-product equals multiplication of transposed input vector by itself, so its shape …

Total answers: 2

How does torch.einsum perform this 4D tensor multiplication?

How does torch.einsum perform this 4D tensor multiplication? Question: I have come across a code which uses torch.einsum to compute a tensor multiplication. I am able to understand the workings for lower order tensors, but, not for the 4D tensor as below: import torch a = torch.rand((3, 5, 2, 10)) b = torch.rand((3, 4, 2, …

Total answers: 1

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

How do I multiply matrices in PyTorch?

How do I multiply matrices in PyTorch? Question: With numpy, I can do a simple matrix multiplication like this: a = numpy.ones((3, 2)) b = numpy.ones((2, 1)) result = a.dot(b) However, this does not work with PyTorch: a = torch.ones((3, 2)) b = torch.ones((2, 1)) result = torch.dot(a, b) This code throws the following error: …

Total answers: 4

Python: Divide and Conquer Recursive Matrix Multiplication

Python: Divide and Conquer Recursive Matrix Multiplication Question: I’m trying to implement the divide and conquer matrix multiplication (8 recursion version not Strassen). I thought I had it figured out but it is producing weird output with too many nested lists and the wrong values. I suspect the problem is how I’m summing the 8 …

Total answers: 3

How to get element-wise matrix multiplication (Hadamard product) in numpy?

How to get element-wise matrix multiplication (Hadamard product) in numpy? Question: I have two matrices a = np.matrix([[1,2], [3,4]]) b = np.matrix([[5,6], [7,8]]) and I want to get the element-wise product, [[1*5,2*6], [3*7,4*8]], equaling [[5,12], [21,32]] I have tried print(np.dot(a,b)) and print(a*b) but both give the result [[19 22], [43 50]] which is the matrix …

Total answers: 5

Efficient element-wise multiplication of a matrix and a vector in TensorFlow

Efficient element-wise multiplication of a matrix and a vector in TensorFlow Question: What would be the most efficient way to multiply (element-wise) a 2D tensor (matrix): x11 x12 .. x1N … xM1 xM2 .. xMN by a vertical vector: w1 … wN to obtain a new matrix: x11*w1 x12*w2 … x1N*wN … xM1*w1 xM2*w2 … …

Total answers: 1

Difference between numpy dot() and Python 3.5+ matrix multiplication @

Difference between numpy dot() and Python 3.5+ matrix multiplication @ Question: I recently moved to Python 3.5 and noticed the new matrix multiplication operator (@) sometimes behaves differently from the numpy dot operator. In example, for 3d arrays: import numpy as np a = np.random.rand(8,13,13) b = np.random.rand(8,13,13) c = a @ b # Python …

Total answers: 6