Skip to content

Instantly share code, notes, and snippets.

@phreeza
Created February 27, 2017 15:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save phreeza/f8210e50ef54598f433732e4f69a42aa to your computer and use it in GitHub Desktop.
Save phreeza/f8210e50ef54598f433732e4f69a42aa to your computer and use it in GitHub Desktop.
import tensorflow as tf
node1 = tf.constant(3.0, tf.float32)
node2 = tf.constant(4.0) # also tf.float32 implicitly
with tf.Session() as sess:
W = tf.Variable([.3], tf.float32, name='W')
b = tf.Variable([-.3], tf.float32, name='b')
x = tf.placeholder(tf.float32, name='x')
with tf.name_scope("LinearModel"):
y = W * x + b
f = tf.sin(y,name='f')
init = tf.global_variables_initializer()
sess.run(init)
print sess.run(y, {x:[1,2,3,4]})
with tf.name_scope("Updates"):
fixW = tf.assign(W, [-1.])
fixb = tf.assign(b, [1.])
sess.run([fixW, fixb])
print "After transform"
print sess.run(y, {x:[1,2,3,4]})
grad_f = tf.gradients(f,x,name='dfdx')
grad_y = tf.gradients(y,x,name='dydx')
print "Gradients"
ret = sess.run([grad_f,grad_y], {x:[1,2,3,4]})
print "df/dx: ",ret[0][0]
print "dy/dx: ",ret[1][0]
summary_writer = tf.summary.FileWriter('logs',
sess.graph)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment