在使用测试代码如下:
import tensorflow.compat.v1 as tf
tf.disable_v2_behavior()
a = tf.placeholder(tf.int16)
b = tf.placeholder(tf.int16)
add = tf.add(a, b)
mul = tf.multiply(a, b)
config = tf.ConfigProto()
config.gpu_options.allow_growth = True
with tf.Session(config=config) as sess:
print(sess.run(add, feed_dict={a: 3, b: 4}))
print(sess.run(mul, feed_dict={a: 3, b: 4}))
print(sess.run([add, mul], feed_dict={a: 3, b: 4}))
报错
AttributeErrorTraceback (most recent call last) <ipython-input-2-099bddc6c1d7> in <module> 1 import tensorflow as tf 2 ----> 3 a = tf.placeholder(tf.int16) 4 b = tf.placeholder(tf.int16) 5 add = tf.add(a, b) AttributeError: module 'tensorflow' has no attribute 'placeholder'
查看tf版本。
我的是因为在tf2下使用了tf1的API。
解决方式:
使用
import tensorflow.compat.v1 as tf
tf.disable_v2_behavior()
替换后的结果
import tensorflow.compat.v1 as tf
tf.disable_v2_behavior()
a = tf.placeholder(tf.int16)
b = tf.placeholder(tf.int16)
add = tf.add(a, b)
mul = tf.multiply(a, b)
config = tf.ConfigProto()
config.gpu_options.allow_growth = True
with tf.Session(config=config) as sess:
print(sess.run(add, feed_dict={a: 3, b: 4}))
print(sess.run(mul, feed_dict={a: 3, b: 4}))
print(sess.run([add, mul], feed_dict={a: 3, b: 4}))