当前位置: 代码迷 >> 综合 >> TensorFlow 异常:AttributeError: module 'tensorflow' has no attribute 'mul'
  详细解决方案

TensorFlow 异常:AttributeError: module 'tensorflow' has no attribute 'mul'

热度:37   发布时间:2023-12-17 21:50:54.0

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

执行代码:

import tensorflow as tfsess = tf.Session()a = tf.placeholder("float")
b = tf.placeholder("float")
c = tf.constant(6.0)
d = tf.mul(a, b)
y = tf.mul(d, c)
print(sess.run(y, feed_dict={a: 3, b: 3}))A = [[1.1,2.3],[3.4,4.1]]
Y = tf.matrix_inverse(A)
print(sess.run(Y))
sess.close()

异常信息:

File "F:/demo/learning_tensorflow/1.py", line 8, in <module>d = tf.mul(a, b)
AttributeError: module 'tensorflow' has no attribute 'mul'

原因:TensorFlow的“mul”函数变成“multiply”函数了。TensorFlow版本不同,使用的函数不同。

修改过后:

import tensorflow as tfsess = tf.Session()a = tf.placeholder("float")
b = tf.placeholder("float")
c = tf.constant(6.0)
d = tf.multiply(a, b)
y = tf.multiply(d, c)
print(sess.run(y, feed_dict={a: 3, b: 3}))A = [[1.1,2.3],[3.4,4.1]]
Y = tf.matrix_inverse(A)
print(sess.run(Y))
sess.close()

运行结果:

54.0
[[-1.2386707   0.69486403][ 1.0271904  -0.33232632]]Process finished with exit code 0
tf.mul函数已经被换成了tf.multiply后运行正常。ok,已经解决异常。
  相关解决方案