0
0
TensorFlowml~20 mins

Installation and GPU setup in TensorFlow - ML Experiment: Train & Evaluate

Choose your learning style9 modes available
Experiment - Installation and GPU setup
Problem:You want to install TensorFlow and set up your computer to use the GPU for faster machine learning training.
Current Metrics:No installation done yet, so no GPU usage or TensorFlow functionality available.
Issue:TensorFlow is not installed, and GPU acceleration is not set up, so training will be slow on CPU only.
Your Task
Install TensorFlow with GPU support and verify that TensorFlow can detect and use the GPU.
Use TensorFlow version 2.12 or later.
Verify GPU availability using TensorFlow commands.
Do not change hardware or install unsupported GPU drivers.
Hint 1
Hint 2
Hint 3
Hint 4
Solution
TensorFlow
import tensorflow as tf

print("TensorFlow version:", tf.__version__)

gpus = tf.config.list_physical_devices('GPU')
if gpus:
    print(f"GPUs detected: {len(gpus)}")
    for gpu in gpus:
        print(f" - {gpu}")
else:
    print("No GPU detected. Using CPU.")

# Simple test: create a tensor and run a computation
with tf.device('/GPU:0' if gpus else '/CPU:0'):
    a = tf.constant([[1.0, 2.0], [3.0, 4.0]])
    b = tf.constant([[1.0, 1.0], [0.0, 1.0]])
    c = tf.matmul(a, b)
    print("Result of matrix multiplication:", c.numpy())
Installed TensorFlow 2.12 with GPU support using 'pip install tensorflow'.
Verified GPU detection with 'tf.config.list_physical_devices("GPU")'.
Ran a simple matrix multiplication on GPU to confirm setup.
Results Interpretation

Before: TensorFlow not installed, no GPU detected, training only on CPU.

After: TensorFlow 2.12 installed, GPU detected and used for computation, faster training possible.

Installing TensorFlow with GPU support and verifying GPU availability ensures your machine learning models can train faster by using the GPU hardware.
Bonus Experiment
Try running a small neural network training on the GPU and compare the training time with CPU-only mode.
💡 Hint
Use TensorFlow's device context manager to force CPU or GPU usage and measure time with Python's time module.