如何计算没有输入的神经网络输出的梯度?

时间:2019-07-18 00:55:31

标签: python pytorch

我想计算(num1 * output1,num1 * output2)w.r.t的偏导数。 num1。神经网络的输入为(num1、2、3、4)。预期的输出暗淡为2。有没有办法做到这一点?

number_of_inputs = 4
hidden_layer = 64
number_of_outputs = 2

class NeuralNetwork(nn.Module):

    def __init__(self):
        super(NeuralNetwork, self).__init__()
        self.linear1 = nn.Linear(number_of_inputs,hidden_layer)
        self.linear2 = nn.Linear(hidden_layer,number_of_outputs)

        self.activation = nn.Tanh()

    def forward(self, x):
        output = self.linear1(x)
        output = self.activation(output)
        output = self.linear2(output)

        return output

a = NeuralNetwork()

# a(input1, input2, input3, input4) -> (output1, output2)

num1 = torch.tensor(1, dtype=torch.float, requires_grad=True)
b = a(torch.tensor([num1, 2, 3, 4], dtype=torch.float, requires_grad=True))

0 个答案:

没有答案