In [1]:
import tensorflow as tf

In [2]:
node1 = tf.constant(3.0, tf.float32)
node2 = tf.constant(4.0) # also tf.float32 implicity
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 [5]:
node3 = tf.add(node1, node2)
print("node3: ", node3)
print("sess.run(node3):", sess.run(node3))


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

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

In [7]:
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 [9]:
add_and_triple = adder_node * 3.
print(sess.run(add_and_triple, {a: 3, b: 4.5}))


22.5

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 [15]:
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 [16]:
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 [17]:
optimizer = tf.train.GradientDescentOptimizer(0.01)
train = optimizer.minimize(loss)

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

In [21]:
# complete progame
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 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})
    
# evaluation 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

In [27]:
# high lever API tf.contrib.learn

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 `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.
print(estimator.evaluate(input_fn=input_fn))


WARNING:tensorflow:Using temporary folder as model directory: /var/folders/s_/w4xy8nsj0935yzcfv770d6kh0000gn/T/tmp4aDRkL
INFO:tensorflow:Using default config.
INFO:tensorflow:Using config: {'_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 0x103d0cbd0>, '_tf_config': gpu_options {
  per_process_gpu_memory_fraction: 1
}
, '_task_id': 0, '_save_summary_steps': 100, '_save_checkpoints_steps': None, '_evaluation_master': '', '_keep_checkpoint_every_n_hours': 10000, '_master': ''}
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/lipingzhang/anaconda/lib/python2.7/site-packages/tensorflow/contrib/learn/python/learn/estimators/head.py:1362: 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/s_/w4xy8nsj0935yzcfv770d6kh0000gn/T/tmp4aDRkL/model.ckpt.
INFO:tensorflow:loss = 3.5, step = 1
INFO:tensorflow:global_step/sec: 1286.7
INFO:tensorflow:loss = 0.0483285, step = 101
INFO:tensorflow:global_step/sec: 1395.87
INFO:tensorflow:loss = 0.00872186, step = 201
INFO:tensorflow:global_step/sec: 1387.6
INFO:tensorflow:loss = 0.00167969, step = 301
INFO:tensorflow:global_step/sec: 1381.65
INFO:tensorflow:loss = 0.000259653, step = 401
INFO:tensorflow:global_step/sec: 1421.38
INFO:tensorflow:loss = 4.57344e-05, step = 501
INFO:tensorflow:global_step/sec: 1367.93
INFO:tensorflow:loss = 1.31809e-05, step = 601
INFO:tensorflow:global_step/sec: 1380.51
INFO:tensorflow:loss = 1.47275e-06, step = 701
INFO:tensorflow:global_step/sec: 1562.16
INFO:tensorflow:loss = 2.84579e-07, step = 801
INFO:tensorflow:global_step/sec: 1736.29
INFO:tensorflow:loss = 7.54463e-08, step = 901
INFO:tensorflow:Saving checkpoints for 1000 into /var/folders/s_/w4xy8nsj0935yzcfv770d6kh0000gn/T/tmp4aDRkL/model.ckpt.
INFO:tensorflow:Loss for final step: 2.10288e-09.
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/lipingzhang/anaconda/lib/python2.7/site-packages/tensorflow/contrib/learn/python/learn/estimators/head.py:1362: 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-10-24-23:11:18
INFO:tensorflow:Finished evaluation at 2017-10-24-23:11:19
INFO:tensorflow:Saving dict for global step 1000: global_step = 1000, loss = 8.32309e-09
WARNING:tensorflow:Skipping summary for global_step, must be a float or np.float32.
{'loss': 8.3230853e-09, 'global_step': 1000}

In [30]:
# a custom model
import numpy as np
import tensorflow as tf
# Declare list of features, we only have one real-valued feature
def model(features, lables, mode):
    # Build a linear model and predict values
    W = tf.get_variable("W", [1], dtype = tf.float64)
    b = tf.get_variable("b", [1], dtype = tf.float64)
    y = W * features['x'] + b
    # Loss sub-graph
    loss = tf.reduce_sum(tf.square(y - lables))
    # traing sub-graph
    global_step = tf.train.get_global_step()
    optimizer = tf.train.GradientDescentOptimizer(0.01)
    train = tf.group(optimizer.minimize(loss), tf.assign_add(global_step, 1))
   
    # ModelFnOps connects subgraphs we built to the
    # appropriate functionality.
    return tf.contrib.learn.ModelFnOps(mode = mode, predictions = y, loss = loss, train_op = train)

estimator = tf.contrib.learn.Estimator(model_fn = model)
# define our data set
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, 4, num_epochs = 1000)

# train
estimator.fit(input_fn = input_fn, steps = 1000)
# evaluate our model
print(estimator.evaluate(input_fn=input_fn, steps = 10))


WARNING:tensorflow:Using temporary folder as model directory: /var/folders/s_/w4xy8nsj0935yzcfv770d6kh0000gn/T/tmpq8uCOo
INFO:tensorflow:Using default config.
INFO:tensorflow:Using config: {'_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 0x1161bda50>, '_tf_config': gpu_options {
  per_process_gpu_memory_fraction: 1
}
, '_task_id': 0, '_save_summary_steps': 100, '_save_checkpoints_steps': None, '_evaluation_master': '', '_keep_checkpoint_every_n_hours': 10000, '_master': ''}
INFO:tensorflow:Create CheckpointSaverHook.
INFO:tensorflow:Saving checkpoints for 1 into /var/folders/s_/w4xy8nsj0935yzcfv770d6kh0000gn/T/tmpq8uCOo/model.ckpt.
INFO:tensorflow:loss = 37.2902768667, step = 1
INFO:tensorflow:global_step/sec: 1478.18
INFO:tensorflow:loss = 0.0875576059901, step = 101
INFO:tensorflow:global_step/sec: 1529.28
INFO:tensorflow:loss = 0.0207238728214, step = 201
INFO:tensorflow:global_step/sec: 1492.94
INFO:tensorflow:loss = 0.000934716854201, step = 301
INFO:tensorflow:global_step/sec: 1431.13
INFO:tensorflow:loss = 0.000329198645637, step = 401
INFO:tensorflow:global_step/sec: 1478.33
INFO:tensorflow:loss = 1.45421089357e-05, step = 501
INFO:tensorflow:global_step/sec: 1412.61
INFO:tensorflow:loss = 1.30483188229e-06, step = 601
INFO:tensorflow:global_step/sec: 1392.79
INFO:tensorflow:loss = 1.16593607085e-07, step = 701
INFO:tensorflow:global_step/sec: 1658.43
INFO:tensorflow:loss = 3.35450398742e-09, step = 801
INFO:tensorflow:global_step/sec: 1899.44
INFO:tensorflow:loss = 1.56731819672e-09, step = 901
INFO:tensorflow:Saving checkpoints for 1000 into /var/folders/s_/w4xy8nsj0935yzcfv770d6kh0000gn/T/tmpq8uCOo/model.ckpt.
INFO:tensorflow:Loss for final step: 1.52819654443e-10.
INFO:tensorflow:Starting evaluation at 2017-10-24-23:26:30
INFO:tensorflow:Evaluation [1/10]
INFO:tensorflow:Evaluation [2/10]
INFO:tensorflow:Evaluation [3/10]
INFO:tensorflow:Evaluation [4/10]
INFO:tensorflow:Evaluation [5/10]
INFO:tensorflow:Evaluation [6/10]
INFO:tensorflow:Evaluation [7/10]
INFO:tensorflow:Evaluation [8/10]
INFO:tensorflow:Evaluation [9/10]
INFO:tensorflow:Evaluation [10/10]
INFO:tensorflow:Finished evaluation at 2017-10-24-23:26:31
INFO:tensorflow:Saving dict for global step 1000: global_step = 1000, loss = 8.21781e-11
WARNING:tensorflow:Skipping summary for global_step, must be a float or np.float32.
{'loss': 8.2178139e-11, 'global_step': 1000}

In [ ]: