TensorFlow Installation and Configuration
This comprehensive guide covers TensorFlow installation with GPU acceleration, essential commands for deep learning, and model training workflows.
TensorFlow Installation
CPU Version (Simple)
# Install TensorFlow (CPU)
pip install tensorflow
# Install specific version
pip install tensorflow==2.15.0
# Verify installation
python -c "import tensorflow as tf; print(tf.__version__)"
python -c "import tensorflow as tf; print(tf.config.list_physical_devices())"
# Check TensorFlow build info
python -c "import tensorflow as tf; print(tf.sysconfig.get_build_info())"
GPU Version with CUDA Support
# Prerequisites check
nvidia-smi # Verify NVIDIA GPU
nvcc --version # Verify CUDA
# Install TensorFlow with GPU support
pip install tensorflow[and-cuda]
# Or install specific version
pip install tensorflow-gpu==2.15.0
# Verify GPU availability
python -c "import tensorflow as tf; print(tf.config.list_physical_devices('GPU'))"
python -c "import tensorflow as tf; print(tf.test.is_built_with_cuda())"
python -c "import tensorflow as tf; print(tf.test.is_gpu_available())"
CUDA and cuDNN Setup for TensorFlow
Ubuntu/Linux CUDA Installation
# Add NVIDIA repository
wget https://developer.download.nvidia.com/compute/cuda/repos/ubuntu2204/x86_64/cuda-keyring_1.1-1_all.deb
sudo dpkg -i cuda-keyring_1.1-1_all.deb
sudo apt update
# Install CUDA Toolkit 12.3
sudo apt install -y cuda-toolkit-12-3
# Install cuDNN
sudo apt install -y libcudnn8 libcudnn8-dev
# Add to PATH (add to ~/.bashrc)
export PATH=/usr/local/cuda-12.3/bin:$PATH
export LD_LIBRARY_PATH=/usr/local/cuda-12.3/lib64:$LD_LIBRARY_PATH
# Reload
source ~/.bashrc
# Verify CUDA installation
nvcc --version
nvidia-smi
Windows CUDA Installation
# Download CUDA Toolkit from NVIDIA
# https://developer.nvidia.com/cuda-downloads
# Download cuDNN from NVIDIA
# https://developer.nvidia.com/cudnn
# After installation, verify:
nvcc --version
# Add to System PATH:
# C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v12.3\bin
# C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v12.3\libnvvp
Essential TensorFlow Commands
Basic TensorFlow Operations
# Import TensorFlow
import tensorflow as tf
import numpy as np
# Create tensors
tensor_a = tf.constant([1, 2, 3, 4])
tensor_b = tf.constant([[1, 2], [3, 4]])
tensor_c = tf.zeros([3, 3])
tensor_d = tf.ones([2, 2])
tensor_e = tf.random.normal([2, 3])
# Tensor operations
result = tf.add(tensor_a, 10)
result = tf.multiply(tensor_a, 2)
result = tf.matmul(tensor_b, tensor_b)
# Convert to NumPy
numpy_array = tensor_a.numpy()
# Get tensor info
print(tensor_b.shape)
print(tensor_b.dtype)
print(tensor_b.device)
Building Neural Networks with Keras
# Sequential model
from tensorflow import keras
from tensorflow.keras import layers
model = keras.Sequential([
layers.Dense(128, activation='relu', input_shape=(784,)),
layers.Dropout(0.2),
layers.Dense(64, activation='relu'),
layers.Dense(10, activation='softmax')
])
# Functional API
inputs = keras.Input(shape=(784,))
x = layers.Dense(128, activation='relu')(inputs)
x = layers.Dropout(0.2)(x)
x = layers.Dense(64, activation='relu')(x)
outputs = layers.Dense(10, activation='softmax')(x)
model = keras.Model(inputs=inputs, outputs=outputs)
# Model summary
model.summary()
# Compile model
model.compile(
optimizer='adam',
loss='sparse_categorical_crossentropy',
metrics=['accuracy']
)
Loading and Preprocessing Data
# Load MNIST dataset
(x_train, y_train), (x_test, y_test) = keras.datasets.mnist.load_data()
# Normalize data
x_train = x_train.astype('float32') / 255.0
x_test = x_test.astype('float32') / 255.0
# Reshape data
x_train = x_train.reshape(-1, 784)
x_test = x_test.reshape(-1, 784)
# Create tf.data.Dataset
train_dataset = tf.data.Dataset.from_tensor_slices((x_train, y_train))
train_dataset = train_dataset.shuffle(60000).batch(32)
# Data augmentation
data_augmentation = keras.Sequential([
layers.RandomFlip("horizontal"),
layers.RandomRotation(0.1),
layers.RandomZoom(0.1),
])
Model Training
# Train model
history = model.fit(
x_train, y_train,
batch_size=32,
epochs=10,
validation_split=0.2,
verbose=1
)
# Train with callbacks
callbacks = [
keras.callbacks.EarlyStopping(patience=3, restore_best_weights=True),
keras.callbacks.ModelCheckpoint('best_model.h5', save_best_only=True),
keras.callbacks.ReduceLROnPlateau(factor=0.5, patience=2),
keras.callbacks.TensorBoard(log_dir='./logs')
]
history = model.fit(
x_train, y_train,
batch_size=32,
epochs=50,
validation_data=(x_test, y_test),
callbacks=callbacks
)
Model Evaluation and Prediction
# Evaluate model
test_loss, test_acc = model.evaluate(x_test, y_test, verbose=2)
print(f'Test accuracy: {test_acc}')
# Make predictions
predictions = model.predict(x_test[:10])
predicted_classes = np.argmax(predictions, axis=1)
# Single prediction
single_prediction = model.predict(x_test[0:1])
Save and Load Models
# Save entire model
model.save('my_model.h5')
model.save('my_model.keras')
model.save('saved_model') # SavedModel format
# Load model
loaded_model = keras.models.load_model('my_model.h5')
# Save weights only
model.save_weights('model_weights.h5')
# Load weights
model.load_weights('model_weights.h5')
# Export to TensorFlow Lite
converter = tf.lite.TFLiteConverter.from_keras_model(model)
tflite_model = converter.convert()
with open('model.tflite', 'wb') as f:
f.write(tflite_model)
Convolutional Neural Networks (CNN)
Image Classification with CNN
# Build CNN model
model = keras.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')
])
# Compile and train
model.compile(
optimizer='adam',
loss='sparse_categorical_crossentropy',
metrics=['accuracy']
)
model.fit(x_train, y_train, epochs=10, validation_split=0.2)
Transfer Learning with Pre-trained Models
# Load pre-trained model
base_model = keras.applications.MobileNetV2(
input_shape=(224, 224, 3),
include_top=False,
weights='imagenet'
)
# Freeze base model
base_model.trainable = False
# Add custom layers
model = keras.Sequential([
base_model,
layers.GlobalAveragePooling2D(),
layers.Dense(128, activation='relu'),
layers.Dropout(0.5),
layers.Dense(num_classes, activation='softmax')
])
# Fine-tuning
base_model.trainable = True
for layer in base_model.layers[:100]:
layer.trainable = False
Recurrent Neural Networks (RNN)
LSTM for Sequence Data
# Build LSTM model
model = keras.Sequential([
layers.LSTM(128, return_sequences=True, input_shape=(timesteps, features)),
layers.Dropout(0.2),
layers.LSTM(64),
layers.Dense(32, activation='relu'),
layers.Dense(output_dim, activation='softmax')
])
# GRU model
model = keras.Sequential([
layers.GRU(128, return_sequences=True, input_shape=(timesteps, features)),
layers.Dropout(0.2),
layers.GRU(64),
layers.Dense(output_dim, activation='softmax')
])
GPU Configuration and Optimization
GPU Memory Management
# List GPUs
gpus = tf.config.list_physical_devices('GPU')
print(f"GPUs available: {len(gpus)}")
# Limit GPU memory growth
for gpu in gpus:
tf.config.experimental.set_memory_growth(gpu, True)
# Set memory limit
tf.config.set_logical_device_configuration(
gpus[0],
[tf.config.LogicalDeviceConfiguration(memory_limit=4096)]
)
# Enable mixed precision
from tensorflow.keras import mixed_precision
policy = mixed_precision.Policy('mixed_float16')
mixed_precision.set_global_policy(policy)
# Distributed training
strategy = tf.distribute.MirroredStrategy()
with strategy.scope():
model = build_model()
model.compile(...)
TensorBoard Visualization
TensorBoard Setup
# Add TensorBoard callback
tensorboard_callback = keras.callbacks.TensorBoard(
log_dir='./logs',
histogram_freq=1,
write_graph=True,
write_images=True
)
model.fit(..., callbacks=[tensorboard_callback])
# Launch TensorBoard
tensorboard --logdir=./logs --port=6006
# In Jupyter Notebook
%load_ext tensorboard
%tensorboard --logdir logs
TensorFlow Troubleshooting
Common Issues and Solutions
# GPU not detected
# Check CUDA installation
python -c "import tensorflow as tf; print(tf.config.list_physical_devices('GPU'))"
# Enable verbose logging
import os
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '0'
# CUDA errors
# Ensure CUDA and cuDNN versions match TensorFlow requirements
# Out of memory errors
# Enable memory growth
gpus = tf.config.list_physical_devices('GPU')
for gpu in gpus:
tf.config.experimental.set_memory_growth(gpu, True)
# Or use smaller batch size
model.fit(x_train, y_train, batch_size=16)
# Clear Keras backend
from tensorflow.keras import backend as K
K.clear_session()
Conclusion
This guide covers comprehensive TensorFlow installation and usage for deep learning. VCCLHOSTING GPU servers come with CUDA and TensorFlow pre-configured for optimal performance.