Friday, 6 September 2019

Guide to Eager Execution in TensorFlow

TensorFlow library by default has the execution done when fed (feeding into placeholders then run ops). However, it has the mode called 'eager execution' which calculates stuff just like maths. The example code below shows how to do matrix multiplication:

Source code:
#libs
import tensorflow as tf;

tf.enable_eager_execution();

#matrix 2x3
X = [[1,2,3],
     [4,5,6]];

#matrix 3x4
Y = [[1,2,3,4],
     [5,6,7,8],
     [9,0,1,2]];

print(tf.matmul(X,Y));
print();
print("Xrow1*Ycol1 = 1*1 + 2*5 + 3*9 =",1*1 + 2*5 + 3*9);
print("Xrow1*Ycol2 = 1*2 + 2*6 + 3*0 =",1*2 + 2*6 + 3*0);
print("Xrow1*Ycol3 = 1*3 + 2*7 + 3*1 =",1*3 + 2*7 + 3*1);
print("Xrow1*Ycol4 = 1*4 + 2*8 + 3*2 =",1*4 + 2*8 + 3*2);
print("And so on.");
#eof

Colab link:
https://colab.research.google.com/drive/19mmQhl6h6OGHcOwz6Z0NqZ7pWQo84VMA

No comments:

Post a Comment