TensorFlow
Version 2.1.0
AttributeError
random_normal issue
Software bug

TensorFlow 2.1.0 has no attribute 'random_normal'

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

The has no attribute random_normal error in TensorFlow 2.x happens because API names changed from older TensorFlow versions. Code written for TensorFlow 1.x often calls symbols that were moved or removed in 2.x. The fix is to use the updated API namespace or compatibility module intentionally.

Why the Error Occurs

In TensorFlow 1.x, many random functions were available under top-level names such as tf.random_normal. In TensorFlow 2.x, random generation moved under tf.random.

Old style:

python
# TensorFlow 1.x style
# x = tf.random_normal([2, 3])

TensorFlow 2.x equivalent:

python
1import tensorflow as tf
2
3x = tf.random.normal([2, 3], mean=0.0, stddev=1.0)
4print(x)

Migration Pattern for Legacy Code

If you are migrating an old project, replace deprecated symbols in a controlled sweep.

python
1import tensorflow as tf
2
3# old: tf.random_normal(shape)
4def make_noise(shape):
5    return tf.random.normal(shape)

Do not mix random APIs from multiple eras unless migration boundaries are explicit.

Compatibility Module Option

For short-term migration, tf.compat.v1 can keep legacy code running.

python
1import tensorflow as tf
2
3x = tf.compat.v1.random_normal([2, 3])
4print(x)

This is useful for temporary stabilization, but long-term code should adopt native TensorFlow 2 APIs.

Reproducibility with Seeds

When replacing random APIs, also review seeding strategy.

python
1import tensorflow as tf
2
3tf.random.set_seed(42)
4a = tf.random.normal([2, 2])
5b = tf.random.normal([2, 2])
6print(a)
7print(b)

Stable seeding is essential for comparable training experiments.

Validate Installed Version and Docs

Confirm runtime version before debugging API errors.

python
import tensorflow as tf
print(tf.__version__)

Then consult matching docs for that version. Many errors come from reading examples for a different major release.

Refactor Checklist

  1. Replace deprecated symbols with TensorFlow 2 equivalents.
  2. Remove obsolete graph-mode assumptions where possible.
  3. Verify random seed behavior after migration.
  4. Add tests for shape and dtype expectations.
  5. Remove compatibility imports once migration is complete.

A checklist-based migration avoids piecemeal fixes.

Mapping Old and New Random APIs

A quick mapping helps large migrations:

  • tf.random_normal to tf.random.normal
  • tf.random_uniform to tf.random.uniform
  • seed handling through tf.random.set_seed
python
1import tensorflow as tf
2
3tf.random.set_seed(123)
4a = tf.random.normal([2, 2], dtype=tf.float32)
5b = tf.random.uniform([2, 2], minval=0.0, maxval=1.0)
6print(a)
7print(b)

Using explicit dtype and shape helps keep migration changes deterministic and reviewable.

Migration Test Example

Add tests that verify output shapes and dtypes rather than exact random values.

python
1def test_random_tensor_shape_and_dtype():
2    import tensorflow as tf
3    x = tf.random.normal([4, 3], dtype=tf.float32)
4    assert x.shape == (4, 3)
5    assert x.dtype == tf.float32

These checks protect against accidental API misuse during refactors.

Long-Term Cleanup

After migration stabilizes, remove compat.v1 calls incrementally. Keep one lint or search rule in CI to prevent reintroduction of deprecated symbols. This keeps your TensorFlow codebase aligned with current APIs and easier to maintain over time.

Notebook and Script Consistency

Migration bugs often appear when notebooks and scripts use different TensorFlow versions. Print version at startup in both contexts and pin dependencies in one lock file. Consistent environments reduce confusing API errors and make random behavior easier to compare across experiments.

Code Search Cleanup

Run a repository-wide search for deprecated TensorFlow random APIs after migration and replace remaining occurrences. A one-time cleanup script prevents future runtime surprises.

Common Pitfalls

  • Copying TensorFlow 1.x snippets directly into TensorFlow 2.x code.
  • Using compatibility APIs indefinitely without migration plan.
  • Forgetting to validate random behavior after API replacement.
  • Mixing eager and graph assumptions in migrated code.
  • Debugging against examples from mismatched TensorFlow versions.

Summary

  • tf.random_normal is not a native TensorFlow 2.x API.
  • Use tf.random.normal for modern TensorFlow code.
  • Use tf.compat.v1 only as a temporary migration bridge.
  • Re-check seeds and reproducibility after migration.
  • Align code examples with your installed TensorFlow version.

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.