<jupyter_start><jupyter_text>Introduction to TensorFlow  Graph execution (standard)<jupyter_code>import tensorflow as tf
from datetime import datetime
print tf.executing_eagerly()

tf.reset_default_graph()

# log file for tensorboard
now = datetime.utcnow().strftime("%Y%m%d%H%M%S")
logdir = 'tf_logs/run-'+str(now)

# setup the execution graph to calculate an inner product of two tensors
x = tf.Variable([[1.,2.,3.]])
y = tf.Variable([[4.,5.,6.]])
yt = tf.transpose(y)
z = tf.matmul(x,yt)
init = tf.global_variables_initializer()
file_writer = tf.summary.FileWriter(logdir,tf.get_default_graph())   

# execute in the default session
sess = tf.Session()
sess.run(init)
result = sess.run(z)
print result
sess.close()
file_writer.close()
# inspect the graph on localhost:6006
!tensorboard --logdir tf_logs/
# or do the same calculation this way
with tf.Session() as sess:
    init.run()
    result = z.eval() 
print result   
# or this way
import numpy as np

# input data as numpy arrays
x_data = np.array([[1,2,3]])
y_data = np.array([[4,5,6]])

# execution graph with placeholders instead of variables
x = tf.placeholder(tf.float32,shape=(1,3))
y = tf.placeholder(tf.float32,shape=(1,3))
yt = tf.transpose(y)
z = tf.matmul(x,yt)

# execute in default session
with tf.Session() as sess:
    result = z.eval(feed_dict = {x: x_data, y: y_data})
print result
# same as above, but this time we save the model

saver = tf.train.Saver() # saver node

with tf.Session() as sess:
    init.run()
    result = z.eval(feed_dict = {x: x_data, y: y_data})
    save_path = saver.save(sess,'imagery/model.ckpt') 
print result
# restore the session and run it again

with tf.Session() as sess:
    saver.restore(sess,'imagery/model.ckpt')
    result = z.eval(feed_dict={x:x_data, y:y_data} )
print result<jupyter_output>INFO:tensorflow:Restoring parameters from imagery/model.ckpt
[[32.]]<jupyter_text>Automatic differentiation<jupyter_code>tf.reset_default_graph()

# log file for tensorboard
now = datetime.utcnow().strftime("%Y%m%d%H%M%S")
logdir = 'tf_logs/run-'+str(now)

# a 2x2 tensor of ones
x = tf.Variable(tf.ones((2,2)))

# a computation z = sum(x)^2 = 16
y = tf.reduce_sum(x)
z = y*y

# Derivative of z with respect to the original input tensor x
#     dz/dx_ij = 2*sum(x)*d(x11+x12+x21+x22)/dx_ij = 2*4*1 = 8, for i,j = 1,2
# Derivative of z with respect to y = sum(x)
#     dz/sum(x) = 2*sum(x) = 2*4 = 8
result = tf.gradients(z,[x,y])

file_writer = tf.summary.FileWriter(logdir,tf.get_default_graph())

# run the calculation
with tf.Session() as sess:
    x.initializer.run()
    print 'z = %s' %z.eval()
    print 'dz/dx = \n%s' %result[0].eval()
    print 'dz/dy = %s' %result[1].eval()<jupyter_output>z = 16.0
dz/dx = 
[[8. 8.]
 [8. 8.]]
dz/dy = 8.0<jupyter_text>Eager execution (RESTART THE IPYTHON KERNEL!!)<jupyter_code>import tensorflow as tf
import numpy as np
tf.enable_eager_execution()
print tf.executing_eagerly()
x = np.mat([1.,2.,3.])
y = np.mat([4.,5.,6.])

# in numpy
z = x*y.T
print 'numpy result: %f' %z

# in tensorflow
yt = tf.transpose(y)
z = tf.matmul(x,yt)
print 'tensor result: %s' %z

# convert tensor result to numpy array
print 'convert tensor to numpy: %f' %z.numpy()

# where is the variable z?
print 'location of z: %s' % z.device<jupyter_output>numpy result: 32.000000
tensor result: tf.Tensor([[32.]], shape=(1, 1), dtype=float64)
convert tensor to numpy: 32.000000
location of z: /job:localhost/replica:0/task:0/device:CPU:0<jupyter_text>Automatic differentiation with eager execution<jupyter_code># a 2x2 tensor, now we can print it without a session
x = tf.ones((2, 2))
print 'as tensor x = %s' % x
print 'as numpy array x =\n %s' % x.numpy()
with tf.GradientTape(persistent=True) as t:
  t.watch(x)
# a computation z = sum(x)^2 
  y = tf.reduce_sum(x)   # sum(x)
  z = tf.multiply(y, y)  # sum(x)^2
print 'z = sum(x)^2 = %s' % z.numpy()
# Derivative of z with respect to the original input tensor x
#     dz/dx_ij = 2*sum(x)*d(x11+x12+x21+x22)/dx_ij = 2*4*1 = 8, for i,j = 1,2
dz_dx = t.gradient(z, x)
print 'dz/dx =\n %s' % dz_dx.numpy()
# Derivative of z with respect to sum(x)
#     dz/sum(x) = 2*sum(x) = 2*4 = 8
dz_dy = t.gradient(z, y)
print 'dz/d(sum(x)) = %s' % dz_dy.numpy()
# the tape object is still im memory
print t
# so delete it
del t
# confirm
try: 
    print t
except:
    print 'gone'<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.15606407322654461, 'fraction_numerical': 0.02745995423340961, 'mean_word_length': 4.317518248175182, 'pattern_counts': {'":': 0, '<': 14, '<?xml version=': 0, '>': 14, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '4', 'dir': 'jupyter-structured-clean-dedup', 'id': '427684', 'license': 'MIT', 'n_tokens_mistral': 1803, 'n_tokens_neox': 1728, 'n_words': 468, 'path': 'src/tfintro.ipynb', 'repo_name': 'xuanxiaoliqu/CRC4Docker', 'size': '4370'}