【请教】paddle.fluid.core_noavx.EnforceNotMet: grad_op_maker_ should not be null
Created by: zhangrui-github
-
版本、环境信息: 1)PaddlePaddle版本: 2)CPU:paddlepaddle 1.5.0 3)系统环境:Mac OS 10.14,Python3.7
-
训练信息 1)单机
-
问题描述:请详细描述您的问题,同步贴出报错信息、日志、可复现的代码片段 使用paddle自己搭建了一个模型,在运行到optimizer = fluid.optimizer.Adam(learning_rate=0.001) optimizer.minimize(avg_loss) 的时候出现了这样一个错误, 模型定义 ` def encoder(x): """定义一个编码网络,将输入x转换为两个统计分布参数
Args: x: 输入向量
Returns: 对输入x转换后生成的隐空间的两个参数,均值mean_z和方差的对数log_var_z,以及隐空间模型Q(z|X)的采样sample_z """ hidden_dim = 512 out_dim =2 x = fluid.layers.fc(input=x, size=hidden_dim, act='relu') mean_z = fluid.layers.fc(input=x, size=out_dim) log_var_z = fluid.layers.fc(input=x, size=out_dim)
#从Q(z|X)中采样,使用参数复现的采样技巧(Reparameterization), 使用标准正态分布epsilon = N(0,1)得到 #采样结果sample_z = mean_z + sqrt(var_z)*epsilon dim = mean_z.shape[1] epsilon_layer = fluid.layers.gaussian_random(shape=[-1, dim]) exp_var = fluid.layers.exp( fluid.layers.elementwise_mul(log_var_z, fluid.layers.fill_constant(shape=[1], value=0.5, dtype='float32'))) mul_var_epsilon = fluid.layers.elementwise_mul(exp_var, epsilon_layer) sample_z = fluid.layers.elementwise_add(mean_z, mul_var_epsilon) return mean_z, log_var_z, sample_z
def decoder(z): """定义一个解码网络(生成器),对采样结果进行生成重构
Args:
z: 输入向量
Returns:
对输入的z进行重构成原输入的结果
"""
hidden_dim = 512
original_dim = 28 * 28
z = fluid.layers.fc(input=z, size=hidden_dim, act='relu')
outputs = fluid.layers.fc(input=z, size=original_dim, act='sigmoid')
return outputs
loss函数
def vae_loss(x, decoded_x, mean_z, log_var_z):
"""定义VAE模型的loss
Args:
x: 输入向量
decoded_x: 网络模型的输出,与输入相同大小
mean_z: 隐空间的均值参数
log_var_z: 隐空间的方差对数
Returns:
模型的损失函数,损失由两个部分组成,一部分是重构损失,另一部分是kl损失
"""
#fluid.layers.fill_constant(shape=[1], value=1, dtype='float32') cross_entropy(x, decoded_x, soft_label=True)
original_dim = 28 * 28
reconstruction_loss = original_dim * fluid.layers.cross_entropy(x, decoded_x, soft_label=True)
#mean_reconstruction = fluid.layers.mean(reconstruction_loss)
#kl_loss = -0.5 * sum(1 + log_var_z - square(mean_z) - exp(log_var_z))
kl_loss = fluid.layers.elementwise_sub(
fluid.layers.elementwise_add(log_var_z, fluid.layers.fill_constant(shape=[1], value=1, dtype='float32')),
fluid.layers.square(mean_z))
kl_loss = fluid.layers.elementwise_sub(kl_loss, fluid.layers.exp(log_var_z))
kl_loss = fluid.layers.reduce_sum(kl_loss, dim=-1)
kl_loss = fluid.layers.elementwise_mul(kl_loss,
fluid.layers.fill_constant(shape=[1], value=-0.5, dtype='float32'))
model_loss = fluid.layers.mean(fluid.layers.elementwise_add(reconstruction_loss,
kl_loss))
return model_loss`
模型构建 `def train_program(is_generating=False): """配置train_program
Args:
is_generating: 是否使用模型进行生成
Returns:
模型的训练的损失avg_cost
"""
if not is_generating:
img_size = 28
input_shape = [img_size * img_size]
input = fluid.layers.data(name='input',shape=input_shape, dtype='float32')
mean_z, log_var_z, sample_z = encoder(input)
decoder_input = decoder(sample_z)
avg_cost = vae_loss(input, decoder_input, mean_z, log_var_z)
return decoder_input, avg_cost
else:
z_dim = [2]
z_sample = fluid.layers.data(name='z_sample', shape=z_dim, dtype='float32')
decoder_out = decoder(z_sample)
return decoder_out
` 训练
` use_cuda = False place = fluid.CUDAPlace(0) if use_cuda else fluid.CPUPlace()
startup_program = fluid.default_startup_program()
main_program = fluid.default_main_program()
output, avg_loss = train_program(False)
feeder = fluid.DataFeeder(feed_list=['input'], place=place)
#优化器
**optimizer = fluid.optimizer.Adam(learning_rate=0.001)
optimizer.minimize(avg_loss)** #错误出现在这里
#创建执行器和main_programe
exe = fluid.Executor(place)
exe.run(startup_program)
...... ....... `
想请求帮忙定位一下这个错误大概是那个方面的问题,是模型搭建的错误,还是loss函数的错误