简介:PyTorch如何查看Tensor大小与调整大小
PyTorch如何查看Tensor大小与调整大小
PyTorch是一个广泛使用的深度学习框架,它提供了许多功能强大的工具和张量库来支持我们的研究和开发工作。其中,张量是一个非常重要的概念,它可以看作是一个多维数组,用于存储各种类型的数据。在PyTorch中,我们经常需要查看张量的大小以及调整张量的大小。本文将介绍PyTorch中如何查看张量大小和调整大小的方法。
一、PyTorch如何查看Tensor大小
在PyTorch中,我们可以使用以下几种方法来查看张量的大小:
import torchx = torch.randn(3, 4, 5)print(x.size()) # 输出:(3, 4, 5)
import torchx = torch.randn(3, 4, 5)print(x.numel()) # 输出:60
二、PyTorch如何调整Tensor大小
import torchx = torch.randn(3, 4, 5)print(len(x)) # 输出:3
import torchx = torch.randn(3, 4, 5)y = x.resize_(2, 6, 7)print(x) # 输出:tensor([[-1.5124, -1.5124, -1.5124, -1.5124, -1.5124, -1.5124],# [-1.5124, -1.5124, -1.5124, -1.5124, -1.5124, -1.5124],# [-1.5124, -1.5124, -1.5124, -1.5124, -1.5124, -1.5124]])print(y) # 输出:tensor([[-1.5124, -1.5124, -1.5124, -1.5124, -1.5124, -1.5124],# [-1.5124, -1.5124, -1.5124, -0.8783, -0.8783, -0.8783],# [-0.8783, -0.8783, -0.8783, -0.8783, -0.8783, -0.8783]])