<jupyter_start><jupyter_text>DEVNET Tensorflow GPU Matrix LabWelcome to the DEVNET Tensorflow lab!  In this lab our goal is to use Tensorflow to show you how GPUs help improve performance of data science applications. There are several introductions to Tensorflow included in this Jupyter Notebook. In this walkthrough we will explain some of the benefits of GPUs and show how Tensorflow works on them!  Let's start by loading some of the libraries we will be using for this lab.<jupyter_code># First we load the Tensorflow library.  Tensorflow is built to numerical computation using data graphs.  
import tensorflow as tf
# Then we load NumPy.  NumPy is a library built for matrix functions on Python. 
import numpy as np
# Finally, import the timer so we can time how long it takes to run operations. 
from timeit import default_timer as timer<jupyter_output><empty_output><jupyter_text>1. Making Matrix Operations FasterGPUS were intended to render graphics on computers for games and CAD software.  To do this efficiently and fast theidea was to get a ton of processor cores and make them extremely good at doing matrix operations.  Things were humming along nicely until Machine Learning and Crypto currency started gaining steam.  It turns out that matrix operationsare exactly what is needed to improve performance of machine learning as well as proof of work for cryptocurrencies.  In the code below we will launch tensorflow and then compare the time it takes to perform some matrix operations on the GPU and the CPU.  Don't get hung up on the syntax.  The point of this exercise is to show you the huge improvements in performance we can get by running Tensorflow on a GPU.   1.1 Exercise:  Run the below tensorflow on GPUs and on a CPU and compare the differenceIn the code below we load Tensorflow and some other python libraries.  Normally when we start creating a Tensorflow graph we just use the default device.  However, we can tell what device we want the graph to run on by specifying it with the ```with tf.device()``` directive.  In the code below try two different kinds of devices: ```/gpu:0``` and ```/cpu:0```. Be sure to put them in quotes!<jupyter_code>shape = (15000, 15000)  # The shape of our matrix is 15,000 x 15,000.  That is really big!
with tf.device():   # Try different devices: "/cpu:0" to "/gpu:0" and see the time difference. 
    # create the computational graph.  This doesn't run it, it just gets it ready!
    random_matrix = tf.random_uniform(shape=shape, minval=0, maxval=1)
    dot_operation = tf.matmul(random_matrix, tf.transpose(random_matrix))
    sum_operation = tf.reduce_sum(dot_operation)
    
start = timer()
# now start running the tensorflow computational graph to get the final answer. 
with tf.Session() as session:
    result = session.run(sum_operation)
duration = timer() - start
print("Duration: {:0.2f} seconds".format(duration))<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.053009575923392616, 'fraction_numerical': 0.010601915184678522, 'mean_word_length': 4.4981203007518795, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '2', 'dir': 'jupyter-structured-clean-dedup', 'id': '473919', 'license': 'Apache-2.0', 'n_tokens_mistral': 774, 'n_tokens_neox': 742, 'n_words': 426, 'path': 'lab1.ipynb', 'repo_name': 'vallard/DEVNET-TensorflowLab', 'size': '2924'}