在线Latex公式
本节包含两个内容:
基本概念介绍(CS专业或者硬件玩家基本都了解,这里就不多写了)
读取数据很容易成为瓶颈
这块发展相对迅速,每年都会有不同的框架出现,这个视频是17年的:
主流的还是PyTorch和TensorFlow。框架的好处在于:
(1) Easily build big computational graphs
(2) Easily compute gradients in computational graphs
(3) Run it all efficiently on GPU (wrap cuDNN, cuBLAS, etc)
如果不用框架,直接用numpy实现dl:
Running example: Train a two-layer ReLU network on random data with L2 loss
大概步骤分两步
上面红框分三段,这三段都只是定义计算图,并没有进行实际的计算。
第一段:Create placeholders for input x, weights w1 and w2, and targets y
第二段:Forward pass: compute prediction for y and loss (L2 distance between y and y_pred)
第三段:Tell TensorFlow to compute loss of gradient with respect to w1 and w2.
蓝色框values这句是用数据填充placeholders:
Create numpy arrays that will fill in the placeholders above.
后面两句话:
Run the graph: feed in the numpy arrays for x, y, w1, and w2; get numpy arrays for loss, grad_w1, and grad_w2
如果要训练:Run the graph over and over, use gradient to update weights
老师给的例子里面权重等参数很少,如果很多,在运行GPU版本的时候会有CPU和GPU之间拷贝数据的性能瓶颈,所以给出了改进建议,就是把权重放到计算图里面,包括学习率的更新。
Change w1 and w2 from placeholder (fed on each call) to Variable (persists in the graph between calls)
Add assign operations to update w1 and w2 as part of the graph!
最后:
这里还是有一个问题,就是权重计算后从gpu拷贝到cpu又有性能瓶颈,这里老师用了一个技巧,就是把计算后权重看做个dummy node,让TensorFlow来维护。
最后的代码:
问题:为什么X/Y没有放入计算图?
答:因为使用mini batch的话,每次丢进网络中计算的数据是变化的,所以没有必要放入。
还可以调用更加高层次的函数来进一步简化代码,例如关于初始化:
Use Xavier initializer
还可以使用更加高层次的接口,例如Keras、TFLearn。
课上还提到了:
Tensorboard: Add logging to code to record loss, stats, etc Run server and get pretty graphs!
Distributed Version: Split one graph over multiple machines!
三个抽象层与TensorFlow的对比:
几个要点:
To run on GPU, just cast tensors to a cuda datatype!
PyTorch 的高级接口只有一个叫nn
可以利用modules自定义网络构架
Modules can contain modules,上图中不需要定义反向传播autograd可以自动搞定。
A DataLoader wraps a Dataset and provides minibatching, shuffling, multithreading, for you When you need to load custom data, just write your own Dataset class.这里的DataLoader 在加载数据的时候划分batch会自动帮你搞定随机乱序,很方便。
使用pretrain的模型超方便,只需要一行代码,就会帮你后台下载相关数据和权重
可视化工具看上去比TensorFlow的好看,但是17年的版本还不支持计算图:
With static graphs, framework can optimize the graph for you before it runs!
静态图可以预先做优化!
序列化好的静态计算图可以存储下来,也许可以拿给不同语言上运行,也就是模型可以更好的复用。
动态计算图在运行的时候才确定计算图,所以语言不能切换(因为图的构架是这个语言写的)
动态图简洁性好些,静态图要预先定义好计算图,所以所有的操作符,例如if,都要写到计算图中(看红箭头),而动态图直接用的Python代码。
这里补一个深度之眼学员的优秀回答:
● Core written in C++
● Has Python and MATLAB bindings
● Good for training or finetuning
● feedforward classification models
● Often no need to write code!
● Not used as much in research anymore, still popular for deploying models
总而言之,这个玩意工业应用较多,研究用的少,因为它不依赖Python。
No need to write code!
● Very new - released a week ago =)
● Static graphs, somewhat similar to TensorFlow
● Core written in C++
● Nice Python interface
● Can train model in Python, then serialize and deploy without Python
● Works on iOS / Android, etc
TensorFlow is a safe bet for most projects. Not perfect but has huge community, wide usage. Maybe pair with high-level wrapper (Keras, Sonnet, etc)
I think PyTorch is best for research. However still new, there can be rough patches.
Use TensorFlow for one graph over many machines
Consider Caffe, Caffe2, or TensorFlow for production deployment
Consider TensorFlow or Caffe2 for mobile