PyTorch - Elementary multiplication between a variable and a tensor?

How can I do type multiplication with a variable and tensor in PyTorch? It works great with two tensors. With a variable and a scalar it works fine. But when I try to multiply by elements with a variable and a tensor, I get:

XXXXXXXXXXX in mul
    assert not torch.is_tensor(other)
AssertionError

For example, at startup:

import torch

x_tensor = torch.Tensor([[1, 2], [3, 4]])
y_tensor = torch.Tensor([[5, 6], [7, 8]])

x_variable = torch.autograd.Variable(x_tensor)

print(x_tensor * y_tensor)
print(x_variable * 2)
print(x_variable * y_tensor)

I expect the first and last print statements to show similar results. The first two multiplications work as expected with the error that occurs in the third. I tried using aliases *in PyTorch (i.e. x_variable.mul(y_tensor), torch.mul(y_tensor, x_variable)etc.).

It seems that multiplying the elements between the tensor and the variable is not supported given the error and the code that produces it. It's right? Or am I missing something? Thank!

+4
1

, . ( ) Tensor * Tensor Variable * Variable, Tensor * Variable.

, Tensor Variable, . .

y_variable = torch.autograd.Variable(y_tensor, requires_grad=False)
x_variable * y_variable # returns Variable

, , Variables , . , Tensors , .

+8

Source: https://habr.com/ru/post/1680601/


All Articles