ChatGPT解决这个技术问题 Extra ChatGPT

tensorflow:AttributeError:“模块”对象没有属性“mul”

我用了一天的tensorflow,但是遇到了一些麻烦,当我导入tensorflow时,会出现AttributeError: 'module' object has no attribute 'XXXXXX'

环境

我使用 ubuntu14.04、python2.7、CUDA 工具包 8.0 和 CuDNN v5。我的六和protobuf的版本是:名称:六版本:1.10.0位置:/usr/local/lib/python2.7/dist-packages要求:名称:protobuf版本:3.2.0位置:/usr/local/ lib/python2.7/dist-packages 需要:六、setuptools

这是我的测试代码:

将张量流导入为 tf a = tf.placeholder(tf.int16) b = tf.placeholder(tf.int16) add = tf.add(a, b) mul = tf.mul(a, b) 和 tf.Session() as sess: # 使用变量输入运行每个操作 print "Addition with variables: %i" % sess.run(add, feed_dict={a: 2, b: 3}) print "Multiplication with variables: %i" % sess.运行(mul,feed_dict={a:2,b:3})

我得到这个输出:

https://i.stack.imgur.com/LCHw1.png

tensorflow安装有什么问题吗?或任何其他问题?


M
Meuu

根据tensorflow 1.0.0 release notes

不推荐使用 tf.mul、tf.sub 和 tf.neg,取而代之的是 tf.multiply、tf.subtract 和 tf.negative。

您需要将 tf.mul 替换为 tf.multiply


文档说“已弃用”,但实际上 TF 只是删除了它们......
是的。它们没有被弃用,而是被完全删除。感谢您的回答,为我节省了大量时间!
S
Salvador Dali

此操作以前在 0.x 版本中可用。使用 release of TF 1.0 they introduced breaking changes to the API。此外

不推荐使用 tf.mul、tf.sub 和 tf.neg,取而代之的是 tf.multiply、tf.subtract 和 tf.negative

许多其他功能被重命名和更改,理由如下:

几个 python API 调用已更改为更接近 NumPy。

因此,您已经在网络上或从书中找到的许多脚本将无法正常工作。好消息是它们中的大多数都可以通过迁移来修复script。它可以与 tf_upgrade.py --infile foo.py --outfile foo-upgraded.py 一起运行。它不能解决所有问题(限制列于 here),但会为您节省大量工作。


T
Tensorflow Support

2.0 兼容答案:

如果我们想从 Tensorflow 1.x 迁移到 2.x,tf.multiply 的命令如下所示:

tf.compat.v1.math.multiply, tf.compat.v1.multiply, tf.compat.v2.math.multiply, tf.compat.v2.multiply

如果我们想从 Tensorflow 1.x 迁移到 2.x,tf.subtract 的命令如下所示:

tf.compat.v1.math.subtract, tf.compat.v1.subtract, tf.compat.v2.math.subtract, tf.compat.v2.subtract

如果我们想从 Tensorflow 1.x 迁移到 2.x,tf.negative 的命令如下所示:

tf.compat.v1.math.negative, tf.compat.v1.negative, tf.compat.v2.math.negative, 
tf.compat.v2.negative

有关详细信息,请参阅此 Tensorflow Migration Guide


B
Blue

在 python-3 中使用 tf.multiply 而不是 tf.mul


这个答案增加了什么,原始问题没有涵盖?