首先你肯定知道了什么是自编码,一般我们常见的自编码是使用的多层感知机来实现的,也就是多层的全连接神经网络结构。本小记中我们使用CNN实现一个七层的卷积神经网络构成的自编码器。
自编码器使用很广泛,我觉得它的思想就是同一空间的数据操作的最优结果,数据经过压缩(数据向前传播到达中间的层所得到的结果),然后数据的解压缩过程(数据到达网络的输出),回到原来的空间。这个思想很关键,利用这个思想我们可以对元数据进行一些去噪操作,等等。
训练程序:
#!user/bin/python
# _*_ coding:utf-8 _*_
import tensorflow as tf
import matplotlib.pyplot as pt
import numpy as np
from tensorflow.examples.tutorials.mnist import input_data
mnist = input_data.read_data_sets('../MNIST_data/', one_hot=True)
batch_size = 64
sess = tf.InteractiveSession()
def weight_varibales(shape):
initial = tf.truncated_normal(shape=shape, stddev=0.1)
return tf.Variable(initial)
def bias_variables(shape):
return tf.Variable(tf.constant(0.1, shape=shape))
def conv2d(x, w):
return tf.nn.conv2d(x, w, strides=[1, 2, 2, 1], padding='SAME')
# 四层结构的反卷积操作
inputdata = tf.placeholder(tf.float32, shape=[None, 28 * 28])
real_label = tf.placeholder(tf.float32, shape=[None, 28 * 28]) # 正确的标签
input_image = tf.reshape(inputdata, [-1, 28, 28, 1])
# layer1
w_conv1 = weight_varibales([