张量流自动编码器实现在重建中产生大量噪声

时间:2017-07-18 08:24:55

标签: python machine-learning tensorflow neural-network autoencoder

如标题中所述,我使用张量流实现了堆叠自动编码器,以将图像数据压缩到较低维度。然而,在完成学习之后,重建的图像在整个数据集中包含静态噪声。所以我试着用一个黑色图像训练自动编码器,再现这个嘈杂的图像后,学习曲线保持不变。

Ansible-jira

(左上角是学习曲线,左下角是全黑图像,右下角是重建的嘈杂图像)

以下是我定义堆叠自动编码器的方法。当我想使它成为去噪自动编码器时使用掩码,在这种情况下根本不使用。

# Hyper parameters
learning_rate = 0.0005
training_epochs = 5000
batch_size = 1
total_batch = 1
corruption = 0.3

# Network parameters
n_hidden_6 = 30
n_hidden_5 = 100
n_hidden_4 = 250
n_hidden_3 = 500
n_hidden_2 = 800
n_hidden_1 = 1000
n_input = 1200

# tf Graph input 
X = tf.placeholder("float", [None, n_input])
mask = tf.placeholder("float", [None, n_input])

weights = {
    'encoder_h1': tf.Variable(tf.random_normal([n_input, n_hidden_1]), name = 'encoder_h1'),
    'encoder_h2': tf.Variable(tf.random_normal([n_hidden_1, n_hidden_2]), name = 'encoder_h2'),
    'encoder_h3': tf.Variable(tf.random_normal([n_hidden_2, n_hidden_3]), name = 'encoder_h3'),
    'encoder_h4': tf.Variable(tf.random_normal([n_hidden_3, n_hidden_4]), name = 'encoder_h4'),
    'encoder_h5': tf.Variable(tf.random_normal([n_hidden_4, n_hidden_5]), name = 'encoder_h5'),
    'encoder_h6': tf.Variable(tf.random_normal([n_hidden_5, n_hidden_6]), name = 'encoder_h6'),
    'decoder_h1': tf.Variable(tf.random_normal([n_hidden_6, n_hidden_5]), name = 'decoder_h1'),
    'decoder_h2': tf.Variable(tf.random_normal([n_hidden_5, n_hidden_4]), name = 'decoder_h2'),
    'decoder_h3': tf.Variable(tf.random_normal([n_hidden_4, n_hidden_3]), name = 'decoder_h3'),
    'decoder_h4': tf.Variable(tf.random_normal([n_hidden_3, n_hidden_2]), name = 'decoder_h4'),
    'decoder_h5': tf.Variable(tf.random_normal([n_hidden_2, n_hidden_1]), name = 'decoder_h5'),
    'decoder_h6': tf.Variable(tf.random_normal([n_hidden_1, n_input]), name = 'decoder_h6'),
}

biases = {
    'encoder_b1': tf.Variable(tf.random_normal([n_hidden_1]), name = 'encoder_b1'),
    'encoder_b2': tf.Variable(tf.random_normal([n_hidden_2]), name = 'encoder_b2'),
    'encoder_b3': tf.Variable(tf.random_normal([n_hidden_3]), name = 'encoder_b3'),
    'encoder_b4': tf.Variable(tf.random_normal([n_hidden_4]), name = 'encoder_b4'),
    'encoder_b5': tf.Variable(tf.random_normal([n_hidden_5]), name = 'encoder_b5'),
    'encoder_b6': tf.Variable(tf.random_normal([n_hidden_6]), name = 'encoder_b6'),
    'decoder_b1': tf.Variable(tf.random_normal([n_hidden_5]), name = 'decoder_b1'),
    'decoder_b2': tf.Variable(tf.random_normal([n_hidden_4]), name = 'decoder_b2'),
    'decoder_b3': tf.Variable(tf.random_normal([n_hidden_3]), name = 'decoder_b3'),
    'decoder_b4': tf.Variable(tf.random_normal([n_hidden_2]), name = 'decoder_b4'),
    'decoder_b5': tf.Variable(tf.random_normal([n_hidden_1]), name = 'decoder_b5'),
    'decoder_b6': tf.Variable(tf.random_normal([n_input]), name = 'decoder_b6'),
}

# Building the encoder
def encoder(x, mask):
    # corruption
    x_c = x * mask
    # Encoder Hidden layer with sigmoid activation #1
    layer_1 = tf.nn.sigmoid(tf.add(tf.matmul(x_c, weights['encoder_h1']), 
                                   biases['encoder_b1']))
    # Encoder Hidden layer with sigmoid activation #2
    layer_2 = tf.nn.sigmoid(tf.add(tf.matmul(layer_1, weights['encoder_h2']), 
                                   biases['encoder_b2']))
    # Encoder Hidden layer with sigmoid activation #3
    layer_3 = tf.nn.sigmoid(tf.add(tf.matmul(layer_2, weights['encoder_h3']), 
                                   biases['encoder_b3']))
    # Encoder Hidden layer with sigmoid activation #4
    layer_4 = tf.nn.sigmoid(tf.add(tf.matmul(layer_3, weights['encoder_h4']), 
                                   biases['encoder_b4']))
    # Encoder Hidden layer with sigmoid activation #5
    layer_5 = tf.nn.sigmoid(tf.add(tf.matmul(layer_4, weights['encoder_h5']), 
                                   biases['encoder_b5']))
    # Encoder Hidden layer with sigmoid activation #6
    layer_6 = tf.nn.sigmoid(tf.add(tf.matmul(layer_5, weights['encoder_h6']), 
                                   biases['encoder_b6']))
    return layer_6

# Building the decoder
def decoder(x):
    # Decoder Hidden layer with sigmoid activation #1
    layer_1 = tf.nn.sigmoid(tf.add(tf.matmul(x, weights['decoder_h1']), 
                                   biases['decoder_b1']))
    # Decoder Hidden layer with sigmoid activation #2
    layer_2 = tf.nn.sigmoid(tf.add(tf.matmul(layer_1, weights['decoder_h2']), 
                                   biases['decoder_b2']))
    # Decoder Hidden layer with sigmoid activation #3
    layer_3 = tf.nn.sigmoid(tf.add(tf.matmul(layer_2, weights['decoder_h3']), 
                                   biases['decoder_b3']))
    # Decoder Hidden layer with sigmoid activation #4
    layer_4 = tf.nn.sigmoid(tf.add(tf.matmul(layer_3, weights['decoder_h4']), 
                                   biases['decoder_b4']))
    # Decoder Hidden layer with sigmoid activation #5
    layer_5 = tf.nn.sigmoid(tf.add(tf.matmul(layer_4, weights['decoder_h5']), 
                                   biases['decoder_b5']))
    # Decoder Hidden layer with sigmoid activation #6
    logit = tf.add(tf.matmul(layer_5, weights['decoder_h6']), biases['decoder_b6'])
    layer_6 = tf.nn.sigmoid(logit)

    return layer_6, logit

# Construct model
encoder_op = encoder(X, mask)
decoder_op, logit = decoder(encoder_op)

# Prediction
y_pred = decoder_op
y_true = X

# Define loss and optimizer, minimize the squared error
cost = tf.reduce_mean(tf.pow(y_true - y_pred, 2))
#optimizer = tf.train.AdamOptimizer(learning_rate).minimize(cost)
#cost = tf.reduce_mean(tf.nn.sigmoid_cross_entropy_with_logits(labels = y_true, logits = logit))
optimizer = tf.train.AdamOptimizer(learning_rate).minimize(cost)

# Initializing the variables
init = tf.global_variables_initializer()

任何人都可以帮我解释这个实现可能存在的问题吗?使用其他损失函数或优化器是否更好? 提前谢谢!

0 个答案:

没有答案