In [1]:
import tensorflow as tf
In [2]:
hello = tf.constant('Hello World')
In [3]:
type(hello)
Out[3]:
In [4]:
x = tf.constant(100)
In [5]:
type(x)
Out[5]:
In [10]:
sess = tf.Session()
In [15]:
sess.run(hello)
Out[15]:
In [16]:
type(sess.run(hello))
Out[16]:
In [17]:
sess.run(x)
Out[17]:
In [14]:
type(sess.run(x))
Out[14]:
In [33]:
x = tf.constant(2)
y = tf.constant(3)
In [34]:
with tf.Session() as sess:
print('Operations with Constants')
print('Addition',sess.run(x+y))
print('Subtraction',sess.run(x-y))
print('Multiplication',sess.run(x*y))
print('Division',sess.run(x/y))
You may not always have the constants right away, and you may be waiting for a constant to appear after a cycle of operations. tf.placeholder is a tool for this. It inserts a placeholder for a tensor that will be always fed.
Important: This tensor will produce an error if evaluated. Its value must be fed using the feed_dict
optional argument to Session.run()
,
Tensor.eval()
, or Operation.run()
. For example, for a placeholder of a matrix of floating point numbers:
x = tf.placeholder(tf.float32, shape=(1024, 1024))
Here is an example for integer placeholders:
In [46]:
x = tf.placeholder(tf.int32)
y = tf.placeholder(tf.int32)
In [47]:
x
Out[47]:
In [48]:
type(x)
Out[48]:
In [30]:
add = tf.add(x,y)
sub = tf.sub(x,y)
mul = tf.mul(x,y)
Running operations with variable input:
In [31]:
d = {x:20,y:30}
In [32]:
with tf.Session() as sess:
print('Operations with Constants')
print('Addition',sess.run(add,feed_dict=d))
print('Subtraction',sess.run(sub,feed_dict=d))
print('Multiplication',sess.run(mul,feed_dict=d))
Now let's see an example of a more complex operation, using Matrix Multiplication. First we need to create the matrices:
In [69]:
import numpy as np
# Make sure to use floats here, int64 will cause an error.
a = np.array([[5.0,5.0]])
b = np.array([[2.0],[2.0]])
In [75]:
a
Out[75]:
In [76]:
a.shape
Out[76]:
In [77]:
b
Out[77]:
In [78]:
b.shape
Out[78]:
In [79]:
mat1 = tf.constant(a)
In [80]:
mat2 = tf.constant(b)
The matrix multiplication operation:
In [81]:
matrix_multi = tf.matmul(mat1,mat2)
Now run the session to perform the Operation:
In [82]:
with tf.Session() as sess:
result = sess.run(matrix_multi)
print(result)
That is all for now! Next we will expand these basic concepts to construct out own Multi-Layer Perceptron model!