TensorFlow Code Explanation

This is a README to explain the TensorFlow code provided in the given snippet. The code demonstrates the implementation of linear regression using TensorFlow to predict CO2 emissions based on engine size.

Code Overview

  • Importing TensorFlow
import tensorflow as tf
from tensorflow.python.framework.ops import enable_eager_execution
enable_eager_execution()
The code starts by importing TensorFlow and enabling eager execution mode. Eager execution allows operations to be evaluated immediately instead of constructing a computational graph.

Simple Multiplication

x1 = tf.constant([5])
x2 = tf.constant([6])
res = tf.multiply(x1, x2)
print(res)

This part of the code demonstrates simple multiplication using TensorFlow constants.

Matrix Addition

matrix_one = tf.constant([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
matrix_two = tf.constant([[2, 2, 4], [5, 7, 8], [9, 6, 2]])
result = tf.add(matrix_one, matrix_two)
print(result.numpy())

This part of the code performs matrix addition using TensorFlow constants.

Reading and Analyzing Data

import pandas as pd
import matplotlib.pyplot as plt

data = pd.read_csv('/content/FuelConsumption.csv')
Here, the code imports the pandas library to read a CSV file named 'FuelConsumption.csv'. The loaded data contains information about vehicle features and CO2 emissions.

Linear Regression Implementation
python
Copy code
train_x = data.loc[:, ['ENGINESIZE']].values
train_y = data.loc[:, ['CO2EMISSIONS']].values

w = tf.Variable(20.0)
b = tf.Variable(30.2)

def h(x):
  y = w * x + b
  return y

def costfunction(y_true, y_predict):
  error = tf.reduce_mean(tf.square(y_true - y_predict))
  return error

learningRate = 0.01
training_iteration = 200

w_values = []
b_values = []
loss_values = []

for iteration in range(training_iteration):
  with tf.GradientTape() as tape:
    y_predict = h(train_x)
    cost_value = costfunction(train_y, y_predict)
    loss_values.append(cost_value)
    gradient = tape.gradient(cost_value, [w, b])
    w_values.append(w.numpy())
    b_values.append(b.numpy())
    w.assign_sub(learningRate * gradient[0])
    b.assign_sub(learningRate * gradient[1])

This section demonstrates the implementation of linear regression using TensorFlow. It starts by defining the training data (engine size and CO2 emissions). It then initializes the variables w (weight) and b (bias) as TensorFlow variables.

The h(x) function defines the hypothesis for linear regression (y = wx + b).

The costfunction() calculates the mean squared error loss between the true CO2EMISSIONS and the predicted values.

The main loop performs gradient descent optimization to minimize the cost function. It calculates the gradients, updates w and b, and stores their values and loss for visualization.

Visualization

plt.xlabel('Iterations')
plt.ylabel('Loss')
plt.plot(loss_values, 'go')
plt.scatter(train_x, train_y, color='green')

for w, b in zip(w_values[0:len(w_values)], b_values[0:len(b_values)]):
  plt.plot(train_x, train_x * w + b, color='red', linestyle='dashed')

plt.plot(train_x, train_x * w_values[-1] + b_values[-1], color='black')
plt.show()

Finally, the code uses Matplotlib to visualize the loss values over iterations and scatter plots the training data. It also plots the linear regression lines for different stages of training (dashed lines) and the final regression line (solid black line).