You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

TensorFlow 1.2.1执行tf.random.normal时出现AttributeError报错求助

Fixing AttributeError: module 'tensorflow' has no attribute 'random' in TF 1.2.1

Hey there, I know that error can be frustrating—let's break down what's happening and how to fix it quickly.

Why this error occurs

You're using TensorFlow 1.2.1, which is an older TensorFlow 1.x release. The tf.random.normal() API you're trying to use was introduced in TensorFlow 2.x, so it doesn't exist in TF 1.2.1 at all. That's exactly why you're seeing the AttributeError.

Solution 1: Use TF 1.x's equivalent function (no upgrade needed)

In TensorFlow 1.x, the equivalent function for generating random normal values is tf.random_normal(). Just replace your line of code with this:

c = tf.random_normal(shape=(3,4), dtype=tf.float32)

This function works almost exactly like tf.random.normal()—it accepts the same shape, dtype, mean, stddev parameters you might want to use later.

TF 1.2.1 is extremely outdated (released back in 2017), and it lacks tons of bug fixes, performance improvements, and new features. Upgrading to a newer version will not only fix this issue but also prevent future compatibility problems.

  • To upgrade to the latest stable TensorFlow 2.x version:
    pip install --upgrade tensorflow
    
  • If you need to stay on TensorFlow 1.x for compatibility with other code, upgrade to the final stable TF 1.x release (1.15.x):
    pip install tensorflow==1.15.5
    

Once upgraded, your original tf.random.normal(shape=(3,4), dtype=tf.float32) code will work as expected.


内容的提问来源于stack exchange,提问作者Altair21

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.08 23:57:32