Deep Learning with TensorFlow

Create and train neural networks for various deep learning tasks

Getting Started with TensorFlow

Learn how to build, train, and deploy deep learning models using TensorFlow and Keras. From basic neural networks to advanced architectures for computer vision and NLP.

Prerequisites

  • Python programming experience
  • Basic understanding of neural networks
  • NumPy and mathematics fundamentals
  • GPU-enabled environment (recommended)

1. Neural Network Basics

Build your first neural network with TensorFlow and Keras.

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

# Create a simple neural network
model = models.Sequential([
    layers.Dense(64, activation='relu', input_shape=(784,)),
    layers.Dropout(0.2),
    layers.Dense(32, activation='relu'),
    layers.Dense(10, activation='softmax')
])

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

# Train the model
history = model.fit(
    x_train, y_train,
    epochs=10,
    validation_data=(x_test, y_test)
)

2. Convolutional Neural Networks

Implement CNNs for image classification tasks.

# Create a CNN model
model = models.Sequential([
    layers.Conv2D(32, (3, 3), activation='relu', input_shape=(28, 28, 1)),
    layers.MaxPooling2D((2, 2)),
    layers.Conv2D(64, (3, 3), activation='relu'),
    layers.MaxPooling2D((2, 2)),
    layers.Conv2D(64, (3, 3), activation='relu'),
    layers.Flatten(),
    layers.Dense(64, activation='relu'),
    layers.Dense(10, activation='softmax')
])

# Data augmentation
data_augmentation = tf.keras.Sequential([
    layers.RandomFlip("horizontal"),
    layers.RandomRotation(0.1),
    layers.RandomZoom(0.1),
])

3. Recurrent Neural Networks

Build RNNs and LSTMs for sequence data.

# Create an LSTM model
model = models.Sequential([
    layers.Embedding(max_words, 32),
    layers.LSTM(32, return_sequences=True),
    layers.LSTM(32),
    layers.Dense(64, activation='relu'),
    layers.Dropout(0.5),
    layers.Dense(1, activation='sigmoid')
])

# Text preprocessing
vectorizer = layers.TextVectorization(
    max_tokens=max_words,
    output_mode='int',
    output_sequence_length=sequence_length
)

4. Model Deployment

Deploy your models using TensorFlow Serving.

# Save the model
model.save('saved_model/my_model')

# Convert to TensorFlow Lite
converter = tf.lite.TFLiteConverter.from_saved_model('saved_model/my_model')
tflite_model = converter.convert()

# Save TFLite model
with open('model.tflite', 'wb') as f:
    f.write(tflite_model)

# Load and make predictions
interpreter = tf.lite.Interpreter(model_path="model.tflite")
interpreter.allocate_tensors()