阅读( 87 )次

   评论(0)条

wudixx

学习笔记-第1章:TensorFlow实现自编码器 / 第5节:单隐层自编码器--计算图设计

1.构建计算图


2.老师的源代码

import tensorflow as tf
import numpy as np
import matplotlib.pyplot as plt
import os
from tensorflow.examples.tutorials.mnist import input_data
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'
# 控制训练过程的参数
learning_rate = 0.01
training_epochs = 20
batch_size = 256
display_step = 1
examples_to_show = 10
# 网络模型参数
n_hidden_units = 256 # 隐藏层神经元数量(让编码器和解码器都有同样规模的隐藏层)
n_input_units = 784 # 输入层神经元数量MNIST data input (img shape: 28*28)
n_output_units = n_input_units #解码器输出层神经元数量必须等于输入数据的units数量

def WeightsVariable(n_in, n_out, name_str):
    return tf.Variable(tf.random_normal([n_in, n_out]), dtype=tf.float32, name=name_str)
#根据输出节点数量返回初始化好的指定名称的偏置 Variable
def BiasesVariable(n_out, name_str):
    return tf.Variable(tf.random_normal([n_out]), dtype=tf.float32, name=name_str)

# 构建编码器
def Encoder(x_origin, activate_func=tf.nn.sigmoid):
    # 编码器第一隐藏层(www.studyai.com)
    with tf.name_scope('Layer'):
        weights = WeightsVariable(n_input_units, n_hidden_units, 'weights')
        biases = BiasesVariable(n_hidden_units, 'biases')
        x_code = activate_func(tf.add(tf.matmul(x_origin, weights), biases))
        variable_summaries(weights)
        variable_summaries(biases)
    return x_code

# 构建解码器
def Decoder(x_code, activate_func=tf.nn.sigmoid):
    # 解码器第一隐藏层(www.studyai.com)
    with tf.name_scope('Layer'):
        weights = WeightsVariable(n_hidden_units, n_output_units, 'weights')
        biases = BiasesVariable(n_output_units, 'biases')
        x_decode = activate_func(tf.add(tf.matmul(x_code, weights), biases))
        variable_summaries(weights)
        variable_summaries(biases)
    return x_decode

#调用上面写的函数构造计算图
with tf.Graph().as_default():
    # 计算图输入
    with tf.name_scope('X_Origin'):
        X_Origin = tf.placeholder(tf.float32, [None, n_input_units])
    # 构建编码器模型
    with tf.name_scope('Encoder'):
        X_code = Encoder(X_Origin, activate_func=tf.nn.sigmoid)
    # 构建解码器模型(www.studyai.com)
    with tf.name_scope('Decoder'):
        X_decode = Decoder(X_code, activate_func=tf.nn.sigmoid)
    print('把计算图写入事件文件,在TensorBoard里面查看')
    summary_writer = tf.summary.FileWriter(logdir='logs', graph=tf.get_default_graph())
    summary_writer.flush()

3.添加损失层

# 定义损失节点:重构数据与原始数据的误差平方和损失

with tf.name_scope('Loss'): Loss = tf.reduce_mean(tf.pow(X_Origin - X_decode, 2))

4.定义优化器

 # 定义优化器,训练节点
with tf.name_scope('Train'): Optimizer = tf.train.RMSPropOptimizer(learning_rate) Train = Optimizer.minimize(Loss) Init = tf.global_variables_initializer()


 收藏 (0)  打赏  点赞 (0)

 ©2017 studyai.com 版权所有

关于我们