No output from gradients
Hi, 不知道为啥下面这个 gradient出来结果为空。
def G_Jacobian(netG, XVar, cnts, lambdas, z, iter_str='', adding_noise_var=0, diag_Reg_val=0.1): interpolates = z interpolates.stop_gradient = False interpolates.stop_gradient_ = False G = netG(interpolates) G.stop_gradient = False G.stop_gradient_ = False V = XVar(interpolates, cnts, lambdas) V.stop_gradient = False V.stop_gradient_ = False if len(G.shape) == 4: G = fluid.layers.reshape(G, [G.shape[0], G.shape[1] * G.shape[2] * G.shape[3]]) V = fluid.layers.reshape(V, [V.shape[0], V.shape[1] * V.shape[2] * V.shape[3]])
PixN = G.shape[1]
for i in range(PixN):
pixel = G[:, i]
grad_out = fluid.layers.ones_like(pixel)
grad_out.stop_gradient_ = False
g_gradients = fluid.gradients(targets=pixel, inputs=interpolates,
target_gradients=grad_out)[0]
我觉得跟netG(interpolates)有关,所以我把netG也copy在下面: class generator(fluid.dygraph.Layer): # Network Architecture is exactly same as in infoGAN (https://arxiv.org/abs/1606.03657) # Architecture : FC1024_BR-FC7x7x128_BR-(64)4dc2s_BR-(1)4dc2s_S # from the main, we can see that input_dim=62, input_size=32 and output_dim=1 def init(self, name_scope, input_dim=62, output_dim=1, input_size=32, norm=True, use_bias=True,stddev=1): super(generator, self).init(name_scope) if use_bias == False: de_bias_attr = False else: de_bias_attr = fluid.ParamAttr(name="de_bias",initializer=fluid.initializer.Constant(0.0)) self.input_dim = input_dim self.output_dim = output_dim self.input_size = input_size
self.fc = Linear(name_scope=name_scope + '_fc', input_size=self.input_dim,
output_size=128 * (self.input_size // 8) * (self.input_size // 8))
##128 * (self.input_size // 8) * (self.input_size // 8)
with fluid.dygraph.guard():
if norm:
self.bn = BatchNorm(self.full_name(),
num_channels=128 * (self.input_size // 8) * (self.input_size // 8),
param_attr=fluid.ParamAttr(
name="scale",
initializer=fluid.initializer.NormalInitializer(1.0,0.02)),
bias_attr=fluid.ParamAttr(
name="bias",
initializer=fluid.initializer.Constant(0.0)),
trainable_statistics=True
)
self.bn_2 = BatchNorm(self.full_name(),
num_channels=128,
param_attr=fluid.ParamAttr(
name="scale",
initializer=fluid.initializer.NormalInitializer(1.0,0.02)),
bias_attr=fluid.ParamAttr(
name="bias",
initializer=fluid.initializer.Constant(0.0)),
trainable_statistics=True
)
self.deconv = Conv2DTranspose(self.full_name(),
128,
filter_size=4,
stride=2,
padding=[1, 1],
param_attr=fluid.ParamAttr(
name="this_is_deconv_weights",
initializer=fluid.initializer.NormalInitializer(loc=0.0, scale=stddev)),
bias_attr=de_bias_attr
)
self.deconv_2 = Conv2DTranspose(self.full_name(),
64,
filter_size=4,
stride=2,
padding=[1, 1],
param_attr=fluid.ParamAttr(
name="this_is_deconv_weights",
initializer=fluid.initializer.NormalInitializer(loc=0.0, scale=stddev)),
bias_attr=de_bias_attr
)
self.deconv_3 = Conv2DTranspose(self.full_name(),
1,
filter_size=4,
stride=2,
padding=[1, 1],
param_attr=fluid.ParamAttr(
name="this_is_deconv_weights",
initializer=fluid.initializer.NormalInitializer(loc=0.0, scale=stddev)),
bias_attr=de_bias_attr
)
self.bn_3 = BatchNorm(self.full_name(),
num_channels=64,
param_attr=fluid.ParamAttr(
name="scale",
initializer=fluid.initializer.NormalInitializer(1.0,0.02)),
bias_attr=fluid.ParamAttr(
name="bias",
initializer=fluid.initializer.Constant(0.0)),
trainable_statistics=True
)
def forward(self, input):
x = self.fc(input)
x = self.bn(x)
x = fluid.layers.leaky_relu(x)
x = fluid.layers.reshape(x, [-1, 128, (self.input_size // 8), (self.input_size // 8)])
x = self.deconv(x)
x = self.bn_2(x)
x = fluid.layers.leaky_relu(x)
x = self.deconv_2(x)
x = self.bn_3(x)
x = fluid.layers.leaky_relu(x)
x = self.deconv_3(x)
x = fluid.layers.tanh(x)
#print('size of x in forward discriminator:{}'.format(x.shape))
return x
错误信息如下:
Iteration: [ 3] [ 3/ 1] D_loss: 9.09784412, G_loss: 4.39907074 Train: ite =3 loss = [2.1675575e+10]
Traceback (most recent call last):
File "/home/dingcheng/.pycharm_helpers/pydev/pydevd.py", line 1664, in
原因就是那个gradients结果为空,所以取值报错。 谢谢,
I found a previous post talking about a similar problem: https://github.com/PaddlePaddle/Paddle/issues/21208.
Then, I changed my function as following,
def G_Jacobian(netG, XVar, cnts, lambdas, z, iter_str='', adding_noise_var=0, diag_Reg_val=0.1): interpolates = z interpolates.stop_gradient = False interpolates.stop_gradient_ = False interpolates.persistable = True G = netG(interpolates) G.stop_gradient = False G.stop_gradient_ = False V = XVar(interpolates, cnts, lambdas) V.stop_gradient = False V.stop_gradient_ = False if len(G.shape) == 4: G = fluid.layers.reshape(G, [G.shape[0], G.shape[1] * G.shape[2] * G.shape[3]]) V = fluid.layers.reshape(V, [V.shape[0], V.shape[1] * V.shape[2] * V.shape[3]])
PixN = G.shape[1]
for i in range(PixN):
pixel = G[:, i]
interpolates.clear_gradient()
pixel.backward()
g_gradients = interpolates.gradient()
if i == 0:
g_grad = g_gradients
else:
g_grad = fluid.layers.concat((g_grad, g_gradients), 2)
It looks working though not sure if there are more issues. Let me know if you guys have more thoughts.
现在我发现还是有问题的,因为我发现第一次求梯度时,好像出来的值是正常的,但是到了第二次,梯度值全变为0了, 我搜索了其他issues, 发现了两个帖子,其中一个就说多次梯度回传,就是会有梯度为0出现,不知道你们修复了这个问题没有,有没有其他解决办法。 https://github.com/PaddlePaddle/Paddle/issues/21886 https://github.com/PaddlePaddle/Paddle/issues/21965
我这边的多次梯度结果: g_gradients: name tmp_4837, dtype: VarType.FP32 shape: [10, 18, 1] lod: {} dim: 10, 18, 1 layout: NCHW dtype: float data: [0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0]
v_gradients: name tmp_8932, dtype: VarType.FP32 shape: [10, 18, 1] lod: {} dim: 10, 18, 1 layout: NCHW dtype: float data: [0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0]
你们看有没有解决办法?
你好,咨询了一下相关的同事,这个问题我们暂时还没有修复,我会反馈一下这个问题
Since you haven't replied for more than a year, we have closed this issue/pr. If the problem is not solved or there is a follow-up one, please reopen it at any time and we will continue to follow up. 由于您超过一年未回复,我们将关闭这个issue/pr。 若问题未解决或有后续问题,请随时重新打开,我们会继续跟进。