import tensorflow as tf

a = tf.add(1, 2, name='Add_1_and_2')
b = tf.multiply(a, 3)
c = tf.add(4, 5, name='Add_4_and_5')
d = tf.multiply(c, 6, name='Multiply_c_and_6')
e = tf.multiply(4, 5, name='B_add')
f = tf.div(c, 6, name='B_mul')
g = tf.add(b, d)
h = tf.multiply(g, f)

with tf.Session() as sess:
    writer = tf.summary.FileWriter("output", sess.graph)
    print(sess.run(h))
    writer.close()

# Creating Scopes

import tensorflow as tf

with tf.name_scope("MyGroups"):
    with tf.name_scope("ScopeA"):
        a = tf.add(1, 2, name='Add_1_and_2')
        b = tf.multiply(a, 3)
    with tf.name_scope("ScopeB"):
        c = tf.add(4, 5, name='Add_4_and_5')
        d = tf.multiply(c, 6, name='Multiply_c_and_6')

with tf.name_scope("ScopeC"):
    e = tf.multiply(4, 5, name='B_add')
    f = tf.div(c, 6, name='B_mul')

g = tf.add(b, d)
h = tf.multiply(g, f)

with tf.Session() as sess:
    writer = tf.summary.FileWriter("output", sess.graph)
    print(sess.run(h))
    writer.close()


# E2

import tensorflow as tf
import numpy as np

# x and y are placeholders for our training data
x = tf.placeholder("float")
y = tf.placeholder("float")
# w is the variable storing our values. It is initialised with starting "guesses"
# w[0] is the "a" in our equation, w[1] is the "b"
w = tf.Variable([1.0, 2.0], name="w")
# Our model of y = a*x + b
y_model = tf.multiply(x, w[0]) + w[1]

# Our error is defined as the square of the differences
error = tf.square(y - y_model)
# The Gradient Descent Optimizer does the heavy lifting
train_op = tf.train.GradientDescentOptimizer(0.01).minimize(error)

# Normal TensorFlow - initialize values, create a session and run the model
model = tf.global_variables_initializer()

with tf.Session() as session:
    writer = tf.summary.FileWriter("output", session.graph)
    session.run(model)
    for i in range(1000):
        x_value = np.random.rand()
        y_value = x_value * 2 + 6
        session.run(train_op, feed_dict={x: x_value, y: y_value})

    w_value = session.run(w)
    print("Predicted model: {a:.3f}x + {b:.3f}".format(a=w_value[0], b=w_value[1]))
    writer.close()