X = tf.placeholder(tf.float32, name="X")
Y = tf.placeholder(tf.float32, name="Y")

"""
Explanation: Step 1: read in data from the .xls file
Step 2: create placeholders for input X (number of fire) and label Y (number of theft)
End of explanation
"""


w = tf.Variable(0.0, name='w')
b = tf.Variable(0.0, name='b')

"""
Explanation: Step 3: create weight and bias, initialized to 0
End of explanation
"""


Y_predicted = w * X + b

"""
Explanation: Step 4: build model to predict Y
End of explanation
"""


loss = tf.square(Y - Y_predicted)

"""
Explanation: Step 5: use the square error as the loss function
End of explanation
"""


def huber_loss(labels, predictions, delta=1.0):
    pass

# loss = utils.huber_loss(Y, Y_predicted)

"""
Explanation: Step 5a: implement Huber loss function from lecture and try it out
End of explanation
"""


optimizer = tf.train.GradientDescentOptimizer(learning_rate=0.001).minimize(loss)

sess = tf.Session() # prefer with tf.Session() as sess: in your code

"""
Explanation: Step 6: using gradient descent with learning rate of 0.01 to minimize loss
End of explanation
"""


sess.run(tf.global_variables_initializer())
writer = tf.summary.FileWriter('./graphs/linear_reg', sess.graph)

"""
Explanation: Step 7: initialize the necessary variables, in this case, w and b
End of explanation
"""


for i in range(50): # train the model 50 epochs
    total_loss = 0
    for x, y in data:
        # Session runs train_op and fetch values of loss
        _, l = sess.run([optimizer, loss], feed_dict={X:x, Y:y})
        total_loss += l
    print('Epoch {0}: {1}'.format(i, total_loss/float(n_samples)))

# close the writer when you're done using it
writer.close() 

"""
Explanation: Step 8: train the model
End of explanation
"""


w, b = sess.run([w, b])

"""
Explanation: Step 9: output the values of w and b
End of explanation
"""


X, Y = data[:, 0], data[:, 1]
plt.scatter(X, Y, label="Real data")
plt.plot(X, w * X + b, label="Predicted data", color='r')
plt.show()

"""
Explanation: Step 10: plot the results
End of explanation
"""


