<bdo id='z5htz'></bdo><ul id='z5htz'></ul>
<legend id='z5htz'><style id='z5htz'><dir id='z5htz'><q id='z5htz'></q></dir></style></legend>

    1. <tfoot id='z5htz'></tfoot>

      <small id='z5htz'></small><noframes id='z5htz'>

      <i id='z5htz'><tr id='z5htz'><dt id='z5htz'><q id='z5htz'><span id='z5htz'><b id='z5htz'><form id='z5htz'><ins id='z5htz'></ins><ul id='z5htz'></ul><sub id='z5htz'></sub></form><legend id='z5htz'></legend><bdo id='z5htz'><pre id='z5htz'><center id='z5htz'></center></pre></bdo></b><th id='z5htz'></th></span></q></dt></tr></i><div id='z5htz'><tfoot id='z5htz'></tfoot><dl id='z5htz'><fieldset id='z5htz'></fieldset></dl></div>

      RuntimeError:渐变计算所需的变量之一已被原地操作修改?

      时间:2024-08-11
    2. <tfoot id='IF97N'></tfoot>
        <tbody id='IF97N'></tbody>
    3. <i id='IF97N'><tr id='IF97N'><dt id='IF97N'><q id='IF97N'><span id='IF97N'><b id='IF97N'><form id='IF97N'><ins id='IF97N'></ins><ul id='IF97N'></ul><sub id='IF97N'></sub></form><legend id='IF97N'></legend><bdo id='IF97N'><pre id='IF97N'><center id='IF97N'></center></pre></bdo></b><th id='IF97N'></th></span></q></dt></tr></i><div id='IF97N'><tfoot id='IF97N'></tfoot><dl id='IF97N'><fieldset id='IF97N'></fieldset></dl></div>

      1. <small id='IF97N'></small><noframes id='IF97N'>

          <legend id='IF97N'><style id='IF97N'><dir id='IF97N'><q id='IF97N'></q></dir></style></legend>
            • <bdo id='IF97N'></bdo><ul id='IF97N'></ul>

                本文介绍了RuntimeError:渐变计算所需的变量之一已被原地操作修改?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                我正在使用pytorch-1.5执行一些gan测试。我的代码是非常简单的GAN代码,正好适合sin(X)函数:

                import torch
                import torch.nn as nn
                import numpy as np
                import matplotlib.pyplot as plt
                
                
                # Hyper Parameters
                BATCH_SIZE = 64
                LR_G = 0.0001
                LR_D = 0.0001 
                N_IDEAS = 5  
                ART_COMPONENTS = 15 
                PAINT_POINTS = np.vstack([np.linspace(-1, 1, ART_COMPONENTS) for _ in range(BATCH_SIZE)])
                
                
                def artist_works():  # painting from the famous artist (real target)
                    r = 0.02 * np.random.randn(1, ART_COMPONENTS)
                    paintings = np.sin(PAINT_POINTS * np.pi) + r
                    paintings = torch.from_numpy(paintings).float()
                    return paintings
                
                
                G = nn.Sequential(  # Generator
                    nn.Linear(N_IDEAS, 128),  # random ideas (could from normal distribution)
                    nn.ReLU(),
                    nn.Linear(128, ART_COMPONENTS),  # making a painting from these random ideas
                )
                
                D = nn.Sequential(  # Discriminator
                    nn.Linear(ART_COMPONENTS, 128),  # receive art work either from the famous artist or a newbie like G
                    nn.ReLU(),
                    nn.Linear(128, 1),
                    nn.Sigmoid(),  # tell the probability that the art work is made by artist
                )
                
                opt_D = torch.optim.Adam(D.parameters(), lr=LR_D)
                opt_G = torch.optim.Adam(G.parameters(), lr=LR_G)
                
                
                for step in range(10000):
                    artist_paintings = artist_works()  # real painting from artist
                    G_ideas = torch.randn(BATCH_SIZE, N_IDEAS)  # random ideas
                    G_paintings = G(G_ideas)  # fake painting from G (random ideas)
                
                    prob_artist0 = D(artist_paintings)  # D try to increase this prob
                    prob_artist1 = D(G_paintings)  # D try to reduce this prob
                
                    D_loss = - torch.mean(torch.log(prob_artist0) + torch.log(1. - prob_artist1))
                    G_loss = torch.mean(torch.log(1. - prob_artist1))
                
                    opt_D.zero_grad()
                    D_loss.backward(retain_graph=True)  # reusing computational graph
                    opt_D.step()
                
                    opt_G.zero_grad()
                    G_loss.backward()
                    opt_G.step()
                

                但是当我运行它时得到以下错误:

                RuntimeError: one of the variables needed for gradient computation has been modified by an inplace operation: [torch.FloatTensor [128, 1]], which is output 0 of TBackward, is at version 2; expected version 1 instead. Hint: the backtrace further above shows the operation that failed to compute its gradient. The variable in question was changed in there or anywhere later. Good luck!
                

                我的代码是否有问题?

                推荐答案

                发生这种情况是因为opt_d.step()就地修改了鉴别器的参数。但是这些参数是计算生成器的梯度所必需的。您可以通过将代码更改为:

                来修复此问题
                for step in range(10000):
                    artist_paintings = artist_works()  # real painting from artist
                    G_ideas = torch.randn(BATCH_SIZE, N_IDEAS)  # random ideas
                    G_paintings = G(G_ideas)  # fake painting from G (random ideas)
                
                    prob_artist1 = D(G_paintings)  # G tries to fool D
                
                    G_loss = torch.mean(torch.log(1. - prob_artist1))
                    opt_G.zero_grad()
                    G_loss.backward()
                    opt_G.step()
                
                    prob_artist0 = D(artist_paintings)  # D try to increase this prob
                    # detach here to make sure we don't backprop in G that was already changed.
                    prob_artist1 = D(G_paintings.detach())  # D try to reduce this prob
                
                    D_loss = - torch.mean(torch.log(prob_artist0) + torch.log(1. - prob_artist1))
                    opt_D.zero_grad()
                    D_loss.backward(retain_graph=True)  # reusing computational graph
                    opt_D.step()
                

                您可以在此处找到有关此问题的更多信息https://github.com/pytorch/pytorch/issues/39141

                这篇关于RuntimeError:渐变计算所需的变量之一已被原地操作修改?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                上一篇:将PyTorch张量转换为Python列表 下一篇:BERT记号赋值器&&模型下载

                相关文章

                <legend id='EVKT1'><style id='EVKT1'><dir id='EVKT1'><q id='EVKT1'></q></dir></style></legend>
              1. <tfoot id='EVKT1'></tfoot>

                  <bdo id='EVKT1'></bdo><ul id='EVKT1'></ul>

              2. <i id='EVKT1'><tr id='EVKT1'><dt id='EVKT1'><q id='EVKT1'><span id='EVKT1'><b id='EVKT1'><form id='EVKT1'><ins id='EVKT1'></ins><ul id='EVKT1'></ul><sub id='EVKT1'></sub></form><legend id='EVKT1'></legend><bdo id='EVKT1'><pre id='EVKT1'><center id='EVKT1'></center></pre></bdo></b><th id='EVKT1'></th></span></q></dt></tr></i><div id='EVKT1'><tfoot id='EVKT1'></tfoot><dl id='EVKT1'><fieldset id='EVKT1'></fieldset></dl></div>

                  <small id='EVKT1'></small><noframes id='EVKT1'>