basic concept

import tensorflow


In [7]:
import tensorflow as tf

The Computational Graph


In [8]:
node1 = tf.constant(3.0, dtype=tf.float32)
node2 = tf.constant(4.0) # also tf.float32 implicitly
print(node1, node2)


Tensor("Const_2:0", shape=(), dtype=float32) Tensor("Const_3:0", shape=(), dtype=float32)

Run the nodes


In [9]:
sess = tf.Session()
print(sess.run([node1, node2]))


[3.0, 4.0]

In [10]:
node3 = tf.add(node1, node2)
print("node3: ", node3)
print("sess.run(node3): ", sess.run(node3))


node3:  Tensor("Add_2:0", shape=(), dtype=float32)
sess.run(node3):  7.0

placeholders


In [11]:
a = tf.placeholder(tf.float32)
b = tf.placeholder(tf.float32)
adder_node = a + b  # + provides a shortcut for tf.add(a, b)

evaluate placeholders


In [12]:
print(sess.run(adder_node, {a: 3, b: 4.5}))
print(sess.run(adder_node, {a: [1,3], b: [2, 4]}))


7.5
[ 3.  7.]

In [13]:
add_and_triple = adder_node * 3.
print(sess.run(add_and_triple, {a: 3, b:4.5}))


22.5

variables are trainable parameters to a graph


In [14]:
W = tf.Variable([.3], dtype=tf.float32)
b = tf.Variable([-.3], dtype=tf.float32)
x = tf.placeholder(tf.float32)
linear_model = W * x + b

Call special operation to initialize variables


In [15]:
init = tf.global_variables_initializer()
sess.run(init)

In [16]:
print(sess.run(linear_model, {x:[1, 2, 3, 4]}))


[ 0.          0.30000001  0.60000002  0.90000004]

loss function


In [17]:
y = tf.placeholder(tf.float32)
squared_deltas = tf.square(linear_model - y)
loss = tf.reduce_sum(squared_deltas)
print(sess.run(loss, {x: [1, 2, 3, 4], y:[0, -1, -2, -3]}))


23.66

variables can be changed by tf.assign


In [18]:
fixW = tf.assign(W, [-1.])
fixb = tf.assign(b, [1.])
sess.run([fixW, fixb])
print(sess.run(loss, {x: [1, 2, 3, 4], y:[0, -1, -2, -3]}))


0.0

tf.train API

optimizers slowly change variable to minize loss function


In [20]:
optimizer = tf.train.GradientDescentOptimizer(0.01)
train = optimizer.minimize(loss)

In [21]:
sess.run(init) # reset values to incorrect defaults.
for i in range(1000):
    sess.run(train, {x:[1,2,3,4], y:[0,-1,-2,-3]})
    
print(sess.run([W, b]))


[array([-0.9999969], dtype=float32), array([ 0.99999082], dtype=float32)]

complete program


In [22]:
import numpy as np
import tensorflow as tf

# Model parameters
W = tf.Variable([.3], dtype=tf.float32)
b = tf.Variable([-.3], dtype=tf.float32)
# Model input and output
x = tf.placeholder(tf.float32)
linear_model = W * x + b
y = tf.placeholder(tf.float32)
# loss
loss = tf.reduce_sum(tf.square(linear_model - y)) # sum of the squares
# optimizer
optimizer = tf.train.GradientDescentOptimizer(0.01)
train = optimizer.minimize(loss)
# training data
x_train = [1,2,3,4]
y_train = [0,-1,-2,-3]
# training loop
init = tf.global_variables_initializer()
sess = tf.Session()
sess.run(init) # reset values to wrong
for i in range(1000):
  sess.run(train, {x:x_train, y:y_train})

# evaluate training accuracy
curr_W, curr_b, curr_loss = sess.run([W, b, loss], {x:x_train, y:y_train})
print("W: %s b: %s loss: %s"%(curr_W, curr_b, curr_loss))


W: [-0.9999969] b: [ 0.99999082] loss: 5.69997e-11

tf.contrib.learn is a high level library


In [24]:
import tensorflow as tf
# NumPy is often used to load, manipulate and preprocess data.
import numpy as np

# Declare list of features. We only have one real-valued feature. There are many
# other types of columns that are more complicated and useful.
features = [tf.contrib.layers.real_valued_column("x", dimension=1)]

# An estimator is the front end to invoke training (fitting) and evaluation
# (inference). There are many predefined types like linear regression,
# logistic regression, linear classification, logistic classification, and
# many neural network classifiers and regressors. The following code
# provides an estimator that does linear regression.
estimator = tf.contrib.learn.LinearRegressor(feature_columns=features)

# TensorFlow provides many helper methods to read and set up data sets.
# Here we use two data sets: one for training and one for evaluation
# We have to tell the function how many batches
# of data (num_epochs) we want and how big each batch should be.
x_train = np.array([1., 2., 3., 4.])
y_train = np.array([0., -1., -2., -3.])
x_eval = np.array([2., 5., 8., 1.])
y_eval = np.array([-1.01, -4.1, -7, 0.])
input_fn = tf.contrib.learn.io.numpy_input_fn({"x":x_train}, y_train,
                                              batch_size=4,
                                              num_epochs=1000)
eval_input_fn = tf.contrib.learn.io.numpy_input_fn(
    {"x":x_eval}, y_eval, batch_size=4, num_epochs=1000)

# We can invoke 1000 training steps by invoking the  method and passing the
# training data set.
estimator.fit(input_fn=input_fn, steps=1000)

# Here we evaluate how well our model did.
train_loss = estimator.evaluate(input_fn=input_fn)
eval_loss = estimator.evaluate(input_fn=eval_input_fn)
print("train loss: %r"% train_loss)
print("eval loss: %r"% eval_loss)


INFO:tensorflow:Using default config.
WARNING:tensorflow:Using temporary folder as model directory: /var/folders/qh/505k3rjj48s_dgm9kdxp9dbm0000gn/T/tmpyjtr5x7c
INFO:tensorflow:Using config: {'_save_checkpoints_secs': 600, '_session_config': None, '_tf_random_seed': None, '_environment': 'local', '_task_id': 0, '_evaluation_master': '', '_num_ps_replicas': 0, '_cluster_spec': <tensorflow.python.training.server_lib.ClusterSpec object at 0x10cf94d68>, '_master': '', '_keep_checkpoint_every_n_hours': 10000, '_model_dir': '/var/folders/qh/505k3rjj48s_dgm9kdxp9dbm0000gn/T/tmpyjtr5x7c', '_save_summary_steps': 100, '_is_chief': True, '_save_checkpoints_steps': None, '_keep_checkpoint_max': 5, '_task_type': None, '_num_worker_replicas': 0, '_tf_config': gpu_options {
  per_process_gpu_memory_fraction: 1
}
}
WARNING:tensorflow:From /Users/waywu01/tfenv-py3/lib/python3.5/site-packages/tensorflow/contrib/learn/python/learn/estimators/head.py:625: scalar_summary (from tensorflow.python.ops.logging_ops) is deprecated and will be removed after 2016-11-30.
Instructions for updating:
Please switch to tf.summary.scalar. Note that tf.summary.scalar uses the node name instead of the tag. This means that TensorFlow will automatically de-duplicate summary names based on the scope they are created in. Also, passing a tensor or list of tags to a scalar summary op is no longer supported.
INFO:tensorflow:Create CheckpointSaverHook.
INFO:tensorflow:Saving checkpoints for 1 into /var/folders/qh/505k3rjj48s_dgm9kdxp9dbm0000gn/T/tmpyjtr5x7c/model.ckpt.
INFO:tensorflow:loss = 3.75, step = 1
INFO:tensorflow:global_step/sec: 1116.81
INFO:tensorflow:loss = 0.0193177, step = 101 (0.091 sec)
INFO:tensorflow:global_step/sec: 1267.48
INFO:tensorflow:loss = 0.0101008, step = 201 (0.079 sec)
INFO:tensorflow:global_step/sec: 1300
INFO:tensorflow:loss = 0.00230103, step = 301 (0.077 sec)
INFO:tensorflow:global_step/sec: 1322.68
INFO:tensorflow:loss = 0.000482562, step = 401 (0.076 sec)
INFO:tensorflow:global_step/sec: 1448.81
INFO:tensorflow:loss = 7.8711e-05, step = 501 (0.069 sec)
INFO:tensorflow:global_step/sec: 1452.64
INFO:tensorflow:loss = 2.30302e-05, step = 601 (0.069 sec)
INFO:tensorflow:global_step/sec: 1254.81
INFO:tensorflow:loss = 4.06474e-06, step = 701 (0.080 sec)
INFO:tensorflow:global_step/sec: 1577.94
INFO:tensorflow:loss = 1.19754e-06, step = 801 (0.063 sec)
INFO:tensorflow:global_step/sec: 1659.97
INFO:tensorflow:loss = 1.44976e-07, step = 901 (0.060 sec)
INFO:tensorflow:Saving checkpoints for 1000 into /var/folders/qh/505k3rjj48s_dgm9kdxp9dbm0000gn/T/tmpyjtr5x7c/model.ckpt.
INFO:tensorflow:Loss for final step: 3.51668e-08.
WARNING:tensorflow:From /Users/waywu01/tfenv-py3/lib/python3.5/site-packages/tensorflow/contrib/learn/python/learn/estimators/head.py:625: scalar_summary (from tensorflow.python.ops.logging_ops) is deprecated and will be removed after 2016-11-30.
Instructions for updating:
Please switch to tf.summary.scalar. Note that tf.summary.scalar uses the node name instead of the tag. This means that TensorFlow will automatically de-duplicate summary names based on the scope they are created in. Also, passing a tensor or list of tags to a scalar summary op is no longer supported.
INFO:tensorflow:Starting evaluation at 2017-07-07-08:31:48
INFO:tensorflow:Restoring parameters from /var/folders/qh/505k3rjj48s_dgm9kdxp9dbm0000gn/T/tmpyjtr5x7c/model.ckpt-1000
INFO:tensorflow:Finished evaluation at 2017-07-07-08:31:49
INFO:tensorflow:Saving dict for global step 1000: global_step = 1000, loss = 2.76926e-08
WARNING:tensorflow:From /Users/waywu01/tfenv-py3/lib/python3.5/site-packages/tensorflow/contrib/learn/python/learn/estimators/head.py:625: scalar_summary (from tensorflow.python.ops.logging_ops) is deprecated and will be removed after 2016-11-30.
Instructions for updating:
Please switch to tf.summary.scalar. Note that tf.summary.scalar uses the node name instead of the tag. This means that TensorFlow will automatically de-duplicate summary names based on the scope they are created in. Also, passing a tensor or list of tags to a scalar summary op is no longer supported.
INFO:tensorflow:Starting evaluation at 2017-07-07-08:31:49
INFO:tensorflow:Restoring parameters from /var/folders/qh/505k3rjj48s_dgm9kdxp9dbm0000gn/T/tmpyjtr5x7c/model.ckpt-1000
INFO:tensorflow:Finished evaluation at 2017-07-07-08:31:50
INFO:tensorflow:Saving dict for global step 1000: global_step = 1000, loss = 0.0025394
train loss: {'loss': 2.7692606e-08, 'global_step': 1000}
eval loss: {'loss': 0.0025393998, 'global_step': 1000}

In [ ]: