Cifar10Examples.md 3.06 KB
Newer Older
Chi Song's avatar
Chi Song committed
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
# CIFAR-10 样例

## 概述

[CIFAR-10](https://www.cs.toronto.edu/~kriz/cifar.html) 分类是机器学习中常用的基准问题。 CIFAR-10 数据集是图像的集合。 它也是机器学习领域最常用的数据集之一,包含 60000 万张 32x32 的图像,共有 10 个分类。 因此以 CIFAR-10 分类为例来介绍 NNI 的用法。

### **目标**

总所周知,模型 optimizer (优化器)的选择直接影响了最终指标的性能。 本教程的目标是**调优出性能更好的优化器**,从而为图像识别训练出一个相对较小的卷积网络(CNN)。

本例中,选择了以下常见的深度学习优化器:

> "SGD", "Adadelta", "Adagrad", "Adam", "Adamax"

### **实验**

#### 准备

此样例需要安装 PyTorch。 PyTorch 安装包需要选择所基于的 Python 和 CUDA 版本。

这是环境 python==3.5 且 cuda == 8.0 的样例,然后用下列命令来安装 [ PyTorch](https://pytorch.org/)

```bash
python3 -m pip install http://download.pytorch.org/whl/cu80/torch-0.4.1-cp35-cp35m-linux_x86_64.whl
python3 -m pip install torchvision
```

#### NNI 与 CIFAR-10

**搜索空间**

正如本文目标,要为 CIFAR-10 找到最好的`优化器`。 使用不同的优化器时,还要相应的调整`学习率``网络架构`。 因此,选择这三个参数作为超参,并创建如下的搜索空间。

```json
{
    "lr":{"_type":"choice", "_value":[0.1, 0.01, 0.001, 0.0001]},
    "optimizer":{"_type":"choice", "_value":["SGD", "Adadelta", "Adagrad", "Adam", "Adamax"]},
    "model":{"_type":"choice", "_value":["vgg", "resnet18", "googlenet", "densenet121", "mobilenet", "dpn92", "senet18"]}
}
```

*实现代码:[search_space.json](https://github.com/Microsoft/nni/blob/master/examples/trials/cifar10_pytorch/search_space.json)*

**Trial(尝试)**

这是超参集合的训练代码,关注以下几点:

* 使用 `nni.get_next_parameter()` 来获取下一组训练的超参组合。
* 使用 `nni.report_intermediate_result(acc)` 在每个 epoch 结束时返回中间结果。
Chi Song's avatar
Chi Song committed
50
* 使用 `nni.report_final_result(acc)` 在每个 Trial 结束时返回最终结果。
Chi Song's avatar
Chi Song committed
51
52
53

*实现代码:[main.py](https://github.com/Microsoft/nni/blob/master/examples/trials/cifar10_pytorch/main.py)*

54
还可直接修改现有的代码来支持 NNI,参考:[如何编写 Trial](Trials.md)
Chi Song's avatar
Chi Song committed
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74

**配置**

这是在本机运行 Experiment 的样例(多GPU):

代码:[examples/trials/cifar10_pytorch/config.yml](https://github.com/Microsoft/nni/blob/master/examples/trials/cifar10_pytorch/config.yml)

这是在 OpenPAI 上运行 Experiment 的样例:

代码:[examples/trials/cifar10_pytorch/config_pai.yml](https://github.com/Microsoft/nni/blob/master/examples/trials/cifar10_pytorch/config_pai.yml)

*完整样例:[examples/trials/cifar10_pytorch/](https://github.com/Microsoft/nni/tree/master/examples/trials/cifar10_pytorch)*

#### 运行 Experiment

以上即为 Experiment 的代码介绍,**从命令行运行 config.yml 文件来开始 Experiment**

```bash
nnictl create --config nni/examples/trials/cifar10_pytorch/config.yml
```