查看Tensorflow是GPU还是CPU可在终端输入以下代码
import tensorflow as tf
a = tf.constant([1.0, 2.0, 3.0, 4.0, 5.0, 6.0], shape=[2, 3], name='a')
b = tf.constant([1.0, 2.0, 3.0, 4.0, 5.0, 6.0], shape=[3, 2], name='b')
c = tf.matmul(a, b)
sess = tf.Session(config=tf.ConfigProto(log_device_placement=True))
print (sess.run(c))
报错AttributeError: module 'tensorflow' has no attribute 'Session'。这其实不是安装错误,是因为在新的Tensorflow 2.0版本中已经移除了Session这一模块,改换运行代码
tf.compat.v1.Session()
tf.compat.v1.ConfigProto
更改之后运行结果如下
(这个结果是不支持gpu加速)
其他问题记录
批量解决 在tf2下使用了tf1的API
import tensorflow as tf
print(tf.__path__)
查看tf版本。
解决方式:使用
import tensorflow.compat.v1 as tf
tf.disable_v2_behavior()
代替
import tensorflow as tf