In [189]:
#USN:01FE15BCS237
#Roll:244
#lAB3_Assignment_1

import matplotlib.pyplot as plt
import sys
import numpy as np
import tensorflow as tf
from tensorflow.python.framework import ops

In [190]:
# Specify as Ridge or LASSO
regression_type = 'Ridge'

In [191]:
ops.reset_default_graph()
# Create graph
sess = tf.Session()

In [192]:
#Setting X and Y values
x_vals = np.linspace(-1, 1, 101)
y_vals  = 2 * x_vals + np.random.randn(*x_vals.shape)

In [193]:
# Declare batch size
batch_size = 10

x_data = tf.placeholder(shape=[None, 1], dtype=tf.float32)
y_target = tf.placeholder(shape=[None, 1], dtype=tf.float32)

# make results reproducible
seed = 13
np.random.seed(seed)
tf.set_random_seed(seed)

# Create variables for linear regression
A = tf.Variable(tf.random_normal(shape=[1,1]))
#b = tf.Variable(tf.random_normal(shape=[1,1]))

# Declare model operations
model_output = tf.matmul(x_data, A)

In [194]:
# Select appropriate loss function based on regression type

if regression_type == 'LASSO':
    # Lasso Loss = L2_Loss + heavyside_step,
    # Where heavyside_step ~ 0 if A < constant, otherwise ~ 99
    lasso_param = tf.constant(0.9)
    heavyside_step = tf.truediv(1., tf.add(1., tf.exp(tf.multiply(-50., tf.subtract(A, lasso_param)))))
    regularization_param = tf.multiply(heavyside_step, 99.)
    loss = tf.add(tf.reduce_mean(tf.square(y_target - model_output)), regularization_param)

elif regression_type == 'Ridge':
    # Declare the Ridge loss function
    # Ridge loss = L2_loss + L2 norm of slope
    ridge_param = tf.constant(1.)
    ridge_loss = tf.reduce_mean(tf.square(A))
    loss = tf.expand_dims(tf.add(tf.reduce_mean(tf.square(y_target - model_output)), tf.multiply(ridge_param,ridge_loss)), 0)
    
else:
    print('Invalid regression_type parameter value',file=sys.stderr)

In [195]:
# Declare optimizer
my_opt = tf.train.GradientDescentOptimizer(0.001)
train_step = my_opt.minimize(loss)

In [196]:
# Initialize variables
sess = tf.Session()
init = tf.global_variables_initializer()
sess.run(init)

# Training loop
loss_vec = []
for i in range(1500):
    rand_index = np.random.choice(len(x_vals), size=batch_size)
    rand_x = np.transpose([x_vals[rand_index]])
    rand_y = np.transpose([y_vals[rand_index]])
    sess.run(train_step, feed_dict={x_data: rand_x, y_target: rand_y})
    temp_loss = sess.run(loss, feed_dict={x_data: rand_x, y_target: rand_y})
    loss_vec.append(temp_loss[0])
    if (i+1)%300==0:
        print('Step #' + str(i+1) + ' A = ' + str(sess.run(A)))
        print('Loss = ' + str(temp_loss))
        print('\n')


Step #300 A = [[ 0.04052406]]
Loss = [ 1.38088822]


Step #600 A = [[ 0.27715677]]
Loss = [ 1.15524006]


Step #900 A = [[ 0.389808]]
Loss = [ 1.47768855]


Step #1200 A = [[ 0.43260732]]
Loss = [ 1.55758059]


Step #1500 A = [[ 0.45189795]]
Loss = [ 0.99311703]



In [197]:
# Get the optimal coefficients
[slope] = sess.run(A)


# Get best fit line
best_fit = []
for i in x_vals:
    best_fit.append(slope*i)

In [198]:
%matplotlib inline
# Plot the result
plt.plot(x_vals, y_vals, 'o', label='Data Points')
plt.plot(x_vals, best_fit, 'r-', label='Best fit line', linewidth=3)
plt.legend(loc='upper left')
plt.show()

# Plot loss over time
plt.plot(loss_vec, 'k-')
plt.title(regression_type + ' Loss per Generation')
plt.xlabel('Generation')
plt.ylabel('Loss')
plt.show()



In [ ]:


In [ ]:


In [ ]:


In [ ]:


In [ ]: