TensorFlow Basics


In [1]:
import tensorflow as tf


c:\programdata\anaconda3\lib\site-packages\h5py\__init__.py:36: FutureWarning: Conversion of the second argument of issubdtype from `float` to `np.floating` is deprecated. In future, it will be treated as `np.float64 == np.dtype(float).type`.
  from ._conv import register_converters as _register_converters

In [2]:
print(tf.__version__)


1.7.0

Tensors


In [3]:
hello = tf.constant('Hello')

In [4]:
type(hello)


Out[4]:
tensorflow.python.framework.ops.Tensor

In [5]:
world = tf.constant('World')

In [6]:
result = hello + world

In [7]:
result


Out[7]:
<tf.Tensor 'add:0' shape=() dtype=string>

In [8]:
type(result)


Out[8]:
tensorflow.python.framework.ops.Tensor

In [9]:
with tf.Session() as sess:
    result = sess.run(hello + world)

In [10]:
result


Out[10]:
b'HelloWorld'

Computations


In [11]:
tensor_1 = tf.constant(1)
tensor_2 = tf.constant(2)

In [12]:
type(tensor_1)


Out[12]:
tensorflow.python.framework.ops.Tensor

In [13]:
tensor_1 + tensor_2


Out[13]:
<tf.Tensor 'add_2:0' shape=() dtype=int32>

In [14]:
sess = tf.Session()

In [15]:
sess.run(tensor_1 + tensor_2)


Out[15]:
3

In [16]:
sess.close()

Operations


In [17]:
# Constant
const = tf.constant(10)

In [18]:
# Create a tensor of a specific dimension
# with a given value
fill_mat = tf.fill(dims = (4, 4), value = 10)

In [19]:
# Generate a tensor of zeros
myzeros = tf.zeros(shape = (4, 4))

In [20]:
# Generate a tensor of ones
myones = tf.ones(shape = (4, 4))

In [21]:
# Generate a tensor of random numbers
# Sampled from the normal distribution
# With given mean and standard deviation
myrandn = tf.random_normal(shape = (4, 4), 
                           mean = 0,
                           stddev = 0.5)

In [22]:
# Generate a tensor of random numbers
# Sampled from the uniform distribution
# With given min and max values
myrandu = tf.random_uniform(shape = (4, 4), 
                            minval = 0,
                            maxval = 1)

In [23]:
my_ops = [const, fill_mat, myzeros, myones, myrandn, myrandu]

Interactive Session

Useful for Notebook Sessions


In [24]:
# Using an Interactive Session 
sess = tf.InteractiveSession()

In [25]:
# For interactive session 
# Some functions allow you to run .eval() on them
for op in my_ops:
    print(op.eval())
    print('\n')


10


[[10 10 10 10]
 [10 10 10 10]
 [10 10 10 10]
 [10 10 10 10]]


[[0. 0. 0. 0.]
 [0. 0. 0. 0.]
 [0. 0. 0. 0.]
 [0. 0. 0. 0.]]


[[1. 1. 1. 1.]
 [1. 1. 1. 1.]
 [1. 1. 1. 1.]
 [1. 1. 1. 1.]]


[[ 0.38301593  0.51866984  0.20885724  0.18511339]
 [ 0.14179526 -0.4602855   0.43054888  0.6652547 ]
 [-0.12187407  0.08375393  0.6937749   0.21612667]
 [-0.07789148  0.35029742 -0.09007152 -0.16522272]]


[[0.7571168  0.62784684 0.73356867 0.9508685 ]
 [0.5720631  0.07241786 0.44787788 0.01825118]
 [0.23339236 0.7124164  0.76858854 0.82055473]
 [0.01049113 0.6794585  0.76604354 0.47718096]]


Matrix Multiplication


In [26]:
a = tf.constant([ [1, 2],
                  [3, 4] ])

In [27]:
# Shape: 2 x 2
a.get_shape()


Out[27]:
TensorShape([Dimension(2), Dimension(2)])

In [28]:
# Shape: 2 x 1
b = tf.constant([[10], [100]])

In [29]:
b.get_shape()


Out[29]:
TensorShape([Dimension(2), Dimension(1)])

In [30]:
result = tf.matmul(a,b)

In [31]:
result.eval()


Out[31]:
array([[210],
       [430]])