<jupyter_start><jupyter_text>Benchmark for CPU vs GPU (For Machine Learning):I decided to see the performance difference between training on a CPU vs GPU.I used code from https://www.analyticsvidhya.com/blog/2021/11/benchmarking-cpu-and-gpu-performance-with-tensorflow/ , and will put in any notes that I make from this small experiment. Importing the necessary libraries<jupyter_code>#Importing libraries into the session:
import tensorflow as tf
from tensorflow import keras
import numpy as np
import matplotlib.pyplot as plt<jupyter_output><empty_output><jupyter_text>Testing if TensorFlow is running on the GPU, by listing all the physical devices that can be used:<jupyter_code>tf.config.experimental.list_physical_devices()<jupyter_output>2022-01-18 14:35:16.715512: I tensorflow/stream_executor/cuda/cuda_gpu_executor.cc:939] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero
2022-01-18 14:35:17.499554: I tensorflow/stream_executor/cuda/cuda_gpu_executor.cc:939] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero
2022-01-18 14:35:17.500155: I tensorflow/stream_executor/cuda/cuda_gpu_executor.cc:939] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero<jupyter_text>Testing if I am using CUDA on the GPU (using a GTX 950 in this test)<jupyter_code>tf.test.is_built_with_cuda()<jupyter_output><empty_output><jupyter_text>Loading in a large dataset (This is a keras dataset).This uses the ***famous*** cifar10 dataset, which includes 32*32 colour images, of which is seperated into 50,000 train and 10,000 test images.<jupyter_code>(X_train, y_train), (X_test, y_test) = keras.datasets.cifar10.load_data()<jupyter_output><empty_output><jupyter_text>Checking the image shape. Also checking the data to see if it is as expected.<jupyter_code># checking images shape
X_train.shape, X_test.shape<jupyter_output><empty_output><jupyter_text>Making sure the database is working by querying the first entry<jupyter_code># display single image shape
X_train[0].shape<jupyter_output><empty_output><jupyter_text>Making sure that the labels are as expected<jupyter_code># checking labels
y_train[:5]
# scaling image values between 0-1
X_train_scaled = X_train/255
X_test_scaled = X_test/255
# one hot encoding labels
y_train_encoded = keras.utils.to_categorical(y_train, num_classes = 10, dtype = 'float32')
y_test_encoded = keras.utils.to_categorical(y_test, num_classes = 10, dtype = 'float32')<jupyter_output><empty_output><jupyter_text>Using a function to build a neural netowrk with architecture. Compiling is also included.<jupyter_code>def get_model():
    model = keras.Sequential([
        keras.layers.Flatten(input_shape=(32,32,3)),
        keras.layers.Dense(3000, activation='relu'),
        keras.layers.Dense(1000, activation='relu'),
        keras.layers.Dense(10, activation='sigmoid')    
    ])
    model.compile(optimizer='SGD',
              loss='categorical_crossentropy',
              metrics=['accuracy'])
    return model<jupyter_output><empty_output><jupyter_text>Testing the CPU in a 10 epoch run (This was run on a i7-2600)Looking at telemetry, the CPU was pinned the entire time it was training (100%), and the system was hardly usable.<jupyter_code>%%timeit -n1 -r1
# CPU
with tf.device('/CPU:0'):
    model_cpu = get_model()
    model_cpu.fit(X_train_scaled, y_train_encoded, epochs = 10)<jupyter_output>2022-01-18 13:49:53.958496: I tensorflow/core/platform/cpu_feature_guard.cc:151] This TensorFlow binary is optimized with oneAPI Deep Neural Network Library (oneDNN) to use the following CPU instructions in performance-critical operations:  SSE3 SSE4.1 SSE4.2 AVX
To enable them in other operations, rebuild TensorFlow with the appropriate compiler flags.
2022-01-18 13:49:53.960067: I tensorflow/stream_executor/cuda/cuda_gpu_executor.cc:939] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero
2022-01-18 13:49:53.960474: I tensorflow/stream_executor/cuda/cuda_gpu_executor.cc:939] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero
2022-01-18 13:49:53.960807: I tensorflow/stream_executor/cuda/cuda_gpu_executor.cc:939] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning[...]<jupyter_text>Testing the GPU in a 10 epoch run (This was run on a GTX 950)<jupyter_code>%%timeit -n1 -r1
# GPU
with tf.device('/GPU:0'):
    model_gpu = get_model()
    model_gpu.fit(X_train_scaled, y_train_encoded, epochs = 10)<jupyter_output>2022-01-18 14:35:24.337671: I tensorflow/core/platform/cpu_feature_guard.cc:151] This TensorFlow binary is optimized with oneAPI Deep Neural Network Library (oneDNN) to use the following CPU instructions in performance-critical operations:  SSE3 SSE4.1 SSE4.2 AVX
To enable them in other operations, rebuild TensorFlow with the appropriate compiler flags.
2022-01-18 14:35:24.375531: I tensorflow/stream_executor/cuda/cuda_gpu_executor.cc:939] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero
2022-01-18 14:35:24.376219: I tensorflow/stream_executor/cuda/cuda_gpu_executor.cc:939] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero
2022-01-18 14:35:24.376589: I tensorflow/stream_executor/cuda/cuda_gpu_executor.cc:939] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning[...]
{'fraction_non_alphanumeric': 0.08679245283018867, 'fraction_numerical': 0.06123499142367067, 'mean_word_length': 5.464523281596453, 'pattern_counts': {'":': 0, '<': 38, '<?xml version=': 0, '>': 38, 'https://': 1, 'lorem ipsum': 0, 'www.': 1, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '10', 'dir': 'jupyter-structured-clean-dedup', 'id': '412888', 'license': 'MIT', 'n_tokens_mistral': 2138, 'n_tokens_neox': 1908, 'n_words': 620, 'path': 'Machine Learning with Python/benchmarks-cpu-vs-gpu.ipynb', 'repo_name': 'ClutchKick2207/FreeCodeCamp', 'size': '5830'}