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.
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:
After training, the layer has learned one weight per feature plus one bias term.
Reading the Coefficients
Use get_weights() on the dense layer:
For a two-feature model, weights has shape (2, 1) and bias has shape (1,).
If you want the values in a flatter form:
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):
- '
weightsis(num_features, 1)' - '
biasis(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.
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:
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
- get_config missing while loading previously saved model without custom layers
- get_config missing while loading previously saved model without custom layers
- Get Gradients with Keras Tensorflow 2.0
- Get info of exposed models in Tensorflow Serving
- Get confidence interval from sklearn linear regression in python
- Get Keras model input from inside a custom callback
- Get column index from column name in python pandas
- Get column name based on condition in pandas
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack 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.