Windows環境下 Anaconda中安裝Tensorflow,并且將Tensorflow配置到jupyter notebook中去。
下載:https://www.continuum.io/downloads,我用的是Python 3.5
下載完以后,安裝。
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free/conda config --set show_channel_urls yes
推薦第一行命令輸入兩次,以便把這個鏡像地址放在首位。
一般來說,我們推薦新建一個新的virtualenv,這樣可以預防同一個環境中的包太多,相互發生沖突。
conda create -n tensorflow python=3.5
既然是建議,那么忽視上邊這一句話,也是可以的。
activate tensorflow
激活后,我選擇安裝的是CPU版本,輸入:
pip install --ignore-installed --upgrade https://storage.googleapis.com/tensorflow/windows/cpu/tensorflow-0.12.0-cp35-cp35m-win_amd64.whl
安裝失敗的話多試幾次
source activate tensorflow# On Windows, remove the word 'source'install ipykernelpython -m ipykernel install --user --name tensorflow--display-name "Python (myenv)"
在激活狀態下,安裝ipykernel
,后邊那個展示名稱,自己可以隨便填。進入jupyter notebook ,運行一下測試文件,看是否可以使用。
如果不行,輸入jupyter --paths
檢查一下kernal是不是設置的有問題,比如我的就是C:\Users\wing\AppData\Roaming\jupyter\kernels\tensorflow
一般來說是環境所在的地址有問題,修改一下就可以啦。
通過這個方法,可以把自己手賤多加上去的kernel刪掉。jupyter kernal 刪除
更多建議閱讀官方文檔
我記得這個地方蠻復雜的,我當時往kernel里邊加入R內核,費了大力氣。當時還沒寫博客的習慣,現在又忘記怎么加的,真是日了狗。
import tensorflow as tftf.__version__>>>:'1.0.1'sess = tf.InteractiveSession()x = tf.Variable([1.0, 2.0])a = tf.constant([3.0, 3.0])x.initializer.run()# 使用初始化器 initializer op 的 run() 方法初始化 ‘x‘ sub = tf.sub(x, a)# 增加一個減法 sub op, 從 ‘x‘ 減去 ‘a‘. 運行減法 op, 輸出結果 sub.eval()>>>:array([-2., -1.], dtype=float32)
我碰到了一個module 'tensorflow' has no attribute 'sub'
,原因是tensorflow升級了之后,這個sub
函數被改成了subtract
,代碼改成sub = tf.subtract(x, a)
即可。