import torch
from torch.autograd import Variable
import numpy as np
x = np.transpose(np.array([[1, 2, 3, 4]]))
a = Variable(torch.rand(1), requires_grad=True)
print(a * x) # error!
我希望结果如x = [[2] [4] [6] [8]]如果a = 2
有什么解决方案吗?
答案 0 :(得分:1)
您正在寻找的是矩阵乘法中的点标量积。
尝试:
x = np.transpose(np.array([[1, 2, 3, 4]]))
a = 2
x.dot(a)
输出矩阵[[2] [4] [6] [8]]