如何在 python 中列出 pytorch 张量?

How to make a list of pytorch tensors in python?

如何在 Pytorch 中创建张量列表。 该列表应如下所示:

mylist = [tensor1, tensor2, tensor3]

其中所有张量都有不同的形状

您可以使用 pytorch 内联实例化每个张量或在循环中附加到列表。 内联:

mylist = [torch.rand(2), torch.rand(5), torch.rand(1)]

循环中:

mylist = [torch.rand(i) for i in range(1, 5)]

要创建自定义张量,例如使用 torch.tensor([[1., -1.], [1., -1.]])https://pytorch.org/docs/stable/tensors.html