Skip to content
Deep Learning with TensorFlow

DeepLearning

Build, train and optimize neural networks with TensorFlow and Keras. From fundamental concepts to advanced architectures.

Intermediate Level

25 min read

Hands-On

With TensorFlow code

Deep Learning Quick Tips

Using the GPU

Training um 10x beschleunigen

Batch Normalization

Stabileres Training

Early Stopping

Overfitting vermeiden

TensorBoard

Visualize training

What is Deep Learning?

Deep learning is a subfield of machine learning inspired by the structure and function of the human brain. It uses artificial neural networks with multiple layers (hence "deep") to recognize complex patterns in large data sets.

Benefits of Deep Learning

  • • Automatische Feature-Extraktion
  • - Excellent performance with large data sets
  • • Vielseitig einsetzbar (Bild, Text, Audio)
  • • State-of-the-art results
  • • Continuous improvement

Herausforderungen

  • - Requires large amounts of data
  • • High compute cost (GPU required)
  • • "Black Box" - schwer interpretierbar
  • - Overfitting for small data records
  • • Hyperparameter-Tuning komplex

Hauptanwendungsbereiche

Computer Vision

Image recognition, object detection, medical image analysis

Natural Language Processing

Language translation, chatbots, text analysis

Audio Processing

Speech recognition, music generation, audio analysis

Types of Neural Networks

Feedforward Neural Networks

Einsteiger

Basic neural networks for tabular data

MNIST digit recognition

House price prediction

Classification

Regression

Convolutional Neural Networks (CNN)

Mittel

Especially for image processing and Computer Vision

Image recognition

Object detection

Medical image analysis

Face recognition

Recurrent Neural Networks (RNN)

Fortgeschritten

For sequential data such as text and time series

Language translation

Zeitserien-Vorhersage

Chatbots

Sentiment-Analyse

TensorFlow & Keras Setup

TensorFlow is Google's open-source framework for machine learning, and Keras is the user-friendly high-level API integrated into TensorFlow.

Installation

# CPU version (for first experiments)
pip install tensorflow

# GPU version (for serious projects)
pip install tensorflow-gpu

# Additional helpful packages
pip install numpy pandas matplotlib seaborn
pip install scikit-learn jupyter notebook

# Jupyter Lab for a better development experience
pip install jupyterlab

Getting Started Test

import tensorflow as tf
import keras
import numpy as np

# Check the TensorFlow version
print(f"TensorFlow version: {tf.__version__}")
print(f"Keras version: {keras.__version__}")

# Check GPU availability
print(f"GPU available: {tf.config.list_physical_devices('GPU')}")

# Simplee Tensor-Operationen
a = tf.constant([[1, 2], [3, 4]])
b = tf.constant([[2, 0], [0, 2]])
c = tf.matmul(a, b)
print(f"Matrix Multiplikation:\n{c.numpy()}")

Development environment tips

  • Google Colab: Free GPU/TPU usage for experiments
  • Jupyter Notebooks: Interactive development and visualization
  • TensorBoard: Built-in visualization for training metrics
  • Keras Tuner: Automatische Hyperparameter-optimization

Erstes Deep Learning Modell

We will build a simple neural network for classifying handwritten digits (MNIST dataset).

MNIST digit recognition

import tensorflow as tf
from tensorflow import keras
from tensorflow.keras import layers
import numpy as np
import matplotlib.pyplot as plt

# 1. Load and prepare the data
(x_train, y_train), (x_test, y_test) = keras.datasets.mnist.load_data()

# Normalize the data (0-1 range)
x_train = x_train.astype('float32') / 255.0
x_test = x_test.astype('float32') / 255.0

# Check the shape
print(f"Training Data: {x_train.shape}")
print(f"Training Labels: {y_train.shape}")

# 2. Define model
model = keras.Sequential([
    layers.Flatten(input_shape=(28, 28)),  # 28x28 -> 784
    layers.Dense(128, activation='relu'),   # Hidden Layer 1
    layers.Dropout(0.2),                   # Regularization
    layers.Dense(64, activation='relu'),    # Hidden Layer 2
    layers.Dense(10, activation='softmax') # Output Layer
])

# 3. Compile model
model.compile(
    optimizer='adam',
    loss='sparse_categorical_crossentropy',
    metrics=['accuracy']
)

# 4. Display model architecture
model.summary()

# 5. Train model
history = model.fit(
    x_train, y_train,
    epochs=10,
    batch_size=32,
    validation_split=0.2,
    verbose=1
)

# 6. Evaluate model
test_loss, test_accuracy = model.evaluate(x_test, y_test, verbose=0)
print(f"Test Accuracy: {test_accuracy:.4f}")

# 7. Vorhersagen machen
predictions = model.predict(x_test[:5])
predicted_classes = np.argmax(predictions, axis=1)
print(f"Vorhergesagte Klassen: {predicted_classes}")
print(f"Echte Klassen: {y_test[:5]}")

Visualize training

# Training-Verlauf plotten
plt.figure(figsize=(12, 4))

plt.subplot(1, 2, 1)
plt.plot(history.history['accuracy'])
plt.plot(history.history['val_accuracy'])
plt.title('Model Accuracy')
plt.ylabel('Accuracy')
plt.xlabel('Epoch')
plt.legend(['Train', 'Validation'])

plt.subplot(1, 2, 2)
plt.plot(history.history['loss'])
plt.plot(history.history['val_loss'])
plt.title('Model Loss')
plt.ylabel('Loss')
plt.xlabel('Epoch')
plt.legend(['Train', 'Validation'])

plt.tight_layout()
plt.show()

Save model

# Save the complete model
model.save('mnist_model.h5')

# Save weights only
model.save_weights('mnist_weights.h5')

# Load the model
loaded_model = keras.models.load_model('mnist_model.h5')

# Architecture as JSON
model_json = model.to_json()
with open("model.json", "w") as json_file:
    json_file.write(model_json)

TensorFlow Ecosystem

TensorFlow & Keras

High-level API for simple model development

Intuitive API

Eager Execution

Auto-Differentiation

Distributed Training

TensorBoard

Visualization and monitoring of training

Loss Curves

Model Graph

Histogram

Embeddings

TensorFlow Extended (TFX)

End-to-end ML Pipeline for Production

Data Validation

Model Serving

Monitoring

A/B Testing

TensorFlow Lite

Mobile and edge device deployment

Quantization

Pruning

Hardware Optimization

Cross-Platform

CNN for Computer Vision

Convolutional Neural Networks (CNNs) are designed specifically for processing image data and achieve excellent results in computer vision.

Convolution Layer

Applies filters to the image to detect features such as edges and textures.

  • - Kernel/filter size
  • • Stride (Schrittweite)
  • • Padding (border handling)

Pooling Layer

Reduces the size and makes the model more robust against small shifts.

  • • Max Pooling
  • • Average Pooling
  • • Global Pooling

Fully Connected

Classic dense layers for the final classification after feature extraction.

  • • Flatten Layer
  • • Dense Layer
  • • Dropout

CIFAR-10 image classification with CNN

import tensorflow as tf
from tensorflow import keras
from tensorflow.keras import layers

# 1. Load CIFAR-10 data
(x_train, y_train), (x_test, y_test) = keras.datasets.cifar10.load_data()

# Normalize data
x_train = x_train.astype('float32') / 255.0
x_test = x_test.astype('float32') / 255.0

# Labels to categorical
y_train = keras.utils.to_categorical(y_train, 10)
y_test = keras.utils.to_categorical(y_test, 10)

# 2. Define CNN model
model = keras.Sequential([
    # First convolution block
    layers.Conv2D(32, (3, 3), activation='relu', input_shape=(32, 32, 3)),
    layers.BatchNormalization(),
    layers.Conv2D(32, (3, 3), activation='relu'),
    layers.MaxPooling2D((2, 2)),
    layers.Dropout(0.25),
    
    # Zweite Convolution Block
    layers.Conv2D(64, (3, 3), activation='relu'),
    layers.BatchNormalization(),
    layers.Conv2D(64, (3, 3), activation='relu'),
    layers.MaxPooling2D((2, 2)),
    layers.Dropout(0.25),
    
    # Dritte Convolution Block
    layers.Conv2D(128, (3, 3), activation='relu'),
    layers.BatchNormalization(),
    layers.Dropout(0.25),
    
    # Klassifikation
    layers.Flatten(),
    layers.Dense(512, activation='relu'),
    layers.BatchNormalization(),
    layers.Dropout(0.5),
    layers.Dense(10, activation='softmax')
])

# 3. Compile with improved configuration
model.compile(
    optimizer=keras.optimizers.Adam(learning_rate=0.001),
    loss='categorical_crossentropy',
    metrics=['accuracy']
)

# 4. Define callbacks
callbacks = [
    keras.callbacks.EarlyStopping(patience=10, restore_best_weights=True),
    keras.callbacks.ReduceLROnPlateau(factor=0.5, patience=5),
    keras.callbacks.ModelCheckpoint('best_model.h5', save_best_only=True)
]

# 5. Training with data augmentation
datagen = keras.preprocessing.image.ImageDataGenerator(
    rotation_range=15,
    width_shift_range=0.1,
    height_shift_range=0.1,
    horizontal_flip=True,
    zoom_range=0.1
)

datagen.fit(x_train)

# 6. Train model
history = model.fit(
    datagen.flow(x_train, y_train, batch_size=32),
    epochs=100,
    validation_data=(x_test, y_test),
    callbacks=callbacks,
    verbose=1
)

# 7. Evaluation
test_loss, test_accuracy = model.evaluate(x_test, y_test, verbose=0)
print(f"Test Accuracy: {test_accuracy:.4f}")

Deep Learning best practices

Training optimization

  • • Batch Normalization: Stabileres Training
  • • Dropout: Overfitting verhindern
  • • Early Stopping: Automatisches Stoppen
  • • Learning Rate Scheduling: Adaptive LR
  • • Data Augmentation: More training data

Architektur Design

  • • Start Simple: Then become more complex
  • • Residual Connections: With deep networks
  • • Transfer Learning: Vortrainierte Modelle
  • • Ensemble Methods: combine multiple models
  • - Model Compression: For Production

Avoid common problems

Overfitting

Model memorizes the training data, performs poorly on new data

→ Dropout, Early Stopping, Regularization

Vanishing Gradients

Gradients become too small in deep networks

→ Batch Norm, ReLU, Residual Connections

Next steps

Practical tools

Framework:TensorFlow, PyTorch, Keras
GPU:Google Colab, AWS, Azure
Monitoring:TensorBoard, Weights & Biases
Deployment:TensorFlow Serving, Docker

Recommended project pipeline

  1. 1. Start with MNIST and simple dense networks
  2. 2. Experiment with CNNs on CIFAR-10
  3. 3. Try transfer learning with pre-trained models
  4. 4. Implement RNNs for text classification
  5. 5. Deploy your first model in production