Tutorial


In [ ]:
# Import libraries for simulation
import tensorflow as tf
import numpy as np

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


(<tf.Tensor 'Const:0' shape=() dtype=float32>, <tf.Tensor 'Const_1:0' shape=() dtype=float32>)

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


[3.0, 4.0]

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


('node3: ', <tf.Tensor 'Add:0' shape=() dtype=float32>)
('sess.run(node3): ', 7.0)

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

In [6]:
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 [7]:
print(sess.run(a, {a: 3}))


3.0

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


22.5

In [9]:
print(adder_node)
print(add_and_triple)


Tensor("add:0", dtype=float32)
Tensor("mul:0", dtype=float32)

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

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

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


[ 0.          0.30000001  0.60000002  0.90000004]

In [14]:
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

In [15]:
#assign is also just functions
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

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

In [17]:
%%time

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)]
CPU times: user 681 ms, sys: 146 ms, total: 827 ms
Wall time: 539 ms

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

# Model parameters
W = tf.Variable([.3], tf.float32)
b = tf.Variable([-.3], 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

Introduction to tf.contrib.learn


In [20]:
# 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 `numpy_input_fn`. We have to tell the function how many batches
# of data (num_epochs) we want and how big each batch should be.
x = np.array([1., 2., 3., 4.])
y = np.array([0., -1., -2., -3.])
input_fn = tf.contrib.learn.io.numpy_input_fn({"x":x}, y, batch_size=4,
                                              num_epochs=1000)

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

# Here we evaluate how well our model did. In a real example, we would want
# to use a separate validation and testing data set to avoid overfitting.
estimator.evaluate(input_fn=input_fn)


INFO:tensorflow:Using default config.
INFO:tensorflow:Using config: {'_model_dir': None, '_save_checkpoints_secs': 600, '_num_ps_replicas': 0, '_keep_checkpoint_max': 5, '_tf_random_seed': None, '_task_type': None, '_environment': 'local', '_is_chief': True, '_cluster_spec': <tensorflow.python.training.server_lib.ClusterSpec object at 0x116de6c90>, '_tf_config': gpu_options {
  per_process_gpu_memory_fraction: 1
}
, '_num_worker_replicas': 0, '_task_id': 0, '_save_summary_steps': 100, '_save_checkpoints_steps': None, '_evaluation_master': '', '_keep_checkpoint_every_n_hours': 10000, '_master': ''}
WARNING:tensorflow:Using temporary folder as model directory: /var/folders/9m/n1zg9q9d4fv7n5h_rdxs4jjw0000gn/T/tmpuRfuPa
WARNING:tensorflow:Rank of input Tensor (1) should be the same as output_rank (2) for column. Will attempt to expand dims. It is highly recommended that you resize your input, as this behavior may change.
WARNING:tensorflow:From /Users/jdstokes/anaconda/envs/datasci/lib/python2.7/site-packages/tensorflow/contrib/learn/python/learn/estimators/head.py:615: 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/9m/n1zg9q9d4fv7n5h_rdxs4jjw0000gn/T/tmpuRfuPa/model.ckpt.
INFO:tensorflow:loss = 5.75, step = 1
INFO:tensorflow:global_step/sec: 912.158
INFO:tensorflow:loss = 0.117075, step = 101 (0.111 sec)
INFO:tensorflow:global_step/sec: 868.508
INFO:tensorflow:loss = 0.00432436, step = 201 (0.115 sec)
INFO:tensorflow:global_step/sec: 793.223
INFO:tensorflow:loss = 0.00983225, step = 301 (0.126 sec)
INFO:tensorflow:global_step/sec: 746.167
INFO:tensorflow:loss = 0.00182505, step = 401 (0.134 sec)
INFO:tensorflow:global_step/sec: 757.244
INFO:tensorflow:loss = 0.000177823, step = 501 (0.132 sec)
INFO:tensorflow:global_step/sec: 758.605
INFO:tensorflow:loss = 0.000175431, step = 601 (0.131 sec)
INFO:tensorflow:global_step/sec: 562.847
INFO:tensorflow:loss = 4.35568e-05, step = 701 (0.178 sec)
INFO:tensorflow:global_step/sec: 658.28
INFO:tensorflow:loss = 2.00901e-06, step = 801 (0.153 sec)
INFO:tensorflow:global_step/sec: 757.197
INFO:tensorflow:loss = 4.11854e-06, step = 901 (0.132 sec)
INFO:tensorflow:Saving checkpoints for 1000 into /var/folders/9m/n1zg9q9d4fv7n5h_rdxs4jjw0000gn/T/tmpuRfuPa/model.ckpt.
INFO:tensorflow:Loss for final step: 5.26901e-07.
WARNING:tensorflow:Rank of input Tensor (1) should be the same as output_rank (2) for column. Will attempt to expand dims. It is highly recommended that you resize your input, as this behavior may change.
WARNING:tensorflow:From /Users/jdstokes/anaconda/envs/datasci/lib/python2.7/site-packages/tensorflow/contrib/learn/python/learn/estimators/head.py:615: 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-04-22-17:58:30
INFO:tensorflow:Restoring parameters from /var/folders/9m/n1zg9q9d4fv7n5h_rdxs4jjw0000gn/T/tmpuRfuPa/model.ckpt-1000
INFO:tensorflow:Finished evaluation at 2017-04-22-17:58:30
INFO:tensorflow:Saving dict for global step 1000: global_step = 1000, loss = 6.37466e-07
WARNING:tensorflow:Skipping summary for global_step, must be a float or np.float32.
Out[20]:
{'global_step': 1000, 'loss': 6.3746637e-07}

In [ ]: