TensorFlow2.0 中出现AttributeError: 'module' object has no attribute 'XXX' 统一解决方案

本文解决TensorFlow2.0中因函数名更改导致的AttributeError,包括truncated_normal和GradientDescentOptimizer的使用调整。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一:AttributeError: ‘module’ object has no attribute ‘truncated_normal’

tensorflow2.0中修改了这个函数名,因此会报错。

正态分布这个函数truncated_normal()在C:\ProgramData\Anaconda3\envs\tensorflow\Lib\site-packages\tensorflow_core\python\ops\random_ops.py

打开这个文件后查看源代码,看到下面代码:函数废弃了!!!

@tf_export("random.truncated_normal",
           v1=["random.truncated_normal", "truncated_normal"])
@deprecation.deprecated_endpoints("truncated_normal")
def truncated_normal(shape,
                     mean=0.0,
                     stddev=1.0,
                     dtype=dtypes.float32,
                     seed=None,
                     name=None):

在TensorFlow2.0官方文档中找到解决方案,地址如下:点击查看官方文档
https://www.tensorflow.org/api_docs/python/tf/random/truncated_normal

解决方案

将tf.truncated_normal([image_size * image_size, num_labels]))修改为:

tf.compat.v1.random.truncated_normal([image_size * image_size, num_labels]))

不再报错!!!

二:module ‘tensorflow_core._api.v2.train’ has no attribute ‘gradientdescentoptimizer’

在TensorFlow2.0,Keras 嵌入到TensorFlow中,变成默认的API,函数调用也发生了变化~!

In TensorFlow 2.0, Keras became the default high-level API, and optimizer functions migrated from tf.keras.optimizers into separate API called tf.optimizers. They inherit from Keras class Optimizer. Relevant functions from tf.train aren’t included into TF 2.0. So to access GradientDescentOptimizer, call tf.optimizers.SGD。。

在TensorFlow2.0官方文档中找到解决方案,地址如下:点击查看官方文档
https://www.tensorflow.org/api_docs/python/tf/compat/v1/train/GradientDescentOptimizer

解决方案:

将optimizer = tf.train.GradientDescentOptimizer(0.5).minimize(loss)修改为:

optimizer = tf.compat.v1.train.GradientDescentOptimizer(0.5).minimize(loss)

不再报错!!!

三:总结

由于tensorflow2.0对很多函数做了修改,导致函数的所处的模块发生了改变,当遇到上述AttributeError时,记得打开官方文档,然后全局搜一下报错的函数。比如GradientDescentOptimizer函数,马上就能知道怎么使用这个函数了!!!

官网文档的地址如下: https://www.tensorflow.org/api_docs/python
在这里插入图片描述

引用\[1\]:在编写Python脚本过程中,你遇到了一个报错:AttributeError: 'module' object has no attribute 'core'。这个错误通常是由于命名冲突或者版本不兼容引起的。引用\[2\]:参照通用解决方法并没有解决你的报错,最后你发现自己新建的Python文件名字和内置的函数名冲突,修改了Python文件的名字后问题得到解决。引用\[3\]:在训练YOLO模型时,你遇到了AttributeError: module pynvml has no attribute _nvmlGetFunctionPointer的报错。后来发现这是由于numpy版本问题引起的,因为在NumPy 1.20中已经弃用了numpy.int,在NumPy 1.24中已经删除了。你可以通过重新安装numpy或者修改代码来解决这个问题。 综上所述,你遇到的AttributeError: module pynvml has no attribute _nvmlGetFunctionPointer的报错可能是由于numpy版本不兼容引起的。你可以尝试重新安装numpy或者修改代码来解决这个问题。 #### 引用[.reference_title] - *1* *2* [【Python 脚本报错】AttributeError:‘module‘ has no attribute ‘xxx‘的解决方法](https://blog.csdn.net/qq_35091353/article/details/115609471)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^koosearch_v1,239^v3^insert_chatgpt"}} ] [.reference_item] - *3* [AttributeError: module numpy has no attribute int .报错解决方案](https://blog.csdn.net/weixin_46669612/article/details/129624331)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^koosearch_v1,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值