TensorFlow
Linear Regression
Machine Learning
Coefficients
Data Science

Get coefficients of a linear regression in Tensorflow

ML System Design practice on Codemia

Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.

Practice ML system design

Introduction

In TensorFlow, the coefficients of a linear regression model are just the learned weights and bias of a dense layer with no activation. Once the model is trained, you read those parameters from the layer, which gives you the slope terms for each feature and the intercept term.

Linear Regression as a Dense Layer

A linear regression with n input features can be written as:

y = XW + b

In TensorFlow or Keras, that is equivalent to a Dense(1) layer with no activation:

python
1import numpy as np
2import tensorflow as tf
3from tensorflow import keras
4
5X = np.array([
6    [1.0, 2.0],
7    [2.0, 1.0],
8    [3.0, 4.0],
9    [4.0, 3.0]
10], dtype="float32")
11
12y = np.array([8.0, 7.0, 15.0, 14.0], dtype="float32")
13
14model = keras.Sequential([
15    keras.layers.Input(shape=(2,)),
16    keras.layers.Dense(1)
17])
18
19model.compile(optimizer=keras.optimizers.SGD(learning_rate=0.01), loss="mse")
20model.fit(X, y, epochs=500, verbose=0)

After training, the layer has learned one weight per feature plus one bias term.

Reading the Coefficients

Use get_weights() on the dense layer:

python
1weights, bias = model.layers[0].get_weights()
2
3print("Weights:")
4print(weights)
5print("Bias:")
6print(bias)

For a two-feature model, weights has shape (2, 1) and bias has shape (1,).

If you want the values in a flatter form:

python
1coefficients = weights[:, 0]
2intercept = bias[0]
3
4print("Coefficients:", coefficients)
5print("Intercept:", intercept)

Those are the learned regression coefficients.

Interpreting the Shapes

This is where many people get confused. In Keras:

  • each input feature gets one weight
  • each output unit gets its own bias

So for Dense(1):

  • 'weights is (num_features, 1)'
  • 'bias is (1,)'

If you used Dense(3), you would no longer have a simple single-target regression. You would have three outputs and therefore three separate sets of coefficients.

A Full Example with Prediction

The following example shows training, coefficient extraction, and a manual prediction using the learned values.

python
1import numpy as np
2import tensorflow as tf
3from tensorflow import keras
4
5X = np.array([
6    [1.0, 1.0],
7    [2.0, 0.0],
8    [3.0, 1.0],
9    [4.0, 2.0]
10], dtype="float32")
11
12y = np.array([5.0, 7.0, 11.0, 15.0], dtype="float32")
13
14model = keras.Sequential([
15    keras.layers.Input(shape=(2,)),
16    keras.layers.Dense(1)
17])
18
19model.compile(optimizer=keras.optimizers.Adam(0.05), loss="mse")
20model.fit(X, y, epochs=400, verbose=0)
21
22weights, bias = model.layers[0].get_weights()
23w = weights[:, 0]
24b = bias[0]
25
26sample = np.array([5.0, 1.0], dtype="float32")
27manual_prediction = np.dot(sample, w) + b
28model_prediction = model.predict(sample.reshape(1, -1), verbose=0)[0, 0]
29
30print("w =", w)
31print("b =", b)
32print("manual =", manual_prediction)
33print("model =", model_prediction)

The manual and model predictions should be very close, which confirms that the extracted values are the actual coefficients used by the network.

Accessing Variables Directly

You can also inspect the TensorFlow variables directly:

python
1kernel = model.layers[0].kernel
2bias_var = model.layers[0].bias
3
4print(kernel.numpy())
5print(bias_var.numpy())

This is useful if you want the parameters as tensors rather than plain NumPy arrays.

Notes on Feature Scaling

If you normalized or standardized the features before training, the coefficients correspond to the transformed feature space, not the original raw units. That is not wrong, but it changes how you interpret the numbers.

For example, a coefficient on a z-scored feature reflects the effect of a one-standard-deviation change, not a one-unit raw change. If interpretability matters, track the preprocessing step carefully.

Common Pitfalls

  • Looking at the model predictions and expecting TensorFlow to print the coefficients automatically. The coefficients live in the layer weights, not in the output samples.
  • Forgetting that Dense(1) returns weights shaped (num_features, 1). Flatten or slice the array before treating it as a simple coefficient vector.
  • Misinterpreting the bias as another feature coefficient. The bias is the intercept term, not a slope attached to an input column.
  • Reading coefficients after only a few epochs and assuming the values are final. Poor convergence produces misleading parameters.
  • Interpreting coefficients in raw feature units when the model was trained on scaled inputs. Scaling changes coefficient meaning.

Summary

  • In TensorFlow linear regression, the coefficients are the dense layer weights and bias.
  • After training, call get_weights() on the output layer to read them.
  • For Dense(1), the weight matrix shape is (num_features, 1) and the bias shape is (1,).
  • You can verify the coefficients by computing a manual prediction with XW + b.
  • Always account for preprocessing, especially feature scaling, before interpreting the numbers.

Related reading
Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

ML System Design practice on Codemia

Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.

Practice ML system design

All Rights Reserved.