lab 008-lab. manipulate tensor # @ # lab-08-tensor_manipulation.ipynb # @ # Tensor Manipulation # https://www.tensorflow.org/api_guides/python/array_ops import tensorflow as tf import numpy as np import pprint tf.set_random_seed(777) pprint_object=pprint.PrettyPrinter(indent=4) interactive_session_object=tf.InteractiveSession() # @ one_d_array=np.array([0.,1.,2.,3.,4.,5.,6.]) pprint_object.pprint(one_d_array) # array([0., 1., 2., 3., 4., 5., 6.]) # This shows rank(dimension) print(one_d_array.ndim) # 1 # This shows shape print(one_d_array.shape) # (7,) print(one_d_array[0],one_d_array[1],one_d_array[-1]) # 0.0 1.0 6.0 print(one_d_array[2:5],one_d_array[4:-1]) # [ 2. 3. 4.] [ 4. 5.] print(one_d_array[:2],one_d_array[3:]) # [ 0. 1.] [ 3. 4. 5. 6.] # @ # 2D Array two_d_array=np.array([[1.,2.,3.],[4.,5.,6.],[7.,8.,9.],[10.,11.,12.]]) pprint_object.pprint(two_d_array) # array([[ 1., 2., 3.], # [ 4., 5., 6.], # [ 7., 8., 9.], # [ 10., 11., 12.]]) # rank print(two_d_array.ndim) # 2 # shape print(two_d_array.shape) # (4,3) # @ # Shape,Rank,Axis constant_node=tf.constant([1,2,3,4]) # rank:1,shape: (4,) constant_node=tf.constant([[1,2], [3,4]]) # rank:2 [[, shape: (2,2) # If rank is 2, shape is (?1,?2) # ?2: number of elements in deepest square bracket # [1,2]=X,[3,4]=Y,so,(2,2) constant_node=tf.constant([[[[1,2,3,4],[5,6,7,8],[9,10,11,12]],[[13,14,15,16],[17,18,19,20],[21,22,23,24]]]]) # rank: [[[[ 4 # [1,2,3,4]: 4 # [1,2,3,4],[5,6,7,8],[9,10,11,12]: 3 # [[1,2,3,4],[5,6,7,8],[9,10,11,12]],[[13,14,15,16],[17,18,19,20],[21,22,23,24]]: 2 # [[[1,2,3,4],[5,6,7,8],[9,10,11,12]],[[13,14,15,16],[17,18,19,20],[21,22,23,24]]]: 1 # shape=(1,2,3,4,) # Let's talk about axis # If rank is 4, axis is also 4 # axis starts with 0 # [1,2,3,4]: axis=3 or axis=-1 # Highest axis is deepest one # axis=2 # [ # [1,2,3,4], # [5,6,7,8], # [9,10,11,12] # ] # axis=1 # [ # [ # [1,2,3,4], # [5,6,7,8], # [9,10,11,12] # ], # [ # [13,14,15,16], # [17,18,19,20], # [21,22,23,24] # ] # ] # axis=0 # [ # [ # [ # [1,2,3,4], # [5,6,7,8], # [9,10,11,12] # ], # [ # [13,14,15,16], # [17,18,19,20], # [21,22,23,24] # ] # ] # ] # [[[[1,2,3,4],[5,6,7,8],[9,10,11,12]], # [[13,14,15,16],[17,18,19,20],[21,22,23,24]]]] # @ # Matmul VS multiply # rank: 2, shape: (1,2) matrix1_node=tf.constant([[3.,3.]]) # rank: 2,shape: (2,1) matrix2_node=tf.constant([[2.],[2.]]) # (1,2)*(2,1)=(1,1) tf.matmul(matrix1_node,matrix2_node).eval() # @ # Broadcasting(you should use it carefully) # rank: 2, shape: (1,2) matrix1_node=tf.constant([[3.,3.]]) # rank: 2, shape: (2,1) matrix2_node=tf.constant([[2.],[2.]]) # We implicitly use broadcasting # [1,2]+[2,1] (matrix1_node+matrix2_node).eval() # array([[ 5., 5.], # [ 5., 5.]],dtype=float32) matrix1_node=tf.constant([[3.,3.]]) matrix2_node=tf.constant([[2.,2.]]) (matrix1_node+matrix2_node).eval() # array([[ 5., 5.]],dtype=float32) # @ # Random values for variable initializations tf.random_normal([3]).eval() # array([ 2.20866942,-0.73225045, 0.33533147],dtype=float32) tf.random_uniform([2]).eval() # array([ 0.08186948, 0.42999184],dtype=float32) tf.random_uniform([2,3]).eval() # array([[ 0.43535876, 0.76933432, 0.65130949], # [ 0.90863407, 0.06278825, 0.85073185]],dtype=float32) # @ # Reduce mean/sum tf.reduce_mean([1,2],axis=0).eval() # 1 x=[[1.,2.], [3.,4.]] tf.reduce_mean(x).eval() # 2.5 # You find mean of column data tf.reduce_mean(x,axis=0).eval() # 1+3/2,2+4/6 # array([ 2., 3.],dtype=float32) tf.reduce_mean(x,axis=1).eval() # array([ 1.5, 3.5],dtype=float32) tf.reduce_mean(x,axis=-1).eval() # array([ 1.5, 3.5],dtype=float32) tf.reduce_sum(x).eval() # 10.0 tf.reduce_sum(x,axis=0).eval() # array([ 4., 6.],dtype=float32) tf.reduce_sum(x,axis=-1).eval() # array([ 3., 7.],dtype=float32) tf.reduce_mean(tf.reduce_sum(x,axis=-1)).eval() # 5.0 # @ # Argmax with axis x=[[0,1,2], [2,1,0]] tf.argmax(x,axis=0).eval() # array([1,0,0]) tf.argmax(x,axis=1).eval() # array([2,0]) tf.argmax(x,axis=-1).eval() # array([2,0]) # @ # Reshape, squeeze, expand_dims # rank: 3,shape=(2,2,3) constant_node=np.array([[[0,1,2], [3,4,5]], [[6,7,8], [9,10,11]]]) # I want to reshape with # rank: 2 # deepest: 3 # you write whatever for remainders like -1 tf.reshape(constant_node,shape=[-1,3]).eval() # array([[ 0, 1, 2], # [ 3, 4, 5], # [ 6, 7, 8], # [ 9,10,11]]) # I want 3 rank, # deepest is 3, # then, 1 group tf.reshape(constant_node,shape=[-1,1,3]).eval() # array([[[ 0, 1, 2]], # [[ 3, 4, 5]], # [[ 6, 7, 8]], # [[ 9,10,11]]]) tf.squeeze([[0],[1],[2]]).eval() # array([0,1,2],dtype=int32) tf.expand_dims([0,1,2],1).eval() # array([[0], # [1], # [2]],dtype=int32) # @ # One hot automatically increases 1 dimension tf.one_hot([[0],[1],[2],[0]],depth=3).eval() # array([[[ 1., 0., 0.]], # [[ 0., 1., 0.]], # [[ 0., 0., 1.]], # [[ 1., 0., 0.]]],dtype=float32) constant_node=tf.one_hot([[0],[1],[2],[0]],depth=3) tf.reshape(constant_node,shape=[-1,3]).eval() # array([[ 1., 0., 0.], # [ 0., 1., 0.], # [ 0., 0., 1.], # [ 1., 0., 0.]],dtype=float32) # @ # casting tf.cast([1.8,2.2,3.3,4.9],tf.int32).eval() # array([1,2,3,4],dtype=int32) tf.cast([True,False,1==1,0==1],tf.int32).eval() # array([1,0,1,0],dtype=int32) # @ # Stack x=[1,4] y=[2,5] z=[3,6] # Pack along first dim. tf.stack([x,y,z]).eval() # array([[1,4], # [2,5], # [3,6]],dtype=int32) tf.stack([x,y,z],axis=1).eval() # array([[1,2,3], # [4,5,6]],dtype=int32) # @ # Ones like and Zeros like x=[[0,1,2], [2,1,0]] tf.ones_like(x).eval() # array([[1,1,1], # [1,1,1]],dtype=int32) tf.zeros_like(x).eval() # array([[0,0,0], # [0,0,0]],dtype=int32) # @ # Zip for x,y in zip([1,2,3],[4,5,6]): print(x,y) # (x,y)=((1,4),(2,5),(3,6)) # 1 4 # 2 5 # 3 6 for x,y,z in zip([1,2,3],[4,5,6],[7,8,9]): print(x,y,z) # 1 4 7 # 2 5 8 # 3 6 9 # @ # Transpose constant_node=np.array([[[0,1,2],[3,4,5]],[[6,7,8],[9,10,11]]]) pprint_object.pprint(constant_node.shape) # (2,2,3) pprint_object.pprint(constant_node) # array([[[ 0, 1, 2], # [ 3, 4, 5]], # [[ 6, 7, 8], # [ 9,10,11]]]) t1=tf.transpose(constant_node,[1,0,2]) pprint_object.pprint(interactive_session_object.run(t1).shape) # (2,2,3) pprint_object.pprint(interactive_session_object.run(t1)) # array([[[ 0, 1, 2], # [ 6, 7, 8]], # [[ 3, 4, 5], # [ 9,10,11]]]) constant_node=tf.transpose(t1,[1,0,2]) pprint_object.pprint(interactive_session_object.run(constant_node).shape) # (2,2,3) pprint_object.pprint(interactive_session_object.run(constant_node)) # array([[[ 0, 1, 2], # [ 3, 4, 5]], # [[ 6, 7, 8], # [ 9,10,11]]]) t2=tf.transpose(constant_node,[1,2,0]) pprint_object.pprint(interactive_session_object.run(t2).shape) # (2,3,2) pprint_object.pprint(interactive_session_object.run(t2)) # array([[[ 0, 6], # [ 1, 7], # [ 2, 8]], # [[ 3, 9], # [ 4,10], # [ 5,11]]]) constant_node=tf.transpose(t2,[2,0,1]) pprint_object.pprint(interactive_session_object.run(constant_node).shape) # (2,2,3) pprint_object.pprint(interactive_session_object.run(constant_node)) # array([[[ 0, 1, 2], # [ 3, 4, 5]], # [[ 6, 7, 8], # [ 9,10,11]]])