anshuman23 / tensorflex

Tensorflow bindings for the Elixir programming language :muscle:
https://hexdocs.pm/tensorflex/Tensorflex.html
Apache License 2.0
308 stars 14 forks source link

Added Keras example #21

Closed anshuman23 closed 6 years ago

anshuman23 commented 6 years ago

This example shows how to train a MLP model on the MNIST dataset from scratch in the Keras library and then save it as a .pb graph model for using it in Tensorflex. The MNIST dataset comes in built with the Keras library. I have basically modified one of the existing examples present on the Keras Github repository. The generated model can be read into Tensorflex as:

iex(1)> {:ok, graph} = Tensorflex.read_graph("mnist_mlp.pb")
2018-07-15 15:16:26.542596: I tensorflow/core/platform/cpu_feature_guard.cc:137] Your CPU supports instructions that this TensorFlow binary was not compiled to use: SSE4.1 SSE4.2 AVX AVX2 FMA
                               {:ok,
 %Tensorflex.Graph{
   def: #Reference<0.4212261172.582352897.51740>,
   name: "mnist_mlp.pb"
 }}

iex(2)> Tensorflex.get_graph_ops graph
["Variable_5", "Variable_4", "Variable_3", "Variable_2", "Variable_1",
 "Variable", "iterations", "decay", "rho", "lr", "output/bias",
 "output/bias/read", "output/kernel", "output/kernel/read", "dense_1/bias",
 "dense_1/bias/read", "dense_1/kernel", "dense_1/kernel/read",
 "dropout_1/keras_learning_phase", "dropout_2/cond/pred_id",
 "dropout_2/cond/Switch", "dropout_2/cond/switch_t",
 "dropout_2/cond/dropout/random_uniform/max",
 "dropout_2/cond/dropout/random_uniform/min",
 "dropout_2/cond/dropout/random_uniform/sub",
 "dropout_2/cond/dropout/keep_prob", "dropout_2/cond/mul/y",
 "dropout_1/cond/pred_id", "dropout_1/cond/Switch", "dropout_1/cond/switch_t",
 "dropout_1/cond/dropout/random_uniform/max",
 "dropout_1/cond/dropout/random_uniform/min",
 "dropout_1/cond/dropout/random_uniform/sub",
 "dropout_1/cond/dropout/keep_prob", "dropout_1/cond/mul/y", "input/bias",
 "input/bias/read", "input/kernel", "input/kernel/read", "input_input",
 "input/MatMul", "input/BiasAdd", "input/Relu", "dropout_1/cond/Switch_1",
 "dropout_1/cond/mul/Switch", "dropout_1/cond/mul",
 "dropout_1/cond/dropout/Div", "dropout_1/cond/dropout/Shape",
 "dropout_1/cond/dropout/random_uniform/RandomUniform",
 "dropout_1/cond/dropout/random_uniform/mul", ...]

Keras makes some slight changes to our input and output operation names (since it is built on top of TF) but they are still easily decipherable. They are changed from "input" to "input_input" and from "output" to "output/Softmax".

Once CSV support is added, a blog post can be written about the MNIST dataset and using Keras (or TF) models for the same.