class OutLayer(nn.Module): def __init__(self, in_num, node_num, layer_num, inter_num = 512): super(OutLayer, self).__init__() modules = [] for i in range(layer_num): # last layer, output shape:1 if i == layer_num-1: modules.append(nn.Linear( in_num if layer_num == 1 else inter_num, 1)) else: layer_in_num = in_num if i == 0 else inter_num modules.append(nn.Linear( layer_in_num, inter_num )) modules.append(nn.BatchNorm1d(inter_num)) modules.append(nn.ReLU()) self.mlp = nn.ModuleList(modules) def forward(self, x): out = x for mod in self.mlp: if isinstance(mod, nn.BatchNorm1d): out = out.permute(0,2,1) out = mod(out) out = out.permute(0,2,1) else: out = mod(out) return out
时间: 2023-05-22 07:01:02 浏览: 80
Python RuntimeError: thread.__init__() not called解决方法
这段代码定义了一个名为OutLayer的类,继承自nn.Module。它有四个参数:in_num表示输入的特征数,node_num表示输出的节点数,layer_num表示层数,inter_num表示中间层的节点数,默认为512。在初始化函数中,它创建了一个空的modules列表,并通过循环创建了layer_num个层,每个层都是一个nn.Linear对象,输入特征数为in_num,输出特征数为node_num,中间层的节点数为inter_num。最后,这些层被添加到modules列表中。
阅读全文