tensorflow官方文档中的sub 和mul中的函数已经在API中改名了

本文分享了在学习TensorFlow过程中遇到的两个常见错误:AttributeError: module 'tensorflow' has no attribute 'sub' 和 AttributeError: module 'tensorflow' has no attribute 'mul'。作者通过调试发现了这些函数已被替换为新的函数名,并提供了修正后的代码。

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

在照着tensorflow 官方文档和极客学院中tensorflow中文文档学习tensorflow时,遇到下面的两个问题:

1)AttributeError: module 'tensorflow' has no attribute 'sub'

#进入一个交互式Tensorflow会话
import tensorflow as tf
sess = tf.InteractiveSession()
x = tf.Variable([1.0,2.0])
a = tf.constant([3.0,3.0])
#使用初始化器initalizer op的run()方法初始化'x'
x.initializer.run()
#增加一个减法sub op, 从'x'减去'a',运行减去op,输出结果
sub = tf.sub(x,a)
print(sub.eval())
# 任务完成,关闭回话
sess.close()

执行时报错:

Traceback (most recent call last):
  File "C:/PythonProj/tensorflow/first_tensorflow.py", line 43, in <module>
    sub = tf.sub(x,a)
AttributeError: module 'tensorflow' has no attribute 'sub'

经过在pycharm中tf.自动反显的信息,我发现原来这个sub函数已经被subtract代替了,换成tf.subtract(x,a) ,ok ,一切顺利!

2)AttributeError: module 'tensorflow' has no attribute 'mul'

input1 = tf.constant(3.0)
input2 = tf.constant(2.0)
input3 = tf.constant(5.0)

intermed = tf.add(input2,input3)
mul = tf.mul(input1,intermed)
with tf.Session() as sess:
    result = sess.run([mul,intermed])
    print(result)

报错信息为:

Traceback (most recent call last):
  File "C:/PythonProj/tensorflow/first_tensorflow.py", line 78, in <module>
    mul = tf.mul(input1,intermed)
AttributeError: module 'tensorflow' has no attribute 'mul'

同理,经过在pycharm中tf.反显信息的观察,我发现原来这个tf.mul函数已经被换成了tf.multiply了,修改后,ok!

 

转载于:https://www.cnblogs.com/jasmine-Jobs/p/7257070.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值