PyTorch  Elementwise multiplication between a variable and a tensor?
By : Min Albert
Date : March 29 2020, 07:55 AM
wish help you to fix your issue Yes, you are correct. Elementwise multiplication (like most other operations) is only supported for Tensor * Tensor or Variable * Variable, but not for Tensor * Variable. To perform your multiplication above, wrap your Tensor as a Variable which doesn't require gradients. The additional overhead is insignificant. code :
y_variable = torch.autograd.Variable(y_tensor, requires_grad=False)
x_variable * y_variable # returns Variable

How to write a Keras Layer for basic vector/tensor multiplication
By : user355295
Date : March 29 2020, 07:55 AM
should help you out You can use 1 as the batch dimension for the constant tensor in your Lambda: code :
import keras.backend as K
[...]
def my_lambda(x):
q_array = np.zeros((1, SEQ_LEN))
q_array = ... # your value
q = K.constant(q_array.T)
return Dot()([q, x])
result = Lambda(my_lambda)(word_embs)

Pytorch Inner Product of 3D tensor with 1D Tensor to generate 2D Tensor
By : cronos1412
Date : March 29 2020, 07:55 AM
should help you out Operation : I have pytorch tensor A of dimension [n x m x c] and B of dimension [1 x 1 x c]. I want to take inner product of each of 1 x 1 x c vector from A with B and hence generate a tensor C of dimension [n x m]. , There is a oneliner code :
ans = torch.einsum('nhwc,nc>nhw', img, aud)
import torch
N, H, W, C = 10, 11, 12, 13
img = torch.randn(N, H, W, C)
aud = torch.randn(N, C)
ans = torch.empty(N, H, W)
for batches in range(img.shape[0]):
for i in range(img.shape[1]):
for j in range(img.shape[2]):
ans[batches][i][j] = torch.dot(img[batches][i][j], aud[batches])
ans2 = torch.einsum('nhwc,nc>nhw', img, aud)
assert torch.allclose(ans, ans2, atol=1e6)

PyTorch conversion between tensor and numpy array: the addition operation
By : user2502141
Date : March 29 2020, 07:55 AM
help you fix your problem I am following the 60minute blitz on PyTorch but have a question about conversion of a numpy array to a tensor. Tutorial example here. , This actually has little to do with PyTorch. Compare code :
import numpy as np
a = np.ones(5)
b = a
np.add(a, 1, out=a)
print(b)
a = a + 1
print(b)
a[:] = a + 1
print(b)

Pytorch tensor multiplication with Float tensor giving wrong answer
By : Rahul Singh Tomar
Date : March 29 2020, 07:55 AM
should help you out In default, the tensor dtype is torch.float32 in pytorch. Change it to torch.float64 will give the right result. code :
x = torch.tensor([99397544.0], dtype=torch.float64)
y = torch.tensor([0.1], dtype=torch.float64)
x * y
# tensor([9939754.4000])

