Install TensorFlow - machinelearningsanfrancisco/San-Francisco-Traffic-Predictive-Model GitHub Wiki

Installing Tensorflow for Windows 10, Linux & OSX

To keep it simple, Tensorflow runs linear algebra matrix calculations. It runs very efficiently om systems with GPUs (graphic gaming cards). Not so much on a CPU. Running small models is fine on a CPU for beginner learning purposes. But once you need to start running real sizeable models (multi GB) you'll either want a local system with a solid GPU or use cloud services if you aren't gonna drop coin to have a box that just grinds 24/7 for you.

For a CPU only system
  1. Install Anaconda
  2. Open a command prompt (Linux & OSX) or Anaconda Prompt (Windows 10)
  3. Create an environment. You can usse any name you want, I'm just chose tenserflow.
$ conda create --name tensorflow
  1. Activate the environment
$ conda activate tensorflow
  1. Install packages
$ conda install tensorflow numpy pandas matplotlib seaborn scikit-learn spyder jupyter tensorflow
For a GPU system (Nvidia)
  1. Install Anaconda Anaconda
  2. Create an environment. You can usse any name you want, I just chose tenserflow.
$ conda create --name tensorflow-gpu
  1. Activate the environment
$ conda activate tensorflow-gpu
  1. Install packages
conda install tensorflow-gpu numpy pandas matplotlib seaborn scikit-learn spyder jupyter
  1. Find what versions of cudatoolkit and cuDNN are installed. You will want to match the versions from the list of packages with the versions you download.
$ conda list
  1. Install the the specific version from the archive or the recent cuda version if it matches the conda list version
  2. Install the specific version from the archive or the recent cuDNN version if it matches the conda list version. You'll need to create an account to download.

Test to make sure Tensorflow is working

Open the Anaconda Shell if on windows. Othwise from a command prompt in MacOS and Linux:

$ conda activate [env name]
$ spyder
For a Tensorflow CPU only install, put the following in the spyder editor and run it:
import tensorflow as tf

# Initialize two constants
x1 = tf.constant([1,2,3,4])
x2 = tf.constant([5,6,7,8])

# Multiply
result = tf.multiply(x1, x2)

# Initialize the Session
sess = tf.Session()

# Print the result
print(sess.run(result))

# Close the session
sess.close()

This will run a simple matrix multiplication and you should receive this response:

[ 5 12 21 32]
For a Tensorflow-GPU install, put the following in the spyder editor and run it:
import os
os.environ["CUDA_VISIBLE_DEVICES"]="0"
import tensorflow as tf
import time

n = 8192
dtype = tf.float32
with tf.device("/gpu:0"):
    matrix1 = tf.Variable(tf.ones((n, n), dtype=dtype))
    matrix2 = tf.Variable(tf.ones((n, n), dtype=dtype))
    product = tf.matmul(matrix1, matrix2)


# avoid optimizing away redundant nodes
config = tf.ConfigProto(graph_options=tf.GraphOptions(optimizer_options= \
    tf.OptimizerOptions(opt_level=tf.OptimizerOptions.L0)))
sess = tf.Session(config=config)

sess.run(tf.global_variables_initializer())
iters = 10

# pre-warming
sess.run(product.op)

start = time.time()
for i in range(iters):
  sess.run(product.op)
end = time.time()
ops = n**3 + (n-1)*n**2 # n^2*(n-1) additions, n^3 multiplications
elapsed = (end - start)
rate = iters*ops/elapsed/10**9
print('\n %d x %d matmul took: %.2f sec, %.2f G ops/sec' % (n, n, \
    elapsed/iters, rate))

This will run a matrix multiplication and you should recieve a response similiar this:

8192 x 8192 matmul took: 0.28 sec, 3881.58 G ops/sec