In [2]:
import tensorflow as tf

In [10]:
node1 =  tf.constant(3.0, dtype= tf.float32)
node2 = tf.constant(4.0)

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


[3.0, 4.0]

In [12]:
print(sess.run([node1,node2]))


[3.0, 4.0]

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


Addition is :  [7.0]

In [106]:
M = tf.Variable(.6 ,dtype=tf.float32)
C = tf.Variable(-.6, dtype = tf.float32)
x = tf.placeholder(tf.float32)

In [107]:
y = M*x + C
init = tf.global_variables_initializer()
sess.run(init)

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


[ 0.          0.60000002  1.20000005  1.80000007]

Loss function is created and used for evaluation.


In [109]:
B = tf.placeholder(tf.float32)
sq_deltas = tf.square(y - B)
loss_function = tf.reduce_sum(sq_deltas)

In [110]:
print(sess.run(loss_function, {x : [1,2,3,4], B : [0,-5,-4,-3]}))


81.44

That is totally not okay as is nearly 80 and its too high. But if we want to lower our loss_function, we need to fix the input values.


In [115]:
fixM = tf.assign(M,-1.)
fixC = tf.assign(C, 1)
sess.run([fixM,fixC])


Out[115]:
[-1.0, 1.0]

In [116]:
print(sess.run(loss_function, {x : [1,2,3,4], B : [0,-5,-4,-3]}))


20.0

See, the loss's function is lower than what we got before.


In [118]:
optimizer = tf.train.GradientDescentOptimizer(0.01)
train = optimizer.minimize(loss_function)

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

In [122]:
print(sess.run([M, C]))


[-0.7999984, -1.0000046]

We werent even close to guessing these values, but we got the exact and accurate values of the paramters defined earlier through proper training of the model.

Lets sum up the steps of the linear model.


In [128]:
import tensorflow as tf

# Model parameters
M = tf.Variable([.6], dtype=tf.float32)
C = tf.Variable([-.6], dtype=tf.float32)
# Model input and output
x = tf.placeholder(tf.float32)
y = M * x + C

B = tf.placeholder(tf.float32)

# loss_function
loss_function = tf.reduce_sum(tf.square(y - B)) # sum of the squares

# optimizer
optimizer = tf.train.GradientDescentOptimizer(0.01)
train = optimizer.minimize(loss_function)

# training data
x_train = [1, 2, 3, 4]
y_train = [0,-5,-4,-3]

In [129]:
# 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, B : [0,-5,-4,-3]})

In [130]:
# evaluate training accuracy
curr_M, curr_C, curr_loss_f = sess.run([M, C, loss_function], {x: x_train, B: y_train})
print("W: %s b: %s loss: %s"%(curr_M, curr_C, curr_loss_f))


W: [-0.7999984] b: [-1.00000465] loss: 10.8

In [140]:
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 numeric feature. There are many
# other types of columns that are more complicated and useful.
feature_cols = [tf.feature_column.numeric_column("a", shape=[1])]

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


INFO:tensorflow:Using default config.
WARNING:tensorflow:Using temporary folder as model directory: C:\Users\raghu\AppData\Local\Temp\tmpa5xlqram
INFO:tensorflow:Using config: {'_save_summary_steps': 100, '_save_checkpoints_steps': None, '_tf_random_seed': 1, '_log_step_count_steps': 100, '_keep_checkpoint_every_n_hours': 10000, '_model_dir': 'C:\\Users\\raghu\\AppData\\Local\\Temp\\tmpa5xlqram', '_keep_checkpoint_max': 5, '_session_config': None, '_save_checkpoints_secs': 600}

In [143]:
# 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.
a_train = np.array([0., 5., 3., 4.])
y_train = np.array([0., -6., -7., -3.])
a_eval = np.array([2., 5., 8., 10.])
y_eval = np.array([-1.01, -6.1, -7., 4.])
input_fun = tf.estimator.inputs.numpy_input_fn(
    {"a": a_train}, y_train, batch_size=4, num_epochs=None, shuffle=True)
train_input_fun = tf.estimator.inputs.numpy_input_fn(
    {"a": a_train}, y_train, batch_size=4, num_epochs=1000, shuffle=False)
eval_input_fun = tf.estimator.inputs.numpy_input_fn(
    {"a": a_eval}, y_eval, batch_size=4, num_epochs=1000, shuffle=False)

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


INFO:tensorflow:Create CheckpointSaverHook.
INFO:tensorflow:Saving checkpoints for 1 into C:\Users\raghu\AppData\Local\Temp\tmpa5xlqram\model.ckpt.
INFO:tensorflow:loss = 94.0, step = 1
INFO:tensorflow:global_step/sec: 652.458
INFO:tensorflow:loss = 8.63604, step = 101 (0.153 sec)
INFO:tensorflow:global_step/sec: 996.684
INFO:tensorflow:loss = 10.7734, step = 201 (0.100 sec)
INFO:tensorflow:global_step/sec: 997.786
INFO:tensorflow:loss = 14.6472, step = 301 (0.100 sec)
INFO:tensorflow:global_step/sec: 1190.98
INFO:tensorflow:loss = 15.9084, step = 401 (0.084 sec)
INFO:tensorflow:global_step/sec: 1001.1
INFO:tensorflow:loss = 22.7208, step = 501 (0.100 sec)
INFO:tensorflow:global_step/sec: 865.215
INFO:tensorflow:loss = 14.583, step = 601 (0.116 sec)
INFO:tensorflow:global_step/sec: 864.817
INFO:tensorflow:loss = 10.7745, step = 701 (0.116 sec)
INFO:tensorflow:global_step/sec: 996.08
INFO:tensorflow:loss = 13.2826, step = 801 (0.100 sec)
INFO:tensorflow:global_step/sec: 1185.75
INFO:tensorflow:loss = 13.9536, step = 901 (0.100 sec)
INFO:tensorflow:Saving checkpoints for 1000 into C:\Users\raghu\AppData\Local\Temp\tmpa5xlqram\model.ckpt.
INFO:tensorflow:Loss for final step: 10.855.
Out[143]:
<tensorflow.python.estimator.canned.linear.LinearRegressor at 0x2092faeae80>

In [145]:
# Here we evaluate how well our model did.
train_metrics = estimator.evaluate(input_fn=train_input_fun)
eval_metrics = estimator.evaluate(input_fn=eval_input_fun)
print("train metrics: %r"% train_metrics)
print("eval metrics: %r"% eval_metrics)


INFO:tensorflow:Starting evaluation at 2017-10-11-04:34:17
INFO:tensorflow:Restoring parameters from C:\Users\raghu\AppData\Local\Temp\tmpa5xlqram\model.ckpt-1000
INFO:tensorflow:Finished evaluation at 2017-10-11-04:34:18
INFO:tensorflow:Saving dict for global step 1000: average_loss = 3.49334, global_step = 1000, loss = 13.9734
INFO:tensorflow:Starting evaluation at 2017-10-11-04:34:19
INFO:tensorflow:Restoring parameters from C:\Users\raghu\AppData\Local\Temp\tmpa5xlqram\model.ckpt-1000
INFO:tensorflow:Finished evaluation at 2017-10-11-04:34:19
INFO:tensorflow:Saving dict for global step 1000: average_loss = 59.9188, global_step = 1000, loss = 239.675
train metrics: {'average_loss': 3.4933407, 'loss': 13.973363, 'global_step': 1000}
eval metrics: {'average_loss': 59.91877, 'loss': 239.67508, 'global_step': 1000}

In [ ]: