因为要使用MAML来解决跨环境的RL问题,其中模型参数需要内部更新和外部更新,所以传统的搭建方式不容易实现。借鉴github上的思路,记录一下灵活搭建网络完成多步更新的方式。
1.初始化
模型还是要继承nn.Module,在初试化的时候通过config将模型参数传入,然后将模型参数放入定义的nn.ParameterList()中。
def __init__(self, config, imgc, imgsz):
"""
:param config: network config file, type:list of (string, list)
:param imgc: 1 or 3
:param imgsz: 28 or 84
"""
super(Learner, self).__init__()
self.config = config
# this dict contains all tensors needed to be optimized
self.vars = nn.ParameterList()
# running_mean and running_var
self.vars_bn = nn.ParameterList()
for i, (name, param) in enumerate(self.config):
if name is 'conv2d':
# [ch_out, ch_in, kernelsz, kernelsz]
w = nn.Parameter(torch.ones(*param[:4]))
# gain=1 according to cbfin's implementation
torch.nn.init.kaiming_normal_(w)
self.vars.append(w)
# [ch_out]
self.vars.append(nn.Parameter(torch.zeros(param[0])))
elif name is 'convt2d':
# [ch_in, ch_out, kernelsz, kernelsz, stride, padding]
w = nn.Parameter(torch.ones(*param[:4]))
# gain=1 according to cbfin's implementation
torch.nn.init.kaiming_normal_(w)
self.vars.append(w)
# [ch_in, ch_out]
self.vars.append(nn.Parameter(torch.zeros(param[1])))
elif name is 'linear':
# [ch_out, ch_in]
w = nn.Parameter(torch.ones(*param))
# gain=1 according to cbfinn's implementation
torch.nn.init.kaiming_normal_(w)
self.vars.append(w)
# [ch_out]
self.vars.append(nn.Parameter(torch.zeros(param[0])))
elif name is 'bn':
# [ch_out]
w = nn.Parameter(torch.ones(param[0]))
self.vars.append(w)
# [ch_out]
self.vars.append(nn.Parameter(torch.zeros(param[0])))
# must set requires_grad=False
running_mean = nn.Parameter(torch.zeros(param[0]), requires_grad=False)
running_var = nn.Parameter(torch.ones(param[0]), requires_grad=False)
self.vars_bn.extend([running_mean, running_var])
elif name in ['tanh', 'relu', 'upsample', 'avg_pool2d', 'max_pool2d',
'flatten', 'reshape', 'leakyrelu', 'sigmoid']:
continue
else:
raise NotImplementedError
参数设置如下所示
config = [
('conv2d', [64, 1, 3, 3, 2, 0]),
('relu', [True]),
('bn', [64]),
# ('maxpool', [2, 0, 0]),
('conv2d', [64, 64, 3, 3, 2, 0]),
('relu', [True]),
('bn', [64]),
# ('maxpool', [2, 0, 0]),
('conv2d', [64, 64, 3, 3, 2, 0]),
('relu', [True]),
('bn', [64]),
# ('maxpool', [2, 0, 0]),
('flatten',[]),
('linear', [64, 256]),
('relu', [True]),
('linear', [args['n_way'], 64]),
('softmax', []),
]
最主要的三个是conv2d、maxpool以及linear,其中’conv2d’, [64, 1, 3, 3, 2, 0]代表了[out_channel,in_channel,kernel_size,kernel_size,stride,padding],(‘maxpool’, [2, 0, 0]),代表了[kernel_size,stride,padding],‘linear’, [64, 256]代表了[out_size,in_size]
2.重写forward函数
def forward(self, x, vars=None, bn_training=True):
if vars is None:
vars = self.vars
idx = 0
bn_idx = 0
for name, param in self.config:
if name == 'conv2d':
w, b = vars[idx], vars[idx + 1]
x = F.conv2d(x, w, b, stride=param[4], padding=param[5])
idx += 2
elif name == 'maxpool':
x = F.max_pool2d(x, param[0])
elif name == 'relu':
x = F.relu(x, inplace=param[0])
elif name == 'flatten':
x = x.view(x.size(0), -1)
elif name == 'softmax':
x = F.softmax(x)
elif name is 'bn':
w, b = vars[idx], vars[idx + 1]
running_mean, running_var = self.vars_bn[bn_idx], self.vars_bn[bn_idx+1]
x = F.batch_norm(x, running_mean, running_var, weight=w, bias=b, training=bn_training)
idx += 2
bn_idx += 2
elif name == 'linear':
w, b = vars[idx], vars[idx + 1]
x = F.linear(x, w, b)
idx += 2
else:
raise NotImplementedError
assert idx == len(vars)
assert bn_idx == len(self.vars_bn)
return x
按照顺序遍历初始化时加入到nn.ParameterList()中的参数,并进行计算。其中最复杂的是conv2d以及maxpool。使用torch.nn.functional中的conv2d以及maxpool进行计算,conv2d参数包含(input,weight,bias,stride,padding)其中weight要求是四维的参数类型,具体可以参考pytorch文档。
3.完整实现
import torch
from torch import nn
from torch.nn import functional as F
class Learner(nn.Module):
def __init__(self, config):
super(Learner, self).__init__()
self.config = config
self.vars = nn.ParameterList()
self.vars_bn = nn.ParameterList()
for i,(name,param) in enumerate(self.config):
if name == 'conv2d':
w = nn.Parameter(torch.ones(*param[:4]))
torch.nn.init.kaiming_normal_(w)
self.vars.append(w)
self.vars.append(nn.Parameter(torch.zeros(param[0])))
elif name == 'linear':
w = nn.Parameter(torch.ones(*param))
torch.nn.init.kaiming_normal_(w)
self.vars.append(w)
self.vars.append(nn.Parameter(torch.zeros(param[0])))
elif name is 'bn':
# [ch_out]
w = nn.Parameter(torch.ones(param[0]))
self.vars.append(w)
# [ch_out]
self.vars.append(nn.Parameter(torch.zeros(param[0])))
# must set requires_grad=False
running_mean = nn.Parameter(torch.zeros(param[0]), requires_grad=False)
running_var = nn.Parameter(torch.ones(param[0]), requires_grad=False)
self.vars_bn.extend([running_mean, running_var])
elif name in ['tanh', 'relu', 'upsample', 'avgpool', 'maxpool',
'flatten', 'reshape', 'softmax', 'sigmoid']:
continue
else:
raise NotImplementedError
def forward(self, x, vars=None, bn_training=True):
if vars is None:
vars = self.vars
idx = 0
bn_idx = 0
for name, param in self.config:
if name == 'conv2d':
w, b = vars[idx], vars[idx + 1]
x = F.conv2d(x, w, b, stride=param[4], padding=param[5])
idx += 2
elif name == 'maxpool':
x = F.max_pool2d(x, param[0])
elif name == 'relu':
x = F.relu(x, inplace=param[0])
elif name == 'flatten':
x = x.view(x.size(0), -1)
elif name == 'softmax':
x = F.softmax(x)
elif name is 'bn':
w, b = vars[idx], vars[idx + 1]
running_mean, running_var = self.vars_bn[bn_idx], self.vars_bn[bn_idx+1]
x = F.batch_norm(x, running_mean, running_var, weight=w, bias=b, training=bn_training)
idx += 2
bn_idx += 2
elif name == 'linear':
w, b = vars[idx], vars[idx + 1]
x = F.linear(x, w, b)
idx += 2
else:
raise NotImplementedError
assert idx == len(vars)
assert bn_idx == len(self.vars_bn)
return x
def zero_grad(self, vars=None):
with torch.no_grad():
if vars is None:
for p in self.vars:
if p.grad is not None:
p.grad.zero_()
else:
for p in vars:
if p.grad is not None:
p.grad.zero_()
def parameters(self):
return self.vars