Python tensorflow:AttributeError: 'module' 对象没有属性 'mul'

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/42217059/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 21:24:16  来源:igfitidea点击:

tensorflow:AttributeError: 'module' object has no attribute 'mul'

pythontensorflow

提问by DanDan

I have used tensorflow for ONE day,but there comes some troubles,when I import tensorflow, there would be AttributeError: 'module' object has no attribute 'XXXXXX'

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

Environment

环境

I use ubuntu14.04, python2.7, CUDA toolkit 8.0 and CuDNN v5. And versions of my six and protobuf are: Name: six Version: 1.10.0 Location: /usr/local/lib/python2.7/dist-packages Requires: Name: protobuf Version: 3.2.0 Location: /usr/local/lib/python2.7/dist-packages Requires: six, setuptools

我使用 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

here is my test code:

这是我的测试代码:

import tensorflow as tf
a = tf.placeholder(tf.int16)
b = tf.placeholder(tf.int16)
add = tf.add(a, b)
mul = tf.mul(a, b)
with tf.Session() as sess:
    # Run every operation with variable input
    print "Addition with variables: %i" % sess.run(add, feed_dict={a: 2, b: 3})
    print "Multiplication with variables: %i" % sess.run(mul, feed_dict={a: 2, b: 3})

I get this output:

我得到这个输出:

enter image description here

在此处输入图片说明

Is there any problem with the tensorflow installation? or any other problems?

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

回答by Meuu

According to the tensorflow 1.0.0 release notes,

根据tensorflow 1.0.0 发行说明

tf.mul, tf.suband tf.negare deprecated in favor of tf.multiply, tf.subtractand tf.negative.

tf.mul,tf.subtf.neg不赞成使用tf.multiply,tf.subtracttf.negative

You'll need to replace tf.mulwith tf.multiply.

您需要替换tf.multf.multiply.

回答by Salvador Dali

This operation was previously available in 0.x versions. With the release of TF 1.0 they introduced breaking changes to the API. In addition to

此操作以前在 0.x 版本中可用。随着TF 1.0发布,他们对 API 进行了重大更改。此外

tf.mul, tf.suband tf.negare deprecated in favor of tf.multiply, tf.subtractand tf.negative

tf.mul,tf.sub并被tf.neg弃用而支持tf.multiply, tf.subtracttf.negative

many other functions were renamed and changed with the following justification:

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

Several python API calls have been changed to resemble NumPy more closely.

几个 Python API 调用已更改为更类似于 NumPy。

So a lot of the scripts that you already found on the web or from the books will not work. Good thing is that majority of them can be fixed with their migration script. It can be run with tf_upgrade.py --infile foo.py --outfile foo-upgraded.py. It will not be able to solve everything (limitations are listed here), but will save you a lot of work.

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

回答by Tensorflow Support

2.0 Compatible Answer:

2.0 兼容答案

The Commands for tf.multiply, if we want to migrate from Tensorflow 1.x to 2.x are shown below:

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

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

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

The Commands for tf.subtract, if we want to migrate from Tensorflow 1.x to 2.x are shown below:

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

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

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

The Commands for tf.negative, if we want to migrate from Tensorflow 1.x to 2.x are shown below:

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

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

See this Tensorflow Migration Guidefor more details.

有关更多详细信息,请参阅此Tensorflow 迁移指南

回答by charlie

In python-3 use tf.multiplyinstead of tf.mul.

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