zoukankan      html  css  js  c++  java
  • pytorch中的model.eval()和BN层

    class ConvNet(nn.module):
        def __init__(self, num_class=10):
            super(ConvNet, self).__init__()
            self.layer1 = nn.Sequential(nn.Conv2d(1, 16, kernel_size=5, stride=1, padding=2),
                                        nn.BatchNorm2d(16),
                                        nn.ReLU(),
                                        nn.MaxPool2d(kernel_size=2, stride=2))
            self.layer2 = nn.Sequential(nn.Conv2d(16, 32, kernel_size=5, stride=1, padding=2),
                                        nn.BatchNorm2d(32),
                                        nn.ReLU(),
                                        nn.MaxPool2d(kernel_size=2, stride=2))
            self.fc = nn.Linear(7*7*32, num_classes)
            
        def forward(self, x):
            out = self.layer1(x)
            out = self.layer2(out)
            print(out.size())
            out = out.reshape(out.size(0), -1)
            out = self.fc(out)
            return out
    

      


    # Test the model model.eval() # eval mode (batchnorm uses moving mean/variance instead of mini-batch mean/variance) with torch.no_grad(): correct = 0 total = 0 for images, labels in test_loader: images = images.to(device) labels = labels.to(device) outputs = model(images) _, predicted = torch.max(outputs.data, 1) total += labels.size(0) correct += (predicted == labels).sum().item()

    如果网络模型model中含有BN层,则在预测时应当将模式切换为评估模式,即model.eval()。

    评估模拟下BN层的均值和方差应该是整个训练集的均值和方差,即 moving mean/variance。

    训练模式下BN层的均值和方差为mini-batch的均值和方差,因此应当特别注意。

  • 相关阅读:
    10-多线程笔记-2-锁-3-Lock-4-工具类
    09-多线程笔记-2-锁-3-Lock-3-ReadWriteLock
    08-多线程笔记-2-锁-3-Lock-2-Lock
    07-多线程笔记-2-锁-3-Lock-1-AQS
    空闲时间无聊写的一个软著源代码文档生成器
    Centos7.x创建lvm
    cups API
    debezium 使用踩坑
    hive 行列转换
    mac 上docker 容器动态暴露端口
  • 原文地址:https://www.cnblogs.com/jiangkejie/p/9983451.html
Copyright © 2011-2022 走看看