forward 和 normal_train代码
2021SC@SDUSC
Forward
现定义好网络,实现前向传播
class Network(nn.Module):
def init(self):
super().init()
self.conv1 = nn.Conv2d(in_channels=1,out_channels=6,kernel_size=5)
self.conv2 = nn.Conv2d(in_channels=6,out_channels=12,kernel_size=5)
self.fc1 = nn.Linear(in_features=12*4*4,out_features=120)
self.fc2 =nn.Linear(in_features=120,out_features=60)
self.out = nn.Linear(in_features=60,out_features=10)
输入层
def forward(self,t):
t = t
隐藏cov层
t = self.conv1(t)
t = F.relu(t)
t = F.max_pool2d(t,kernel_size=2,stride=2)
**
normal_train 代码分析
add_argument() 方法
ArgumentParser.add_argument(name or flags…[, action][, nargs][, const][, default][, type][, choices][, required][, help][, metavar][, dest])
创建对象
parser = argparse.ArgumentParser(description='Testing...')
添加单个命令参数
parser.add_argument('test',type=int)
输入指定类型
parser.add_argument('test_1',type=float)
default 在没有命令输入时的默认值
parser.add_argument('-test_2','-i',type=float,default=2)
使参数创建并生效
args = parser.parse_args()
实例代码如下:
parser.add_argument('--epochs', default=50, type=int, metavar='N',
help='number of total epochs to run')
parser.add_argument('--batch-size', default=4, type=int, metavar='N',
help='train batchsize')
parser.add_argument('--batch-size-u', default=24, type=int, metavar='N',
help='train batchsize')
parser.add_argument('--lrmain', '--learning-rate-bert', default=0.00001, type=float,
metavar='LR', help='initial learning rate for bert')
parser.add_argument('--lrlast', '--learning-rate-model', default=0.001, type=float,
metavar='LR', help='initial learning rate for models')