阅读( 31 )次

   评论(0)条

wudixx

学习笔记之第1章:TensorFlow实现自编码器 / 第7节:双隐层自编码器--计算图设计并运行

1.两级编码器

# 构建编码器
def Encoder(x_origin, activate_func=tf.nn.sigmoid):
    # 编码器第一隐藏层
    with tf.name_scope('Layer1'):
        weights = WeightsVariable(n_input_units, n_hidden1_units, 'weights')
        biases = BiasesVariable(n_hidden1_units, 'biases')
        x_code1 = activate_func(tf.add(tf.matmul(x_origin, weights), biases))
    # 编码器第二隐藏层
    with tf.name_scope('Layer2'):
        weights = WeightsVariable(n_hidden1_units, n_hidden2_units, 'weights')
        biases = BiasesVariable(n_hidden2_units, 'biases')
        x_code2 = activate_func(tf.add(tf.matmul(x_code1, weights), biases))
    return x_code2

2.两级解码器

# 构建解码器
def Decoder(x_code, activate_func=tf.nn.sigmoid):
    # 解码器第一隐藏层
    with tf.name_scope('Layer1'):
        weights = WeightsVariable(n_hidden2_units, n_hidden1_units, 'weights')
        biases = BiasesVariable(n_hidden1_units, 'biases')
        x_decode1 = activate_func(tf.add(tf.matmul(x_code, weights), biases))
    # 解码器第二隐藏层
    with tf.name_scope('Layer2'):
        weights = WeightsVariable(n_hidden1_units, n_output_units, 'weights')
        biases = BiasesVariable(n_output_units, 'biases')
        x_decode2 = activate_func(tf.add(tf.matmul(x_decode1, weights), biases))
    return x_decode2

需要注意编码解码和输入输出维数的变化。

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

 ©2017 studyai.com 版权所有

关于我们