【随手记】pytorch
作者:互联网
pytorch
前言
提示:一些常用的小笔记,摘抄自网络。
一、概述
1.引入库
import torch.nn as nn
二、常见方法
2.Dropout
input_size = 28 * 28
hidden_size = 500
num_classes = 10
# 三层神经网络
class NeuralNet(nn.Module):
def __init__(self, input_size, hidden_size, num_classes):
super(NeuralNet, self).__init__()
self.fc1 = nn.Linear(input_size, hidden_size) # 输入层到影藏层
self.relu = nn.ReLU()
self.fc2 = nn.Linear(hidden_size, num_classes) # 影藏层到输出层
self.dropout = nn.Dropout(p=0.5) # dropout训练
def forward(self, x):
out = self.fc1(x)
out = self.dropout(out)
out = self.relu(out)
out = self.fc2(out)
return out
model = NeuralNet(input_size, hidden_size, num_classes)
model.train()
model.eval()
注意:Dropout同其他神经网络层一样,应先进行实例化,再进行使用,否则无效,并且应使用model.train()或者model.eval()。
标签:随手,nn,self,pytorch,hidden,size,model,out 来源: https://blog.csdn.net/weixin_45890238/article/details/122775692