第10章 自编码网络——能够自学习样本特征的网络

10.1 自编码

自编码网络,是一种以重构输入信号为目标的神经网络。
包括输入层、隐藏层、输出层。
代码:

#1 引入头文件,并加载MNIST数据
import tensorflow as tf
import numpy as np
import matplotlib.pyplot as plt

# 导入 MINST 数据集
from tensorflow.examples.tutorials.mnist import input_data
mnist = input_data.read_data_sets("data/", one_hot=True)

'''========================================================'''
#2 定义网络模型

# 网络模型参数
learning_rate = 0.01
n_hidden_1 = 256 # 第一层256个节点
n_hidden_2 = 128 # 第二层128个节点
n_input = 784 # MNIST data 输入 (img shape: 28*28)

# 占位符
x = tf.placeholder("float", [None, n_input])#输入
y = x #输出


#学习参数
weights = {
    'encoder_h1': tf.Variable(tf.random_normal([n_input, n_hidden_1])),
    'encoder_h2': tf.Variable(tf.random_normal([n_hidden_1, n_hidden_2])),
    'decoder_h1': tf.Variable(tf.random_normal([n_hidden_2, n_hidden_1])),
    'decoder_h2': tf.Variable(tf.random_normal([n_hidden_1, n_input])),
}
biases = {
    'encoder_b1': tf.Variable(tf.zeros([n_hidden_1])),
    'encoder_b2': tf.Variable(tf.zeros([n_hidden_2])),
    'decoder_b1': tf.Variable(tf.zeros([n_hidden_1])),
    'decoder_b2': tf.Variable(tf.zeros([n_input])),
}


# 编码
def encoder(x):
    layer_1 = tf.nn.sigmoid(tf.add(tf.matmul(x, weights['encoder_h1']),biases['encoder_b1']))
    layer_2 = tf.nn.sigmoid(tf.add(tf.matmul(layer_1, weights['encoder_h2']), biases['encoder_b2']))
    return layer_2


# 解码
def decoder(x):
    layer_1 = tf.nn.sigmoid(tf.add(tf.matmul(x, weights['decoder_h1']),biases['decoder_b1']))
    layer_2 = tf.nn.sigmoid(tf.add(tf.matmul(layer_1, weights['decoder_h2']),biases['decoder_b2']))
    return layer_2

#输出的节点
encoder_out = encoder(x)
pred = decoder(encoder_out)

# 使用平方差为cost
cost = tf.reduce_mean(tf.pow(y - pred, 2))
optimizer = tf.train.RMSPropOptimizer(learning_rate).minimize(cost)
'''================================================================'''
#3 开始训练

# 训练参数
training_epochs = 20  #一共迭代20次
batch_size = 256     #每次取256个样本
display_step = 5     #迭代5次输出一次信息

# 启动绘话
with tf.Session() as sess:
    sess.run(tf.global_variables_initializer())


    total_batch = int(mnist.train.num_examples/batch_size)
    # 开始训练
    for epoch in range(training_epochs):#迭代

        for i in range(total_batch):
            batch_xs, batch_ys = mnist.train.next_batch(batch_size)#取数据
            _, c = sess.run([optimizer, cost], feed_dict={x: batch_xs})# 训练模型
        if epoch % display_step == 0:# 现实日志信息
            print("Epoch:", '%04d' % (epoch+1),"cost=", "{:.9f}".format(c))

    print("完成!")
    print("************************************************************!")
    '''================================================================'''
    #4 预测模型

    correct_prediction=tf.equal(tf.argmax(pred,1),tf.argmax(y,1))
    #计算错误率
    accuracy=tf.reduce_mean(tf.cast(correct_prediction,"float"))
    print("Accuracy:", 1-accuracy.eval({x:mnist.test.images,y:mnist.test.images}))
    '''================================================================'''
    #5 双比输入和输出

    # 可视化结果
    show_num = 10
    reconstruction = sess.run(
        pred, feed_dict={x: mnist.test.images[:show_num]})

    f, a = plt.subplots(2, 10, figsize=(10, 2))
    for i in range(show_num):
        a[0][i].imshow(np.reshape(mnist.test.images[i], (28, 28)))
        a[1][i].imshow(np.reshape(reconstruction[i], (28, 28)))
    plt.show()

结果:
在这里插入图片描述

Epoch: 0001 cost= 0.192956299
Epoch: 0006 cost= 0.120373718
Epoch: 0011 cost= 0.103519306
Epoch: 0016 cost= 0.094031595
完成!
************************************************************!
Accuracy: 1.0

10.2 自编码进阶
在自编码网络中使用线性解码器对MNIST数据特征进行再压缩,并将其映射到直角坐标系上。
代码:


import tensorflow as tf
import numpy as np
import matplotlib.pyplot as plt

# 导入 MINST 数据集
from tensorflow.examples.tutorials.mnist import input_data
mnist = input_data.read_data_sets("data/", one_hot=True)

#参数设置
learning_rate = 0.01
# hidden layer settings
n_hidden_1 = 256
n_hidden_2 = 64
n_hidden_3 = 16
n_hidden_4 = 2
n_input = 784  # MNIST data 输入 (img shape: 28*28)

#tf Graph输入
x = tf.placeholder("float", [None,n_input])
y=x
weights = {
    'encoder_h1': tf.Variable(tf.random_normal([n_input, n_hidden_1],)),
    'encoder_h2': tf.Variable(tf.random_normal([n_hidden_1, n_hidden_2],)),
    'encoder_h3': tf.Variable(tf.random_normal([n_hidden_2, n_hidden_3],)),
    'encoder_h4': tf.Variable(tf.random_normal([n_hidden_3, n_hidden_4],)),

    'decoder_h1': tf.Variable(tf.random_normal([n_hidden_4, n_hidden_3],)),
    'decoder_h2': tf.Variable(tf.random_normal([n_hidden_3, n_hidden_2],)),
    'decoder_h3': tf.Variable(tf.random_normal([n_hidden_2, n_hidden_1],)),
    'decoder_h4': tf.Variable(tf.random_normal([n_hidden_1, n_input],)),
	}

biases = {
    'encoder_b1': tf.Variable(tf.zeros([n_hidden_1])),
    'encoder_b2': tf.Variable(tf.zeros([n_hidden_2])),
    'encoder_b3': tf.Variable(tf.zeros([n_hidden_3])),
    'encoder_b4': tf.Variable(tf.zeros([n_hidden_4])),

    'decoder_b1': tf.Variable(tf.zeros([n_hidden_3])),
    'decoder_b2': tf.Variable(tf.zeros([n_hidden_2])),
    'decoder_b3': tf.Variable(tf.zeros([n_hidden_1])),
    'decoder_b4': tf.Variable(tf.zeros([n_input])),
	}


def encoder(x):
    layer_1 = tf.nn.sigmoid(tf.add(tf.matmul(x, weights['encoder_h1']),
                                   biases['encoder_b1']))
    layer_2 = tf.nn.sigmoid(tf.add(tf.matmul(layer_1, weights['encoder_h2']),
                                   biases['encoder_b2']))
    layer_3 = tf.nn.sigmoid(tf.add(tf.matmul(layer_2, weights['encoder_h3']),
                                   biases['encoder_b3']))
    layer_4 = tf.add(tf.matmul(layer_3, weights['encoder_h4']),
                                    biases['encoder_b4'])
    return layer_4


def decoder(x):
    layer_1 = tf.nn.sigmoid(tf.add(tf.matmul(x, weights['decoder_h1']),
                                   biases['decoder_b1']))
    layer_2 = tf.nn.sigmoid(tf.add(tf.matmul(layer_1, weights['decoder_h2']),
                                   biases['decoder_b2']))
    layer_3 = tf.nn.sigmoid(tf.add(tf.matmul(layer_2, weights['decoder_h3']),
                                biases['decoder_b3']))
    layer_4 = tf.nn.sigmoid(tf.add(tf.matmul(layer_3, weights['decoder_h4']),
                                biases['decoder_b4']))
    return layer_4


# 构建模型
encoder_op = encoder(x)
y_pred = decoder(encoder_op)	# 784 Features

cost = tf.reduce_mean(tf.pow(y - y_pred, 2))
optimizer = tf.train.AdamOptimizer(learning_rate).minimize(cost)



#训练
training_epochs = 20	# 20 Epoch 训练
batch_size = 256
display_step = 1

with tf.Session() as sess:
    sess.run(tf.global_variables_initializer())
    total_batch = int(mnist.train.num_examples/batch_size)
    # 启动循环开始训练
    for epoch in range(training_epochs):
        # 遍历全部数据集
        for i in range(total_batch):
            batch_xs, batch_ys = mnist.train.next_batch(batch_size)
            _, c = sess.run([optimizer, cost], feed_dict={x: batch_xs})
        # 显示训练中的详细信息
        if epoch % display_step == 0:
            print("Epoch:", '%04d' % (epoch+1),
                  "cost=", "{:.9f}".format(c))

    print("完成!")

    # 可视化结果
    show_num = 10
    encode_decode = sess.run(
        y_pred, feed_dict={x: mnist.test.images[:show_num]})
    # 将样本对应的自编码重建图像一并输出比较
    f, a = plt.subplots(2, 10, figsize=(10, 2))
    for i in range(show_num):
        a[0][i].imshow(np.reshape(mnist.test.images[i], (28, 28)))
        a[1][i].imshow(np.reshape(encode_decode[i], (28, 28)))
    plt.show()

    aa = [np.argmax(l) for l in mnist.test.labels]#将onehot编码转成一般编码
    encoder_result = sess.run(encoder_op, feed_dict={x: mnist.test.images})
    plt.scatter(encoder_result[:, 0], encoder_result[:, 1], c=aa)#mnist.test.labels)
    plt.colorbar()
    plt.show()

结果:
在这里插入图片描述

Epoch: 0001 cost= 0.096007034
Epoch: 0002 cost= 0.085217595
Epoch: 0003 cost= 0.080583066
Epoch: 0004 cost= 0.070086733
Epoch: 0005 cost= 0.063330181
Epoch: 0006 cost= 0.061958965
Epoch: 0007 cost= 0.060479369
Epoch: 0008 cost= 0.055719901
Epoch: 0009 cost= 0.059510261
Epoch: 0010 cost= 0.057593916
Epoch: 0011 cost= 0.056265257
Epoch: 0012 cost= 0.057208359
Epoch: 0013 cost= 0.055873204
Epoch: 0014 cost= 0.057073954
Epoch: 0015 cost= 0.055268638
Epoch: 0016 cost= 0.056733578
Epoch: 0017 cost= 0.050698649
Epoch: 0018 cost= 0.052249111
Epoch: 0019 cost= 0.052231155
Epoch: 0020 cost= 0.052817684
完成!

10.5 去噪声自编码
对MNIST集原始输入图片加入噪声,在自编码网络中进行训练,以得到抗干扰更强的特征提取模型。
代码:

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


# 创建网络模型及定义学习参数变量
mnist = input_data.read_data_sets("/data/", one_hot=True)

train_X = mnist.train.images
train_Y = mnist.train.labels
test_X = mnist.test.images
test_Y = mnist.test.labels

tf.reset_default_graph()

# 将784维通过一层压缩变为256维。
n_input = 784
n_hidden_1 = 256

# 占位符
x = tf.placeholder("float", [None, n_input])
y = tf.placeholder("float", [None, n_input])
dropout_keep_prob = tf.placeholder("float")

# 学习参数
weights = {
    'h1': tf.Variable(tf.random_normal([n_input, n_hidden_1])),
    'h2': tf.Variable(tf.random_normal([n_hidden_1, n_hidden_1])),
    'out': tf.Variable(tf.random_normal([n_hidden_1, n_input]))
}
biases = {
    'b1': tf.Variable(tf.zeros([n_hidden_1])),
    'b2': tf.Variable(tf.zeros([n_hidden_1])),
    'out': tf.Variable(tf.zeros([n_input]))
}


# 网络模型
def denoise_auto_encoder(_X, _weights, _biases, _keep_prob):
    layer_1 = tf.nn.sigmoid(tf.add(tf.matmul(_X, _weights['h1']), _biases['b1']))
    layer_1out = tf.nn.dropout(layer_1, _keep_prob)
    layer_2 = tf.nn.sigmoid(tf.add(tf.matmul(layer_1out, _weights['h2']), _biases['b2']))
    layer_2out = tf.nn.dropout(layer_2, _keep_prob)
    return tf.nn.sigmoid(tf.matmul(layer_2out, _weights['out']) + _biases['out'])


reconstruction = denoise_auto_encoder(x, weights, biases, dropout_keep_prob)

# COST
cost = tf.reduce_mean(tf.pow(reconstruction - y, 2))
# OPTIMIZER
optm = tf.train.AdamOptimizer(0.01).minimize(cost)

# 训练参数,开始训练
epochs = 20
batch_size = 256
disp_step = 2

with tf.Session() as sess:
    sess.run(tf.global_variables_initializer())

    print("开始训练")
    for epoch in range(epochs):
        num_batch = int(mnist.train.num_examples / batch_size)
        total_cost = 0.
        for i in range(num_batch):
            # 生成噪声数据
            batch_xs, batch_ys = mnist.train.next_batch(batch_size)
            batch_xs_noisy = batch_xs + 0.3 * np.random.randn(batch_size, 784)
            feeds = {x: batch_xs_noisy, y: batch_xs, dropout_keep_prob: 1.}
            sess.run(optm, feed_dict=feeds)
            total_cost += sess.run(cost, feed_dict=feeds)

        # 显示训练日志
        if epoch % disp_step == 0:
            print("Epoch %02d/%02d average cost: %.6f"
                  % (epoch, epochs, total_cost / num_batch))

    print("完成")

# 数据可视化
    show_num = 10
    test_noisy = mnist.test.images[:show_num] + 0.3 * np.random.randn(show_num, 784)
    encode_decode = sess.run(
        reconstruction, feed_dict={x: test_noisy, dropout_keep_prob: 1.})
    f, a = plt.subplots(3, 10, figsize=(10, 3))
    for i in range(show_num):
        a[0][i].imshow(np.reshape(test_noisy[i], (28, 28)))
        a[1][i].imshow(np.reshape(mnist.test.images[i], (28, 28)))
        a[2][i].matshow(np.reshape(encode_decode[i], (28, 28)), cmap=plt.get_cmap('gray'))
    plt.show()

# 测试鲁棒性
    # 换一种噪声测试一个
    randidx = np.random.randint(test_X.shape[0], size=1)
    orgvec = test_X[randidx, :]
    testvec = test_X[randidx, :]
    label = np.argmax(test_Y[randidx, :], 1)

    print("label is %d" % (label))
    # Noise type

    print("Salt and Pepper Noise")
    noisyvec = testvec
    rate = 0.15
    noiseidx = np.random.randint(test_X.shape[1]
                                 , size=int(test_X.shape[1] * rate))
    noisyvec[0, noiseidx] = 1 - noisyvec[0, noiseidx]

    outvec = sess.run(reconstruction, feed_dict={x: noisyvec, dropout_keep_prob: 1})
    outimg = np.reshape(outvec, (28, 28))

    # Plot
    plt.matshow(np.reshape(orgvec, (28, 28)), cmap=plt.get_cmap('gray'))
    plt.title("Original Image")
    plt.colorbar()

    plt.matshow(np.reshape(noisyvec, (28, 28)), cmap=plt.get_cmap('gray'))
    plt.title("Input Image")
    plt.colorbar()

    plt.matshow(outimg, cmap=plt.get_cmap('gray'))
    plt.title("Reconstructed Image")
    plt.colorbar()
    plt.show()


结果:

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

开始训练
Epoch 00/20 average cost: 0.101208
Epoch 02/20 average cost: 0.077133
Epoch 04/20 average cost: 0.073884
Epoch 06/20 average cost: 0.070184
Epoch 08/20 average cost: 0.067796
Epoch 10/20 average cost: 0.066744
Epoch 12/20 average cost: 0.065942
Epoch 14/20 average cost: 0.064924
Epoch 16/20 average cost: 0.064323
Epoch 18/20 average cost: 0.063780
完成
label is 3
Salt and Pepper Noise

10.6 自编码综合
首先建立一个去噪自编码,然后再对第一层的输出做一层的输出做一次简单的自编码压缩,然后再将第二层的输出做一个softmax的分类,最后,把这3个网络里的中间层拿出来,组成一个新的网络进行微调。
代码:

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

mnist = input_data.read_data_sets("/data/", one_hot=True)

train_X = mnist.train.images
train_Y = mnist.train.labels
test_X = mnist.test.images
test_Y = mnist.test.labels
print("MNIST ready")

tf.reset_default_graph()

# NETOWRK PARAMETERS
n_input = 784
n_hidden_1 = 256  # 第一层自编码
n_hidden_2 = 128  # 第二层自编码
n_classes = 10

# PLACEHOLDERS
# 第一层输入
x = tf.placeholder("float", [None, n_input])
y = tf.placeholder("float", [None, n_input])
dropout_keep_prob = tf.placeholder("float")
# 第二层输入
l2x = tf.placeholder("float", [None, n_hidden_1])
l2y = tf.placeholder("float", [None, n_hidden_1])
# 第三层输入
l3x = tf.placeholder("float", [None, n_hidden_2])
l3y = tf.placeholder("float", [None, n_classes])

# WEIGHTS
weights = {
    # 网络1  784-256-784
    'h1': tf.Variable(tf.random_normal([n_input, n_hidden_1])),
    'l1_h2': tf.Variable(tf.random_normal([n_hidden_1, n_hidden_1])),
    'l1_out': tf.Variable(tf.random_normal([n_hidden_1, n_input])),
    # 网络2  256-128-256
    'l2_h1': tf.Variable(tf.random_normal([n_hidden_1, n_hidden_2])),
    'l2_h2': tf.Variable(tf.random_normal([n_hidden_2, n_hidden_2])),
    'l2_out': tf.Variable(tf.random_normal([n_hidden_2, n_hidden_1])),
    # 网络3  128-10
    'out': tf.Variable(tf.random_normal([n_hidden_2, n_classes]))
}
biases = {
    'b1': tf.Variable(tf.zeros([n_hidden_1])),
    'l1_b2': tf.Variable(tf.zeros([n_hidden_1])),
    'l1_out': tf.Variable(tf.zeros([n_input])),

    'l2_b1': tf.Variable(tf.zeros([n_hidden_2])),
    'l2_b2': tf.Variable(tf.zeros([n_hidden_2])),
    'l2_out': tf.Variable(tf.zeros([n_hidden_1])),

    'out': tf.Variable(tf.zeros([n_classes]))
}

# 第一层的编码输出
l1_out = tf.nn.sigmoid(tf.add(tf.matmul(x, weights['h1']), biases['b1']))


# l1 decoder MODEL
def noise_l1_autodecoder(layer_1, _weights, _biases, _keep_prob):
    layer_1out = tf.nn.dropout(layer_1, _keep_prob)
    layer_2 = tf.nn.sigmoid(tf.add(tf.matmul(layer_1out, _weights['l1_h2']), _biases['l1_b2']))
    layer_2out = tf.nn.dropout(layer_2, _keep_prob)
    return tf.nn.sigmoid(tf.matmul(layer_2out, _weights['l1_out']) + _biases['l1_out'])


# 第一层的解码输出
l1_reconstruction = noise_l1_autodecoder(l1_out, weights, biases, dropout_keep_prob)

# COST
l1_cost = tf.reduce_mean(tf.pow(l1_reconstruction - y, 2))
# OPTIMIZER
l1_optm = tf.train.AdamOptimizer(0.01).minimize(l1_cost)


# l2 decoder MODEL
def l2_autodecoder(layer1_2, _weights, _biases):
    layer1_2out = tf.nn.sigmoid(tf.add(tf.matmul(layer1_2, _weights['l2_h2']), _biases['l2_b2']))
    return tf.nn.sigmoid(tf.matmul(layer1_2out, _weights['l2_out']) + _biases['l2_out'])


# 第二层的编码输出
l2_out = tf.nn.sigmoid(tf.add(tf.matmul(l2x, weights['l2_h1']), biases['l2_b1']))
# 第二层的解码输出
l2_reconstruction = l2_autodecoder(l2_out, weights, biases)

# COST
l2_cost = tf.reduce_mean(tf.pow(l2_reconstruction - l2y, 2))
# OPTIMIZER
optm2 = tf.train.AdamOptimizer(0.01).minimize(l2_cost)

# l3  分类
l3_out = tf.matmul(l3x, weights['out']) + biases['out']
l3_cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=l3_out, labels=l3y))
l3_optm = tf.train.AdamOptimizer(0.01).minimize(l3_cost)

# 3层 级联
# 1联2
l1_l2out = tf.nn.sigmoid(tf.add(tf.matmul(l1_out, weights['l2_h1']), biases['l2_b1']))
# 2联3
pred = tf.matmul(l1_l2out, weights['out']) + biases['out']
# Define loss and optimizer
cost3 = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=pred, labels=l3y))
optm3 = tf.train.AdamOptimizer(0.001).minimize(cost3)
print("l3 级联 ")

epochs = 50
batch_size = 100
disp_step = 10
load_epoch = 49
'''
with tf.Session() as sess:
    sess.run(tf.global_variables_initializer())

    print ("开始训练")
    for epoch in range(epochs):
        num_batch  = int(mnist.train.num_examples/batch_size)
        total_cost = 0.
        for i in range(num_batch):
            batch_xs, batch_ys = mnist.train.next_batch(batch_size)
            batch_xs_noisy = batch_xs + 0.3*np.random.randn(batch_size, 784)
            feeds = {x: batch_xs_noisy, y: batch_xs, dropout_keep_prob: 0.5}
            sess.run(l1_optm, feed_dict=feeds)
            total_cost += sess.run(l1_cost, feed_dict=feeds)
        # DISPLAY
        if epoch % disp_step == 0:
            print ("Epoch %02d/%02d average cost: %.6f" 
                   % (epoch, epochs, total_cost/num_batch))


    print(sess.run(weights['h1'])) 
    print (weights['h1'].name) 

    print ("完成")    
    show_num = 10
    test_noisy = mnist.test.images[:show_num] + 0.3*np.random.randn(show_num, 784)
    encode_decode = sess.run(
        l1_reconstruction, feed_dict={x: test_noisy, dropout_keep_prob: 1.})
    f, a = plt.subplots(3, 10, figsize=(10, 3))
    for i in range(show_num):
        a[0][i].imshow(np.reshape(test_noisy[i], (28, 28)))
        a[1][i].imshow(np.reshape(mnist.test.images[i], (28, 28)))
        a[2][i].matshow(np.reshape(encode_decode[i], (28, 28)), cmap=plt.get_cmap('gray'))
    plt.show()
'''

'''

with tf.Session() as sess:
    sess.run(tf.global_variables_initializer())




    print ("开始训练")
    for epoch in range(epochs):
        num_batch  = int(mnist.train.num_examples/batch_size)
        total_cost = 0.
        for i in range(num_batch):
            batch_xs, batch_ys = mnist.train.next_batch(batch_size)

            l1_h = sess.run(l1_out, feed_dict={x: batch_xs, y: batch_xs, dropout_keep_prob: 1.})
            _,l2cost = sess.run([optm2,l2_cost], feed_dict={l2x: l1_h, l2y: l1_h })
            total_cost += l2cost

       # log
        if epoch % disp_step == 0:
            print ("Epoch %02d/%02d average cost: %.6f" 
                   % (epoch, epochs, total_cost/num_batch))

    print(sess.run(weights['h1'])) 
    print (weights['h1'].name)  
    print ("完成  layer_2 训练")

    show_num = 10
    testvec = mnist.test.images[:show_num]
    out1vec = sess.run(l1_out, feed_dict={x: testvec,y: testvec, dropout_keep_prob: 1.})
    out2vec = sess.run(l2_reconstruction, feed_dict={l2x: out1vec})

    f, a = plt.subplots(3, 10, figsize=(10, 3))
    for i in range(show_num):
        a[0][i].imshow(np.reshape(testvec[i], (28, 28)))
        a[1][i].matshow(np.reshape(out1vec[i], (16, 16)), cmap=plt.get_cmap('gray'))
        a[2][i].matshow(np.reshape(out2vec[i], (16, 16)), cmap=plt.get_cmap('gray'))
    plt.show()



'''

'''
with tf.Session() as sess:
    sess.run(tf.global_variables_initializer())




    print ("开始训练")
    for epoch in range(epochs):
        num_batch  = int(mnist.train.num_examples/batch_size)
        total_cost = 0.
        for i in range(num_batch):
            batch_xs, batch_ys = mnist.train.next_batch(batch_size)               
            l1_h = sess.run(l1_out, feed_dict={x: batch_xs, y: batch_xs, dropout_keep_prob: 1.})               
            l2_h = sess.run(l2_out, feed_dict={l2x: l1_h, l2y: l1_h })
            _,l3cost = sess.run([l3_optm,l3_cost], feed_dict={l3x: l2_h, l3y: batch_ys})

            total_cost += l3cost
        # DISPLAY
        if epoch % disp_step == 0:
            print ("Epoch %02d/%02d average cost: %.6f" 
                   % (epoch, epochs, total_cost/num_batch))


    print ("完成  layer_3 训练")
    # 测试 model
    correct_prediction = tf.equal(tf.argmax(pred, 1), tf.argmax(l3y, 1))
    # 计算准确率
    accuracy = tf.reduce_mean(tf.cast(correct_prediction, "float"))
    print ("Accuracy:", accuracy.eval({x: mnist.test.images, l3y: mnist.test.labels}))


'''
with tf.Session() as sess:
    sess.run(tf.global_variables_initializer())

    print("开始训练")
    for epoch in range(epochs):
        num_batch = int(mnist.train.num_examples / batch_size)
        total_cost = 0.
        for i in range(num_batch):
            batch_xs, batch_ys = mnist.train.next_batch(batch_size)

            feeds = {x: batch_xs, l3y: batch_ys}
            sess.run(optm3, feed_dict=feeds)
            total_cost += sess.run(cost3, feed_dict=feeds)
        # DISPLAY
        if epoch % disp_step == 0:
            print("Epoch %02d/%02d average cost: %.6f"
                  % (epoch, epochs, total_cost / num_batch))

    print("完成  级联 训练")
    # 测试 model
    correct_prediction = tf.equal(tf.argmax(pred, 1), tf.argmax(l3y, 1))
    # 计算准确率
    accuracy = tf.reduce_mean(tf.cast(correct_prediction, "float"))
    print("Accuracy:", accuracy.eval({x: mnist.test.images, l3y: mnist.test.labels}))

# '''
l3 级联 
开始训练
Epoch 00/50 average cost: 1.447921
Epoch 10/50 average cost: 0.074504
Epoch 20/50 average cost: 0.011597
Epoch 30/50 average cost: 0.001466
Epoch 40/50 average cost: 0.001663
完成  级联 训练
Accuracy: 0.9548

10.8 变分自编码
程序:

#1 引入库,定义占位符
import numpy as np
import tensorflow as tf
import matplotlib.pyplot as plt
from scipy.stats import norm

from tensorflow.examples.tutorials.mnist import input_data

mnist = input_data.read_data_sets("/data/")  # , one_hot=True)

n_input = 784
n_hidden_1 = 256
n_hidden_2 = 2

x = tf.placeholder(tf.float32, [None, n_input])

zinput = tf.placeholder(tf.float32, [None, n_hidden_2])
'''----------------------------------------------------'''
#3 定义学习参数

weights = {

    'w1': tf.Variable(tf.truncated_normal([n_input, n_hidden_1],
                                          stddev=0.001)),
    'b1': tf.Variable(tf.zeros([n_hidden_1])),

    'mean_w1': tf.Variable(tf.truncated_normal([n_hidden_1, n_hidden_2],
                                               stddev=0.001)),
    'log_sigma_w1': tf.Variable(tf.truncated_normal([n_hidden_1, n_hidden_2],
                                                    stddev=0.001)),

    'w2': tf.Variable(tf.truncated_normal([n_hidden_2, n_hidden_1],
                                          stddev=0.001)),

    'b2': tf.Variable(tf.zeros([n_hidden_1])),
    'w3': tf.Variable(tf.truncated_normal([n_hidden_1, n_input],
                                          stddev=0.001)),

    'b3': tf.Variable(tf.zeros([n_input])),

    'mean_b1': tf.Variable(tf.zeros([n_hidden_2])),

    'log_sigma_b1': tf.Variable(tf.zeros([n_hidden_2]))
}
'''----------------------------------------------------'''
#3 定义网络结构

h1 = tf.nn.relu(tf.add(tf.matmul(x, weights['w1']), weights['b1']))
z_mean = tf.add(tf.matmul(h1, weights['mean_w1']), weights['mean_b1'])
z_log_sigma_sq = tf.add(tf.matmul(h1, weights['log_sigma_w1']), weights['log_sigma_b1'])

# sample from gaussian distribution
eps = tf.random_normal(tf.stack([tf.shape(h1)[0], n_hidden_2]), 0, 1, dtype=tf.float32)
z = tf.add(z_mean, tf.multiply(tf.sqrt(tf.exp(z_log_sigma_sq)), eps))
h2 = tf.nn.relu(tf.matmul(z, weights['w2']) + weights['b2'])
reconstruction = tf.matmul(h2, weights['w3']) + weights['b3']

h2out = tf.nn.relu(tf.matmul(zinput, weights['w2']) + weights['b2'])
reconstructionout = tf.matmul(h2out, weights['w3']) + weights['b3']
'''-----------------------------------------------------------------'''
#4 构建模型的反向传播

# cost
reconstr_loss = 0.5 * tf.reduce_sum(tf.pow(tf.subtract(reconstruction, x), 2.0))
latent_loss = -0.5 * tf.reduce_sum(1 + z_log_sigma_sq
                                   - tf.square(z_mean)
                                   - tf.exp(z_log_sigma_sq), 1)
cost = tf.reduce_mean(reconstr_loss + latent_loss)

optimizer = tf.train.AdamOptimizer(learning_rate=0.001).minimize(cost)
'''------------------------------------------------------------------'''
#5 设置参数,进行训练

training_epochs = 50#迭代50次
batch_size = 128
display_step = 3

with tf.Session() as sess:
    sess.run(tf.global_variables_initializer())

    for epoch in range(training_epochs):
        avg_cost = 0.
        total_batch = int(mnist.train.num_examples / batch_size)

        # 遍历全部数据集
        for i in range(total_batch):
            batch_xs, batch_ys = mnist.train.next_batch(batch_size)  # 取数据

            # Fit training using batch data
            _, c = sess.run([optimizer, cost], feed_dict={x: batch_xs})
            # c = autoencoder.partial_fit(batch_xs)
        # 显示训练中的详细信息
        if epoch % display_step == 0:
            print("Epoch:", '%04d' % (epoch + 1), "cost=", "{:.9f}".format(c))

    print("完成!")

    # 测试
    print("Result:", cost.eval({x: mnist.test.images}))
    print("*******************************************")
    '''------------------------------------------------'''

    # 可视化结果
    show_num = 10
    pred = sess.run(
        reconstruction, feed_dict={x: mnist.test.images[:show_num]})

    f, a = plt.subplots(2, 10, figsize=(10, 2))
    for i in range(show_num):
        a[0][i].imshow(np.reshape(mnist.test.images[i], (28, 28)))
        a[1][i].imshow(np.reshape(pred[i], (28, 28)))
    plt.draw()

    pred = sess.run(
        z, feed_dict={x: mnist.test.images})
    # x_test_encoded = encoder.predict(x_test, batch_size=batch_size)
    plt.figure(figsize=(6, 6))
    plt.scatter(pred[:, 0], pred[:, 1], c=mnist.test.labels)
    plt.colorbar()
    plt.show()
    '''--------------------------------------------------------------'''
    #6 高斯分布取样,生成模拟数据

    # display a 2D manifold of the digits
    n = 15  # figure with 15x15 digits
    digit_size = 28
    figure = np.zeros((digit_size * n, digit_size * n))
    grid_x = norm.ppf(np.linspace(0.05, 0.95, n))
    grid_y = norm.ppf(np.linspace(0.05, 0.95, n))

    for i, yi in enumerate(grid_x):
        for j, xi in enumerate(grid_y):
            z_sample = np.array([[xi, yi]])
            x_decoded = sess.run(reconstructionout, feed_dict={zinput: z_sample})

            digit = x_decoded[0].reshape(digit_size, digit_size)
            figure[i * digit_size: (i + 1) * digit_size,
            j * digit_size: (j + 1) * digit_size] = digit

    plt.figure(figsize=(10, 10))
    plt.imshow(figure, cmap='Greys_r')
    plt.show()

结果:
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

Epoch: 0001 cost= 2872.595703125
Epoch: 0004 cost= 2431.135253906
Epoch: 0007 cost= 2299.947753906
Epoch: 0010 cost= 2311.812011719
Epoch: 0013 cost= 2279.201171875
Epoch: 0016 cost= 2222.620849609
Epoch: 0019 cost= 2271.043212891
Epoch: 0022 cost= 2079.413085938
Epoch: 0025 cost= 2295.992187500
Epoch: 0028 cost= 2029.589843750
Epoch: 0031 cost= 2096.290527344
Epoch: 0034 cost= 2078.635742188
Epoch: 0037 cost= 2198.721191406
Epoch: 0040 cost= 2121.172607422
Epoch: 0043 cost= 2084.535156250
Epoch: 0046 cost= 1892.449462891
Epoch: 0049 cost= 2131.024658203
完成!
Result: 163601.69
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值