文章目录(Table of Contents)
介绍
很多时候,我们在使用Pytorch时,想要能够动态的改变网络,希望使用for循环进行add layers,这里记录一下找到的几种方式。可以直接看方式三。
动态改变方式
下面介绍三种方式,可以直接看方式三,个人比较喜欢用方式三,也是比较好用的一种方式。
方式一
通过moduleList来进行改变。但是有缺点, 需要自己向ModuleList中增加结构, 还是麻烦的。可以看下面的例子。
- class MyModel(nn.Module):
- def __init__(self, in_features, nb_classes, nb_hidden_layer,
- hidden_size, act=nn.ReLU):
- super(MyModel, self).__init__()
- self.act = act()
- self.fc1 = nn.Linear(in_features, hidden_size)
- self.fcs = nn.ModuleList([nn.Linear(hidden_size, hidden_size),
- nn.Linear(hidden_size, hidden_size),
- nn.Linear(hidden_size, hidden_size)])
- self.out = nn.Linear(hidden_size, nb_classes)
- def forward(self, x):
- x = self.act(self.fc1(x))
- for l in self.fcs:
- x = F.relu(l(x))
- x = self.out(x)
- return x
- model = MyModel(2, 3, 4, 5, nn.ReLU)
- print(model)
![Pytorch动态改变网络结构(Dynamically add or delete layers)](https://img.mathpretty.com/20190420_114625_pxt2xkj.jpg)
方式二
通过OrderedDict来实现,这种方式还是比较方便的。
- from collections import OrderedDict
- class model(nn.Module):
- def __init__(self, n_layers):
- super(model,self).__init__()
- layers = OrderedDict()
- for i in range(n_layers):
- layers[str(i)] = nn.Linear(5,5)
- self.layers = nn.Sequential(layers)
- model(n_layers=5)
![Pytorch动态改变网络结构(Dynamically add or delete layers)](https://img.mathpretty.com/20190420_114734_jre6n74.jpg)
方式三
使用传参的方式进行动态改变网络结构,需要注意的是,这里在传入网络的时候,需要使用layers进行操作。详细的介绍可以查看这篇文章:Python入门教程[3]-函数
- class model(nn.Module):
- def __init__(self, n_layers):
- super(model,self).__init__()
- layers = []
- for i in range(n_layers):
- layers.append(nn.Linear(5,5))
- # 组成nn.Sequential
- # 这里传参需要使用 *layers, 将参数打包
- self.layers = nn.Sequential(*layers)
- def forward(self,x):
- x = self.layers(x)
- return(x)
- model(n_layers=5)
![Pytorch动态改变网络结构(Dynamically add or delete layers)](https://img.mathpretty.com/20190420_114734_jre6n74.jpg)
一个实例
下面,放一个我们在具体使用的时候,会书写的结构。
- # 每一层加上BatchNorm和Relu
- class model(nn.Module):
- def __init__(self, n_layers):
- super(model,self).__init__()
- layers = []
- for i in range(n_layers):
- layers.append(nn.Linear(5,5))
- layers.append(nn.BatchNorm1d(5))
- layers.append(nn.ReLU())
- # 组成nn.Sequential
- # 这里传参需要使用 *layers, 将参数打包
- self.input = nn.Linear(2,5)
- self.relu = nn.ReLU()
- self.layers = nn.Sequential(*layers)
- self.output = nn.Linear(5,2)
- def forward(self,x):
- out = self.input(x)
- out = self.relu(out)
- out = self.layers(out)
- out = self.output(out)
- return(out)
- mymodel = model(n_layers=5)
- print(mymodel)
![Pytorch动态改变网络结构(Dynamically add or delete layers)](https://img.mathpretty.com/20190420_115551_m7fvq2t.jpg)
看一下输入一个值的效果:
![Pytorch动态改变网络结构(Dynamically add or delete layers)](https://img.mathpretty.com/20190420_115707_xhsx2vy.jpg)
参考资料
![Pytorch动态改变网络结构(Dynamically add or delete layers)](https://img.mathpretty.com/20190420_201106_w78x99p.jpg)
- 微信公众号
- 关注微信公众号
-
- QQ群
- 我们的QQ群号
-
评论