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

10-1 自编码
通过构建一个两层降维的自编码网络,将MNIST数据集的数据特征提取出来,并通过这些特征再重建一个MNIST数据集。

程序:

#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("F:/PycharmProjects/test20190701/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()

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

Extracting F:/PycharmProjects/test20190701/data\train-images-idx3-ubyte.gz

Extracting F:/PycharmProjects/test20190701/data\train-labels-idx1-ubyte.gz

Extracting F:/PycharmProjects/test20190701/data\t10k-images-idx3-ubyte.gz
Extracting F:/PycharmProjects/test20190701/data\t10k-labels-idx1-ubyte.gz

Epoch: 0001 cost= 0.199856699
Epoch: 0006 cost= 0.127162486
Epoch: 0011 cost= 0.116274573
Epoch: 0016 cost= 0.106865443
完成!
************************************************************!
Accuracy: 0.9996000000101048

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

程序:


#1 引入头文件,定义学习参数变量
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])),
	}
'''====================================================================='''
#2 定义网络模型

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)
'''====================================================================='''
#3 开始训练

#训练
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("完成!")
    '''====================================================================='''
    #4 双比输入和输出

    # 可视化结果
    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()
    '''====================================================================='''
    #5 显示数据的二维特征

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

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

Extracting /data/train-images-idx3-ubyte.gz

Successfully downloaded train-labels-idx1-ubyte.gz 28881 bytes.

Extracting /data/train-labels-idx1-ubyte.gz
Successfully downloaded t10k-images-idx3-ubyte.gz 1648877 bytes.
Extracting /data/t10k-images-idx3-ubyte.gz
Successfully downloaded t10k-labels-idx1-ubyte.gz 4542 bytes.

Extracting /data/t10k-labels-idx1-ubyte.gz
Epoch: 0001 cost= 0.098088555
Epoch: 0002 cost= 0.081548415
Epoch: 0003 cost= 0.074361034
Epoch: 0004 cost= 0.069985919
Epoch: 0005 cost= 0.066199340
Epoch: 0006 cost= 0.067776829
Epoch: 0007 cost= 0.067173541
Epoch: 0008 cost= 0.065238215
Epoch: 0009 cost= 0.061293330
Epoch: 0010 cost= 0.062647395
Epoch: 0011 cost= 0.060980082
Epoch: 0012 cost= 0.062286288
Epoch: 0013 cost= 0.059983090
Epoch: 0014 cost= 0.058413241
Epoch: 0015 cost= 0.057253409
Epoch: 0016 cost= 0.057223063
Epoch: 0017 cost= 0.056708831
Epoch: 0018 cost= 0.058358241
Epoch: 0019 cost= 0.057317283
Epoch: 0020 cost= 0.054877508
完成!

10-3 卷积网络自编码
在自编码网络中使用卷积网络完成MNIST的自编码功能。

程序:

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

程序:


#1 引入头文件,创建网络模型及定义学习参数变量
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()

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)
'''---------------------------------------------------------------------------------'''
#2 设置训练参数,开始训练

# 训练参数
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):
            '''---------------------------------------------------------------------------------'''
            #3 生成噪声数据

            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("完成")
    '''---------------------------------------------------------------------------------'''
    #4 数据可视化

    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()
    '''---------------------------------------------------------------------------------'''
    #5 测试鲁棒性

    # 换一种噪声测试一个
    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.097855
Epoch 02/20 average cost: 0.079376
Epoch 04/20 average cost: 0.075205
Epoch 06/20 average cost: 0.073017
Epoch 08/20 average cost: 0.071194
Epoch 10/20 average cost: 0.069011
Epoch 12/20 average cost: 0.067995
Epoch 14/20 average cost: 0.066617
Epoch 16/20 average cost: 0.065852
Epoch 18/20 average cost: 0.065668
完成
label is 5
Salt and Pepper Noise

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

程序1:


#1 引入头文件,创建网络模型及定义学习参数变量
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()
'''----------------------------------------'''
#2 定义占位符

# 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])
'''----------------------------------------'''
#3 定义学习参数

# 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]))
}
'''----------------------------------------'''
#4 第1层网络结构

# 第一层的编码输出
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)
'''----------------------------------------'''
#5 第2层网络结构

# 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)
'''----------------------------------------'''
#6 第3层网络结构

# 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)
'''----------------------------------------'''
#7 定义级联网络结构

# 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 级联 ")
'''----------------------------------------'''
#8 第1层网络训练

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 ("完成")
    print("**************************************************")
    '''----------------------------------------'''
    # 结果1可视化

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

结果1:

MNIST ready

l3 级联 

开始训练
Epoch 00/50 average cost: 0.112957
Epoch 10/50 average cost: 0.035921
Epoch 20/50 average cost: 0.033433
Epoch 30/50 average cost: 0.032461
Epoch 40/50 average cost: 0.032034
[[-0.36378413 -0.49970618  1.7050649  ...  0.09803656  0.72312486
  -0.73460436]
 [-0.4987043  -1.3530658   2.5956523  ... -1.9337609  -0.5464082
  -1.1919619 ]
 [ 0.24221909  0.4015173  -0.22808756 ...  1.6253659  -2.059349
  -1.1482382 ]
 ...
 [ 1.312324    2.6536787  -0.44841388 ...  0.82971406 -0.78862363
   0.17776114]
 [ 1.5881209  -0.88560134  0.31182453 ...  0.03002243  1.8114684
  -0.9737606 ]
 [-0.455956   -0.26287037 -0.5249553  ...  0.15877177  0.5214224
  -2.3164146 ]]
Variable:0
完成
**************************************************

在这里插入图片描述在这里插入图片描述程序2:


#1 引入头文件,创建网络模型及定义学习参数变量
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()
'''----------------------------------------'''
#2 定义占位符

# 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])
'''----------------------------------------'''
#3 定义学习参数

# 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]))
}
'''----------------------------------------'''
#4 第1层网络结构

# 第一层的编码输出
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)
'''----------------------------------------'''
#5 第2层网络结构

# 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)
'''----------------------------------------'''
#6 第3层网络结构

# 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)
'''----------------------------------------'''
#7 定义级联网络结构

# 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 级联 ")
'''----------------------------------------'''
#8 第1层网络训练

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 ("完成")
#     print("**************************************************")
#     '''----------------------------------------'''
#     # 结果1可视化
#
#     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()


'''----------------------------------------'''
#第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)

            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 训练")
    '''----------------------------------------'''
    # 结果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()

#'''--------------------------------------------------------------------------'''

结果2:

  MNIST ready
    
 l3 级联 

开始训练
Epoch 00/50 average cost: 0.133966
Epoch 10/50 average cost: 0.054745
Epoch 20/50 average cost: 0.050164
Epoch 30/50 average cost: 0.049177
Epoch 40/50 average cost: 0.048318
[[ 1.2153654  -0.8267792   0.21997271 ... -0.27086955  0.8055329
   0.26718062]
 [-0.630587    1.2070179  -0.5544209  ... -0.10880499 -0.45082295
  -0.20330633]
 [-0.00319472 -0.05782367 -0.8804949  ...  0.21035148  0.5838483
  -0.2197053 ]
 ...
 [-0.8742391   0.03139252  0.59312135 ... -0.5207626   0.01928413
  -0.93899083]
 [-0.6045013  -0.5026074  -0.30895695 ...  0.9569476  -0.27804196
  -0.34385848]
 [ 0.15701267 -0.99690706  1.484573   ... -1.065224   -0.21876362
  -0.29044783]]
Variable:0
完成  layer_2 训练

在这里插入图片描述在这里插入图片描述在这里插入图片描述
程序3:


#1 引入头文件,创建网络模型及定义学习参数变量
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()
'''----------------------------------------'''
#2 定义占位符

# 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])
'''----------------------------------------'''
#3 定义学习参数

# 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]))
}
'''----------------------------------------'''
#4 第1层网络结构

# 第一层的编码输出
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)
'''----------------------------------------'''
#5 第2层网络结构

# 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)
'''----------------------------------------'''
#6 第3层网络结构

# 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)
'''----------------------------------------'''
#7 定义级联网络结构

# 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 级联 ")
'''----------------------------------------'''
#8 第1层网络训练

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 ("完成")
#     print("**************************************************")
#     '''----------------------------------------'''
#     # 结果1可视化
#
#     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()


'''----------------------------------------'''
#第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)
#
#             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 训练")
#     '''----------------------------------------'''
#     # 结果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()

#'''--------------------------------------------------------------------------'''
#第3层网络训练

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 训练")
    '''----------------------------------------------------------------'''
    #11 栈式自编码网络yanzheng

    # 测试 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}))

结果3:

MNIST ready

l3 级联 

开始训练
Epoch 00/50 average cost: 2.024301
Epoch 10/50 average cost: 0.710576
Epoch 20/50 average cost: 0.703978
Epoch 30/50 average cost: 0.703496
Epoch 40/50 average cost: 0.702418
完成  layer_3 训练
Accuracy: 0.7886

程序4:


#1 引入头文件,创建网络模型及定义学习参数变量
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()
'''----------------------------------------'''
#2 定义占位符

# 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])
'''----------------------------------------'''
#3 定义学习参数

# 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]))
}
'''----------------------------------------'''
#4 第1层网络结构

# 第一层的编码输出
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)
'''----------------------------------------'''
#5 第2层网络结构

# 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)
'''----------------------------------------'''
#6 第3层网络结构

# 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)
'''----------------------------------------'''
#7 定义级联网络结构

# 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 级联 ")
'''----------------------------------------'''
#8 第1层网络训练

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 ("完成")
#     print("**************************************************")
#     '''----------------------------------------'''
#     # 结果1可视化
#
#     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()


'''----------------------------------------'''
#第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)
#
#             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 训练")
#     '''----------------------------------------'''
#     # 结果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()

#'''--------------------------------------------------------------------------'''
#第3层网络训练

# 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 训练")
#     '''----------------------------------------------------------------'''
#     #11 栈式自编码网络yanzheng
#
#     # 测试 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}))
'''---------------------------------------------------------'''
#12 级联微调

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

结果4:

MNIST ready

l3 级联 

开始训练
Epoch 00/50 average cost: 1.931872
Epoch 10/50 average cost: 0.073096
Epoch 20/50 average cost: 0.010284
Epoch 30/50 average cost: 0.001219
Epoch 40/50 average cost: 0.000134
完成  级联 训练
Accuracy: 0.958

10-7 分布自编码综合
对自编码模型进行分布式模型存储与载入,使每一层都可以单个环节逐一训练。

程序:

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值