本文主要介绍使用TensorFlow实现DenoisingAutoencoder(图像去噪自动编码器)。
下面是示例代码:
# 导入相关模块
import numpy as np
import sys
import tensorflow as tf
import matplotlib.pyplot as plt
'''
IPython有一组预定义的“魔术函数”,您可以使用命令行样式语法调用
它们。有两种魔法,一种是线导向(line-oriented),另一种是单元
导向(cell-oriented)。line magics以%字符作为前缀,其工作方式
与操作系统命令行调用非常相似:它们作用于整行,line magics可以返
回结果,也可以进行赋值使用;cell magics是以%%开头,它需要出现
在单元的第一行,而且是作用于整个单元。
使用此方法时,绘制命令的输出将在前端显示,就像Jupyter笔记本一样
,直接显示在生成命令的代码单元格的下方,生成的绘图也将存储在笔记
本文档中。不过这个方法好像只适用于Jupyter notebook和Jupyter
QtConsole。
'''
%matplotlib inline
# 导入数据集
from tensorflow.examples.tutorials.mnist import input_data
mnist = input_data.read_data_sets("MNIST_data/", one_hot=True)
inputs_ = tf.placeholder(tf.float32, [None, 28, 28, 1])
targets_ = tf.placeholder(tf.float32, [None, 28, 28, 1])
def lrelu(x, alpha=0.1):
return tf.maximum(alpha * x, x)
### Encoder
with tf.name_scope('en-convolutions'):
conv1 = tf.layers.conv2d(inputs_, filters=32,
kernel_size=(3, 3),
strides