天天看點

TensorFlow(1):使用Docker鏡像搭建TensorFlow環境

​​TensorFlow(1):使用Docker鏡像搭建TensorFlow環境​​

1,關于TensorFlow

TensorFlow 随着AlphaGo的勝利也火了起來。 

google又一次成為大家膜拜的大神了。google大神在引導這機器學習的方向。 

同時docker 也是一個非常好的工具,大大的友善了開發環境的建構,之前需要配置安裝。 

看各種文檔,現在隻要一個 pull 一個 run 就可以把環境弄好了。 

同時如果有寫地方需要個性化定制,直接在docker的鏡像上面再加一層更新檔就好了。 

自己的需求就能滿足了,同時還可以将這個通用的方法分享出去。

2,下載下傳TensorFlow images

使用hub.docker.com的鏡像

docker pull tensorflow/tensorflow:latest      

使用daocloud 的鏡像,在國内用速度還是挺快的,如果docker.io的鏡像慢,可以用daocloud的。 

這個速度非常的快。一樣用的。版本也挺新的。

docker pull daocloud.io/daocloud/tensorflow:latest      

3,啟動鏡像

啟動指令,設定端口,同時配置volume 資料卷,用于永久儲存資料。加上 –rm 在停止的時候删除鏡像。

sudo mkdir -p /data/tensorflow/notebooks
docker run -it --rm --name myts -v /data/tensorflow/notebooks:/notebooks -p 8888:8888 daocloud.io/daocloud/tensorflow:latest      

啟動的時候并不是daemon 模式的,而是前台模式,同時顯示了運作的日志。

W 06:48:13.425 NotebookApp] WARNING: The notebook server is listening on all IP addresses and not using encryption. This is not recommended. [I 06:48:13.432 NotebookApp] Serving notebooks from local directory: /notebooks [I 06:48:13.432 NotebookApp] 0 active kernels [I 06:48:13.432 NotebookApp] The Jupyter Notebook is running at: http://[all ip addresses on your system]:8888/?token=2031705799dc7a5d58bc51b1f406d8771f0fdf3086b95642 [I 06:48:13.433 NotebookApp] Use Control-C to stop this server and shut down all kernels (twice to skip confirmation). [C 06:48:13.433 NotebookApp] Copy/paste this URL into your browser when you connect for the first time, to login with a token: http://localhost:8888/?token=2031705799dc7a5d58bc51b1f406d8771f0fdf3086b95642      

打開浏覽器就可以直接看到界面了。 

同時可以編輯内容: 

寫第一個 hello world: 

import tensorflow as tf
a = tf.constant(10)
b = tf.constant(32)

with tf.Session():
    c = tf.add(a,b)
    print(c)
    print(c.eval())      

其他的使用參考中文手冊: 

​​

​​

​​ ​​

裡面有pdf 可以下載下傳使用。

還有一個超級炫酷吊炸天的playground : 

​​

​​

​​ ​​

5,打個更新檔

vi run_jupyter.sh

#!/usr/bin/env bash
jupyter notebook --no-browser --NotebookApp.token='token1234' > /notebooks/jupyter-notebook.log      

然後重新打一個docker鏡像。 

vi Dockerfile

FROM daocloud.io/daocloud/tensorflow:latest
RUN rm -f /run_jupyter.sh
COPY run_jupyter.sh /run_jupyter.sh
ENTRYPOINT ["/run_jupyter.sh"]      

這樣就固定token了。

docker build -t mytf:1.0 .
docker run -it --rm --name myts -v /data/tensorflow/notebooks:/notebooks -p 8888:8888 -d mytf:1.0      

然後就可以 -d 參數,将docker 運作放到背景。然後就可以使用 docker exec -it xxx bash 登入進去檢視系統的狀況了。

4,總結

docker 真的是非常好的技術,能夠快速的搭建好環境,省去了繁瑣的安裝配置過程。 

最後使用參數将環境跑起來,同時也可以根據自己的需求,給鏡像增加新的功能,就像是蓋房子。 

一層一層的蓋。所有的層,構成了一個整體的房子。 

同時對于 TensorFlow 來說是一個程式員必須的技能了。就像是 lucence一樣,其實大家都不太了解那個索引算法的。 

但是還是可以建立出一個索引分詞來。 

TensorFlow 也是一樣的。當做一個工具來使用就好了,具體的算法也不太精通。 

繼續閱讀