机器学习作为人工智能重要的技术,已经在计算机视觉、自然语言处理、医学诊断等等领域得到了广泛的应用。TensorFlow 是谷歌推出的开源的分布式机器学习框架,它也是Github社区上最受关注的机器学习项目,目前点赞已经超过3万个星。
TensorFlow提供了多种安装方式,配置也相对简单,但是对于初学者而言,从零开始搭建一个TensorFlow学习环境依然具有一些挑战。幸运的是TensorFlow提供了 基于Docker的部署方式,开发者可以快速上手。
本文是系列中的第一篇文章,会基于Docker快速创建一个Tensorflow学习环境。
为了利用Docker和Docker Compose编排搭建实验环境,我们需要
安装 Docker for Mac/Windows 或在Linux上安装 Docker和Docker Compose。可以使用阿里云提供 Docker Engine和 Docker Toolbox的镜像网站
在GitHub上有很多Tensorflow的学习资料, 其中 https://github.com/aymericdamien/TensorFlow-Examples 是一个很好的教程。在文中提供了由浅入深的示例来介绍Tensorflow的功能。
首先执行如下命令获得教程代码 (包含对Tensorflow 1.0 的支持)
登录后复制
git clone https://github.com/denverdino/TensorFlow-Examplescd TensorFlow-Examples1.2.
为了运行这个教程你需要安装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"1.2.3.4.5.6.7.8.9.10.11.12.13.14.15.16.17.18.19.20.21.22.23.24.25.26.27.28.
执行如下命令一键创建Tensorflow的学习环境
登录后复制
docker-compose up -d1.
我们可以检查启动的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/tcp1.2.3.4.5.6.
可以直接通过 http://127.0.0.1:8888/ 从浏览器中访问Tensorflow的Jupyter交互实验环境
登录密码为: tensorflow
通过 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的输出路径。
注:
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库、算法库等资源。阿里云容器服务支持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"1.2.3.4.5.6.7.8.9.10.11.12.13.14.15.16.17.18.19.20.21.22.23.24.25.
注:
aliyun.routing
标签,我们可以轻松定义Jupyter和TensorBoard的访问访问端点几分钟之后,我们就可以在云端有一个学习环境来体验Tensorflow。
我们可以利用Docker和阿里云容器服务轻松在本地和云端搭建Tensorflow的学习环境。Docker作为一个标准化的软件交付手段,可以大大简化应用软件的部署和运维复杂度。阿里云容器服务支持以Docker Compose的方式进行容器编排,并提供了众多扩展,可以方便地支持基于容器的微服务应用的云端部署和管理。
阿里云容器服务还会和高性能计算(HPC)团队一起配合,之后在阿里云上提供结合GPU加速和Docker集群管理的机器学习解决方案,在云端提升机器学习的效能。
登录后复制
python convolutional.pyExtracting data/train-images-idx3-ubyte.gzExtracting
data/train-labels-idx1-ubyte.gzExtracting data/t10k-images-idx3-ubyte
.gzExtracting data/t10k-labels-idx1-ubyte.gzI tensorflow/core/common_runtime/local_device
.cc:25] Local device intra op parallelism threads: 1I tensorflow/core/common_runtime/local_session
.cc:45] Local session inter op parallelism threads: 1Initialized!Epoch 0.00Minibatch loss: 12.053,
learning rate: 0.010000Minibatch 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()1.2.3.4.5.6.7.8.9.10.11.12.13.14.15.16.17.
18.19.20.21.22.23.24.25.26.27.28.29.30.31.32.33.34.35.36.37.38.39.40.41.42.43.44.45.46.47.48.49.50.
免责声明:本文系网络转载或改编,未找到原创作者,版权归原作者所有。如涉及版权,请联系删