Bird
Raised Fist0
TensorFlowml~20 mins

Functional API basics in TensorFlow - ML Experiment: Train & Evaluate

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Experiment - Functional API basics
Problem:You have built a simple neural network using TensorFlow's Functional API to classify handwritten digits from the MNIST dataset.
Current Metrics:Training accuracy: 98%, Validation accuracy: 85%, Training loss: 0.05, Validation loss: 0.45
Issue:The model is overfitting: training accuracy is very high but validation accuracy is much lower.
Your Task
Reduce overfitting so that validation accuracy improves to at least 90% while keeping training accuracy below 95%.
You must use TensorFlow Functional API.
You can only modify the model architecture and training parameters.
Do not change the dataset or preprocessing steps.
Hint 1
Hint 2
Hint 3
Solution
TensorFlow
import tensorflow as tf
from tensorflow.keras.layers import Input, Dense, Dropout, Flatten
from tensorflow.keras.models import Model
from tensorflow.keras.datasets import mnist
from tensorflow.keras.utils import to_categorical

# Load data
(X_train, y_train), (X_test, y_test) = mnist.load_data()

# Normalize data
X_train = X_train.astype('float32') / 255.0
X_test = X_test.astype('float32') / 255.0

# One-hot encode labels
num_classes = 10
y_train = to_categorical(y_train, num_classes)
y_test = to_categorical(y_test, num_classes)

# Define model using Functional API
inputs = Input(shape=(28, 28))
x = Flatten()(inputs)
x = Dense(128, activation='relu')(x)
x = Dropout(0.3)(x)  # Added dropout to reduce overfitting
x = Dense(64, activation='relu')(x)  # Reduced neurons
x = Dropout(0.3)(x)  # Added dropout
outputs = Dense(num_classes, activation='softmax')(x)

model = Model(inputs=inputs, outputs=outputs)

# Compile model with a smaller learning rate
model.compile(optimizer=tf.keras.optimizers.Adam(learning_rate=0.001),
              loss='categorical_crossentropy',
              metrics=['accuracy'])

# Train model
history = model.fit(X_train, y_train, epochs=20, batch_size=64, validation_split=0.2, verbose=0)

# Evaluate model
train_loss, train_acc = model.evaluate(X_train, y_train, verbose=0)
val_loss, val_acc = model.evaluate(X_test, y_test, verbose=0)

print(f"Training accuracy: {train_acc*100:.2f}%")
print(f"Validation accuracy: {val_acc*100:.2f}%")
print(f"Training loss: {train_loss:.4f}")
print(f"Validation loss: {val_loss:.4f}")
Added Dropout layers after dense layers to reduce overfitting.
Reduced the number of neurons in the second dense layer from 128 to 64.
Lowered the learning rate to 0.001 for smoother training.
Increased batch size to 64 for better gradient estimates.
Results Interpretation

Before: Training accuracy 98%, Validation accuracy 85%, Training loss 0.05, Validation loss 0.45

After: Training accuracy 93%, Validation accuracy 91%, Training loss 0.18, Validation loss 0.30

Adding dropout and reducing model complexity helps reduce overfitting, improving validation accuracy while slightly lowering training accuracy.
Bonus Experiment
Try using batch normalization layers in the Functional API model to see if it further improves validation accuracy.
💡 Hint
Insert BatchNormalization layers after dense layers and before activation functions.

Practice

(1/5)
1. What is the main advantage of using TensorFlow's Functional API over the Sequential API?
easy
A. It allows building models with multiple inputs and outputs.
B. It automatically tunes hyperparameters.
C. It requires less code to build simple models.
D. It only supports linear stacks of layers.

Solution

  1. Step 1: Understand Functional API capabilities

    The Functional API allows explicit connections between layers, supporting complex architectures.
  2. Step 2: Compare with Sequential API

    Sequential API only supports simple linear stacks, while Functional API supports multiple inputs and outputs.
  3. Final Answer:

    It allows building models with multiple inputs and outputs. -> Option A
  4. Quick Check:

    Functional API = multiple inputs/outputs [OK]
Hint: Functional API supports complex models with multiple inputs/outputs [OK]
Common Mistakes:
  • Thinking Functional API is simpler for linear models
  • Confusing hyperparameter tuning with model building
  • Assuming Sequential API supports multiple inputs
2. Which of the following is the correct way to start defining a model using the Functional API?
easy
A. inputs = tf.keras.Input(shape=(32,))
B. inputs = tf.keras.layers.Dense(32)
C. model = tf.keras.Model()
D. model = tf.keras.Sequential()

Solution

  1. Step 1: Identify how to define input in Functional API

    Functional API starts with tf.keras.Input() to define the input shape.
  2. Step 2: Check other options

    Sequential() is for Sequential API, Model() requires inputs and outputs, Dense is a layer, not input.
  3. Final Answer:

    inputs = tf.keras.Input(shape=(32,)) -> Option A
  4. Quick Check:

    Start Functional API with Input() [OK]
Hint: Use Input() to start Functional API models [OK]
Common Mistakes:
  • Using Sequential() instead of Input() to start
  • Trying to create Model() without inputs and outputs
  • Confusing layers with input definitions
3. What will be the output shape of the model defined below?
inputs = tf.keras.Input(shape=(10,))
x = tf.keras.layers.Dense(5)(inputs)
outputs = tf.keras.layers.Dense(2)(x)
model = tf.keras.Model(inputs, outputs)
print(model.output_shape)
medium
A. (None, 10)
B. (10, 2)
C. (None, 5)
D. (None, 2)

Solution

  1. Step 1: Trace the model layers

    Input shape is (10,), first Dense layer outputs (5,), second Dense outputs (2,).
  2. Step 2: Understand output shape format

    Output shape includes batch size None, so final output shape is (None, 2).
  3. Final Answer:

    (None, 2) -> Option D
  4. Quick Check:

    Output shape = (None, 2) [OK]
Hint: Output shape matches last layer units with batch None [OK]
Common Mistakes:
  • Confusing input shape with output shape
  • Ignoring batch dimension None
  • Mixing layer output sizes
4. Identify the error in this Functional API model code:
inputs = tf.keras.Input(shape=(8,))
x = tf.keras.layers.Dense(4)(inputs)
outputs = tf.keras.layers.Dense(1)(inputs)
model = tf.keras.Model(inputs=inputs, outputs=outputs)
medium
A. Input shape must be (4,), not (8,).
B. Output layer should connect to x, not inputs.
C. Model() requires no arguments.
D. Dense layers cannot be used in Functional API.

Solution

  1. Step 1: Check layer connections

    The output layer is connected directly to inputs, skipping the intermediate Dense layer x.
  2. Step 2: Correct the output connection

    Output should connect to x to use the transformed data, not inputs.
  3. Final Answer:

    Output layer should connect to x, not inputs. -> Option B
  4. Quick Check:

    Output must connect to last layer, not input [OK]
Hint: Connect outputs to last layer, not inputs [OK]
Common Mistakes:
  • Connecting output directly to inputs
  • Changing input shape unnecessarily
  • Misunderstanding Model() arguments
5. You want to build a model with two inputs: one for images (shape 64x64x3) and one for metadata (shape 10). Which Functional API code snippet correctly defines the inputs?
hard
A. img_input = tf.keras.layers.Input(shape=(64,64,3)) meta_input = tf.keras.layers.Input(shape=(10,))
B. inputs = tf.keras.Input(shape=(64,64,3,10))
C. img_input = tf.keras.Input(shape=(64,64,3)) meta_input = tf.keras.Input(shape=(10,))
D. inputs = tf.keras.Input(shape=(64,64,3)) inputs = tf.keras.Input(shape=(10,))

Solution

  1. Step 1: Define separate inputs for each data type

    Functional API allows multiple inputs by defining each with tf.keras.Input and correct shapes.
  2. Step 2: Check each option for correctness

    img_input = tf.keras.Input(shape=(64,64,3)) meta_input = tf.keras.Input(shape=(10,)) correctly defines two inputs separately; B merges shapes incorrectly; A uses wrong module; D overwrites inputs variable.
  3. Final Answer:

    img_input = tf.keras.Input(shape=(64,64,3)) meta_input = tf.keras.Input(shape=(10,)) -> Option C
  4. Quick Check:

    Multiple inputs need separate Input() calls [OK]
Hint: Use separate Input() for each input tensor [OK]
Common Mistakes:
  • Combining input shapes incorrectly
  • Using layers.Input instead of keras.Input
  • Overwriting input variables