Come trasformare l'output della rete neurale e ancora in treno?

0

Domanda

Ho una rete neurale che uscite output. Io voglio trasformare output prima della perdita e backpropogation accadere.

Ecco il mio codice:

with torch.set_grad_enabled(training):
                  outputs = net(x_batch[:, 0], x_batch[:, 1]) # the prediction of the NN
                  # My issue is here:
                  outputs = transform_torch(outputs)
                  loss = my_loss(outputs, y_batch)

                  if training:
                      scheduler.step()
                      loss.backward()
                      optimizer.step()

Ho una funzione di trasformazione che ho messo la mia uscita attraverso:

def transform_torch(predictions):
    torch_dimensions = predictions.size()
    torch_grad = predictions.grad_fn
    cuda0 = torch.device('cuda:0')
    new_tensor = torch.ones(torch_dimensions, dtype=torch.float64, device=cuda0, requires_grad=True)
    for i in range(int(len(predictions))):
      a = predictions[i]
      # with torch.no_grad(): # Note: no training happens if this line is kept in
      new_tensor[i] = torch.flip(torch.cumsum(torch.flip(a, dims = [0]), dim = 0), dims = [0])
    return new_tensor

Il mio problema è che ho un errore sulla penultima riga:

RuntimeError: a view of a leaf Variable that requires grad is being used in an in-place operation.

Qualche suggerimento? Ho già provato con "con la torcia.no_grad():" (commentato), ma questo risultato è molto scarsa preparazione e credo che le pendenze non backpropogate correttamente dopo la funzione di trasformazione.

Grazie!

1

Migliore risposta

1

L'errore è abbastanza corretta su ciò che il problema è - quando si crea un nuovo tensore con requires_grad = Truecreare un nodo foglia nel grafico (proprio come parametri di un modello) e non ha permesso di fare operazione sul posto su di esso.

La soluzione è semplice, non è necessario creare il new_tensor in anticipo. Non è supposto per essere un nodo foglia; creare al volo

new_tensor = [ ]
for i in range(int(len(predictions))):
    a = predictions[i]
    new_tensor.append(torch.flip(torch.cumsum(torch.flip(a, ...), ...), ...))

new_tensor = torch.stack(new_tensor, 0)    

Questo new_tensor eredita tutte le proprietà come dtype, device da predictions e avrà require_grad = True già.

2021-11-20 04:18:52

In altre lingue

Questa pagina è in altre lingue

Русский
..................................................................................................................
Polski
..................................................................................................................
Română
..................................................................................................................
한국어
..................................................................................................................
हिन्दी
..................................................................................................................
Français
..................................................................................................................
Türk
..................................................................................................................
Česk
..................................................................................................................
Português
..................................................................................................................
ไทย
..................................................................................................................
中文
..................................................................................................................
Español
..................................................................................................................
Slovenský
..................................................................................................................