어떻게 변환하는 출력의 신경 네트워크는 여전히 훈련하는가?

0

질문

나는 신경 네트워크는 출력 output. 고 싶 변환 output 하기 전에 손실 및 backpropogation 일어난다.

여기에는 나의 일반적인 코드:

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()

나는 변환 기능을 나를 통해서 출력:

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

내 문제는 나에 오류가 발생한 다음 마지막 줄:

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

어떤 방법이 있나요? 나가도를 사용하여"성화와 함께.no_grad():"(주석)하지만,이 결과에서 아주 가난한 교육 및 내가 믿는 것을 기울이지 않 backpropogate 제대로 후 변환 기능입니다.

감사합니다!

1

최고의 응답

1

에 오류가 매우 정확한에 대해 무엇이 문제가-새로 만드는 경우 텐서 가 requires_grad = True을 만들 잎 노드의 그래프에서(다만의 매개변수 모델)및 허용하지 않는 장소에서 작업습니다.

해결책은 간단하다,당신은 당신이 필요하지 않습을 만들기 new_tensor 니다. 그것은 없어야 할 리프 노드;단지 그것을 만들에 비

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)    

new_tensor 를 상속하는 모든 같은 속성 dtype, devicepredictionsrequire_grad = True 이미.

2021-11-20 04:18:52

다른 언어로

이 페이지는 다른 언어로되어 있습니다

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