概述:基于Docker的TensorFlow机器学习框架搭建和实例源码解读,TensorFlow作为最火热的机器学习框架之一,Docker是的容器,可以很好的结合起来,为机器学习或者科研人员提供便捷的机器学习开发环境,探索人工智能的奥秘,容器随开随用方便快捷。源码解析TensorFlow容器创建和示例程序运行,为热爱机器学者降低学习难度。
默认机器已经装好了Docker(Docker安装和使用可以看我另一篇博文:Ubuntu16.04安装Docker1.12+开发实例+hello world+web应用容器)。
1.下载TensorFlow镜像
docker pull tensorflow/tensorflow
#或者
#sudo docker pull tensorflow/tensorflow
2.创建TensorFlow容器,源码解读
docker run
--name xiaolei
-tensortflow -it -p 8888:
8888 -v ~/tensorflow:/notebooks/
data tensorflow/tensorflow
docker run运行镜像, --name为容器创建别名, -it保留命令行运行, -p 8888:8888将本地的8888端口http://localhost:8888/映射, -v ~/tensorflow:/notebooks/data 将本地的~/tensorflow文件夹挂载到新建容器的/notebooks/data下(这样创建的文件可以保存到本地~/tensorflow) tensorflow/tensorflow为指定的镜像,默认标签为latest(即tensorflow/tensorflow:latest)
3.开启TensorFlow容器
3.1.可以直接从命令行中右键打开连接,或者在浏览器中输入localhost:8888,然后将命令行中的token粘贴上去。
4.开始TensorFlow编程(Python语言)
4.1.在首页可以New一个Python项目
4.2.tensorflow示例源码解读
from __future__ import print_function
import tensorflow
as tf
with tf.Session():
input1 = tf.
constant([
1.0,
1.0,
1.0,
1.0])
input2 = tf.
constant([
2.0,
2.0,
2.0,
2.0])
output = tf.
add(input1, input2)
result = output.eval()
print(
"result: ",
result)
4.3.运行程序,输出的结果为(运行成功)
result:
[ 3. 3. 3. 3.]
5.其他 linux,TensorFlow,Docker相关操作
5.1.关闭TensorFlow和开启TensorFlow环境
#关闭tensorflow容器
docker stop xiaolei-tensortflow
#开启TensorFlow容器
docker start xiaolei-tensortflow
#浏览器中输入 http://localhost:8888/
5.2.解决文件的读写权限
ls
-l
sudo chown -R xiaolei tensorflow/
sudo chgrp -R xiaolei tensorflow/
相关资源:基于docker安装tensorflow的完整步骤