Python 培训损失并没有减少

Python 培训损失并没有减少,python,tensorflow,Python,Tensorflow,我正在尝试在tensorflow中使用CNN实现自动编码器。首先,我在MNIST数据集上训练了我的模型,一切都很好,我得到了较低的损失,当我运行推理模型时,它工作得很好(提供了良好的输出图像)。但后来我决定在CelebA数据集上测试我的网络,但我的模型失败了,损失从未减少。模型处理速度很快,我尝试降低学习速度。尽管我降低了学习速度,但训练所需的时间没有太大差别 在这里,我将尝试将我使用的所有代码 **注意,我还设置了GitHub存储库,以防您更容易阅读代码 这些是模型编码器和解码器的功能 主要函

我正在尝试在tensorflow中使用CNN实现自动编码器。首先,我在MNIST数据集上训练了我的模型,一切都很好,我得到了较低的损失,当我运行推理模型时,它工作得很好(提供了良好的输出图像)。但后来我决定在CelebA数据集上测试我的网络,但我的模型失败了,损失从未减少。模型处理速度很快,我尝试降低学习速度。尽管我降低了学习速度,但训练所需的时间没有太大差别

在这里,我将尝试将我使用的所有代码

**注意,我还设置了GitHub存储库,以防您更容易阅读代码

这些是模型编码器和解码器的功能

主要函数如下所示

dataset = tf.data.Dataset.from_tensor_slices(filenames)
dataset = dataset.shuffle(len(filenames))
dataset = dataset.map(parse_function, num_parallel_calls=4)
#dataset = dataset.map(train_preprocess, num_parallel_calls=4)
dataset = dataset.repeat().batch(batch_size)
#dataset = dataset.apply(tf.contrib.data.batch_and_drop_remainder(batch_size))
dataset = dataset.prefetch(1)

iterator = tf.data.Iterator.from_structure(dataset.output_types,
                                           dataset.output_shapes)

next_element = iterator.get_next()
init_op = iterator.make_initializer(dataset)

#print(next_element)
x = next_element
#plt.imshow(x)
#x = tf.reshape(x, [64, 64, 64, 3])

ENC = Encoder(shape)
DEC = Decoder(shape)

encoding = ENC.encoder_conv_net(x)

print("Encoding output shape " + str(encoding.shape))    

output = DEC.decoder_conv_net(encoding, [64,64])

print(output.shape)
loss = tf.reduce_mean(tf.squared_difference(x, output))

opt = tf.train.AdamOptimizer(learning_rate=0.1e-5)
train = opt.minimize(loss)
saver = tf.train.Saver()
init = tf.global_variables_initializer()
我用正常的方式称这次列车会议

with tf.Session(graph=graph) as sess:
  #saver.restore(sess, '')

  sess.run(init) 
  sess.run(init_op)

  a = sess.run(next_element)

  for ind in tqdm(range(nb_epoch)):    
      loss_acc, outputs, _ = sess.run([loss, output, train])
      print(loss_acc)

      if ind % 40 == 0:
          print(loss_acc)
          saver.save(sess, save_path = "./checkpoints/" \
                       "/model_face.ckpt", global_step = ind) 
在所有这些训练开始后,没有一个错误,但我的损失并没有减少

这里还有一些实用函数

def parse_function(filename):
  image_string = tf.read_file(filename)
  image = tf.image.decode_jpeg(image_string, channels=3)
  image = tf.image.convert_image_dtype(image, tf.float32)
  image = tf.image.resize_images(image, [64, 64])
  return image

def train_preprocess(image):
  image = tf.image.random_flip_left_right(image)
  image = tf.image.random_brightness(image, max_delta=32.0 / 255.0)
  image = tf.image.random_saturation(image, lower=0.5, upper=1.5)
  image = tf.clip_by_value(image, 0.0, 1.0)
  return image

你能打印x的值,输出和梯度吗? 我对不变损失的第一个想法是: 1.如果x始终为零,则输出保持不变。损耗保持不变 2.如果x不是零,但在每一步中保持不变,如果梯度始终为零(权重不更新),则输出保持不变,损耗保持不变
但由于您可以在mnist上成功运行模型,因此此显示模型还可以,因此我怀疑问题可能更多地与数据有关。

通过将激活功能更改为softmax,它更适合您的图像编码:

image = tf.clip_by_value(image, 0.0, 1.0)
损失开始于
0.14066154


随着训练次数的增加,损失似乎降到了~
0.08216808
,这是合理的,因为我只在一台Titan Xp上训练了几分钟的模型。

我确实试过检查x和输出,但x工作正常,就像我试着运行
plt.imshow(a[0,:,:,:])
我得到了图像。但是,当我运行
plt.imshow(输出[0,:,:,:,:])
时,我得到一个错误
浮点图像RGB值必须在0..1范围内。
这会是一个问题吗?起初,我认为这是缺乏培训的问题。这可能是一个评论。你能打印x的值、输出和梯度吗?这些是你给OP的建议,以改善问题。我同意代码太大,但我已经提供了一个解释,可能会给出在哪里寻找问题的线索。正如我提到的,我的训练损失没有改变。它停留在某个值+-0.1之间,因此,我既没有提供培训也没有提供验证损失图。如果有人有任何问题,我很乐意提供更多的信息,如果我错过了一些东西,如下面的一个。从有关输出的评论中挑选。输出上哪些值不在0..1范围内?这些值介于-1和1之间您的损失函数可能不取绝对差,并且由于可能存在正误差和负误差,因此总和趋于收敛到0。您是否尝试将它们映射到范围0..1?您的训练图像的值介于0.0和1.0之间,没有
image=tf.clip\u by\u value(image,0.0,1.0)
。例如,为什么不更改softmax的此激活?您将
tf.nn.tanh(deconv4)
更改为
tf.nn.softmax(deconv4)
对吗?是的。和
tf.nn.tanh(fc1)
to
tf.nn.softmax(fc1)
image = tf.clip_by_value(image, 0.0, 1.0)