Google's Tensor-flow basic operations

tensorflow basic operations

Tensorflow Basics

REF:https://www.tensorflow.org/programmers_guide/low_level_intro
In order to run tensorfluw on jupyter, install it using pip install tensorflow
In simple terms tensorflow means the flow of tensors through various mathematical nodes.
A sequence of interconnected nodes is a tensor graph.
A tensor is the simplest unit of data in a tensorflow graph.
In [1]:
# To start-off, import tensorflow 
import tensorflow as tf
In [2]:
# Additional libraries:
import numpy as np

1. Types of tensors

Each tensor has three basic properties:
dtype (e.g. int32, float64, string)
Rank - The dimension of a tensor e.g.1D for a vector tensor. rank = 0 >> constant
Shape - The length of each dimension e.g. (2,3) for a rank = 2 or 2D tensor
1.1. tf.constant
In [3]:
# Type 1: Constant
# Example: dtype = string , rank = 0 , shape = ()
name = tf.constant('Ndamu')
name
Out[3]:
<tf.Tensor 'Const:0' shape=() dtype=string>
In [4]:
# Example: dtype = int32 , rank = 0 , shape = ()
number = tf.constant(100)
number
Out[4]:
<tf.Tensor 'Const_1:0' shape=() dtype=int32>
1.2. tf.Variable
Represents shared, persistent state manipulated by your program.
After construction, the type and shape of the variable are fixed. The value can be changed using one of the assign methods.
This differes with tf.constant because its value can be change in future using tf.assign()
In [13]:
my_variable = tf.Variable(tf.random_normal((1,1)))
my_variable
Out[13]:
<tf.Variable 'Variable:0' shape=(1, 1) dtype=float32_ref>
1.3. tf.placeholder
Reserves a space in memory for a value that is unknown at the time of declaration
In [14]:
x = tf.placeholder(tf.float32, shape=(10, 10))
x
Out[14]:
<tf.Tensor 'Placeholder:0' shape=(10, 10) dtype=float32>
1.4. tf.sparseTensor
In [15]:
sparse = tf.SparseTensor( indices=[[0, 0], [1, 2]], values=[1, 2], dense_shape=[3, 4] )
sparse
Out[15]:
<tensorflow.python.framework.sparse_tensor.SparseTensor at 0x7fd80840e1d0>
In the example above:
We create a tensor of zeros with shape = (3,4)
We then add the values = [1,2] in indices (0,0) and (1,2) respectively.

2. Basic operations

Operations e.g. y+y are executed in a tensorflow session using the run() method
In [17]:
# Example: Addition operation
y = tf.constant(100)

sess = tf.Session()
sess.run( y+y )
Out[17]:
200
In [18]:
# Example: Multiplication operation

sess = tf.Session()
sess.run( y*y )
Out[18]:
10000
Multiple opperations can be combined in a session using the with keyword
In [19]:
with tf.Session() as sess:
    print('Operations with Constants: ')
    print('Addition',sess.run(y+y))
    print('Subtraction',sess.run(y-y))
    print('Multiplication',sess.run(y*y))
    print('Division',sess.run(y/y))
Operations with Constants: 
Addition 200
Subtraction 0
Multiplication 10000
Division 1.0

3. Matrix Operations

In [20]:
a = np.array([[5.0,5.0]])
b = np.array([[2.0],[2.0]])
In [21]:
a.shape
Out[21]:
(1, 2)
In [22]:
b.shape
Out[22]:
(2, 1)
In [25]:
# Example: Multiply matrices
matrix_mult = tf.matmul( tf.constant(a), tf.constant(b) )
In [26]:
with tf.Session() as sess:
    result = sess.run(matrix_mult)
    print(result)
[[20.]]

Comments

Popular posts from this blog

Tensorflow application on the mnist data set