我有一个非常简单的代码:
linear = nn.Linear(3, 4)
# Create ones (batch_size, 3) as input
input = torch.ones(5, 3)
# Use the linear layer to compute the output
output = linear(input)
print(output) # should be (batch_size, 4)
print(input)
print(linear)
有了这个输出
tensor([[-0.0253, 0.3926, -0.5856, 0.7869],
[-0.0253, 0.3926, -0.5856, 0.7869],
[-0.0253, 0.3926, -0.5856, 0.7869],
[-0.0253, 0.3926, -0.5856, 0.7869],
[-0.0253, 0.3926, -0.5856, 0.7869]], grad_fn=<AddmmBackward0>)
tensor([[1., 1., 1.],
[1., 1., 1.],
[1., 1., 1.],
[1., 1., 1.],
[1., 1., 1.]])
Linear(in_features=3, out_features=4, bias=True)
我想知道随机 (5,4) 张量从哪里来。 怎么改成全0呢?
编辑
这段代码有什么问题?
从列表创建张量:
如果您想使用自己的值初始化 Pytorch 张量,可以按如下方式快速完成:
import torch
my_list = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
my_tensor = torch.tensor(my_list)