Created
April 25, 2019 13:06
-
-
Save nunoplopes/5d98b7f8301ca3b74f6a32bd974fbae5 to your computer and use it in GitHub Desktop.
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
import tensorflow as tf | |
from tensorflow.contrib.compiler import xla | |
import numpy as np | |
a = tf.Variable(1.0, use_resource=True) | |
def repeat(count, body, vars): | |
return tf.while_loop(lambda *args : True, body, vars, maximum_iterations=count) | |
def model(x): | |
def body(x): | |
return a*x | |
return repeat(5, body, [x]) | |
def run(): | |
def build(features): | |
logits = model(features) | |
loss = tf.math.reduce_sum(logits) | |
optimizer = tf.train.MomentumOptimizer(learning_rate=.001, momentum=0.9) | |
grads_and_vars = optimizer.compute_gradients(loss) | |
train_op = optimizer.apply_gradients(grads_and_vars) | |
return loss, train_op | |
with tf.device('cpu'): | |
features = tf.placeholder(tf.float32, shape=[10]) | |
compiled = xla.compile(build, inputs=[features]) | |
with tf.Session() as sess: | |
sess.run(tf.global_variables_initializer()) | |
print(sess.run(compiled, feed_dict = { features: np.random.random(10) })) | |
if __name__ == '__main__': | |
run() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment