<jupyter_start><jupyter_text>Linear Regression with TensorFlow Steps:-  Introduction -  Importing Libraries -  Preparing Data -  Building Graph -  Training Model -  Visualizing Graph -  Conclusion   Introduction In this notebook, we'll be using the TensorFlow deep learning framework for linear Regression over a small car $ CO_2 $ emissions dataset. Our gole is to build a model that can predict car $ CO_2 $ emissions before the car menufacture. Here we've some pre-recorded real-time features column they have relation with the label. We'll build a computation graph and feed them by features and labeled data to build our model. Then we can use the model to predict new cars $ CO_2 $ emissions before it manufactures even if it ever has seen before.  Importing LibrariesIn this sections, we'll import some necessary python libraries and TensorFlow.<jupyter_code>import numpy as np
import pandas as pd
%matplotlib inline
import matplotlib
import matplotlib.pyplot as plt

import tensorflow as tf<jupyter_output><empty_output><jupyter_text>Preparing DataIn this sections, we'll - Read our dataset using pandas as dataframe- Visualize data using matplotlib scatter plot- Clean data (if necessary)- Select features and label<jupyter_code>emissions = pd.read_csv('../Datasets/CO2EMISSIONS/FuelConsumptionCo2.csv')
emissions.head()
ENGINESIZE = emissions['ENGINESIZE']
CO2EMISSIONS = emissions['CO2EMISSIONS']

train_X = np.array(ENGINESIZE)
train_Y = np.array(CO2EMISSIONS)
number = train_X.shape[0]
plt.scatter(train_X, train_Y)
plt.show()<jupyter_output><empty_output><jupyter_text>Building GraphIn this sections, we'll - Define placeholders for input X (number of fire) and label Y (number of theft)- Define weight and bias, initialized to 0- Use the square error as the loss function- Use the gradient descent with learning rate of 0.01 to minimize the loss<jupyter_code>X = tf.placeholder(tf.float32, name="X")
Y = tf.placeholder(tf.float32, name="Y")

W = tf.Variable(np.random.randn(), name="weight")
b = tf.Variable(np.random.randn(), name="bias")
predictions = tf.add(tf.multiply(X, W), b)
loss_function = tf.reduce_sum(tf.pow(predictions - Y, 2))/(2 * number)
optimizer = tf.train.GradientDescentOptimizer(learning_rate = 0.01).minimize(loss_function)
init = tf.global_variables_initializer()<jupyter_output><empty_output><jupyter_text>Training ModelIn this sections, we'll - Train the model and iterate 1000 epochs.- Feed features and labeled data to the newral-newtwork.- Show performance and plot model.<jupyter_code>with tf.Session() as sess:
    sess.run(init)

    print("Start Training...")
    for epoch in range(1000):
        for (x, y) in zip(train_X, train_Y):
            sess.run(optimizer, feed_dict={X: x, Y: y})

        if (epoch+1) % 100 == 0:
            c = sess.run(loss_function, feed_dict={X: train_X, Y: train_Y})
            print("Epoch:", '%04d' % (epoch+1), "Loss =",
                  "{:.5f}".format(c), "Weight =", sess.run(W), "Bias =", sess.run(b))

    print("\nOptimization Finished!\n")
    training_loss = sess.run(loss_function, feed_dict={X: train_X, Y: train_Y})
    print("Final Training Loss=", training_loss, "Weight =",
          sess.run(W), "Bias =", sess.run(b), '\n')

    plt.plot(train_X, train_Y, 'o', label='Training Data')
    plt.plot(train_X, sess.run(W) * train_X +
             sess.run(b), label='Fitted Model')
    plt.legend()
    plt.show()<jupyter_output>Start Training...
Epoch: 0100 Loss = 1139.10852 Weight = 63.144855 Bias = 31.647346
Epoch: 0200 Loss = 973.91211 Weight = 59.949665 Bias = 44.089058
Epoch: 0300 Loss = 849.69495 Weight = 57.178802 Bias = 54.877728
Epoch: 0400 Loss = 756.27802 Weight = 54.775845 Bias = 64.23461
Epoch: 0500 Loss = 686.03540 Weight = 52.691944 Bias = 72.34834
Epoch: 0600 Loss = 633.21497 Weight = 50.88486 Bias = 79.38475
Epoch: 0700 Loss = 593.50104 Weight = 49.317856 Bias = 85.486046
Epoch: 0800 Loss = 563.61975 Weight = 47.958443 Bias = 90.78071
Epoch: 0900 Loss = 541.17523 Weight = 46.780212 Bias = 95.36736
Epoch: 1000 Loss = 524.27625 Weight = 45.757576 Bias = 99.350555

Optimization Finished!

Final Training Loss= 524.27625 Weight = 45.757576 Bias = 99.350555
{'fraction_non_alphanumeric': 0.11121741210236785, 'fraction_numerical': 0.0808419038507534, 'mean_word_length': 4.307106598984771, 'pattern_counts': {'":': 0, '<': 16, '<?xml version=': 0, '>': 16, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '4', 'dir': 'jupyter-structured-clean-dedup', 'id': '490274', 'license': 'MIT', 'n_tokens_mistral': 1639, 'n_tokens_neox': 1363, 'n_words': 435, 'path': 'TensorFlow/02_Linear_Regression.ipynb', 'repo_name': 'tasbirul/Deep-Learning-With-Keras', 'size': '4181'}