当前位置:   article > 正文

PyTorch中的.train()与self.training

self.training

设置.train(),self.training=True

设置.eval(),self.training=False

class MyNet(nn.Module):
    def __init__(self):
        super(MyNet, self).__init__()
        self.features = nn.Sequential(
            nn.Conv2d(1, 32, kernel_size=3, padding=1, bias = False),
            nn.BatchNorm2d(32, affine=False),
        )

        self.classifier = nn.Linear(512, 20, bias=False)
        
    def forward(self, input):
        x_features = self.features(input)
        x = x_features.view(x_features.size(0), -1)
        
        if self.training is False:
            return x
        
        x = self.classifier(x)
        return x
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
m = MyNet()
m.train()
print(m.training)
m.eval()
print(m.training)
  • 1
  • 2
  • 3
  • 4
  • 5
True
False
  • 1
  • 2
input = torch.randn(1, 1, 4, 4)
m.train()
output = m(input)
print(output.size())
m.eval()
output1 = m(input)
print(output1.size())
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
torch.Size([1, 20])
torch.Size([1, 512])
  • 1
  • 2
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/繁依Fanyi0/article/detail/105699
推荐阅读
相关标签
  

闽ICP备14008679号