tensorflow

  1. 云栖社区>
  2. 博客>
  3. 正文

tensorflow

developerguy 2017-10-23 22:33:00 浏览1445
展开阅读全文

https://github.com/aymericdamien/TensorFlow-Examples.git

摘要: 利用Docker和阿里云容器服务轻松在本地和云端搭建Tensorflow的学习环境

tensorflow_jpeg

本系列将利用Docker和阿里云容器服务,帮助您上手TensorFlow的机器学习方案

机器学习作为人工智能重要的技术,已经在计算机视觉、自然语言处理、医学诊断等等领域得到了广泛的应用。TensorFlow 是谷歌推出的开源的分布式机器学习框架,它也是Github社区上最受关注的机器学习项目,目前点赞已经超过3万个星。

TensorFlow提供了多种安装方式,配置也相对简单,但是对于初学者而言,从零开始搭建一个TensorFlow学习环境依然具有一些挑战。幸运的是TensorFlow提供了基于Docker的部署方式,开发者可以快速上手。

本文是系列中的第一篇文章,会基于Docker快速创建一个Tensorflow学习环境。

准备Docker环境

为了利用Docker和Docker Compose编排搭建实验环境,我们需要

安装Docker for Mac/Windows 或在Linux上安装Docker和Docker Compose。可以使用阿里云提供Docker EngineDocker Toolbox的镜像网站

本地环境搭建

在GitHub上有很多Tensorflow的学习资料, 其中 https://github.com/aymericdamien/TensorFlow-Examples 是一个很好的教程。在文中提供了由浅入深的示例来介绍Tensorflow的功能。

首先执行如下命令获得教程代码 (包含对Tensorflow 1.0 的支持)

git clone https://github.com/denverdino/TensorFlow-Examples
cd TensorFlow-Examples

为了运行这个教程你需要安装Tensorflow的执行环境,并配置"jupyter", "tensorboard"来进行交互操作。

一个最简单的方法是在当前目录,创建如下的docker-compose.yml模板

version: '2'
services:
  jupyter:
    image: registry.cn-hangzhou.aliyuncs.com/denverdino/tensorflow:1.0.0
    container_name: jupyter
    ports:
      - "8888:8888"
    environment:
      - PASSWORD=tensorflow
    volumes:
      - "/tmp/tensorflow_logs"
      - "./notebooks:/root/notebooks"
    command:
      - "/run_jupyter.sh"
      - "/root/notebooks"
  tensorboard:
    image: registry.cn-hangzhou.aliyuncs.com/denverdino/tensorflow:1.0.0
    container_name: tensorboard
    ports:
      - "6006:6006"
    volumes_from:
      - jupyter
    command:
      - "tensorboard"
      - "--logdir"
      - "/tmp/tensorflow_logs"
      - "--host"
      - "0.0.0.0"

执行如下命令一键创建Tensorflow的学习环境

docker-compose up -d 

我们可以检查启动的Docker容器

yili@yili-mbp:~/work/TensorFlow-Examples$ docker-compose ps

              Name                            Command               State                Ports               
------------------------------------------------------------------------------------------------------------
tensorflowexamples_jupyter_1       /run_jupyter.sh /root/note ...   Up      6006/tcp, 0.0.0.0:8888->8888/tcp 
tensorflowexamples_tensorboard_1   tensorboard --logdir /tmp/ ...   Up      0.0.0.0:6006->6006/tcp, 8888/tcp 

可以直接通过 http://127.0.0.1:8888/ 从浏览器中访问Tensorflow的Jupyter交互实验环境

登录密码为: tensorflow

14738178907665

通过 http://127.0.0.1:6006 从浏览器中访问模型可视化工具TensorBoard
注:可以运行 http://127.0.0.1:8888/notebooks/4_Utils/tensorboard_basic.ipynb 来实验Tensorboard的功能,示例中Tensorboard容器配置的log目录是 “/tmp/tensorflow_logs”。对于用户自己的notebook,可以参照tensorboard_basic在代码中设置log的输出路径。

14738194651219

注:

  • 其中registry.cn-hangzhou.aliyuncs.com/denverdino/tensorflow:1.0.0是基于tensorflow/tensorflow:1.0.0镜像构建的,只添加了apt源和pipy源的阿里云镜像。 大家也可以参照https://github.com/denverdino/tensorflow-docker中的Dockerfile自己构建,预先添加自己所需的python库、算法库等资源。
  • 利用volumes机制,jupyter可以直接从当前notebooks目录获取示例。jupyter和tensorboard两个容器也通过可以文件卷来共享事件日志。

阿里云容器服务上体验

阿里云容器服务支持Docker Compose模板部署,通过下面模板我们可以轻松把Tensorflow的学习环境部署到云端

version: '2'
services:
  jupyter:
    image: registry.cn-hangzhou.aliyuncs.com/denverdino/tensorflow-examples:1.0.0
    volumes:
      - "/tmp/tensorflow_logs"
    environment:
      - PASSWORD=tensorflow
    labels: 
      aliyun.routing.port_8888: jupyter
    command:
      - "/run_jupyter.sh"
      - "/root/notebooks"
  tensorboard:
    image: registry.cn-hangzhou.aliyuncs.com/denverdino/tensorflow:1.0.0
    labels: 
      aliyun.routing.port_6006: tensorboard
    volumes_from:
      - jupyter
    command:
      - "tensorboard"
      - "--logdir"
      - "/tmp/tensorflow_logs"
      - "--host"
      - "0.0.0.0"

注:

  • 利用aliyun.routing标签,我们可以轻松定义Jupyter和TensorBoard的访问访问端点
  • 如果是老集群,需要点击容器服务agent升级来提供所需特性和稳定性增强。

几分钟之后,我们就可以在云端有一个学习环境来体验Tensorflow。

14738212612862

14738177187646

14738178032537

14738178506257

总结

我们可以利用Docker和阿里云容器服务轻松在本地和云端搭建Tensorflow的学习环境。Docker作为一个标准化的软件交付手段,可以大大简化应用软件的部署和运维复杂度。阿里云容器服务支持以Docker Compose的方式进行容器编排,并提供了众多扩展,可以方便地支持基于容器的微服务应用的云端部署和管理。

阿里云容器服务还会和高性能计算(HPC)团队一起配合,之后在阿里云上提供结合GPU加速和Docker集群管理的机器学习解决方案,在云端提升机器学习的效能。

想了解更多容器服务内容,请访问 https://www.aliyun.com/product/containerservice

https://yq.aliyun.com/articles/60601

 

 

 

python convolutional.py
Extracting data/train-images-idx3-ubyte.gz
Extracting data/train-labels-idx1-ubyte.gz
Extracting data/t10k-images-idx3-ubyte.gz
Extracting data/t10k-labels-idx1-ubyte.gz
I tensorflow/core/common_runtime/local_device.cc:25] Local device intra op parallelism threads: 1
I tensorflow/core/common_runtime/local_session.cc:45] Local session inter op parallelism threads: 1
Initialized!
Epoch 0.00
Minibatch loss: 12.053, learning rate: 0.010000
Minibatch error: 90.6%
W tensorflow/core/kernels/bias_op.cc:42] Resource exhausted: OOM when allocating tensor with shapedim { size: 5000 } dim { size: 28 } dim { size: 28 } dim { size: 32 }
W tensorflow/core/common_runtime/executor.cc:1027] 0x591ac80 Compute status: Resource exhausted: OOM when allocating tensor with shapedim { size: 5000 } dim { size: 28 } dim { size: 28 } dim { size: 32 }
[[Node: BiasAdd_2 = BiasAdd[T=DT_FLOAT, _device="/job:localhost/replica:0/task:0/cpu:0"](Conv2D_2, Variable_1)]]
Traceback (most recent call last):
File "convolutional.py", line 270, in <module>
tf.app.run()
File "/usr/lib/python2.7/site-packages/tensorflow/python/platform/default/_app.py", line 11, in run
sys.exit(main(sys.argv))
File "convolutional.py", line 258, in main
validation_prediction.eval(), validation_labels)
File "/usr/lib/python2.7/site-packages/tensorflow/python/framework/ops.py", line 405, in eval
return _eval_using_default_session(self, feed_dict, self.graph, session)
File "/usr/lib/python2.7/site-packages/tensorflow/python/framework/ops.py", line 2728, in _eval_using_default_session
return session.run(tensors, feed_dict)
File "/usr/lib/python2.7/site-packages/tensorflow/python/client/session.py", line 345, in run
results = self._do_run(target_list, unique_fetch_targets, feed_dict_string)
File "/usr/lib/python2.7/site-packages/tensorflow/python/client/session.py", line 419, in _do_run
e.code)
tensorflow.python.framework.errors.ResourceExhaustedError: OOM when allocating tensor with shapedim { size: 5000 } dim { size: 28 } dim { size: 28 } dim { size: 32 }
[[Node: BiasAdd_2 = BiasAdd[T=DT_FLOAT, _device="/job:localhost/replica:0/task:0/cpu:0"](Conv2D_2, Variable_1)]]
Caused by op u'BiasAdd_2', defined at:
File "convolutional.py", line 270, in <module>
tf.app.run()
File "/usr/lib/python2.7/site-packages/tensorflow/python/platform/default/_app.py", line 11, in run
sys.exit(main(sys.argv))
File "convolutional.py", line 229, in main
validation_prediction = tf.nn.softmax(model(validation_data_node))
File "convolutional.py", line 169, in model
relu = tf.nn.relu(tf.nn.bias_add(conv, conv1_biases))
File "/usr/lib/python2.7/site-packages/tensorflow/python/ops/nn_ops.py", line 101, in bias_add
return gen_nn_ops._bias_add(value, bias, name=name)
File "/usr/lib/python2.7/site-packages/tensorflow/python/ops/gen_nn_ops.py", line 163, in _bias_add
return _op_def_lib.apply_op("BiasAdd", value=value, bias=bias, name=name)
File "/usr/lib/python2.7/site-packages/tensorflow/python/ops/op_def_library.py", line 633, in apply_op
op_def=op_def)
File "/usr/lib/python2.7/site-packages/tensorflow/python/framework/ops.py", line 1710, in create_op
original_op=self._default_original_op, op_def=op_def)
File "/usr/lib/python2.7/site-packages/tensorflow/python/framework/ops.py", line 988, in __init__
self._traceback = _extract_stack()

网友评论

登录后评论
0/500
评论
developerguy
+ 关注