Google's Tensor-flow basic operations
Tensorflow Basics¶
REF:https://www.tensorflow.org/programmers_guide/low_level_introIn 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
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]:
In [4]:
# Example: dtype = int32 , rank = 0 , shape = ()
number = tf.constant(100)
number
Out[4]:
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()
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]:
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]:
1.4. tf.sparseTensor
In [15]:
sparse = tf.SparseTensor( indices=[[0, 0], [1, 2]], values=[1, 2], dense_shape=[3, 4] )
sparse
Out[15]:
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.
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]:
In [18]:
# Example: Multiplication operation
sess = tf.Session()
sess.run( y*y )
Out[18]:
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))
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]:
In [22]:
b.shape
Out[22]:
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)
Comments
Post a Comment