Introduction to Dense Layers for Deep Learning with 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.

  1. Set hyperparameters
  2. Set layers
  3. Define placeholders
  4. Define layers
  5. Define architecture
  6. Define variable dictionary
  7. Build Model
  8. Define loss & optimizer
  9. 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%.

Data Science
Building AlexNet with TensorFlow and Running it with AWS SageMaker

In the last post, we built AlexNet with Keras. This is the second part of AlexNet building. Let’s rewrite the Keras code from the previous post (see Building AlexNet with Keras) with TensorFlow and run it in AWS SageMaker instead of the local machine. AlexNet is in fact too heavy …

Data Science
Building AlexNet with Keras

As the legend goes, the deep learning networks created by Alex Krizhevsky, Geoffrey Hinton and Ilya Sutskever (now largely know as AlexNet) blew everyone out of the water and won Image Classification Challenge (ILSVRC) in 2012. This heralded the new era of deep learning. AlexNet is the most influential modern …

Data Science
2
Introduction to Dense Layers for Deep Learning with Keras

The most basic neural network architecture in deep learning is the dense neural networks consisting of dense layers (a.k.a. fully-connected layers). In this layer, all the inputs and outputs are connected to all the neurons in each layer. Keras is the high-level APIs that runs on TensorFlow (and CNTK or …