【问题标题】:How can I make a neural network that has multiple outputs using pytorch?如何使用 pytorch 制作具有多个输出的神经网络?
【发布时间】:2021-09-11 00:44:58
【问题描述】:

我的问题是否正确?我到处寻找,但找不到任何东西。我很确定在我学习 keras 时已经解决了这个问题,但是如何在 pytorch 中实现呢?

【问题讨论】:

  • 多重输出到底是什么意思?能举个例子吗?
  • 正如您从不同的答案中看到的那样,您所问的并不是很清楚。如果您可以通过添加示例来澄清您的问题,那就太好了!

标签: pytorch


【解决方案1】:

使用 pytorch 可以轻松实现多个输出。

这是一个这样的网络。

import torch.nn as nn

class NeuralNetwork(nn.Module):
  def __init__(self):
    super(NeuralNetwork, self).__init__()
    self.linear1 = nn.Linear(in_features = 3, out_features = 1)
    self.linear2 = nn.Linear(in_features = 3,out_features = 2)

  def forward(self, x):
    output1 = self.linear1(x)
    output2 = self.linear2(x)
    return output1, output2

【讨论】:

  • this PyTorch discussion thread 中有一个讨论和一些类似的例子。
  • 嘿,我怀疑一个线性层怎么会有多个输出?如果它只是一个线性回归模型,那么它应该只输出一个值(wT.X + b)对吗?
【解决方案2】:

有几种方法可以使用 PyTorch 构建用于分类的神经网络。

import torch
from torch import nn
import torch.nn.functional as F

class Network(nn.Module):
    def __init__(self):
        super().__init__()
        # Inputs to hidden layer linear transformation
        self.hidden = nn.Linear(784, 256)
        # Output layer, 10 units - one for each digit
        self.output = nn.Linear(256, 10)
        
    def forward(self, x):
        # Hidden layer with sigmoid activation
        x = F.sigmoid(self.hidden(x))
        # Output layer with softmax activation
        x = F.softmax(self.output(x), dim=1)
        
        return x

Network 类旨在处理数字图像,与nn.Sequential 相比,它允许更多自定义。 self.output 的最后一个数字是 10,这意味着我们将有 10 个输出,每个数字 1 个输出。我们将output 通过softmax 函数来计算类概率,即。查看哪个数字对于某个数字图像的概率最高。

我们为softmax 添加dim=1 选项的原因是为了跨列进行计算,因此每行的概率总和将为1。

例子:

>>> input = torch.tensor([[1., 2., 3.], [2., 1., 3.], [4., 2., 6.]])
>>> F.softmax(input, dim=1)
tensor([[0.0900, 0.2447, 0.6652],
    [0.2447, 0.0900, 0.6652],
    [0.1173, 0.0159, 0.8668]])
# dim=1, row sums add up to 1

>>> F.softmax(input, dim=0)
tensor([[0.0420, 0.4223, 0.0453],
    [0.1142, 0.1554, 0.0453],
    [0.8438, 0.4223, 0.9094]])
# dim=0, column sums add up to 1

可以使用nn.Sequential 构建相同的模型,这是一种快速完成相同操作的方法:

# Hyperparameters for our network
input_size = 784
hidden_sizes = [128, 64]
output_size = 10

# Build a feed-forward network
model = nn.Sequential(nn.Linear(input_size, hidden_sizes[0]),
                      nn.ReLU(),
                      nn.Linear(hidden_sizes[0], hidden_sizes[1]),
                      nn.ReLU(),
                      nn.Linear(hidden_sizes[1], output_size),
                      nn.Softmax(dim=1))

您可以查看Udacity's Neural Networks in PyTorch Notebook tutorial 了解更多说明。他们的部分深度学习纳米学位内容也可通过free course, Intro to Deep Learning with PyTorch 获得。

【讨论】:

    【解决方案3】:

    如果您正在研究多类分类和一个简单的神经网络,您可以通过多种方式进行,作为初学者尝试在 PyTorch 中创建一个类作为 nn.Module 的子类

    class Network(nn.Module):
        def __init__(self):
            super().__init__()
    
            # Inputs to hidden layer linear transformation
            self.hidden = nn.Linear(784, 256)
            # Output layer, 10 units - one for each digit
            self.output = nn.Linear(256, 10)
    
            # Define sigmoid activation and softmax output 
            self.sigmoid = nn.Sigmoid()
            self.softmax = nn.Softmax(dim=1)
    
        def forward(self, x):
            # Pass the input tensor through each of our operations
            x = self.hidden(x)
            x = self.sigmoid(x)
            x = self.output(x)
            x = self.softmax(x)
    
            return x
    model = Network()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-11-16
      • 2021-04-12
      • 1970-01-01
      • 2018-02-28
      • 1970-01-01
      • 2012-01-16
      相关资源
      最近更新 更多