我从哪里获得这个张量中的随机值?

问题描述 投票:0回答:1

我有一个非常简单的代码:

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呢?

编辑

这段代码有什么问题?

python matrix deep-learning pytorch tensor
1个回答
0
投票

从列表创建张量:

如果您想使用自己的值初始化 Pytorch 张量,可以按如下方式快速完成:

import torch

my_list = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
my_tensor = torch.tensor(my_list)
© www.soinside.com 2019 - 2024. All rights reserved.