如何使用pytorch创建具有多个输出的神经网络?


易米烊光
2025-03-18 09:17:44 (3天前)
  1. 我的问题是对的吗?我到处看,但找不到一件事。当我学习keras时,我很确定这是解决了,但我如何在pytorch中实现它?

2 条回复
  1. 0# LOGO | 2019-08-31 10-32



    如果你正在研究多类分类和简单的神经网络,你可以采用多种方式,作为初学者尝试在PyTorch中创建一个类作为nn.Module的子类来开始




    1. class Network(nn.Module):
      def init(self):
      super().init()

    2.     # Inputs to hidden layer linear transformation
    3.     self.hidden = nn.Linear(784, 256)
    4.     # Output layer, 10 units - one for each digit
    5.     self.output = nn.Linear(256, 10)
    6.     # Define sigmoid activation and softmax output 
    7.     self.sigmoid = nn.Sigmoid()
    8.     self.softmax = nn.Softmax(dim=1)
    9. def forward(self, x):
    10.     # Pass the input tensor through each of our operations
    11.     x = self.hidden(x)
    12.     x = self.sigmoid(x)
    13.     x = self.output(x)
    14.     x = self.softmax(x)
    15.     return x
    16. model = Network()

    17. </code>

登录 后才能参与评论