No output from gradients
Created by: leonleeldc
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 main() File "/home/dingcheng/.pycharm_helpers/pydev/pydevd.py", line 1658, in main globals = debugger.run(setup['file'], None, None, is_module) File "/home/dingcheng/.pycharm_helpers/pydev/pydevd.py", line 1068, in run pydev_imports.execfile(file, globals, locals) # execute the script File "/home/dingcheng/.pycharm_helpers/pydev/_pydev_imps/_pydev_execfile.py", line 18, in execfile exec(compile(contents+"\n", file, 'exec'), glob, loc) File "/media/data2/dingcheng/workspace/baidu/ccl/Likelihood_Gan/mnist_model_paddle/g_ig_T_cnn_wGAN_Info_paddle.py", line 951, in main() File "/media/data2/dingcheng/workspace/baidu/ccl/Likelihood_Gan/mnist_model_paddle/g_ig_T_cnn_wGAN_Info_paddle.py", line 945, in main igan.train() File "/media/data2/dingcheng/workspace/baidu/ccl/Likelihood_Gan/mnist_model_paddle/g_ig_T_cnn_wGAN_Info_paddle.py", line 620, in train self.opt.adding_noise_var, self.opt.diag_Reg_val) File "/media/data2/dingcheng/workspace/baidu/ccl/Likelihood_Gan/mnist_model_paddle/utils_paddle.py", line 412, in G_LLK_Geom diag_Reg_val) File "/media/data2/dingcheng/workspace/baidu/ccl/Likelihood_Gan/mnist_model_paddle/utils_paddle.py", line 500, in G_Jacobian target_gradients=grad_out)[0] IndexError: list index out of range
原因就是那个gradients结果为空,所以取值报错。 谢谢,