【问题标题】:Pytorch Where Does resNet add values?Pytorch resNet 在哪里加值?
【发布时间】:2018-12-22 06:32:06
【问题描述】:

我正在研究 ResNet,我发现了一个使用加号跳过连接的实现。像下面这样

Class Net(nn.Module):
    def __init__(self):
        super(Net, self).__int_() 
            self.conv = nn.Conv2d(128,128)

    def forward(self, x):
        out = self.conv(x) // line 1 
        x = out + x    // skip connection  // line 2

现在我已经调试并打印了第 1 行前后的值。输出如下:

在第 1 行之后
x = [1,128,32,32]
出 = [1,128,32,32]

第 2 行之后
x = [1,128,32,32] // 仍然

参考链接:https://github.com/kuangliu/pytorch-cifar/blob/bf78d3b8b358c4be7a25f9f9438c842d837801fd/models/resnet.py#L62

我的问题是它在哪里增加了价值?我是说之后

x = out + x

操作,值加到哪里了?

PS:Tensor 格式为 [batch, channel, height, width]。

【问题讨论】:

  • 嗨!你能改进一下这个问题吗?即你真正想知道什么?为什么加法在这里有效?当然,如果 out 是 0 张量,x 的值可以保持不变。但是,您的张量看起来很奇怪。你不确定你的意思是张量的大小吗?
  • 这看起来像张量的大小而不是张量
  • "这看起来像张量的大小而不是张量!!"能详细点吗?

标签: neural-network deep-learning pytorch tensor resnet


【解决方案1】:

正如@UmangGupta 在 cmets 中提到的,您打印的似乎是张量的形状(即3x3 矩阵的“形状”是[3, 3]),而不是它们的内容。 在您的情况下,您正在处理 1x128x32x32 张量)。

希望阐明形状和内容之间区别的示例:

import torch

out = torch.ones((3, 3))
x = torch.eye(3, 3)
res = out + x

print(out.shape)
# torch.Size([3, 3])
print(out)
# tensor([[ 1.,  1.,  1.],
#         [ 1.,  1.,  1.],
#         [ 1.,  1.,  1.]])
print(x.shape)
# torch.Size([3, 3])
print(x)
# tensor([[ 1.,  0.,  0.],
#         [ 0.,  1.,  0.],
#         [ 0.,  0.,  1.]])
print(res.shape)
# torch.Size([3, 3])
print(res)
# tensor([[ 2.,  1.,  1.],
#         [ 1.,  2.,  1.],
#         [ 1.,  1.,  2.]])

【讨论】:

    猜你喜欢
    • 2019-05-24
    • 2021-05-18
    • 2020-03-26
    • 2021-10-23
    • 2021-09-08
    • 2019-02-08
    • 2020-04-14
    • 2019-07-17
    • 1970-01-01
    相关资源
    最近更新 更多