Defining neural networks with Keras

Introduction to TensorFlow in Python

Isaiah Hull

Visiting Associate Professor of Finance, BI Norwegian Business School

Classifying sign language letters

This image shows the sign language letter A.

This image shows the sign language letter B.

This image shows the sign language letter C.

This image shows the sign language letter D.

Introduction to TensorFlow in Python

The sequential API

This image shows a neural network with 16 nodes in the first hidden layer, 8 in the second, and 4 in the output layer.

Introduction to TensorFlow in Python

The sequential API

  • Input layer
  • Hidden layers
  • Output layer
  • Ordered in sequence
Introduction to TensorFlow in Python

Building a sequential model

# Import tensorflow
from tensorflow import keras

# Define a sequential model
model = keras.Sequential()
# Define first hidden layer
model.add(keras.layers.Dense(16, activation='relu', input_shape=(28*28,)))
Introduction to TensorFlow in Python

Building a sequential model

# Define second hidden layer
model.add(keras.layers.Dense(8, activation='relu'))
# Define output layer
model.add(keras.layers.Dense(4, activation='softmax'))
# Compile the model
model.compile('adam', loss='categorical_crossentropy')
# Summarize the model
print(model.summary())
Introduction to TensorFlow in Python

The functional API

The figure shows a neural network with two sets of inputs.

Introduction to TensorFlow in Python

Using the functional API

# Import tensorflow
import tensorflow as tf

# Define model 1 input layer shape
model1_inputs = tf.keras.Input(shape=(28*28,))

# Define model 2 input layer shape
model2_inputs = tf.keras.Input(shape=(10,))
# Define layer 1 for model 1
model1_layer1 = tf.keras.layers.Dense(12, activation='relu')(model1_inputs)

# Define layer 2 for model 1
model1_layer2 = tf.keras.layers.Dense(4, activation='softmax')(model1_layer1)
Introduction to TensorFlow in Python

Using the functional API

# Define layer 1 for model 2
model2_layer1 = tf.keras.layers.Dense(8, activation='relu')(model2_inputs)

# Define layer 2 for model 2
model2_layer2 = tf.keras.layers.Dense(4, activation='softmax')(model2_layer1)
# Merge model 1 and model 2
merged = tf.keras.layers.add([model1_layer2, model2_layer2])
# Define a functional model
model = tf.keras.Model(inputs=[model1_inputs, model2_inputs], outputs=merged)

# Compile the model
model.compile('adam', loss='categorical_crossentropy')
Introduction to TensorFlow in Python

Let's practice!

Introduction to TensorFlow in Python

Preparing Video For Download...