LINEAR RESGRESSION EXP-2

Ex2


import numpy as np

import tensorflow.compat.v1 as tf

import matplotlib.pyplot as plt

tf.disable_v2_behavior()

#fixing seeds

np.random.seed(101)

tf.set_random_seed(101)

# Generating random linear data

x = np.linspace(0, 50, 50)

y = np.linspace(0, 50, 50)

# Adding noise to the random linear data

x += np.random.uniform(-4, 4, 50)

y += np.random.uniform(-4, 4, 50)

n = len(x)

# Plot of Training Data

plt.scatter(x, y)

plt.xlabel('x')

plt.ylabel('y')

plt.title("Training Data")

plt.show()

X = tf.placeholder("float")

Y = tf.placeholder("float")

#defining weights and bias

W = tf.Variable(np.random.randn(), name = "W")

b = tf.Variable(np.random.randn(), name = "b")

#hyperparameters

lr = 0.01

epochs = 100

# Hypothesis

y_pred = tf.add(tf.multiply(X, W), b)

# Mean Squared Error Cost Function

cost = tf.reduce_sum(tf.pow(y_pred-Y, 2)) / (2 * n)

# Gradient Descent Optimizer

optimizer = tf.train.GradientDescentOptimizer(lr).minimize(cost)

# Global Variables Initializer

init = tf.global_variables_initializer()

# Starting the Tensorflow Session

with tf.Session() as sess:

    # Initializing the Variables

    sess.run(init)

    # Iterating through all the epochs

    for epoch in range(epochs):

        # Feeding each data point into the optimizer using Feed Dictionary

        for (_x, _y) in zip(x, y):

            sess.run(optimizer, feed_dict = {X : _x, Y : _y})

        # Displaying the result after every 50 epochs

        if (epoch + 1) % 50 == 0:

            # Calculating the cost a every epoch

            c = sess.run(cost, feed_dict = {X : x, Y : y})

            print("Epoch", (epoch + 1), ": cost =", c, "W =", sess.run(W), "b =", sess.run(b))

    # Storing necessary values to be used outside the Session

    training_cost = sess.run(cost, feed_dict ={X: x, Y: y})

    weight = sess.run(W)

    bias = sess.run(b)

# Calculating the predictions

predictions = weight * x + bias

print("Training cost =", training_cost, "Weight =", weight, "bias =", bias, '\n')

# Plotting the Results

plt.plot(x, y, 'ro', label ='Original data')

plt.plot(x, predictions, label ='Fitted line')

plt.title('Linear Regression Result')

plt.legend()

plt.show()

Comments

Popular posts from this blog

Naturally Simulated Prosthetic Arm With Electro Myo Grapy(EMG)-Robotic Arm (Product Creation):

IOT based Home automation Project Controlled by Google Assistant--Product Creation

SEQUENCE MODEL EX-13