最近看到一份不错的深度学习资源 --Stanford 中的 CS20SI:《TensorFlow for Deep Learning Research》, 正好跟着学习一下 TensorFlow 的基础, 还是收获颇丰, 随手整理成博客随时翻阅.
为什么选择 TensorFlow?
自从 12 年 AlexNet 获得 ImageNet 大赛的冠军后, 深度学习开始流行起来, 也因为硬件的快速发展 GPU 并行计算配合易用的 API, 让深度学习以及神经网络大放光彩.
深度学习的框架其实有很多, 目前来说最火的还要数 Pytorch,TensorFlow 以及 keras. 其中 Pytorch 比较适合学术研究, 自己搞着玩, 如果工业实践就不太适合了. TensorFlow 由于时间比较久, 学起来比较困难, 不过有完整的开发, 部署方案, 还有大量的 GitHub 项目可供参考. Keras 则是 TensorFlow 的一个高级 API, 同类的还有 TensorFlow 的 TFLearn 等等.
总结来说, 如果你是学生, 只是为了论文或者学习, 那么推荐 Pytorch; 如果你是公司的开发者, 想要在业务中使用深度学习, 推荐直接使用 TensorFlow, 如果使用最新的 1.12, 那么官方的示例里面就已经是 Keras 了; 如果你是从 GitHub 上面下载了源码想要学习, 那就得去学习对应版本的 TensorFlow API 了.
在总结一下 Tensoflow 的优点:
易用性: 有对应 Python 的 API
可移植性: 一套代码就可以适应单个或者多个 CPU,GPU, 移动设备等
灵活性: 可以部署在树莓派, 安卓, Windows,iOS,Linux 等上
可视化: 有 tensorboard 提供开发的可视化界面, 方便跟踪调参
检查点: 可以通过检查点记录保存实验数据
自动微积分: 自动求解梯度
庞大的社区: 一年内拥有 10000 + 的开发者, 3000 + 的项目
大量基于 TensorFlow 的项目代码
使用 TensorFlow 的公司包括: Google,OpenAI,DeepMind,SnapChat,Airbus,eBay 等
基于 TensorFlow 可以做很多事情, 比如图像 CV, 自然语言处理 NLP, 语音识别等等.
基础知识
1. 简化的 API
下面就来学习下 TensorFlow 的基础知识, TensorFlow 不仅提供了基础的语法, 还提供了一些简化的 API:
TF Learn,tf.contrib.learn, 基于 scikit-learn 风格的 API
TF Slim,tf.contrib.slim, 轻量级的 tf 构建 API, 可以自动配置默认值, 简化使用
Keras, 更高级更抽象的 API, 使用 Keras 之后, 就像叠积木一样创建模型, 不过对于背后的原理隐藏的太深太深...
2. 数据流图
如果做过大数据或者接触过 java8 的流计算, 对这种数据流图应该比较了解. 就是我们在程序执行前, 先构建好计算的流程框架, 然后执行的时候现去读取数据分配资源执行计算. 这样一方面把构建与计算分离, 另一方面也可以代码自己做更深的优化.
比如上面的数据流图中, 事先定义好整个网络的结构, 然后计算的时候直接传入 5 和 3, 就能得到结果 23 了.
3. Tensor 张量
张量, 不是张亮, 更不是麻辣烫, 它是一种高维数据的统称. 比如:
0 维的张量, 我们也叫做标量 scalar 或者数字,
1 维的张量, 叫做向量 vector
2 维的张量, 叫做矩阵 matrix
因此 TensorFlow, 可以理解为 Tensor+Flow, 即张量的数据流.
4. 数据流图的例子
- import tensorflow as tf
- # 第一个例子, 计算两个数的加法
- a = tf.constant(2)
- b = tf.constant(3)
- x = tf.add(a, b)
- with tf.Session() as sess:
- print(sess.run(x))
在上面的代码中, 就构建了一个最基本的数据流图的计算例子.
其中
- a = tf.constant(2)
- b = tf.constant(3)
- x = tf.add(a, b)
就是在构建图. 而想要拿到 x 的值, 就必须新建一个 session(这个时候才会分配资源), 执行 run 方法 (这个时候才会执行).
5. tensorboard 的使用
为了方便查看构建图, 需要学会怎么使用 TensorBoard. 在上面的代码中, 只需要增加 Tensorboard 的声明即可:
- import tensorflow as tf
- # 第一个例子, 计算两个数的加法
- a = tf.constant(2)
- b = tf.constant(3)
- x = tf.add(a, b)
- with tf.Session() as sess:
- writer = tf.summary.FileWriter('./graphs', sess.graph)
- print(sess.run(x))
- writer.close()
然后在命令行中输入
tensorboard --logdir=/Users/xingoo/PycharmProjects/xxx/graphs
登录 localhost:6006 就可以看到下面的内容.
可以看到左边有描述每个节点的意思, 点击 add 后, 可以到关于 add 节点的描述. 由于构建的图很简单, 就是两个数相加, 因此整个图只有三个圈圈. 并且按照默认的操作进行了命名.
6. 更复杂点的例子
增加以下图的复杂度, 并且同时对两个结果计算:
- import tensorflow as tf
- # tf.constant(value, dtype=None, shape=None, name='Const', verify_shape=False)
- a = tf.constant([1, 3], name="a")
- b = tf.constant([[0, 1], [2, 3]], name="b")
- x = tf.add(a, b, name="add")
- y = tf.multiply(a, b, name="mul")
- with tf.Session() as sess:
- writer = tf.summary.FileWriter('./graphs', sess.graph)
- x, y = sess.run([x, y])
- print(x)
- print(y)
- writer.close()
由于 x,y 是独立运算没有什么交集, 因此在图中, 他们是独立的两个操作.
7. 关于图
回头再来看看 tensorFlow 中的图到底是什么呢? 当使用第 5 部分中的代码构建 graph 时, 可以直接输出 graph 的定义:
- import tensorflow as tf
- a = tf.constant(2)
- b = tf.constant(3)
- x = tf.add(a, b)
- with tf.Session() as sess:
- print(sess.graph.as_graph_def())
得到如下的内容:
- node {
- name: "Const"
- op: "Const"
- attr {
- key: "dtype"
- value {
- type: DT_INT32
- }
- }
- attr {
- key: "value"
- value {
- tensor {
- dtype: DT_INT32
- tensor_shape {
- }
- int_val: 2
- }
- }
- }
- }
- node {
- name: "Const_1"
- op: "Const"
- attr {
- key: "dtype"
- value {
- type: DT_INT32
- }
- }
- attr {
- key: "value"
- value {
- tensor {
- dtype: DT_INT32
- tensor_shape {
- }
- int_val: 3
- }
- }
- }
- }
- node {
- name: "Add"
- op: "Add"
- input: "Const"
- input: "Const_1"
- attr {
- key: "T"
- value {
- type: DT_INT32
- }
- }
- }
- versions {
- producer: 26
- }
每个 node 基本都包含下面你的内容:
{
name: 我们自己起的名字, 如果没有则是 op + 自增的数,
op: 操作
attr: 类型
attr: 值 {
形状, 初始值
}
}
通过上面的 JSON, 就能完美的组合出 web 中看到的图了.
至于 graph 到底怎么用, 就看后面一节课的内容吧.
参考
CS 20SI: Tensorflow for Deep Learning Research https://web.stanford.edu/class/cs20si/2017/
来源: https://www.cnblogs.com/xing901022/p/10164264.html