#!/usr/bin/env python # coding: utf-8 # ## Math # - https://www.tensorflow.org/api_docs/python/math_ops/ # ### 0. Loading TF and Fixtures # In[2]: import tensorflow as tf sess = tf.InteractiveSession() # In[3]: def namestr(obj, namespace): return [name for name in namespace if namespace[name] is obj][0] # In[5]: m1 = tf.constant(value = [[1., 2.]]) m2 = tf.constant(value = [[3.],[4.]]) m3 = tf.constant(value = [[[1., 2., 3.], [4., 5., 6.]]]) m4 = tf.constant(value = [[[1., 2., 3.], [4., 5., 6.]], [[7., 8., 9.] ,[10., 11., 12.]]]) m5 = tf.constant(value = [[[1., 2.], [3., 4.]]]) m6 = tf.constant(value = [[3., 4., 5.]]) def printFixture(isShapeOut, tensorMatrixList): print "======Fixture=======" for m in tensorMatrixList: print "Tensor Matrix - " + namestr(m, globals()) if (isShapeOut): print "Shape:", m.get_shape() print m.eval() print print "====================" printFixture(True, (m1, m2, m3, m4, m5, m6)) # ### 1. Arithmetic Operators # #### - tf.add(x, y, name=None) # - https://www.tensorflow.org/versions/master/api_docs/python/math_ops/arithmetic_operators#add # In[4]: printFixture(True, (m1, m2)) r1 = tf.add(m1, m2) print r1.eval() r2 = m1 + m2 print r2.eval() # #### - tf.multiply(x, y, name=None) # - https://www.tensorflow.org/api_docs/python/tf/multiply # In[5]: printFixture(True, (m1, m2)) r1 = tf.multiply(m1, m2) print r1.eval() r2 = tf.multiply(m1, m2) print r2.eval() r3 = m1 * m2 print r3.eval() # ### 3. Matrix Math Functions # #### - tf.matmul(a, b, transpose_a=False, transpose_b=False, adjoint_a=False, adjoint_b=False, a_is_sparse=False, b_is_sparse=False, name=None) # - https://www.tensorflow.org/api_docs/python/tf/matmul # In[6]: printFixture(True, (m1, m2)) r1 = tf.matmul(a = m1, b = m2) #(1, 2) x (2, 1) print r1.eval() r2 = tf.matmul(a = m2, b = m1) #(2, 1) x (1, 2) print r2.eval()