Skip to content

Instantly share code, notes, and snippets.

@mtsokol
Created July 3, 2020 07:00
Show Gist options
  • Save mtsokol/d22adf0637e34a32727f312b9bd8df89 to your computer and use it in GitHub Desktop.
Save mtsokol/d22adf0637e34a32727f312b9bd8df89 to your computer and use it in GitHub Desktop.
import tensorflow as tf
from tensorflow.keras.backend import placeholder
print(tf.__version__)
print(tf.executing_eagerly())
x = placeholder(shape=(1, 1), dtype=tf.float32, name='x')
t = placeholder(shape=(1, 1), dtype=tf.float32, name='t')
W0 = tf.Variable(tf.random.normal((1,20)), dtype=tf.float32)
W1 = tf.Variable(tf.random.normal((2,20)), dtype=tf.float32)
W2 = tf.Variable(tf.random.normal((20,1)), dtype=tf.float32)
# Working basic version
with tf.GradientTape() as g:
g.watch(x)
inpt = x
print(inpt)
ur = tf.sigmoid(tf.matmul(inpt, W0))
ur = tf.sigmoid(tf.matmul(ur, W2))
u_t = g.gradient(ur, x)
print(u_t)
# Not working version
with tf.GradientTape() as g:
g.watch(x)
g.watch(t)
inpt = tf.concat([x,t], 1)
print(inpt)
ur = tf.sigmoid(tf.matmul(inpt, W1))
ur = tf.sigmoid(tf.matmul(ur, W2))
u_t = g.gradient(ur, x)
print(u_t)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment