While working on a machine learning project using TensorFlow, I encountered an error that said “Module ‘tensorflow’ has no attribute ‘truncated_normal’“.This error typically occurs when you’re using code written for an older version of TensorFlow in a newer environment.
The issue stems from the fact that TensorFlow 2.x reorganized many functions that were available directly in the tf namespace in TensorFlow 1.x. This can be frustrating, especially when you’re following tutorials or using code that was written for TensorFlow 1.x.
In this article, I’ll walk you through several solutions to fix this error and get your code running again. Let’s get started..!
Understand the Error
When you see the error “Module ‘tensorflow’ has no attribute ‘truncated_normal'”, it means you’re trying to use the tf.truncated_normal function that existed in TensorFlow 1.x but has been moved or renamed in TensorFlow 2.x.
In older versions of TensorFlow, you could initialize weights using:
# TensorFlow 1.x code
weights = tf.truncated_normal([784, 10], stddev=0.1)But in TensorFlow 2.x, this function has been moved to different modules.
Read Module ‘tensorflow’ has no attribute ‘get_variable’
Method 1: Use tf.random.truncated_normal()
The simplest solution is to use the new location of the function in TensorFlow 2.x:
import tensorflow as tf
# Create a tensor with truncated normal distribution
weights = tf.random.truncated_normal(shape=[784, 10], mean=0.0, stddev=0.1)
print(weights)I executed the above example code and added the screenshot below.

This approach uses the current API and is the recommended way to generate truncated normal distributions in TensorFlow 2.x.
Check out AttributeError: Module ‘tensorflow’ has no attribute ‘variable_scope’
Method 2: Use tf.compat.v1 for Backward Compatibility
If you’re working with legacy code and don’t want to update all instances of tf.truncated_normal you can use TensorFlow’s compatibility module:
import tensorflow as tf
# Disable eager execution for TensorFlow 1.x behavior
tf.compat.v1.disable_eager_execution()
# Use the compatibility API
weights = tf.compat.v1.truncated_normal([784, 10], stddev=0.1)
# Create a session to evaluate the tensor
with tf.compat.v1.Session() as sess:
print(sess.run(weights))I executed the above example code and added the screenshot below.

This approach is particularly useful when transitioning large codebases from TensorFlow 1.x to 2.x, as mentioned in the Python Guides article on this error.
Read AttributeError: Module ‘keras.optimizers’ has no attribute ‘sgd’
Method 3: Use TruncatedNormal Initializer
If you’re working with Keras layers or other parts of TensorFlow that require initializers, use the TruncatedNormal initializer:
import tensorflow as tf
# Create a Dense layer with TruncatedNormal kernel initializer
layer = tf.keras.layers.Dense(
units=10,
input_shape=(784,),
kernel_initializer=tf.keras.initializers.TruncatedNormal(mean=0.0, stddev=0.1)
)
# Create a simple Sequential model with the layer
model = tf.keras.Sequential([layer])
# Pass dummy data through the model to initialize weights
dummy_input = tf.random.uniform(shape=(1, 784)) # batch size of 1
output = model(dummy_input)
# Print initialized weights of the Dense layer
print("Initialized weights (kernel):")
print(layer.kernel.numpy())
print("\nInitialized bias:")
print(layer.bias.numpy())
print("\nModel output for dummy input:")
print(output.numpy())I executed the above example code and added the screenshot below.

This approach is ideal when building neural networks using the Keras API in TensorFlow 2.x.
Check out AttributeError: Module ‘keras.optimizers’ has no attribute ‘rmsprop’
Method 4: Use tf.initializers.TruncatedNormal()
Another approach is to use the initializers module directly:
import tensorflow as tf
# Create a truncated normal initializer
initializer = tf.initializers.TruncatedNormal(mean=0.0, stddev=0.1)
# Use the initializer to create a tensor
weights = initializer(shape=[784, 10])
print(weights)As noted in the Python Guides article on this AttributeError, this is a clean approach for TensorFlow 2.x code.
Real-World Example: Image Classification for US Traffic Signs
Let’s see a more practical example where we might use truncated normal initialization in a real project. Imagine we’re building an image classifier for US traffic signs to be used in self-driving cars:
import tensorflow as tf
import numpy as np
# Load US traffic signs dataset (placeholder for actual data loading)
# In a real project, you'd load actual data here
input_shape = (32, 32, 3) # 32x32 RGB images
num_classes = 43 # Number of different US traffic signs
# Create a CNN model with truncated normal initialization
model = tf.keras.Sequential([
tf.keras.layers.Conv2D(
32, (3, 3),
activation='relu',
input_shape=input_shape,
kernel_initializer=tf.keras.initializers.TruncatedNormal(stddev=0.1)
),
tf.keras.layers.MaxPooling2D((2, 2)),
tf.keras.layers.Conv2D(
64, (3, 3),
activation='relu',
kernel_initializer=tf.keras.initializers.TruncatedNormal(stddev=0.1)
),
tf.keras.layers.MaxPooling2D((2, 2)),
tf.keras.layers.Flatten(),
tf.keras.layers.Dense(
128,
activation='relu',
kernel_initializer=tf.keras.initializers.TruncatedNormal(stddev=0.1)
),
tf.keras.layers.Dense(
num_classes,
kernel_initializer=tf.keras.initializers.TruncatedNormal(stddev=0.1)
)
])
model.compile(
optimizer='adam',
loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True),
metrics=['accuracy']
)
# Model summary
model.summary()In this example, we’re using truncated normal initialization for the weights of all layers in our convolutional neural network, which helps prevent the vanishing/exploding gradient problem during training.
Read AttributeError: Module ‘tensorflow’ has no attribute ‘dimension’
Choose the Right Approach
When deciding which method to use, consider:
- New Projects: For new projects, always use the TensorFlow 2.x API (
tf.random.truncated_normalortf.keras.initializers.TruncatedNormal). - Legacy Code: For existing projects with a lot of TensorFlow 1.x code, using
tf.compat.v1might be easier temporarily. - Keras Models: When building Keras models, use the initializers API for cleaner code.
Troubleshoot Common Issues
If you’re still encountering issues, check these common Issues:
- TensorFlow Version: Verify your TensorFlow version with:
import tensorflow as tf
print(tf.__version__)- Import Conflicts: Ensure you don’t have multiple TensorFlow versions installed or imported.
- Virtual Environment: Consider creating a fresh virtual environment if you have conflicting package versions.
I hope this article helped you understand and fix the “Module ‘tensorflow’ has no attribute ‘truncated_normal'” error. TensorFlow’s API changes between versions can be challenging, but with the approaches outlined above, you should be able to adapt your code to work with the latest versions.
Remember that staying up-to-date with the current TensorFlow API is the best long-term solution, but compatibility modules can help during transition periods.
You may like to read:
- AttributeError: module ‘tensorflow.keras.layers’ has no attribute ‘multiheadattention’
- Solve the ModuleNotFoundError: no module named ‘tensorflow_hub’
- ModuleNotFoundError: No module named ‘tensorflow.contrib’

I am Bijay Kumar, a Microsoft MVP in SharePoint. Apart from SharePoint, I started working on Python, Machine learning, and artificial intelligence for the last 5 years. During this time I got expertise in various Python libraries also like Tkinter, Pandas, NumPy, Turtle, Django, Matplotlib, Tensorflow, Scipy, Scikit-Learn, etc… for various clients in the United States, Canada, the United Kingdom, Australia, New Zealand, etc. Check out my profile.