Created
March 9, 2017 16:13
-
-
Save koaning/c26b2dd5c2bdeaf6d7479a68bd7023bb to your computer and use it in GitHub Desktop.
tensorflow layer example
This file contains 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
import tensorflow as tf | |
import numpy as np | |
import uuid | |
x = tf.placeholder(shape=[None, 3], dtype=tf.float32) | |
nn = tf.layers.dense(x, 3, activation=tf.nn.sigmoid) | |
nn = tf.layers.dense(nn, 5, activation=tf.nn.sigmoid) | |
encoded = tf.layers.dense(nn, 2, activation=tf.nn.sigmoid) | |
nn = tf.layers.dense(encoded, 5, activation=tf.nn.sigmoid) | |
nn = tf.layers.dense(nn, 3, activation=tf.nn.sigmoid) | |
cost = tf.reduce_mean((nn - x)**2) | |
optimizer = tf.train.RMSPropOptimizer(0.01).minimize(cost) | |
init = tf.global_variables_initializer() | |
tf.summary.scalar("cost", cost) | |
merged_summary_op = tf.summary.merge_all() | |
with tf.Session() as sess: | |
sess.run(init) | |
uniq_id = "/tmp/tensorboard-layers-api/" + uuid.uuid1().__str__()[:6] | |
summary_writer = tf.summary.FileWriter(uniq_id, graph=tf.get_default_graph()) | |
x_vals = np.random.normal(0, 1, (10000, 3)) | |
for step in range(10000): | |
_, val, summary = sess.run([optimizer, cost, merged_summary_op], | |
feed_dict={x: x_vals}) | |
if step % 5 == 0: | |
print("step: {}, value: {}".format(step, val)) | |
summary_writer.add_summary(summary, step) |
Brilliant gist, really helped me understand how to train tensorflow models without the use of the Estimator API or Keras.
It was very helpful for me, thanks!
Simpliest tutorial ive seen. Thanks
Elegant, simple & succint code. Thanks
Thanks 👍
Awesome!!!! Really so unique - didn't find anything this good anywhere! Thank you so much and congratulations on such a great post.
Those are kind words ... but you should realise this code is ... quite old by now.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hey there! This gist is great!
Could you please tell me how to use the intializer and the regularizer parameters of the tf.layers.dense functional interface?
I am mostly interested in passing my own functions as the initializer and regularizer