![](/img/trans.png)
[英]What is the difference between x.view(x.size(0), -1) and torch.nn.Flatten() layer and torch.flatten(x)? pytorch question
[英]Why return self.head(x.view(x.size(0), -1)) in the nn.Module for pyTorch reinforcement learning example
我知道平衡极点示例需要 2 个输出。 强化学习 (DQN) 教程
这是 self.head 的 output
print ('x',self.head)
x = Linear(in_features=512, out_features=2, bias=True)
当我运行下面的时期是输出:
print (self.head(x.view(x.size(0), -1)))
return self.head(x.view(x.size(0), -1))
tensor([[-0.6945, -0.1930]])
tensor([[-0.0195, -0.1452]])
tensor([[-0.0906, -0.1816]])
tensor([[ 0.0631, -0.9051]])
tensor([[-0.0982, -0.5109]])
...
x 的大小为:
x = torch.Size([121, 32, 2, 8])
所以我想了解 x.view(x.size(0), -1) 在做什么?
我从代码中的注释中了解到它正在返回:返回张量([[left0exp,right0exp]...])。
但是,作为 torch.Size([121, 32, 2, 8]) 的 x 是如何减少到大小为 2 的张量的呢?
有没有其他更有意义的写作方式? 如果我有 4 个输出怎么办。 我将如何表示? 为什么是 x.size(0)。 为什么是-1?
所以似乎将 self.head 与 4 个输出变为 2 个输出。 那是对的吗?
在底部是 class 我指的是:
class DQN(nn.Module):
def __init__(self, h, w, outputs):
super(DQN, self).__init__()
self.conv1 = nn.Conv2d(3, 16, kernel_size=5, stride=2)
self.bn1 = nn.BatchNorm2d(16)
self.conv2 = nn.Conv2d(16, 32, kernel_size=5, stride=2)
self.bn2 = nn.BatchNorm2d(32)
self.conv3 = nn.Conv2d(32, 32, kernel_size=5, stride=2)
self.bn3 = nn.BatchNorm2d(32)
# Number of Linear input connections depends on output of conv2d layers
# and therefore the input image size, so compute it.
def conv2d_size_out(size, kernel_size = 5, stride = 2):
return (size - (kernel_size - 1) - 1) // stride + 1
convw = conv2d_size_out(conv2d_size_out(conv2d_size_out(w)))
convh = conv2d_size_out(conv2d_size_out(conv2d_size_out(h)))
linear_input_size = convw * convh * 32
self.head = nn.Linear(linear_input_size, outputs)
# Called with either one element to determine next action, or a batch
# during optimization. Returns tensor([[left0exp,right0exp]...]).
def forward(self, x):
x = F.relu(self.bn1(self.conv1(x)))
x = F.relu(self.bn2(self.conv2(x)))
x = F.relu(self.bn3(self.conv3(x)))
return self.head(x.view(x.size(0), -1))
x.view(x.size(0), -1)
正在展平张量,这是因为线性层只接受一个向量(一维数组)。 为了分解它, x.view()
重塑了指定形状的张量(更多信息)。 x.shape(0)
返回张量的第一维(这是批量大小,应该保持不变)。 x.view()
中的-1
是一个填充符,换句话说,它的维度是我们不知道的,所以 PyTorch 会自动计算它。 例如,如果x = torch.tensor([1,2,3,4])
,要将张量重塑为2x2
,您可以执行x.view(2,2)
或x.view(2,-1)
或x.view(-1,2)
。 output 形状不是2
的张量形状,而是121,2
的张量形状( 121
是批量大小, 2
来自线性层output
)。 因此,要将 output 的大小从 2 更改为 4,您必须将__init__
function 中的输出参数更改为 4。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.