docker环境下安装tensorflow-阿里云开发者社区

开发者社区> 人工智能> 正文
登录阅读全文

docker环境下安装tensorflow

简介: 下载tensorflow 镜像并运行 [root@Ieat1 ~]# docker run -d --name tensorflow -it -p 8888:8888 tensorflow/tensorflow ff716bcb8642e258eb7...

下载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

tf1.png

新建notebook


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

看到运行成功


tf3.png

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

版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。

分享: