TensorFlow Essentials
Essential TensorFlow operations and patterns for deep learning.
Installation
1# CPU version
2pip install tensorflow
3
4# GPU version
5pip install tensorflow[and-cuda]
6
7# Check installation
8python -c "import tensorflow as tf; print(tf.__version__)"
Basic Tensor Operations
1import tensorflow as tf
2
3# Create tensors
4scalar = tf.constant(42)
5vector = tf.constant([1, 2, 3])
6matrix = tf.constant([[1, 2], [3, 4]])
7
8# Tensor operations
9a = tf.constant([[1, 2], [3, 4]])
10b = tf.constant([[5, 6], [7, 8]])
11
12# Element-wise operations
13c = a + b
14d = a * b
15
16# Matrix multiplication
17e = tf.matmul(a, b)
18
19# Reshaping
20reshaped = tf.reshape(a, [4, 1])
Building Models (Keras API)
1from tensorflow import keras
2from tensorflow.keras import layers
3
4# Sequential model
5model = keras.Sequential([
6 layers.Dense(128, activation='relu', input_shape=(784,)),
7 layers.Dropout(0.2),
8 layers.Dense(64, activation='relu'),
9 layers.Dense(10, activation='softmax')
10])
11
12# Functional API
13inputs = keras.Input(shape=(784,))
14x = layers.Dense(128, activation='relu')(inputs)
15x = layers.Dropout(0.2)(x)
16x = layers.Dense(64, activation='relu')(x)
17outputs = layers.Dense(10, activation='softmax')(x)
18model = keras.Model(inputs=inputs, outputs=outputs)
19
20# Compile
21model.compile(
22 optimizer='adam',
23 loss='sparse_categorical_crossentropy',
24 metrics=['accuracy']
25)
Training
1# Train model
2history = model.fit(
3 x_train, y_train,
4 batch_size=32,
5 epochs=10,
6 validation_split=0.2,
7 callbacks=[
8 keras.callbacks.EarlyStopping(patience=3),
9 keras.callbacks.ModelCheckpoint('best_model.h5', save_best_only=True)
10 ]
11)
12
13# Evaluate
14test_loss, test_acc = model.evaluate(x_test, y_test)
15
16# Predict
17predictions = model.predict(x_new)
Custom Training Loop
1import tensorflow as tf
2
3# Define model, loss, optimizer
4model = MyModel()
5loss_fn = keras.losses.SparseCategoricalCrossentropy()
6optimizer = keras.optimizers.Adam()
7
8# Training step
9@tf.function
10def train_step(x, y):
11 with tf.GradientTape() as tape:
12 predictions = model(x, training=True)
13 loss = loss_fn(y, predictions)
14
15 gradients = tape.gradient(loss, model.trainable_variables)
16 optimizer.apply_gradients(zip(gradients, model.trainable_variables))
17 return loss
18
19# Training loop
20for epoch in range(epochs):
21 for x_batch, y_batch in dataset:
22 loss = train_step(x_batch, y_batch)
Saving and Loading
1# Save entire model
2model.save('my_model.h5')
3model.save('my_model') # SavedModel format
4
5# Load model
6loaded_model = keras.models.load_model('my_model.h5')
7
8# Save weights only
9model.save_weights('weights.h5')
10model.load_weights('weights.h5')
TensorBoard
1# Setup TensorBoard callback
2tensorboard_callback = keras.callbacks.TensorBoard(
3 log_dir='./logs',
4 histogram_freq=1
5)
6
7model.fit(x_train, y_train, callbacks=[tensorboard_callback])
8
9# View in terminal
10# tensorboard --logdir=./logs
Related Snippets
- Data Augmentation
Data augmentation techniques for Keras and PyTorch - DNN Policy Learning Theory
Deep Neural Network policy learning with mathematical foundations. Policy … - Graph RAG Techniques
Graph-based Retrieval-Augmented Generation for enhanced context and relationship … - Image to Vector Embeddings
Image embeddings convert visual content into dense vector representations that … - Keras Essentials
High-level Keras API for building neural networks quickly. Installation 1# Keras … - LangChain Recipes
Practical recipes for building LLM applications with LangChain: prompts, chains, … - ONNX Model Conversion
ONNX (Open Neural Network Exchange) for converting models between frameworks. … - PyTorch Essentials
Essential PyTorch operations and patterns for deep learning. Installation 1# CPU … - Q-Learning Theory
Q-Learning algorithm theory with mathematical foundations. Markov Decision … - RAG (Retrieval-Augmented Generation)
Retrieval-Augmented Generation techniques for enhancing LLM responses with … - Sound to Vector Embeddings
Audio embeddings convert sound signals (speech, music, environmental sounds) … - Tensor Mathematics & Backpropagation
Tensor mathematics fundamentals and backpropagation theory with detailed … - TensorFlow Lite
TensorFlow Lite for deploying models on mobile and embedded devices. Convert … - Text to Vector Embeddings
Text embeddings convert textual content into dense vector representations that …