张量流中的自动编码器。损失不会减少

时间:2018-02-15 08:18:55

标签: python tensorflow

我不明白为什么损失不会改变。我尝试过的事情: 根据尺寸公式改变编码和解码层数,改变学习率,改变优化功能,将两批作为无噪声图像进给,改变批量大小,检查输入的有效性。输出样本如下。这是整个代码。

我对TensorFlow相对较新,可能会非常愚蠢。

id
price
created_at

输出: 152.3966 152.28357 152.38466 152.44324 152.20834 152.43982 152.36153 152.38193 152.28334 152.45685 152.28116 152.4884 ....

1 个答案:

答案 0 :(得分:1)

  #Network
tf.reset_default_graph()

noise_imgs = tf.placeholder(tf.float32, [None, 28, 28, 1])
imgs = tf.placeholder(tf.float32, [None, 28, 28, 1])

# Building the encoder
def encoder(x):
   out1 = tf.layers.conv2d(x, 32, [3, 3], padding="valid", activation=tf.nn.relu) #26*26*32 
   out1pool = tf.layers.max_pooling2d(inputs=out1, pool_size=[2, 2], strides=2) #13*13*32
   out2 = tf.layers.conv2d(out1pool, 64, [3, 3], padding="valid", activation=tf.nn.relu) #11*11*64
   out1pool2 = tf.layers.max_pooling2d(inputs=out2, pool_size=[2, 2], strides=2) #5*5*64

   flat_inputs = tf.contrib.layers.flatten(out1pool2)
   hundred = tf.layers.dense(flat_inputs, units=100)
   return hundred

   # Building the decoder
def decoder(x):
   img = tf.reshape(x, [-1, 10, 10, 1])

   l1 = tf.layers.conv2d_transpose(img, 32, [7, 7], padding="valid", activation=tf.nn.relu)
   l2 = tf.layers.conv2d_transpose(l1, 1, [13, 13], padding="valid", activation=tf.nn.relu)          
   return l2 

   # Construct model
encoder_op = encoder(noise_imgs)
decoder_op = decoder(encoder_op)

loss = tf.sqrt(tf.reduce_sum(tf.square(imgs-decoder_op)))
optim = tf.train.AdamOptimizer(learning_rate = 0.001).minimize(loss)


# Start Training
noise_constant=0.2
num_iter = 1000
batch_size = 128

sess = tf.InteractiveSession()
tf.global_variables_initializer().run()

# Training
for i in range(num_iter):  
    batch_x, _ = mnist.train.next_batch(batch_size)

    #shape (64, 784)
    batch = batch_x.reshape([batch_size, 28, 28, 1])

    noise_matrix = noise_constant * np.random.randn(batch_size, 784)
    noise_matrix = noise_matrix.reshape([batch_size, 28, 28, 1])

    batch_img_noise = batch + noise_matrix
    batch_img_noise = batch_img_noise.reshape([batch_size, 28, 28, 1])


    # Run optimization op (backprop) and cost op (to get loss value)
    _, l = sess.run([optim, loss], feed_dict={noise_imgs: batch_img_noise , imgs: batch})
    if i % 100 == 0:
        print("Iter", i, ":", l)

我对您的代码进行了一些更改,例如将L替换为loss,以使其在我的本地计算机上运行。

确实收敛了:

Iter 0 : 105.12259
Iter 100 : 58.750557
Iter 200 : 46.29199
Iter 300 : 43.19689
Iter 400 : 39.70022
Iter 500 : 38.924805
Iter 600 : 36.81252
Iter 700 : 36.478275
Iter 800 : 37.10568
Iter 900 : 36.200474

您可以使用matplotlib.pyplot可视化解码器的输出以进行健全性检查。我做了,它确实有效。

但是,您可能需要考虑将损失从tf.sqrt(tf.reduce_sum(tf.square(imgs-decoder_op)))更改为tf.reduce_sum(tf.square(imgs-decoder_op))