docker環境下安裝tensorflow

weixin_34146805發表於2018-08-28

下載tensorflow 映象並執行

[root@Ieat1 ~]# docker run -d  --name tensorflow -it -p 8888:8888 tensorflow/tensorflow
ff716bcb8642e258eb7007f3f0c6756a82998d2844df8b374df85c9faf1b0629

通過觀察發現新建的notebook都在容器的/notebooks目錄下,為了使notebook不丟失,我們可以把它放在宿主機的目錄上,比如/data/tensorflow/notebooks,啟動時指定卷
docker run -d --name tensorflow -v /data/tensorflow/notebooks:/notebooks -it -p 8888:8888 tensorflow/tensorflow

檢視docker日誌,發現提示我們訪問地址 http://127.0.0.1:8888/?token=061bdda51d27eaab82049d1eda42bd63381a4c4d33eaee67

[root@Ieat1 ~]# docker logs -f tensorflow
[I 06:11:01.349 NotebookApp] Writing notebook server cookie secret to /root/.local/share/jupyter/runtime/notebook_cookie_secret
[W 06:11:01.372 NotebookApp] WARNING: The notebook server is listening on all IP addresses and not using encryption. This is not recommended.
[I 06:11:01.383 NotebookApp] Serving notebooks from local directory: /notebooks
[I 06:11:01.383 NotebookApp] The Jupyter Notebook is running at:
[I 06:11:01.383 NotebookApp] http://(ff716bcb8642 or 127.0.0.1):8888/?token=061bdda51d27eaab82049d1eda42bd63381a4c4d33eaee67
[I 06:11:01.383 NotebookApp] Use Control-C to stop this server and shut down all kernels (twice to skip confirmation).
[C 06:11:01.383 NotebookApp] 
    
    Copy/paste this URL into your browser when you connect for the first time,
    to login with a token:
        http://(ff716bcb8642 or 127.0.0.1):8888/?token=061bdda51d27eaab82049d1eda42bd63381a4c4d33eaee67

訪問後看到 jupyter介面,我們可以線上編輯程式碼

jupyter介紹參考 https://www.jianshu.com/p/91365f343585

4097351-f7205b4a597fdb47.png
tf1.png

新建notebook


4097351-4255d975fe2c03b4.png
tf2.png

輸入示例程式碼點選Run執行

import tensorflow as tf
import numpy as np

# 使用 NumPy 生成假資料(phony data), 總共 100 個點.
x_data = np.float32(np.random.rand(2, 100)) # 隨機輸入
y_data = np.dot([0.100, 0.200], x_data) + 0.300

# 構造一個線性模型
# 
b = tf.Variable(tf.zeros([1]))
W = tf.Variable(tf.random_uniform([1, 2], -1.0, 1.0))
y = tf.matmul(W, x_data) + b

# 最小化方差
loss = tf.reduce_mean(tf.square(y - y_data))
optimizer = tf.train.GradientDescentOptimizer(0.5)
train = optimizer.minimize(loss)

# 初始化變數
init = tf.initialize_all_variables()

# 啟動圖 (graph)
sess = tf.Session()
sess.run(init)

# 擬合平面
for step in range(0, 201):
    sess.run(train)
    if step % 20 == 0:
        print step, sess.run(W), sess.run(b)

示例程式碼地址 http://www.tensorfly.cn/tfdoc/get_started/introduction.html

看到執行成功


4097351-a2a1449fb53fe442.png
tf3.png

參考 https://hub.docker.com/r/tensorflow/tensorflow/

相關文章