#!/usr/bin/env python # coding: utf-8 # # Tensor Manipulation # In[ ]: # https://www.tensorflow.org/api_guides/python/array_ops import tensorflow as tf import numpy as np import pprint tf.set_random_seed(777) # for reproducibility pp = pprint.PrettyPrinter(indent=4) sess = tf.InteractiveSession() # ## Simple Array # In[55]: t = np.array([0., 1., 2., 3., 4., 5., 6.]) pp.pprint(t) print(t.ndim) # rank print(t.shape) # shape print(t[0], t[1], t[-1]) print(t[2:5], t[4:-1]) print(t[:2], t[3:]) # # 2D Array # In[58]: t = np.array([[1., 2., 3.], [4., 5., 6.], [7., 8., 9.], [10., 11., 12.]]) pp.pprint(t) print(t.ndim) # rank print(t.shape) # shape # ## Shape, Rank, Axis # In[5]: t = tf.constant([1,2,3,4]) tf.shape(t).eval() # In[6]: t = tf.constant([[1,2], [3,4]]) tf.shape(t).eval() # In[60]: t = 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]]]]) tf.shape(t).eval() # In[8]: [ [ [ [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 # In[9]: matrix1 = tf.constant([[3., 3.]]) matrix2 = tf.constant([[2.],[2.]]) tf.matmul(matrix1, matrix2).eval() # In[10]: (matrix1*matrix2).eval() # ## Watch out broadcasting # In[11]: matrix1 = tf.constant([[3., 3.]]) matrix2 = tf.constant([[2.],[2.]]) (matrix1+matrix2).eval() # In[12]: matrix1 = tf.constant([[3., 3.]]) matrix2 = tf.constant([[2., 2.]]) (matrix1+matrix2).eval() # ## Random values for variable initializations # In[2]: tf.random_normal([3]).eval() # In[3]: tf.random_uniform([2]).eval() # In[4]: tf.random_uniform([2, 3]).eval() # ## Reduce Mean/Sum # In[13]: tf.reduce_mean([1, 2], axis=0).eval() # In[14]: x = [[1., 2.], [3., 4.]] tf.reduce_mean(x).eval() # In[15]: tf.reduce_mean(x, axis=0).eval() # In[16]: tf.reduce_mean(x, axis=1).eval() # In[17]: tf.reduce_mean(x, axis=-1).eval() # In[18]: tf.reduce_sum(x).eval() # In[19]: tf.reduce_sum(x, axis=0).eval() # In[20]: tf.reduce_sum(x, axis=-1).eval() # In[21]: tf.reduce_mean(tf.reduce_sum(x, axis=-1)).eval() # ## Argmax with axis # In[22]: x = [[0, 1, 2], [2, 1, 0]] tf.argmax(x, axis=0).eval() # In[23]: tf.argmax(x, axis=1).eval() # In[24]: tf.argmax(x, axis=-1).eval() # ## Reshape, squeeze, expand_dims # In[25]: t = np.array([[[0, 1, 2], [3, 4, 5]], [[6, 7, 8], [9, 10, 11]]]) t.shape # In[26]: tf.reshape(t, shape=[-1, 3]).eval() # In[27]: tf.reshape(t, shape=[-1, 1, 3]).eval() # In[28]: tf.squeeze([[0], [1], [2]]).eval() # In[29]: tf.expand_dims([0, 1, 2], 1).eval() # ## One hot # In[30]: tf.one_hot([[0], [1], [2], [0]], depth=3).eval() # In[31]: t = tf.one_hot([[0], [1], [2], [0]], depth=3) tf.reshape(t, shape=[-1, 3]).eval() # ## casting # In[32]: tf.cast([1.8, 2.2, 3.3, 4.9], tf.int32).eval() # In[33]: tf.cast([True, False, 1 == 1, 0 == 1], tf.int32).eval() # ## Stack # In[34]: x = [1, 4] y = [2, 5] z = [3, 6] # Pack along first dim. tf.stack([x, y, z]).eval() # In[35]: tf.stack([x, y, z], axis=1).eval() # ## Ones like and Zeros like # In[36]: x = [[0, 1, 2], [2, 1, 0]] tf.ones_like(x).eval() # In[37]: tf.zeros_like(x).eval() # ## Zip # # In[38]: for x, y in zip([1, 2, 3], [4, 5, 6]): print(x, y) # In[39]: for x, y, z in zip([1, 2, 3], [4, 5, 6], [7, 8, 9]): print(x, y, z) # ## Transpose # In[40]: t = np.array([[[0, 1, 2], [3, 4, 5]], [[6, 7, 8], [9, 10, 11]]]) pp.pprint(t.shape) pp.pprint(t) # In[41]: t1 = tf.transpose(t, [1, 0, 2]) pp.pprint(sess.run(t1).shape) pp.pprint(sess.run(t1)) # In[42]: t = tf.transpose(t1, [1, 0, 2]) pp.pprint(sess.run(t).shape) pp.pprint(sess.run(t)) # In[43]: t2 = tf.transpose(t, [1, 2, 0]) pp.pprint(sess.run(t2).shape) pp.pprint(sess.run(t2)) # In[44]: t = tf.transpose(t2, [2, 0, 1]) pp.pprint(sess.run(t).shape) pp.pprint(sess.run(t))