简体   繁体   English

了解 PyTorch 张量形状

[英]Understanding PyTorch Tensor Shape

I have a simple question regarding the shape of tensor we define in PyTorch.我有一个关于我们在 PyTorch 中定义的张量形状的简单问题。 Let's say if I say:假设我说:

input = torch.randn(32, 35)

This will create a matrix with 32 row and 35 columns.这将创建一个 32 行 35 列的矩阵。 Now when I define:现在,当我定义:

input2 = torch.randn(1,2,32, 35)

What can I say about the dimension of the new matrix input2?关于新矩阵 input2 的维度,我能说些什么? How can I define the rows and columns here?我如何在这里定义行和列? I mean do I have two matrices with shapes 32*35 packed by the tensor?我的意思是我有两个由张量打包的形状为 32*35 的矩阵吗?

I want to better understand the geometry behind this.我想更好地理解这背后的几何。 Thanks.谢谢。

Consider tensor shapes as the number of lists that a dimension holds.将张量形状视为维度包含的列表数量。 For instance, a tensor shaped (4, 4, 2) will have four elements, which will all contain 4 elements, which in turn have 2 elements.例如,一个形状为 (4, 4, 2) 的张量将有四个元素,这些元素都包含 4 个元素,而这些元素又包含 2 个元素。

  1. The first holds 4 elements.第一个包含 4 个元素。
  2. The second holds 4 elements.第二个包含 4 个元素。
  3. The third dimension holds 2 elements.第三个维度包含 2 个元素。

在此处输入图片说明

Here's what the data would look like:数据如下所示:

[[[0.86471446, 0.26302726],
  [0.04137454, 0.00349315],   
  [0.06559607, 0.45617865],
  [0.0219786, 0.27513594]],

 [[0.60555118, 0.10853228],
  [0.07059685, 0.32746256],
  [0.99684617, 0.07496456],
  [0.55169005, 0.39024103]],

 [[0.55891377, 0.41151245],
  [0.3434965, 0.12956237],
  [0.74908291, 0.69889266],
  [0.98600141, 0.8570597]],

 [[0.7903229, 0.93017741],
  [0.54663242, 0.72318166],
  [0.6099451, 0.96090241],
  [0.63772238, 0.78605599]]]

In other words, four elements of four elements of two elements.也就是说,四元素二元素四元素。

Yes, that is correct.对,那是正确的。 Your input2 tensor has a rank of 4. (Rank is the Dimension) and the bounds of each dimension are (1,2,32,35)您的 input2 张量的等级为 4。(等级是维度)并且每个维度的界限是 (1,2,32,35)

  1. The first dimension can hold one element.第一个维度可以容纳一个元素。
  2. The second can hold two.第二个可以容纳两个。
  3. The third can hold 32 elements.第三个可以容纳 32 个元素。
  4. The forth dimension can hold 35 elements.第四维可以容纳 35 个元素。

EDIT: I find it is useful to think of higher-dimensional arrays as a series of lists.编辑:我发现将高维数组视为一系列列表很有用。 In your case, a rank 4 tensor, would be a list of lists of lists of lists.在您的情况下,等级 4 张量将是列表列表的列表。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM