Function definition:
def run_training(train_X, train_Y):
Inputs variables:
X = tf.placeholder(tf.float32, [m, n])
Y = tf.placeholder(tf.float32, [m, 1])
Weight and bias representation
W = tf.Variable(tf.zeros([n, 1], dtype=np.float32), name="weight")
b = tf.Variable(tf.zeros([1], dtype=np.float32), name="bias")
Lineal Model:
with tf.name_scope("linear_Wx_b") as scope:
activation = tf.add(tf.matmul(X, W), b)
Cost:
with tf.name_scope("cost") as scope:
cost = tf.reduce_sum(tf.square(activation - Y)) / (2 * m)
tf.summary.scalar("cost", cost)
Training:
with tf.name_scope("train") as scope:
optimizer = tf.train.GradientDescentOptimizer(0.07).minimize(cost)
TensorFlow session:
with tf.Session() as sess:
merged = tf.summary.merge_all()
writer = tf.summary.FileWriter(log_file, sess.graph)
Note: merged and writer are part of the TensorBoard strategy to track the model behavior.
init = tf.global_variables_initializer()
sess.run(init)
Repeating 1.5k times the training loop:
for step in range(1500):
result, _ = sess.run([merged, optimizer], feed_dict={X: np.asarray(train_X), Y: np.asarray(train_Y)})
writer.add_summary(result, step)
Print Training Cost:
training_cost = sess.run(cost, feed_dict={X: np.asarray(train_X), Y: np.asarray(train_Y)})
print "Training Cost: ", training_cost, "W=", sess.run(W), "b=", sess.run(b), '\n'
Concrete prediction based on the model trained:
print "Prediction for 3.5 years"
predict_X = np.array([3.5], dtype=np.float32).reshape([1, 1])
predict_X = (predict_X - mean) / std
predict_Y = tf.add(tf.matmul(predict_X, W), b)
print "Child height(Y) =", sess.run(predict_Y)