gpt4 book ai didi

python - 训练损失不减少

转载 作者:太空宇宙 更新时间:2023-11-04 02:02:53 24 4
gpt4 key购买 nike

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

这里我会尽量把我用到的所有代码都放上

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

self.batch_size = 64
self.shape = shape

self.output_height = 64
self.output_width = 64
self.gf_dim = 64
self.c_dim = 3

self.strides_size = 2
self.kernel_size = 2
self.padding = 'SAME'
def encoder_conv_net(self, input_):

self.conv1 = Model.batch_norm(self, Model.conv_2d(self, input_, [3,3,self.c_dim,32], name = 'conv1'))

self.conv2 = Model.batch_norm(self, Model.conv_2d(self, self.conv1, [3,3,32,64], name = 'conv2'))

self.conv3 = Model.batch_norm(self, Model.conv_2d(self, self.conv2, [3,3,64,128], name = 'conv3'))

self.conv4 = Model.batch_norm(self, Model.conv_2d(self, self.conv3, [3,3,128,128], name = 'conv4'))

fc = tf.reshape(self.conv4, [ -1, 512 ])

dropout1 = tf.nn.dropout(fc, keep_prob=0.5)

fc1 = Model.fully_connected(self, dropout1, 512)
return tf.nn.tanh(fc1)

def decoder_conv_net(self,
input_,
shape):

g_width, g_height = shape[1], shape[0]
g_width2, g_height2 = np.ceil(shape[1]/2), np.ceil(shape[0]/2)
g_width4, g_height4 = np.ceil(shape[1]/4), np.ceil(shape[0]/4)
g_width8, g_height8 = np.ceil(shape[1]/8), np.ceil(shape[0]/8)

input_ = tf.reshape(input_, [-1, 4, 4, 128])

print(input_.shape, g_width8, self.gf_dim)
deconv1 = Model.deconv_2d(self, input_, [self.batch_size, g_width8, g_height8, self.gf_dim * 2],
[5,5],
name = 'deconv_1')

deconv2 = Model.deconv_2d(self, deconv1, [self.batch_size, g_width4, g_height4, self.gf_dim * 2],
[5,5],
name = 'deconv_2')

deconv3 = Model.deconv_2d(self, deconv2, [self.batch_size, g_width2, g_height2, self.gf_dim],
[5,5],
name = 'deconv_3')

deconv4 = Model.deconv_2d(self, deconv3, [self.batch_size, g_width, g_height, self.c_dim],
[5,5],
name = 'deconv_4',
relu = False)

return tf.nn.tanh(deconv4)

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

主要功能是这样的

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()

我以正常的方式调用这个train session

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

最佳答案

通过将激活函数更改为更适合您的图像编码的softmax:

image = tf.clip_by_value(image, 0.0, 1.0)

损失从 0.14066154 开始。

增加训练时期的数量,损失似乎低至 ~0.08216808,这是合理的,因为我只在单个 Titan Xp 上训练了几分钟的模型.

关于python - 训练损失不减少,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55382747/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com