Introduction to Dense Layers for Deep Learning with TensorFlow
- By : Mydatahack
- Category : Data Science, Deep Learning
- Tags: Deep Learning, Dense Net, Iris, TensorFlow
TensorFlow offers both high- and low-level APIs for Deep Learning. Coding in TensorFlow is slightly different from other machine learning frameworks. You first need to define the variables and architectures. This is because the entire code is executed outside of Python with C++ and the python code itself is just a bunch of definitions.
The aim of this post is to replicate the previous Keras code into TensorFlow. Before writing code in TensorFlow, it is better to use high-level APIs like Keras to build the model (read Introduction to Dense Net with Keras for a preparation).
Steps
(1) Import Modules
(2) Data Preparation
As in the previous post, we are importing the Iris dataset from a csv file. This step is the same as before.
You can also check the official TensorFlow documents about deep learning on Iris dataset here. The way the dataset is preprocessed is quite different from what I did and will be an interesting read.
(3) Defining Variables and Models
Before running the code, we need to define variables and models. The model is the same as the one defined in the previous post with Keras.
Even for more complicated models (e.g. with added convolutional layers), you can use the same steps.
- Set hyperparameters
- Set layers
- Define placeholders
- Define layers
- Define architecture
- Define variable dictionary
- Build Model
- Define loss & optimizer
- Define evaluation metrics
Here is the code from the steps above.
(4) Initialise and Run
Once everything is set up, initialise variables and execute the code in session! After 1000 epochs, you will see the test accuracy of 96%.