Skip to content

TensorFlow Cheat Sheet

Florian Pfisterer edited this page Jun 24, 2018 · 7 revisions

Setting Up Your Local Environment

It is assumed that you have Python (2 or 3) already installed.

Install virtualenv

pip install virtualenv

Create a new virtual environment

cd ~
mkdir .env
virtualenv ~/.env/tf

Activate the environment

Do this step each time you're starting to work on your project:

source ~/.env/tf/bin/activate

To leave the virtual environment, use deactivate. You can also create an alias so you can simply type tf to activate the environment: Append alias tf="source ~/.env/tf/bin/activate" to your ~/.bash_profile.

Install TensorFlow

pip install tensorflow

TensorFlow Cookbook

Project Structure Conventions

@Simsso

Basic Setup

This is how basic TensorFlow code for a simple model is usually structured:

# imports and hyperparameters
import tensorflow as tf

LEARNING_RATE = 0.01
NUM_STEPS = 1000
BATCH_SIZE = 50

# -- define the graph --
# here, we assume that a training example has 784 dimensions - e.g. MNIST images - and that the labels are one-hot encoded

# create placeholders for the data inputs
# (None in the shape means that it will be determined at runtime - depending on our batch size)
x = tf.placeholder(tf.float32, shape=[None, 784], name="input-x")
labels = tf.placeholder(tf.float32, shape=None, name="true-labels")

# define the network architecture (here, we use a simple 2-layer feedforward ReLU-activated neural network
hidden_layer = tf.layers.dense(inputs=x, units=200, activation=tf.nn.relu, name="hidden-layer-200")
logits = tf.layers.dense(inputs=hidden_layer, units=10, name="logits-10")
probs = tf.nn.softmax(inputs=logits, name="probabilities-softmax")

# define the loss function and the optimizer training step
loss = tf.nn.softmax_cross_entropy_with_logits_v2(labels=labels, logits=logits)
optimizer = tf.AdamOptimizer(learning_rate=LEARNING_RATE)
train = optimizer.minimize(loss)

# -- run the session --
with tf.Session() as session:
    session.run(tf.global_variables_initializer()) # don't forget this
    
    for _ in range(NUM_STEPS):
        # in the feed_dict, we need to include values for the placeholders we defined earlier
        x_batch, y_batch = get_training_batch(BATCH_SIZE)
        session.run(train, feed_dict={x: x_batch, labels: y_batch})

    # -- evaluate the model --
    x_val, y_val = get_validation_data()
    y_predict = session.run(probs, feed_dict={x: x_val})
    accuracy = tf.reduce_mean(tf.square(y_val - y_predict))

    print("Accuracy on validation set: {:.4}%".format(accuracy * 100))