Python tensorflow中的自动排序。损失没有减少

Python tensorflow中的自动排序。损失没有减少,python,tensorflow,Python,Tensorflow,我不明白为什么损失没有改变。我尝试过的事情: 根据尺寸公式改变编码和解码的层数,改变学习率,改变优化函数,将两个批次作为无噪声的图像输入,改变批次大小,检查输入的有效性。下面提供了输出示例。这是完整的代码 我对TensorFlow比较陌生,它可能有点傻 import tensorflow as tf import numpy as np from tensorflow.examples.tutorials.mnist import input_data %matplotlib inline im

我不明白为什么损失没有改变。我尝试过的事情: 根据尺寸公式改变编码和解码的层数,改变学习率,改变优化函数,将两个批次作为无噪声的图像输入,改变批次大小,检查输入的有效性。下面提供了输出示例。这是完整的代码

我对TensorFlow比较陌生,它可能有点傻

import tensorflow as tf
import numpy as np
from tensorflow.examples.tutorials.mnist import input_data
%matplotlib inline
import matplotlib.pyplot as plt

#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, _ = data.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_x + noise_matrix
    batch_img_noise = batch_img_noise.reshape([64, 28, 28, 1])


    # Run optimization op (backprop) and cost op (to get loss value)
     _, l = sess.run([optim, L], feed_dict={noise_imgs: batch_img_noise , imgs: batch})
   print(l)
输出: 152.3966 152.28357 152.38466 152.44324 152.20834 152.43982 152.36153 152.38193 152.28334 152.45685 152.28116 152.4884

  #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)
我对您的代码做了一些更改,比如用
loss
替换
L
,以使其在本地计算机上运行

它确实汇聚了:

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.qRT(tf.ReffeIsand(tf.Simple(imgs-CdDelyOp)))< <代码> > <代码> tf.TrimeErthand(TF.Simple(imgs-CdDelyOP))