pytorch 模型的train模式与eval模式实例
2020-03-11 17:43
856 查看
原因
对于一些含有batch normalization或者是Dropout层的模型来说,训练时的froward和验证时的forward有计算上是不同的,因此在前向传递过程中需要指定模型是在训练还是在验证。
源代码
[docs] def train(self, mode=True): r"""Sets the module in training mode. This has any effect only on certain modules. See documentations of particular modules for details of their behaviors in training/evaluation mode, if they are affected, e.g. :class:`Dropout`, :class:`BatchNorm`, etc. Returns: Module: self """ self.training = mode for module in self.children(): module.train(mode) return self [docs] def eval(self): r"""Sets the module in evaluation mode. This has any effect only on certain modules. See documentations of particular modules for details of their behaviors in training/evaluation mode, if they are affected, e.g. :class:`Dropout`, :class:`BatchNorm`, etc. """ #该方法调用了nn.train()方法,把参数默认值改为false. 增加聚合性 return self.train(False)
在使用含有BN层,dropout层的神经网路来说,必须要区分训练和验证
以上这篇pytorch 模型的train模式与eval模式实例就是小编分享给大家的全部内容了,希望能给大家一个参考
您可能感兴趣的文章:
相关文章推荐
- pytorch 模型的train模式与eval模式
- Pytorch中的train和eval模式详解
- pytorch构建多模型实例
- PyTorch加载预训练模型实例(pretrained)
- pytorch:model.train和model.eval用法及区别详解
- 使用PyTorch进行训练和测试时一定注意要把实例化的model指定train/eval
- JavaScript中“单实例模式(单值模型)”的实现
- pytorch 求网络模型参数实例
- pytorch中的pre-train函数模型引用及修改(增减网络层,修改某层参数等)
- JavaScript中“单实例模式(单值模型)”的实现
- pytorch中获取模型input/output shape实例
- 设计模式----模型模式 实例
- Pytorch修改ResNet模型全连接层进行直接训练实例
- Pytorch加载部分预训练模型的参数实例
- 实例探索Java模式之路——原始模型
- 设计模式实例-策略模式
- 单实例模式 加锁
- 装饰者模式---汉堡实例
- 【干货】基于pytorch的CNN、LSTM神经网络模型调参小结
- JavaScript设计模式之观察者模式实例详解