tensorflow環境搭建
2019.03.03 13:31:58字數 337閱讀 187
anaconda
首先安裝anaconda,官網下載,直接安裝,可參考網上教程
python虛擬環境創建
conda create -n tensorflow python=3.7 ipykernel
執行上述命令創建名字爲tensorflow的環境,指定該環境python版本爲3.7;安裝ipykernel包,便於後續jupyter的使用;
conda env list
查看所有環境,帶*表示當前激活環境,默認爲base環境
conda activate tensorflow
激活tensorflow環境
conda deactivate tensorflow
關閉tensorflow環境,回到base環境
tensorflow安裝
conda search ‘*tensorflow*’
conda search ‘*tensorflow-gpu*’
搜索tensorflow版本
conda install -n tensorflow tensorflow-gpu=1.12.0
指定爲tensorflow環境安裝1.12.0的gpu版本的tensorflow
import tensorflow as tf
hello = tf.constant('Hello, TensorFlow!')
sess = tf.Session()
print sess.run(hello)
驗證是否安裝成功
jupyter notebook使用
jupyter notebook
啓動jupyter notebook,點擊新建,選擇kernel(上面conda create時安裝ipykernel會自動創建和該虛擬環境關聯的kernel),即可創建並在頁面上運行python代碼。若kernel不是指定環境的,會無法import該環境的module。
若創建環境時忘記安裝ipykernel,可在激活環境後安裝
conda activate tensorflow
conda install ipykernel
python -m ipykernel install --name tensorflow
最後一行爲創建名爲tersorflow的kernel,重啓jupyter notebook後即可看見新建的kernel。