Skip to content

Instantly share code, notes, and snippets.

@koaning
Created March 9, 2017 16:13
Show Gist options
  • Save koaning/c26b2dd5c2bdeaf6d7479a68bd7023bb to your computer and use it in GitHub Desktop.
Save koaning/c26b2dd5c2bdeaf6d7479a68bd7023bb to your computer and use it in GitHub Desktop.
tensorflow layer example
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)
@akanimax
Copy link

akanimax commented Oct 6, 2017

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

@vellamike
Copy link

vellamike commented Oct 31, 2017

Brilliant gist, really helped me understand how to train tensorflow models without the use of the Estimator API or Keras.

@oleksandrkim
Copy link

It was very helpful for me, thanks!

@yyc9268
Copy link

yyc9268 commented Mar 20, 2019

Simpliest tutorial ive seen. Thanks

@alfredo-g-zapiola
Copy link

Elegant, simple & succint code. Thanks

@neeleshca
Copy link

Thanks 👍

@izeinoun
Copy link

Awesome!!!! Really so unique - didn't find anything this good anywhere! Thank you so much and congratulations on such a great post.

@koaning
Copy link
Author

koaning commented May 19, 2020

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