<jupyter_start><jupyter_text>Import Tensorflow Operations  Transpose Operations Create a numpy array as below: pay attention to the perm paramater<jupyter_code>import numpy as np
import tensorflow as tf
array = np.array([[1, 2, 3] ,  [4, 5, 6]])<jupyter_output><empty_output><jupyter_text>Get the shape of the array:<jupyter_code>print("shape of the array is :", array.shape)<jupyter_output>shape of the array is : (2, 3)<jupyter_text>Lets use tf.transpose  operation on the "array" above and explain the output. This we do by parsing the array generated above into the transpose operation and run it in a session.<jupyter_code>transpose_array= tf.transpose(array, perm=[1,0])<jupyter_output><empty_output><jupyter_text>Run in a session<jupyter_code>with tf.Session() as sess:
    init = tf.initialize_all_variables()
    sess.run(init)
    print ("the transposed array is: \n " , transpose_array.eval())
    print ("the resulting shape is : " , result.shape)<jupyter_output>the transposed array is: 
  [[1 4]
 [2 5]
 [3 6]]
the resulting shape is :  (3, 2)<jupyter_text>Explanation:   We have an array of shape (2,3) and we use the tf.transpose specifying the perm=[1,0]. This optional parameter perm which we specified will basically permute the array like below:        * [1,0] permute such that dimension 1 --->(becomes) 1, and dimension 0  --> 1.       * Basically this is exchange of dimension      * So that array of shape  [2,3]  becomes [3,2]  Further Examples<jupyter_code>twothreefour = np.array([ [[1,2,3,4], [5,6,7,8], [9,10,11,12]] , 
                        [[13,14,15,16], [17,18,19,20], [21,22,23,24]] ])
# shape of the array above 
print("Shape is : ", twothreefour.shape)
twofourthree = tf.transpose(twothreefour, perm=[0,2,1]) 
with tf.Session() as sess:
    init = tf.initialize_all_variables()
    sess.run(init)
    print (twofourthree.eval())
    print ("\n")
    print ("shape is: ", twofourthree.get_shape())<jupyter_output>[[[ 1  5  9]
  [ 2  6 10]
  [ 3  7 11]
  [ 4  8 12]]

 [[13 17 21]
  [14 18 22]
  [15 19 23]
  [16 20 24]]]


shape is:  (2, 4, 3)<jupyter_text>Using the same explanation we have that perm [0,2,1]. Show that an array of shape (2,3,4) becaomes (2,4,3) The last one as exercise:<jupyter_code>threetwofour = tf.transpose(twothreefour, perm=[1,0,2]) 
with tf.Session() as sess:
    init = tf.initialize_all_variables()
    sess.run(init)
    print (threetwofour.eval())
    print("\n")
    print("My shape is:", threetwofour.get_shape())<jupyter_output>[[[ 1  2  3  4]
  [13 14 15 16]]

 [[ 5  6  7  8]
  [17 18 19 20]]

 [[ 9 10 11 12]
  [21 22 23 24]]]


My shape is: (3, 2, 4)<jupyter_text>Using perm= [1,0,2]. Explain the output (3,2,4) obtained from the array of shape (2,4,3) after the transpose operation in a session.   Placeholder This creates a placeholder for a tensor. It is used in most mpodels to hold the dimension of variables. 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( ).<jupyter_code>x = tf.placeholder(tf.float32, shape=(1024, 1024))
y = tf.matmul(x, x)

with tf.Session() as sess:
    #print(sess.run(y))  # ERROR: will fail because x was not fed.
    rand_array = np.random.rand(1024, 1024)
    
    result = sess.run(y, feed_dict={x: rand_array})  # Will succeed.
    print ("The result has shape ", result.shape)<jupyter_output>The result has shape  (1024, 1024)<jupyter_text>Note :    * that in most models we feed in the training data     * We montly feed in the data in batches by specifying the batch_size.    * The data is scheduled at each time and a random sample of batch size is collected and fed to model    * This is done for computational efficiency     * It allows compuatation of bactch stochastic gradient.   Reshape ,Gather, Dynamic_partition and Split  Gather Operation (  tf.gather( ) )  Generate an array and test the gether operation. Note this approach for fast prototyping:     * We generate an array in Numpy and test the operations of tensor flow on it. Use: Gather slices from params according to indices.indices must be an integer tensor of any dimension (usually 0-D or 1-D). This is best illustrated by an example:<jupyter_code>array = np.array([[1,2,3],[4,9,6],[2,3,4],[7,8,0]])
array.shape
gather_output0  = tf.gather(array,1)
gather_output01  = tf.gather(array,2)
gather_output02  = tf.gather(array,3)

gather_output11  = tf.gather(array,[1,2])
gather_output12  = tf.gather(array,[1,3])
gather_output13  = tf.gather(array,[3,2])




gather_output  = tf.gather(array,[1,0,2])
gather_output1  = tf.gather(array,[1,1,2])
gather_output2  = tf.gather(array,[1,2,1])


with tf.Session() as sess:
    print (gather_output0.eval());print("\n")
    print (gather_output01.eval());print("\n")
    print (gather_output02.eval());print("\n")  
    print (gather_output11.eval());print("\n")
    print (gather_output12.eval());print("\n")
    print (gather_output13.eval());print("\n")
    
    print (gather_output.eval());print("\n")
    print (gather_output1.eval());print("\n")
    print (gather_output2.eval());print("\n")
    #print (gather_output2.eval());print("\n")<jupyter_output>[4 9 6]


[2 3 4]


[7 8 0]


[[4 9 6]
 [2 3 4]]


[[4 9 6]
 [7 8 0]]


[[7 8 0]
 [2 3 4]]


[[4 9 6]
 [1 2 3]
 [2 3 4]]


[[4 9 6]
 [4 9 6]
 [2 3 4]]


[[4 9 6]
 [2 3 4]
 [4 9 6]]<jupyter_text>And looking at this simple example:    * Initialise  simple array    * test gather operation<jupyter_code>array_simple = np.array([1,2,3])
print "shape of simple array is: ", array_simple.shape
gather1  = tf.gather(array1,[0])
gather01 = tf.gather(array1,[1])
gather02 = tf.gather(array1,[2])

gather2 = tf.gather(array1,[1,2])
gather3 = tf.gather(array1,[0,1])

with tf.Session() as sess:
    print (gather1.eval());print("\n")
    print (gather01.eval());print("\n")
    print (gather02.eval());print("\n")
    print (gather2.eval());print("\n")
    print (gather3.eval());print("\n")<jupyter_output>[1]


[2]


[3]


[2 3]


[1 2]<jupyter_text>tf.reshape( ) Note:      *  Use the same array that was initiated    *  Do reshape using tf.reshape( )<jupyter_code>array.shape # Confirm array shape
print ("This is the array\n" ,array) # see the output and compare with the initial array,
reshape_ops= tf.reshape(array,[-1,4]) # Note the parameters in reshpe
reshape_ops1= tf.reshape(array,[-1,3]) # Note the parameters in reshpe
reshape_ops2= tf.reshape(array,[-1,6]) # Note the parameters in reshpe

reshape_ops_back1= tf.reshape(array,[6,-1]) # Note the parameters in reshpe
reshape_ops_back2= tf.reshape(array,[3,-1]) # Note the parameters in reshpe
reshape_ops_back3= tf.reshape(array,[4,-1]) # Note the parameters in reshpe

with tf.Session() as sess:
    print(reshape_ops.eval());print("\n")
    print(reshape_ops1.eval());print("\n")
    print(reshape_ops2.eval());print("\n")
    print ("Output when we reverse the parameters:");print("\n")
    print(reshape_ops_back1.eval());print("\n")
    print(reshape_ops_back2.eval());print("\n")
    print(reshape_ops_back3.eval());print("\n")<jupyter_output>[[1 2 3 4]
 [9 6 2 3]
 [4 7 8 0]]


[[1 2 3]
 [4 9 6]
 [2 3 4]
 [7 8 0]]


[[1 2 3 4 9 6]
 [2 3 4 7 8 0]]


Output when we reverse the parameters:


[[1 2]
 [3 4]
 [9 6]
 [2 3]
 [4 7]
 [8 0]]


[[1 2 3 4]
 [9 6 2 3]
 [4 7 8 0]]


[[1 2 3]
 [4 9 6]
 [2 3 4]
 [7 8 0]]<jupyter_text>Note: The input size and output size must be the same. ---otherwise it gives error. Simple way to check this out is to make sure the input can be paritioned into the the reshape parameters by doing simple multiplications.  dynamic_cell_partitions This is declared as : tf.dynamic_partition (array, partitions, num_partitions, name=None)  Note:    * we decalare number_partitions --- number of partitions    * Use our array initialised earlier    * We declare the partition as [0 1 0 1] . This signifies the partitions we want 0's fall to one partition and 1 the other partitions given that we have two num_partitions=2.    * The output is a list<jupyter_code>print ("This is the array\n" ,array) # This is out array<jupyter_output>This is the array
 [[1 2 3]
 [4 9 6]
 [2 3 4]
 [7 8 0]]<jupyter_text>We show how to make two and three partitions below<jupyter_code>num_partitions = 2
num_partitions1 = 3

partitions = [0, 0, 1, 1]
partitions1 = [0 ,1 ,1, 2 ]
dynamic_ops =tf.dynamic_partition(array, partitions, num_partitions, name=None) # 2 partitions
dynamic_ops1 =tf.dynamic_partition(array, partitions1, num_partitions1, name=None) # 3 partitions

with tf.Session() as sess:
    run = sess.run(dynamic_ops)
    run1 = sess.run(dynamic_ops1)
    print("Output for 2 partitions: ")
    print (run[0]);print("\n")
    print(run[1]) ;print("\n")# Compare result with initial array. Out is list
    print("Output for three partitions: ")
    
    print (run1[0]);print("\n")
    print (run1[1]);print("\n")
    print (run1[2]);print("\n")<jupyter_output>Output for 2 partitions: 
[[1 2 3]
 [4 9 6]]


[[2 3 4]
 [7 8 0]]


Output for three partitions: 
[[1 2 3]]


[[4 9 6]
 [2 3 4]]


[[7 8 0]]<jupyter_text>tf.split( )  * Make sure you use an up to date tensorflow version. Otherwise in older versions, this implemetation will give error  This is specified in the documentation as below: tf.split(value, num_or_size_splits, axis=0, num=None, name='split'). It  splits a tensor into subtensors. This is best illustrated by an example:    * we define (5,30) aray in numpy    * we split the array along axis 1    * We  specify the number of splits as 1-Dimen Tensor along axis 1. So we have 3 splits.     ------Specify an arrayCreate a (5 by 30) numpy array. The syntax using numpy is shown below<jupyter_code>ArrayBeforeSplitting = np.arange(150).reshape(5,30) 
print ("Array shape without split operation is : " ,ArrayBeforeSplitting.shape)<jupyter_output>('Array shape without split operation is : ', (5, 30))<jupyter_text>----- specify number of splits<jupyter_code>split_1D = tf.Variable([8,13,9])
print("specify number of partions using 1-Dimen Variable:" , tf.shape(split_1D))<jupyter_output>('specify number of partions using 1-Dimen Variable:', <tf.Tensor 'Shape:0' shape=(1,) dtype=int32>)<jupyter_text>-------- Use tf.split Make 3 splits aong y axis so that we have  (5,8) ,(5,13),(5,9) splits. The axis 1 add up to give 30-- we can see axis 1 has 30 elements so the partition along that axis should add up to 30 otherwise it gives error.<jupyter_code>split1,split2,split3 = tf.split(ArrayBeforeSplitting,split_1D,1)
# we have 3 splits along axis 1 specified spcifically
# by the split_1D . That is split axis 1D (with 30 elements) into partions with 8 ,13, and 9 elements while the x axis
#remains constant
#INitialise global variables. because split_ID is a variable and needs to be initialised before being
#used in a computational graph
init_op = tf.global_variables_initializer()

with tf.Session() as sess:
    sess.run(init_op) # run variable initialisation.
    result=split1.eval();print("\n")
    print(result)
    print("the shape of the first split operation is : ",result.shape)
    result2=split2.eval();print("\n")
    print(result2)
    print("the shape of the second split operation is : ",result2.shape)
    
    result3=split3.eval();print("\n")
    print(result3)
    print("the shape of the third split operation is : ",result3.shape)<jupyter_output>[[  0   1   2   3   4   5   6   7]
 [ 30  31  32  33  34  35  36  37]
 [ 60  61  62  63  64  65  66  67]
 [ 90  91  92  93  94  95  96  97]
 [120 121 122 123 124 125 126 127]]
('the shape of the first split operation is : ', (5, 8))


[[  8   9  10  11  12  13  14  15  16  17  18  19  20]
 [ 38  39  40  41  42  43  44  45  46  47  48  49  50]
 [ 68  69  70  71  72  73  74  75  76  77  78  79  80]
 [ 98  99 100 101 102 103 104 105 106 107 108 109 110]
 [128 129 130 131 132 133 134 135 136 137 138 139 140]]
('the shape of the second split operation is : ', (5, 13))


[[ 21  22  23  24  25  26  27  28  29]
 [ 51  52  53  54  55  56  57  58  59]
 [ 81  82  83  84  85  86  87  88  89]
 [111 112 113 114 115 116 117 118 119]
 [141 142 143 144 145 146 147 148 149]]
('the shape of the third split operation is : ', (5, 9))
{'fraction_non_alphanumeric': 0.12951115485564305, 'fraction_numerical': 0.07701771653543307, 'mean_word_length': 3.809861932938856, 'pattern_counts': {'":': 0, '<': 49, '<?xml version=': 0, '>': 51, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 8, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '15', 'dir': 'jupyter-structured-clean-dedup', 'id': '112142', 'license': 'MIT', 'n_tokens_mistral': 4907, 'n_tokens_neox': 4277, 'n_words': 1514, 'path': 'TensorFlowTutorials/Getting_Started_With_Tensor_Flow/5_TensorflowTranspose_and_other_important_operations.ipynb', 'repo_name': 'adderbyte/Dynamic-Behaviour-Modelling-With-LSTM-.-AIR-POLLUTION-As-case-study', 'size': '12192'}