Implementing Linear Regression with TensorFlow (v1 Compatibility)

Ensure Matplotlib renders inline in a Jupyter notebook. Import NumPy and TensorFlow, using compatibility mode for v1 placeholders. Disable TensorFlow v2 behaviors.

%matplotlib inline
import numpy as np
import tensorflow.compat.v1 as tf
tf.disable_v2_behavior()
import matplotlib.pyplot as plt

plt.rcParams["figure.figsize"] = (14, 8)

Generate 100 evenly spaced x-values from -3 to 3. Create corresponding y-values as sin(x) plus uniform noise in the range [-0.5, 0.5]. Visualize the synthetic dataset.

n_observations = 100
xs = np.linspace(-3, 3, n_observations)
ys = np.sin(xs) + np.random.uniform(-0.5, 0.5, n_observations)
plt.scatter(xs, ys)
plt.show()

Define TensorFlow placeholders for input features and target values. These will be fed actual data during training.

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

Initialize the model parameters: weight (W) and bias (b), using random normal initialization.

W = tf.Variable(tf.random.normal([1]), name='weight')
b = tf.Variable(tf.random.normal([1]), name='bias')

Build the linear model to predict Y: Y_pred = X * W + b.

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

Define the loss function as the squared error between predicted and actual values.

loss = tf.square(Y - Y_pred, name='loss')

Configure the optimizer: use gradient descent with a learning rate of 0.01 to minimiez the loss.

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

Set the number of training epochs. Execute the computation graph within a TensorFlow session.

n_samples = xs.shape[0]
with tf.Session() as sess:
    sess.run(tf.global_variables_initializer())
    writer = tf.summary.FileWriter('./graphs/linear_reg', sess.graph)
    
    for epoch in range(50):
        total_loss = 0
        for x_val, y_val in zip(xs, ys):
            _, current_loss = sess.run([optimizer, loss], feed_dict={X: x_val, Y: y_val})
            total_loss += current_loss
        if epoch % 5 == 0:
            print('Epoch {0}: average loss = {1}'.format(epoch, total_loss / n_samples))
    
    writer.close()
    
    trained_W, trained_b = sess.run([W, b])

Display the learned weight and bias values.

print(trained_W, trained_b)
print("W:" + str(trained_W[0]))
print("b:" + str(trained_b[0]))

Plot the original data points together with the fitted regression line.

plt.plot(xs, ys, 'bo', label='Real data')
plt.plot(xs, xs * trained_W + trained_b, 'r', label='Predicted data')
plt.legend()
plt.show()

Tags: TensorFlow Linear Regression Machine Learning Gradient Descent

Posted on Tue, 30 Jun 2026 17:02:00 +0000 by Mikedean