Python torch.flatte()和nn.flatte()之间的差异
Python torch.flatte()和nn.flatte()之间的差异,python,neural-network,pytorch,tensor,difference,Python,Neural Network,Pytorch,Tensor,Difference,torch.flatte()和torch.nn.flatte()之间有什么区别?在PyTorch中,展平有三种形式 作为直接应用于张量的张量方法(oop样式):x.flatte() 作为函数(函数形式)应用为:torch.flatten(x) 作为一个模块(层nn.module)。通常用于模型定义中 这三个都是相同的,共享相同的实现,唯一的区别是nn。展平默认情况下将start\u dim设置为1,以避免展平第一个轴(通常是批处理轴)。而另外两个从轴=0展平到轴=-1,即整个张量,如果
torch.flatte()
和torch.nn.flatte()
之间有什么区别?在PyTorch中,展平有三种形式
- 作为直接应用于张量的张量方法(oop样式):
x.flatte()
- 作为函数(函数形式)应用为:
torch.flatten(x)
- 作为一个模块(层
)。通常用于模型定义中nn.module
这三个都是相同的,共享相同的实现,唯一的区别是
nn。展平
默认情况下将start\u dim
设置为1
,以避免展平第一个轴(通常是批处理轴)。而另外两个从轴=0
展平到轴=-1
,即整个张量,如果没有参数的话。你可以把火炬.展平()
的工作想象成只做张量的展平操作,而不附加任何字符串。你给一个张量,它变平,然后返回。就这些
相反,nn.flatte()
要复杂得多(即,它是一个神经网络层)。由于是面向对象的,它继承自nn.Module
,尽管它用于展平张量。你可以把它看作是torch.flatte()
上的一个语法糖块
重要区别:一个显著区别是,
torch.flatte()
始终返回一个1D张量作为结果,前提是输入至少为1D或更大,而nn.flatte()
始终返回一个2D张量,前提是输入至少为2D或更大(使用1D张量作为输入,它将抛出一个)
比较:
是一个API,而torch.flatte()
是一个神经网络层nn.flatte()
是一个python函数,而torch.flatte()
是一个python类nn.flatte()
- 基于以上几点,
可以在野外使用(例如,用于简单的张量运算),而torch.flatte()
预计将在nn.flatte()
块中作为层之一使用nn.Sequential()
没有关于计算图的信息,除非它卡在其他图形感知块中(使用torch.flatte()
标志设置为tensor.requires_grad
),而True
始终由autograd跟踪nn.flatte()
不能接受和处理(如线性/conv1D)层作为输入,而torch.flatte()
主要用于处理这些神经网络层nn.flatte()
和torch.flatte()
都将视图返回到输入张量。因此,对结果的任何修改也会影响输入张量。(见下面的代码)nn.flatte()
代码演示:
# input tensors to work with
In [109]: t1 = torch.arange(12).reshape(3, -1)
In [110]: t2 = torch.arange(12, 24).reshape(3, -1)
In [111]: t3 = torch.arange(12, 36).reshape(3, 2, -1) # 3D tensor
使用火炬展平。展平():
使用nn.flant()展平
花絮:torch.flatte()
是nn.flatte()
的前身,它从一开始就存在。然后,出现了一个合法的问题,因为这是几乎所有convnet(就在softmax之前或其他地方)的共同要求。因此,它后来被添加到
最近也有关于模型手术的报道。在C代码中,Flatte是否仍然使用了Reformate?发现如下:。似乎它使用了视图
,这是一种重塑!
In [113]: t1flat = torch.flatten(t1)
In [114]: t1flat
Out[114]: tensor([ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11])
# modification to the flattened tensor
In [115]: t1flat[-1] = -1
# input tensor is also modified; thus flattening is a view.
In [116]: t1
Out[116]:
tensor([[ 0, 1, 2, 3],
[ 4, 5, 6, 7],
[ 8, 9, 10, -1]])
In [123]: nnfl = nn.Flatten()
In [124]: t3flat = nnfl(t3)
# note that the result is 2D, as opposed to 1D with torch.flatten
In [125]: t3flat
Out[125]:
tensor([[12, 13, 14, 15, 16, 17, 18, 19],
[20, 21, 22, 23, 24, 25, 26, 27],
[28, 29, 30, 31, 32, 33, 34, 35]])
# modification to the result
In [126]: t3flat[-1, -1] = -1
# input tensor also modified. Thus, flattened result is a view.
In [127]: t3
Out[127]:
tensor([[[12, 13, 14, 15],
[16, 17, 18, 19]],
[[20, 21, 22, 23],
[24, 25, 26, 27]],
[[28, 29, 30, 31],
[32, 33, 34, -1]]])