Pytorch自定义创建BP神经网络
2024-09-02 14:23:52
class BPNet(nn.Module):
def __init__(self, in_dim, n_hidden_1, n_hidden_2,\
n_hidden_3, n_hidden_4, n_hidden_5, out_dim):
super(BPNet, self).__init__()
self.layer1 = nn.Sequential(nn.Linear(in_dim, n_hidden_1))
self.layer2 = nn.Sequential(nn.Linear(n_hidden_1, n_hidden_2), nn.BatchNorm1d(n_hidden_2))
self.layer3 = nn.Sequential(nn.Linear(n_hidden_2, n_hidden_3), nn.BatchNorm1d(n_hidden_3), nn.ReLU(True))
self.layer4 = nn.Sequential(nn.Linear(n_hidden_3, n_hidden_4), nn.BatchNorm1d(n_hidden_4), nn.ReLU(True), nn.Dropout(0.1))
self.layer5 = nn.Sequential(nn.Linear(n_hidden_4, n_hidden_5), nn.BatchNorm1d(n_hidden_5))
self.layer6 = nn.Sequential(nn.Linear(n_hidden_5, out_dim)) def forward(self, x):
x = self.layer1(x)
x = self.layer2(x)
x = self.layer3(x)
x = self.layer4(x)
x = self.layer5(x)
x = self.layer6(x)
return x
net = BPNet(in_dim=5, n_hidden_1=20, n_hidden_2=250, n_hidden_3=500, n_hidden_4=250, n_hidden_5=50, out_dim=2) # 实例化网络 简洁写法
cfg = {
'': [20, 200, 500, 200, 50], } class BPNet(nn.Module):
def __init__(self, name):
super(BPNet, self).__init__()
self.features = self._make_layers(cfg[name])
self.classifier = nn.Sequential(
nn.Linear(cfg[name][-1], 2)
) def forward(self, x):
out = self.features(x)
out = out.view(out.size(0), -1)
out = self.classifier(out)
return out def _make_layers(self, cfg):
layers = []
in_dim = 5
for x in cfg:
layers += [nn.Linear(in_dim, x),
nn.BatchNorm1d(x),
nn.ReLU(inplace=True)]
in_dim = x
return nn.Sequential(*layers)
net = BPNet('1')
最新文章
- C# 管理员身份运行程序
- linux升级openssl
- 隐藏UITableView多余的分割线
- Zigzag
- iOS 常用英语翻译
- linux下安装软件后的环境变量设置
- kindle paperwhite 使用说明
- 杜教的AAA树
- ExtJS4 根据分配不同的树形菜单在不同的角色登录后
- Ubuntu16.04.1上搭建分布式的Redis集群
- 开源一套基于vue全家桶的webapp
- 判断NaN in JavaScript
- SQL优化小技巧
- Codeforces Round #497 (Div. 2)
- Powershell渗透测试系列–进阶篇
- In abstract algebra, a congruence relation (or simply congruence) is an equivalence relation on an algebraic structure (such as a group, ring, or vector space) that is compatible with the structure in
- Oracle 10g使用amdu抽取数据文件
- MYSQL常用函数(控制流函数)
- Hadoop生态圈-zookeeper的API用法详解
- CloudSetuper
热门文章
- tomcat启动时间5分钟左右org.apache.catalina.util.SessionIdGeneratorBase.createSecureRandom Creation of SecureRandom instance for session ID generation using [SHA1PRNG] took [342,445] milliseconds.
- scrapy框架xpath的几点说明
- Ruby中的Hash(哈希),你可以理解为字典
- Quantitative Proteomics of Enriched Esophageal and Gut Tissues from the Human Blood Fluke Schistosoma mansoni Pinpoints Secreted Proteins for Vaccine Development (解读人:张聪敏)
- Vue中使用echarts,ajax请求的远程数据赋值给图表不刷新的问题和解决办法
- 洛谷 P3909 异或之积 题解
- Java中的集合类、Lambda、鲁棒性简述
- jdk下httpserver源码解析
- 图解GC流程
- ASP.NET Core技术研究-探秘Host主机启动过程