tensorflow中gpu和cpu切換

兩者同時存在時使用gpu
要想使用cpu,在代碼裏添加如下命令:

matrix1=tf.constant([[3.,3.]])
matrix2=tf.constant([[2.],[2.]])
with tf.Session() as ses:
    with tf.device("/cpu:0"):
		product=tf.matmul(matrix1,matrix2)      

說明一下:
“/cpu:0” The CPU of your machine
“/gpu:0” The GPU of yout machine ,if you have one
如果要換成gpu的話,照着改可以了

os.environ[“CUDA_VISIBLE_DEVICES”] = "-1"
一行即可

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章