This notebook is an introduction to tensorflow, a popular deep-learning framework. Tensorflow has brilliant integration with Python. This notebook uses the tensorflow python3 APIs. This notebook is a modified fork of tensorflow examples repository.
This is a simple hello world example using tensorflow.
In this example, a tensorflow constant op is created. This op is added as a node to the default graph. The value returned by the constructor represents the output of this constant op.
In [0]:
import tensorflow as tf
In [0]:
hello = tf.constant('Hello, TensorFlow!')
In [0]:
sess = tf.Session()
In [4]:
print(sess.run(hello))
In [0]:
a = tf.constant(2)
b = tf.constant(3)
In [6]:
print (a)
In [7]:
with tf.Session() as sess:
print ("a: %i" % sess.run(a), "b: %i" % sess.run(b))
print ("Addition with constants using tensorflow: %i" % sess.run(a+b))
print ("Multiplication with constants using tensorflow: %i" % sess.run(a*b))
In [0]:
a = tf.placeholder(tf.int16)
b = tf.placeholder(tf.int16)
In [0]:
add = tf.add(a, b)
mul = tf.multiply(a, b)
In [10]:
with tf.Session() as sess:
input_dict = {a: 2, b: 3}
print ("a: %i" % sess.run(a, feed_dict=input_dict), "b: %i" % sess.run(b, feed_dict=input_dict))
print ("Addition with variables using tensorflow: %i" % sess.run(add, feed_dict=input_dict))
print ("Multiplication with variables using tensorflow: %i" % sess.run(mul, feed_dict=input_dict))
This portion of the tutorial uses the matrix multiplication example from TensorFlow official tutorial. Create a Constant op that produces a 1x2 matrix. The op is added as a node to the default graph. The value returned by the constructor represents the output of the Constant op.
In [0]:
matrix1 = tf.constant([[3., 3.]])
In [0]:
matrix2 = tf.constant([[2.],[2.]])
Create a Matmul op that takes 'matrix1' and 'matrix2' as inputs. The returned value, 'product', represents the result of the matrix multiplication.
In [0]:
mat_mul = tf.matmul(matrix1, matrix2)
Running the matmul op we call the session 'run()' method, passing 'product' which represents the output of the matmul op. This indicates to the call that we want to get the output of the matmul op back. All inputs needed by the op are run automatically by the session. They typically are run in parallel. The call 'run(product)' thus causes the execution of threes ops in the graph: the two constants and matmul. The output of the op is returned in 'result' as a numpy ndarray
object.
In [14]:
with tf.Session() as sess:
result = sess.run(mat_mul)
print (result)
Eager execution is an imperative, define-by-run interface where operations are executed immediately as they are called from Python. This makes it easier to get started with TensorFlow, and can make research and development more intuitive. A vast majority of the TensorFlow API remains the same whether eager execution is enabled or not. As a result, the exact same code that constructs TensorFlow graphs (e.g. using the layers API) can be executed imperatively by using eager execution. Conversely, most models written with Eager enabled can be converted to a graph that can be further optimized and/or extracted for deployment in production without changing code. - Rajat Monga
In [0]:
from __future__ import absolute_import, division, print_function
import numpy as np
import tensorflow as tf
import tensorflow.contrib.eager as tfe
In [2]:
print("Enabling Eager mode ...")
tfe.enable_eager_execution()
In [3]:
print("Initialize constant tensors ...")
a = tf.constant(2)
print("a = %i" % a)
b = tf.constant(3)
print("b = %i" % b)
In [4]:
print("Running operations, without tf.Session")
c = a + b
print("a + b = %i" % c)
d = a * b
print("a * b = %i" % d)
In [5]:
print("Mixed operations with tensors and numpy arrays ...")
a = tf.constant([[2., 1.],
[1., 0.]], dtype=tf.float32)
print("Tensor:\n a = %s" % a)
b = np.array([[3., 0.],
[5., 1.]], dtype=np.float32)
print("NumpyArray:\n b = %s" % b)
In [6]:
print("Running matrix operations without tf.Session ...")
c = a + b
print("a + b = %s" % c)
d = tf.matmul(a, b)
print("a * b = %s" % d)
In [7]:
print("Iterating through Tensor 'a': ...")
for i in range(a.shape[0]):
for j in range(a.shape[1]):
print(a[i][j])
In [0]:
import tensorflow as tf
class tf_basic_ops:
def __init__(self, a = None, b = None):
self.a = a
self.b = b
a = tf.placeholder(tf.int16)
b = tf.placeholder(tf.int16)
self.input_dict = {a: self.a, b: self.b}
def tf_add(self):
add = tf.add(self.a, self.b)
with tf.Session() as sess:
return sess.run(add, feed_dict=self.input_dict)
def tf_mul(self):
mul = tf.multiply(self.a , self.b)
with tf.Session() as sess:
return sess.run(mul, feed_dict=self.input_dict)
def tf_mat_mul(self):
mat_mul = tf.matmul(self.a , self.b)
with tf.Session() as sess:
return sess.run(mat_mul, feed_dict=self.input_dict)
In [0]:
tf_ops = tf_basic_ops(a=2, b = 3)
In [19]:
tf_ops.tf_add()
Out[19]:
In [20]:
tf_ops.tf_mul()
Out[20]:
In [0]:
tf_matrix_ops = tf_basic_ops(a=[[3., 3.]], b = [[2.],[2.]])
In [22]:
tf_matrix_ops.tf_mat_mul()
Out[22]:
In [0]: