-
Notifications
You must be signed in to change notification settings - Fork 1
TensorFlow Cheat Sheet
Florian Pfisterer edited this page Jun 24, 2018
·
7 revisions
It is assumed that you have Python (2 or 3) already installed.
pip install virtualenv
cd ~
mkdir .env
virtualenv ~/.env/tf
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
.
pip install tensorflow
@Simsso
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)
# 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")
# define the loss function and the optimizer training step
loss = tf.losses.sparse_softmax_cross_entropy(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(logits, feed_dict={x: x_val})
accuracy = tf.reduce_mean(tf.square(y_val - y_predict))
print("Accuracy on validation set: {:.4}%".format(accuracy * 100))
- Machine Learning
- Infrastructure
- Challenge
- Misc