代码之家  ›  专栏  ›  技术社区  ›  Roy

当训练只有输出对所有输入的导数时,训练神经网络

  •  1
  • Roy  · 技术社区  · 3 年前

    F 有1000个输入。我想训练一个模型来预测 F 给定输入。然而,在训练数据集中,我们只知道 F 关于每个输入,而不是 F 它自己。我怎样才能构造一个有这种局限性的神经网络呢 tensorflow pytorch ?

    0 回复  |  直到 3 年前
        1
  •  2
  •   Innat    3 年前

    我想你可以用 torch.autograd

    (a) 可训练的 nn.Module 表示(未知)函数 F :

    class UnknownF(nn.Module):
      def __init__(self, ...):
        # whatever combinations of linear layers and activations and whatever...
    
      def forward(self, x):
        # x is 1000 dim vector
        y = self.layers(x)
        # y is a _scalar_ output
        return y
    
    model = UnknownF(...)  # instansiate the model of the unknown function
    

    x = torch.randn(n, 1000, requires_grad=True)  # n examples of 1000-dim vectors
    dy = torch.randn(n, 1000)  # the corresponding n-dim gradients of the n inputs
    

    (c) 优化器:

    opt = torch.optim.SGD(model.parameters(), lr=0.1)
    

    criterion = nn.MSELoss()
    
    for e in range(num_epochs):
      for i in range(n):
        # batch size = 1, pick one example
        x_ = x[i, :]
        dy_ = dy[i, :] 
        opt.zero_grad()
        # predict the unknown output
        y_ = model(x_)
        # compute the gradients of the model using autograd:
        pred_dy_ = autograd.grad(y_, x_, create_graph=True)[0]
        # compute the loss between the model's gradients and the GT ones:
        loss = criterion(pred_dy_, dy_)
        loss.backward()
        opt.step()  # update model's parameters accordingly.