使用 Pytorch 对网络结构进行超参数优化

问题描述

我已经构建了一个具有自定义架构的神经网络,设计如下。 不,我想执行超参数优化以找到最佳学习率、批量大小,以及每个隐藏层的神经元数量和层数。

一开始我有三个单独的 NN,它们连接起来,然后通过一些密集层再次处理。

直到现在我还没有找到在 Pytorch 超参数优化中调整层数的方法。 理想情况下,我也想测试一下这三个网是否需要不同的层数。

class Individual(nn.Module):
    def __init__(self):
        super(Individual,self).__init__()
        self.fc1_left = nn.Linear(30,24)
        self.fc1_right = nn.Linear(30,24)
        self.fc1_cor = nn.Linear(2,4)
        self.fc2_cor = nn.Linear(4,4)
        self.fc2_left = nn.Linear(24,4)
        self.fc2_right = nn.Linear(24,4)
        self.fc3 = nn.Linear(12,36)
        self.fc4 = nn.Linear(36,num_classes)
        self.Sigmoid_left = nn.Sigmoid()
        self.Sigmoid_right = nn.Sigmoid()

    def forward(self,x_left,x_right,x_cor,x_con):
        
        x_con_l = x_con[:,0].view(-1,1)
        x_con_r = x_con[:,1].view(-1,1)        
     
        x_left = F.relu(self.fc1_left(x_left))
        x_left = F.relu(self.fc2_left(x_left))
        x_left = torch.mul(x_left,x_con_l)

        x_right = F.relu(self.fc1_right(x_right))
        x_right = F.relu(self.fc2_right(x_right))
        x_right = torch.mul(x_right,x_con_r)   
        
        x_cor = F.relu(self.fc1_cor(x_cor))
        x_cor = F.relu(self.fc2_cor(x_cor))
        
        
        combined = torch.cat((x_left.view(x_left.size(0),-1),x_right.view(x_right.size(0),-1)),dim=1)

        combined = torch.cat((combined.view(combined.size(0),x_cor.view(x_cor.size(0),dim=1)        
        
        x1 = F.relu(self.fc3(combined))
        x2 = F.relu(self.fc4(x1))
        
        return x2

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)