October DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsClean PCRecommendedOne scan can reveal what keeps slowing WindowsLook for cleanup and repair opportunities.Run ScanOctober DealsAmazon USDeal season is back - check today's better picksAmazon US: current deals, useful picks and tech finds.See Picks×
Skip to content
World desk3 min

How to Fix “Module ‘tensorflow’ Has No Attribute ‘truncated_normal’” Error

TensorFlow 2 moved truncated-normal generation under tf.random. Choose the tensor, Keras initializer, or compatibility API that matches what your old code is doing.
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

In TensorFlow 2, replace tf.truncated_normal(...) with tf.random.truncated_normal(...) when you need a random tensor. If the call sets a Keras layer’s weights, use tf.keras.initializers.TruncatedNormal instead. The old name belongs to TensorFlow 1-era code; changing to eager or graph mode is not the first fix.

Why does TensorFlow have no attribute truncated_normal?

TensorFlow 2 exposes truncated-normal tensor generation at tf.random.truncated_normal, rather than the old top-level tf.truncated_normal path. The official API reference lists tf.compat.v1.truncated_normal and tf.compat.v1.random.truncated_normal as compatibility aliases. TensorFlow’s API reference documents the current function and aliases.

The error usually means code written for an older TensorFlow API is running with a newer version. Confirm the version in the same interpreter or notebook kernel that produced the traceback:

import tensorflow as tf
print(tf.__version__)

Replace the call according to what it does

Generating a random tensor

Use tf.random.truncated_normal and carry over the old arguments—especially stddev, since the function defaults to 1.0:

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
weights = tf.random.truncated_normal(
    shape=[784, 10],
    mean=0.0,
    stddev=0.1,
)

The API accepts shape, mean, stddev, dtype, seed, and name. Its values follow a normal distribution, but samples more than two standard deviations from the mean are discarded and redrawn. See the function’s documented arguments and behavior.

Initializing a Keras layer’s weights

If the old call was supplied as a layer’s weight initializer, use the Keras initializer API rather than generating a tensor directly:

Rank #2
Sale
Hands-On Machine Learning with Scikit-Learn, Keras, and TensorFlow: Concepts, Tools, and Techniques to Build Intelligent Systems
  • Use scikit-learn to track an example ML project end to end
  • Explore several models, including support vector machines, decision trees, random forests, and ensemble methods
  • Exploit unsupervised learning techniques such as dimensionality reduction, clustering, and anomaly detection
  • Dive into neural net architectures, including convolutional nets, recurrent nets, generative adversarial networks, autoencoders, diffusion models, and transformers
  • Use TensorFlow and Keras to build and train neural nets for computer vision, natural language processing, generative models, and deep reinforcement learning
layer = tf.keras.layers.Dense(
    10,
    kernel_initializer=tf.keras.initializers.TruncatedNormal(
        mean=0.0,
        stddev=0.1,
    ),
)

This expresses an initialization strategy for the layer’s kernel. The initializer takes the mean and standard deviation; set them to the intended values from the old code. The error-specific guide shows this Keras use case.

Keeping legacy graph or session code temporarily

For code that still relies on TensorFlow 1-style graph/session conventions, the compatibility alias is available:

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
values = tf.compat.v1.truncated_normal(
    shape=[784, 10],
    mean=0.0,
    stddev=0.1,
)

Use this as a transition option when the surrounding program still needs legacy conventions, not as evidence that the whole program has been migrated. For new or modernized code, prefer the native TensorFlow 2 path.

What if changing the API path does not fix the error?

  1. Check the active environment. Print tf.__version__ in the process or notebook kernel that failed; a terminal and notebook may be using different Python environments.
  2. Check which package was imported. Look for a project file or folder named tensorflow that could shadow the installed package, and verify that the program is using the environment where TensorFlow was installed.
  3. Read the traceback’s origin. If the failing call is inside a third-party Keras or backend library rather than your own code, check that dependency’s compatibility with the installed TensorFlow version. The right remedy depends on the actual versions and traceback; do not downgrade blindly.
  4. For many TensorFlow 1 symbols, use the migration tool and review its output. TensorFlow’s TF 1.x migration guide describes tf_upgrade_v2 for automated symbol rewrites. Run it as one part of a migration, then inspect the report, update remaining code, and test behavior: the tool cannot convert every API or guarantee behavioral compatibility.
Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Should you disable eager execution?

Not to solve this missing attribute by itself. The direct TensorFlow 2 API is tf.random.truncated_normal; changing execution mode does not restore the old top-level name. Eager-execution changes are relevant only when the broader legacy program specifically depends on graph/session semantics. An API rename also does not guarantee that other TensorFlow 1-era calls in the same program will work unchanged.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

Leave a Reply

Your email address will not be published. Required fields are marked *

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

More from the Wire

  1. World desk4 min
    How to Spot an AI Voice Scam Before Sending MoneyDon’t rely on how a caller sounds. Pause, call back through a known number, and verify the emergency with another trusted person before sending money.
  2. Mountain View desk4 min
    Google’s SynthID Detector: How to Check AI-Generated Images, Video and AudioGoogle’s SynthID Detector looks for an embedded watermark in supported images, video and audio. Here is what its results do—and do not—show.
  3. Redmond desk20 min
    How to create a link to File or Folder in Windows 11Windows 11 gives you several ways to point to a file or folder without moving or duplicating it. You can create a desktop shortcut,…
Recommended PC Tool
Recommended PC Tool
Windows Errors? Fix Them Before They SpreadFree repair scan
Outdated Drivers Are Slowing You DownFree scan - exact matches

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.