-
大小: 4KB文件類型: .py金幣: 1下載: 0 次發布日期: 2021-05-12
- 語言: Python
- 標簽: autoencoder??自編碼器??tensorflow??機器學習??深度學習??
資源簡介
tensorflow實現的自編碼器,帶有詳細注釋,使用MNIST作為數據集,安裝好python及tensorflow即可運行
代碼片段和文件信息
#?-*-?coding:?utf-8?-*-
from?__future__?import?division?print_function?absolute_import
import?tensorflow?as?tf
import?numpy?as?np
import?matplotlib.pyplot?as?plt
#?Import?MNIST?data
from?tensorflow.examples.tutorials.mnist?import?input_data
mnist?=?input_data.read_data_sets(“MNIST_data“?one_hot=True)
#?Parameters
learning_rate?=?0.01
training_epochs?=?20
batch_size?=?256
display_step?=?1
examples_to_show?=?10
#?Network?Parameters
n_hidden_1?=?256?#?1st?layer?num?features
n_hidden_2?=?128?#?2nd?layer?num?features
n_input?=?784?#?MNIST?data?input?(img?shape:?28*28)
#?tf?Graph?input?(only?pictures)
X?=?tf.placeholder(“float“?[None?n_input])
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.random_normal([n_hidden_1]))
????‘encoder_b2‘:?tf.Variable(tf.random_normal([n_hidden_2]))
????‘decoder_b1‘:?tf.Variable(tf.random_normal([n_hidden_1]))
????‘decoder_b2‘:?tf.Variable(tf.random_normal([n_input]))
}
#?Building?the?encoder
def?encoder(x):
????#?Encoder?Hidden?layer?with?sigmoid?activation?#1
????layer_1?=?tf.nn.sigmoid(tf.add(tf.matmul(x?weights[‘encoder_h1‘])
???????????????????????????????????biases[‘encoder_b1‘]))
????#?Decoder?Hidden?layer?with?sigmoid?activation?#2
????layer_2?=?tf.nn.sigmoid(tf.add(tf.matmul(layer_1?weights[‘encoder_h2‘])
???????????????????????????????????biases[‘encoder_b2‘]))
????return?layer_2
#?Building?the?decoder
def?decoder(x):
????#?Encoder?Hidden?layer?with?sigmoid?activation?#1
????layer_1?=?tf.nn.sigmoid(tf.add(tf.matmul(x?weights[‘decoder_h1‘])
?
- 上一篇:CNN卷積神經網絡TensorFlow代碼
- 下一篇:神經網絡模型python模板
評論
共有 條評論