# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3
#     language: python
#     name: python3
# ---

import tensorflow as tf
import numpy as np
sess = tf.Session()

# #                                           Contants,Sequences,Variables

'''Ordinary multiplication'''
a = tf.constant([1,2],name = 'a')
b = tf.constant([[2,3],
                 [3,4]],name = 'b')
mul = tf.multiply(a,b,name = 'mul_a_b')
print(sess.run(mul))

'''Array of Zeros'''
zeros = tf.zeros([2,3],name = 'zeros')
print(sess.run(zeros))

'''Array like zeros'''
zeros_arr = tf.zeros_like([[[2,3],[2,5]],[[2,3],[2,5]]])
print(sess.run(zeros_arr))

'''Array of Ones'''
ones = tf.ones([2,3],name = 'zeros')
print(sess.run(ones))

'''Array like zeros'''
ones_arr = tf.ones_like([[[2,3],[2,5]],[[2,3],[2,5]]])
print(sess.run(ones_arr))

'''Array fill with values'''
print(sess.run(tf.fill([2,3],8,name = 'fill')))

'''lin_space same as numpy.lin_space'''
print(sess.run(tf.lin_space(1.0,10.0,10)))

'''Range series of numbers (tf.range is not iteratable)'''
print(sess.run(tf.range(10)))

'''Random generated constants'''
print('Random Normal :\n\n',sess.run( tf.random_normal([2,3])),'\n')
print('Truncated Normal :\n\n',sess.run( tf.truncated_normal([2,3]) ),'\n')
print('Random Uniform :\n\n',sess.run( tf.random_uniform([2,3]) ),'\n')
'''## Random crop is a image function ##'''
# print('Random Crop :\n\n',sess.run( tf.random_crop(image,[256,256,3]) ),'\n')
print('Multinomial :\n\n',sess.run( tf.multinomial(tf.log([[10., 10.]]), 5) ),'\n')
print('Random gamma :\n\n',sess.run( tf.random_gamma([10],[.5,1.5]) ),'\n')

'''To set a graph-level seed'''
# tf.set_random_seed()

# ## Arithametic operations

'''Absolute value (|x|)'''
print(sess.run(tf.abs(-25)))

'''Negative (-)'''
print(sess.run(tf.negative(25)))

'''Sign(+ -)'''
print(sess.run(tf.sign(-25)),'\n',sess.run(tf.sign(25)))

'''Reciprocal (1/x)'''
x = tf.constant(5,dtype= tf.float32)
print(sess.run(tf.reciprocal(x)))

'''Square (x^2)'''
print(sess.run(tf.square(25)))

'''Reciprocal of square root (y = 1/sqrt(x))'''
x = tf.constant(5,dtype= tf.float32)
print(sess.run(tf.rsqrt(x)))

'''Power (y^x)'''
print(sess.run(tf.pow(5,2)))

'''Exponential (y = e^x)'''
x = tf.constant(10,dtype= tf.float32)
print(sess.run(tf.exp(x)))

# ### Divsion method

a = tf.constant([2,2],name = 'a')
b = tf.constant([[1,2],[2,3]],name = 'b')
print(sess.run(tf.div(b, a)))
print(sess.run(tf.floordiv(b, a)))
# print(sess.run(tf.realdiv(c, d)))# Error: only works for real values
print(sess.run(tf.truncatediv(b, a)))
print(sess.run(tf.floor_div(b, a)))
print('\nTrue division\n')
print(sess.run(tf.truediv(b, a)))
print(sess.run(tf.divide(b, a)))
print(sess.run(tf.tensordot(a, b, 1)))

'''scalar are treated as the 0 -d tensors'''
t_o = 2
print(sess.run(tf.zeros_like(t_o)))
print(sess.run(tf.ones_like(t_o)))

'''Arrays are treated as 1 -d array'''
t_1 = ['a','b','c']
print(sess.run(tf.zeros_like(t_1)))
#print(sess.run(tf.ones_like(t_1))) #TypeError: Expected string, got 1 of type 'int' instead.

t_3 = [[True,True,False],
       [False,False,False],
       [True,False,True]]
print(sess.run(tf.zeros_like(t_3)),'\n')
print(sess.run(tf.ones_like(t_3)),'\n')
print(sess.run(tf.constant(t_3)),'\n')

'''Tensorflow uses the numpy datatypes'''
a = tf.zeros([2, 3], np.int32)
print(type(a))
a_np = sess.run(a)
print(type(a_np))
'''We should specify the datatypes for certain declartions because the tensorflow default takes the python datatypes'''
'''So in certain cases like complex number we should declare explicitly'''

'''Get graph definition from session'''
print(sess.graph.as_graph_def())
'''we can see that the constants as stored in tensor contants so for defining constant there is a after effect of'''
'''This makes loading graphs expensive when constants are big'''

# ### Variables

# +
'''Ordinary Variable initialization'''
a = tf.Variable(2,name= 'scalar' )
b = tf.Variable([2,3],name= 'matrix' )
c = tf.Variable(tf.zeros([800,10]))
sess.run(a.initializer)
sess.run(b.initializer)
sess.run(c.initializer)

print(sess.run(a),'\n')
print(sess.run(b),'\n')
print(sess.run(c),'\n')
'''The tf.Variables contains many ops'''
# -

'''Variable initializer with get variables'''
s = tf.get_variable("scalar", initializer=tf.constant(2)) 
m = tf.get_variable("matrix", initializer=tf.constant([[0, 1], [2, 3]]))
W = tf.get_variable("big_matrix", shape=(784, 10), initializer=tf.zeros_initializer())
sess.run(tf.global_variables_initializer())
#sess.run(tf.variables_initializer([s, m])) #->needed variable initialization
# sess.run(W.initializer) #-> single variable initializer then to get data do W.eval()
print(sess.run(s))
print(sess.run(m))
print(sess.run(W))
'''This variables cannot be reinitialized'''

'''tf.assign'''
W = tf.Variable(10)
assign_op = W.assign(100)
with tf.Session() as sess:
    sess.run(W.initializer)
    sess.run(assign_op)
    print(W.eval())

'''tf.assign'''
W = tf.Variable(10)
assign_op = W.assign(100 * W)
with tf.Session() as sess:
    sess.run(W.initializer)
    print(sess.run(assign_op))
    print(sess.run(assign_op))
    print(sess.run(assign_op))
    print(sess.run(assign_op))
    print(sess.run(assign_op))
#     print(W.eval())

'''assign_add and assign_sub'''
my_var = tf.Variable(10)
with tf.Session() as sess:
    sess.run(my_var.initializer)
    print(sess.run(my_var.assign_add(10))) # >> 20
    print(sess.run(my_var.assign_add(10))) # >> 30
    print(sess.run(my_var.assign_sub(2))) # >> 18

'''Control dependencies'''
a = tf.Variable(2,name= 'scalar' )
b = tf.Variable(2,name= 'scalar' )
sess =  tf.Session() 
sess.run(a.initializer)
sess.run(b.initializer)
g = tf.get_default_graph()
with g.control_dependencies([a,b]):
    d = tf.add(a,b)
    print(sess.run(d))
    '''This will be executed after the a and b is executed'''

# ### Placeholders

'''Placeholder are used so that we can assign unknown values'''
a = tf.placeholder(tf.float32,name='a')
b = tf.Variable([5,5,5],name = 'b',dtype = tf.float32)
c = tf.add(a,b)
sess.run(a.initializer)
with tf.Session() as sess:
    sess.run(b.initializer)
    print(sess.run(c,feed_dict={a: [1,1,1]})) # we provide values for palceholder using the feed dict
    

'''check if the tensor is feedable'''
g = tf.get_default_graph()
g.is_feedable(a)

'''we can override the operation by giving the feed dict value'''
a = tf.add(2,3)
b = tf.multiply(a,5)
with tf.Session() as sess:
    print(sess.run(b))
    print(sess.run(b,feed_dict={a:2}))

# ### Lazy loading  
# `Defer creating/initializing an object 
# until it is needed
# `

# ##### `Normal loading`

x = tf.Variable(10, name='x')
y = tf.Variable(20, name='y')
z = tf.add(x, y)
writer = tf.summary.FileWriter('tmp/graph/normal_loading', tf.get_default_graph())
with tf.Session() as sess:
    sess.run(tf.global_variables_initializer())
    for _ in range(10):
        sess.run(z)
writer.close()

# #### `Lazy loading`

# +
x = tf.Variable(10, name='x')
y = tf.Variable(20, name='y')

writer = tf.summary.FileWriter('tmp/graph/lazy_loading', tf.get_default_graph())
with tf.Session() as sess:
    sess.run(tf.global_variables_initializer())
    for _ in range(10):
        sess.run(tf.add(x, y)) # someone decides to be clever to save one line of code
writer.close()
# -

# Thus both execute correctly thus then the change comes when you run with a million
# iteration loop of session run in lazy loading the graph get heavy and it will have a 
# huge load The actual thing what is happening behind scene is the in lazy loading node 
# for add increases with run so we will get so many nodes if get go look at the 
# ```python
# tf.get_default_graph().as_graph_def() 
# ``` 
# we can see that

# ### MatMultiplication

'''Ordinary MatMul-tiplication'''
mat_a = tf.constant([[1,2],
                    [5,2]],name = 'mat_a')
mat_b = tf.constant([[2,3],
                     [3,4]],name = 'mat_b')
mat_mul = tf.matmul(mat_a,mat_b,name = 'matmul_a_b')
sess = tf.Session()
print(sess.run(mat_mul))

x = tf.constant(5,dtype=tf.float64)
x + tf.cast(tf.equal(x, 0), x.dtype)

StarcoderdataJupyterScriptsDedupFiltered