Created
July 9, 2019 09:20
-
-
Save e-lin/879a086440af52b96f50d7e7739a48a4 to your computer and use it in GitHub Desktop.
AutoEncoder: Tying weights
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
X = tf.placeholder(tf.float32, shape=[None, n_inputs]) | |
weights1_init = initializer([n_inputs, n_hidden1]) | |
weights2_init = initializer([n_hidden1, n_hidden2]) | |
weights1 = tf.Variable(weights1_init, dtype=tf.float32, name="weights1") | |
weights2 = tf.Variable(weights2_init, dtype=tf.float32, name="weights2") | |
weights3 = tf.transpose(weights2, name="weights3") # tied weights | |
weights4 = tf.transpose(weights1, name="weights4") # tied weights | |
biases1 = tf.Variable(tf.zeros(n_hidden1), name="biases1") | |
biases2 = tf.Variable(tf.zeros(n_hidden2), name="biases2") | |
biases3 = tf.Variable(tf.zeros(n_hidden3), name="biases3") | |
biases4 = tf.Variable(tf.zeros(n_outputs), name="biases4") | |
hidden1 = activation(tf.matmul(X, weights1) + biases1) | |
hidden2 = activation(tf.matmul(hidden1, weights2) + biases2) | |
hidden3 = activation(tf.matmul(hidden2, weights3) + biases3) | |
outputs = tf.matmul(hidden3, weights4) + biases4 | |
reconstruction_loss = tf.reduce_mean(tf.square(outputs - X)) | |
reg_loss = regularizer(weights1) + regularizer(weights2) | |
loss = reconstruction_loss + reg_loss | |
optimizer = tf.train.AdamOptimizer(learning_rate) | |
training_op = optimizer.minimize(loss) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment