PyTorch expansion简介

506 阅读4分钟

PyTorch expand

PyTorch expansion简介

PyTorch expand是张量库中可用的属性,可用于执行扩展的操作。该属性将指定的张量对象作为输入扩展到一个新的维度。张量被展开的新维度是一个单子维度。

在这篇文章中,我们将尝试了解什么是PyTorch扩展,如何使用PyTorch扩展,如何执行PyTorch扩展;例如,PyTorch扩展和我们方面的结论。

这里还有一点需要注意的是,由于扩展后创建的是一个新的视图,而不是一个对象,所以在扩展操作中没有做任何内存分配。相反,内部发生的情况是值大小为1的维度被扩展,并通过将stride属性的值设置为0变成更大的尺寸。

唯一需要传输的参数是指定张量对象所需扩展的大小的尺寸。

什么是PyTorch expansion?

PyTorch expand是张量库中可用的属性,它允许用户在一个特定的新的单子维度中扩展所提供的张量输入。

  • 当我们使用扩展属性来扩展指定的张量对象时,不会创建一个新的副本,我们可以看到新的视图为我们传递的同一个原始张量值。
  • 当特定维度的值被设置为-1时,那么源张量的扩展就不会沿着该维度发生。
  • 让我们考虑一个例子来理解这种行为。当我们有一个特定的张量对象(5,1)时,我们可以使用tensor.expand()属性以及1尺寸的维度值来执行扩展。

如何使用PyTorch expand?

我们可以使用张量库中的PyTorch expand属性,只需在程序的顶部导入张量即可。我们可以沿着维度来扩展张量,这个维度在数值上是一个单子。尽管扩展张量到更多的维度也是允许的,但由此产生的张量将包含所有附加在前端的新值。另外,在这样做的时候,我们不能将维数的大小设置为-1。

如何进行PyTorch扩展?

让我们讨论一下如何在PyTorch中使用tensor.expand()属性进行PyTorch扩展,具体方法如下-- 1.

  • 第一步将是导入所需的torch库,其中有张量和扩展的定义。在你导入库之前,先检查一下你是否已经安装了它。导入语句看起来像--导入Torch。
  • 现在,我们必须定义一个张量的对象,它将作为扩展的来源,其值至少应包含一个具有单子值的维度。例如,它可以被定义为 - sampleEducbaTensor = torch.tensor ( [[1],[2],[3]] )
  • 现在,是时候沿着单子维度对张量值进行扩展操作了。注意一件事:沿非单子维度扩展张量对象将导致运行时错误。我们很快就会在例子部分看到一个与此相关的例子。扩展可以通过使用语句educbaResultantExpandedTensor = sampleEducbaTensor.expand (3,2)来完成。
  • 最后一步是向用户显示结果输出,其中包括我们的扩展张量 --

Print ("扩展后的张量是," educbaResultantExpandedTensor)

PyTorch扩展实例

现在让我们来了解一下PyTorch expand的实现,以及它是如何在某些例子的帮助下工作的。

示例#1

在这个例子中,我们将尝试把大小为(3,1)的张量扩展到(3,2)。在这样做的时候,扩展只发生在数值为1的单一维度上,而其他尺寸为3的维度保持不动,没有变化。

# We will import the torch library that will be required to create tensor object
import torch
# define and initiate the tensor object
sampleEducbaTensor = torch.tensor([[1],[2],[3]])
# Display the value of tensor in the output screen
print("Input tensor value :\n", sampleEducbaTensor )
print("Size of input tensor :\n", sampleEducbaTensor.size())
# Perform the expansion of the original tensor
sampleExpandedValue = sampleEducbaTensor.expand(3,2)
print("Value of tensor after expanding :\n", sampleExpandedValue )

执行上述程序的输出结果如下

PyTorch expand output 1

例子#2

现在,我们将把大小为(1,3)的张量扩展到(3,3)。即使在这种情况下,张量的扩展也将沿着1的尺寸进行。

# We will import the torch library that will be required to create tensor object
import torch
# define and initiate the tensor object
sampleEducbaTensor2 = torch.tensor([[1,2,3]])
# Display the value of tensor in the output screen
print("Value of input tensor object :\n", sampleEducbaTensor2 )
# size of tensor is [1,3] print("Size of input tensor :\n", sampleEducbaTensor2.size())
# Perform the expansion of the original tensor
sampleEducbaExpandedTensor = sampleEducbaTensor2.expand(3,-1)
print("Value of the resultant expanded tensor:\n", sampleEducbaExpandedTensor )
print("Size of output tensor :\n", sampleEducbaExpandedTensor.size())

执行上述程序后,输出结果如下

PyTorch expand output 2

例子 #3

当我们试图沿着非单一维度扩展张量时,将导致运行时错误,如本例所示

# We will import the torch library that will be required to create tensor object
import torch
# define and initiate the tensor object
sampleEducbaTensor3 = torch.tensor([[1,2,3]])
# Display the value of tensor in the output screen
print("Value of input tensor :\n", sampleEducbaTensor3 )
# size of tensor is [1,3] print("Size of the input tensor object :\n", sampleEducbaTensor3.size())
sampleEducbaTensor3.expand(3,4)

在执行上述程序时,由于我们要沿着更大的数字的维度扩展张量,所以会出现以下的输出和运行时错误。

output 3

结论

PyTorch扩展用于沿着具有单子大小的维度扩展张量对象。在这样做的时候,只创建一个新的张量视图,而不是一个新的对象。