tensorflow查看电脑的CPU和GPU

1、查看电脑GPU和CPU

import os
from tensorflow.python.client import device_lib
os.environ["TF_CPP_MIN_LOG_LEVEL"] = "99"
 
if __name__ == "__main__":
    print(device_lib.list_local_devices())

tensorflow查看电脑的CPU和GPU

2.指定CPU或GPU进行计算

    #使用CPU进行计算
    with tf.device("/cpu:0"):
        a = tf.constant([1.0,2.0,3.0,4.0,5.0,6.0],shape=[2,3])
        b = tf.constant([1.0,2.0,3.0,4.0,5.0,6.0],shape=[3,2])
        c = tf.matmul(a,b)
        #查看计算时硬件的使用情况
        sess = tf.Session(config=tf.ConfigProto(log_device_placement=True))
        print(sess.run(c))

3.查看tensor详细情况

        #设置运行时候的参数
        options = tf.RunOptions(output_partition_graphs=True)
        metadata = tf.RunMetadata()
        c_val = sess.run(c,options=options,run_metadata=metadata)
        print(metadata.partition_graphs)
        #关闭session
        sess.close()