import tensorflow as tf
print(tf.__version__)
import numpy as np
# to make this notebook's output stable across runs
np.random.seed(42)


"""
Explanation: Open the session of Tensorflow
bash-3.2$ source activate tensorflow   
(tensorflow) bash-3.2$ jupyter notebook
End of explanation
"""


x = tf.placeholder(dtype=tf.float64, name='x')
y = tf.placeholder(dtype=tf.float64, name='y')

w = tf.Variable(0., dtype=tf.float64, name='w')
b = tf.Variable(0., dtype=tf.float64, name='b')
tf.summary.scalar('w', w);
tf.summary.scalar('b', b);


with tf.name_scope('y1'):
    y1 = w*x + b

with tf.name_scope('loss'):
    loss = tf.reduce_mean(tf.square(y - y1))
    tf.summary.scalar('loss',loss)

optimizer = tf.train.GradientDescentOptimizer(0.5)
step = tf.Variable(0, name='step', trainable=False)
train = optimizer.minimize(loss, global_step=step)
initialize = tf.global_variables_initializer()

from datetime import datetime

now = datetime.utcnow().strftime("%Y%m%d")
root_logdir = "tf_logs"
logdir = "{}/run-{}/".format(root_logdir, now)

summary = [tf.summary.merge_all(), step]

writer = tf.summary.FileWriter(logdir)

logdir

"""
Explanation: Regression
End of explanation
"""


x_dat = np.random.rand(100)
y_dat = 3.*x_dat + 2. + 0.1*np.random.rand(100)
feed_dict = {x:x_dat, y:y_dat}

sess = tf.Session()
writer.add_graph(sess.graph)
sess.run(initialize)

for step in range(201):
    sess.run(train, feed_dict)
    if step%20 == 0:
        print(step, sess.run([w,b,loss], feed_dict))
        writer.add_summary(*sess.run(summary, feed_dict))

"""
Explanation: tensorboard
$ source activate tensorflow
$ tensorboard --logdir tf_logs/run-20170719/
End of explanation
"""


