1. 程式人生 > >TensorFlow(1):使用docker鏡像搭建TensorFlow環境

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

根據 free nts 安裝配置 wiki 永久 ebo 關於 exec

1,關於TensorFlow


TensorFlow 隨著AlphaGo的勝利也火了起來。
google又一次成為大家膜拜的大神了。google大神在引導這機器學習的方向。
同時docker 也是一個非常好的工具,大大的方便了開發環境的構建,之前需要配置安裝。
看各種文檔,現在只要一個 pull 一個 run 就可以把環境弄好了。
同時如果有寫地方需要個性化定制,直接在docker的鏡像上面再加一層補丁就好了。
自己的需求就能滿足了,同時還可以將這個通用的方法分享出去。

2,下載TensorFlow images


使用hub.docker.com的鏡像

docker pull tensorflow/tensorflow:latest
  • 1

使用daocloud 的鏡像,在國內用速度還是挺快的,如果docker.io的鏡像慢,可以用daocloud的。
這個速度非常的快。一樣用的。版本也挺新的。

docker pull daocloud.io/daocloud/tensorflow:latest 
  • 1

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
  • 1
  • 2

啟動的時候並不是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
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

打開瀏覽器就可以直接看到界面了。
技術分享圖片

同時可以編輯內容:
技術分享圖片

寫第一個 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())
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

其他的使用參考中文手冊:
https://github.com/jikexueyuanwiki/tensorflow-zh
裏面有pdf 可以下載使用。

還有一個超級炫酷吊炸天的playground :
http://playground.tensorflow.org/
技術分享圖片

5,打個補丁


vi run_jupyter.sh

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

然後重新打一個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"]
  • 1
  • 2
  • 3
  • 4

這樣就固定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
  • 1
  • 2

技術分享圖片

然後就可以 -d 參數,將docker 運行放到後臺。然後就可以使用 docker exec -it xxx bash 登錄進去查看系統的狀況了。

4,總結


本文的原文連接是: http://blog.csdn.net/freewebsys/article/details/70237003 未經博主允許不得轉載。
博主地址是:http://blog.csdn.net/freewebsys

docker 真的是非常好的技術,能夠快速的搭建好環境,省去了繁瑣的安裝配置過程。
最後使用參數將環境跑起來,同時也可以根據自己的需求,給鏡像增加新的功能,就像是蓋房子。
一層一層的蓋。所有的層,構成了一個整體的房子。
同時對於 TensorFlow 來說是一個程序員必須的技能了。就像是 lucence一樣,其實大家都不太了解那個索引算法的。
但是還是可以創建出一個索引分詞來。
TensorFlow 也是一樣的。當做一個工具來使用就好了,具體的算法也不太精通。
有一個說法,數據量上去了,用大數據優化,比算法優化要效果好。

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