cifar10_examples.md 3.67 KB
Newer Older
Yan Ni's avatar
Yan Ni committed
1
2
3
4
5
6
7
8
# CIFAR-10 examples

## Overview

[CIFAR-10][3] classification is a common benchmark problem in machine learning. The CIFAR-10 dataset is the collection of images. It is one of the most widely used datasets for machine learning research which contains 60,000 32x32 color images in 10 different classes. Thus, we use CIFAR-10 classification as an example to introduce NNI usage.

### **Goals**

Chi Song's avatar
Chi Song committed
9
As we all know, the choice of model optimizer is directly affects the performance of the final metrics. The goal of this tutorial is to **tune a better performace optimizer** to train a relatively small convolutional neural network (CNN) for recognizing images.
Yan Ni's avatar
Yan Ni committed
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67

In this example, we have selected the following common deep learning optimizer:

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

### **Experimental**

#### Preparations

This example requires pytorch. Pytorch install package should be chosen based on python version and cuda version.

Here is an example of the environment python==3.5 and cuda == 8.0, then using the following commands to install [pytorch][2]:

```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
```

#### CIFAR-10 with NNI

**Search Space**

As we stated in the target, we target to find out the best `optimizer` for training CIFAR-10 classification. When using different optimizers, we also need to adjust `learning rates` and `network structure` accordingly. so we chose these three parameters as hyperparameters and write the following search space.

```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"]}
}
```

*Implemented code directory: [search_space.json][8]*

**Trial**

The code for CNN training of each hyperparameters set, paying particular attention to the following points are specific for NNI:

* Use `nni.get_next_parameter()` to get next training hyperparameter set.
* Use `nni.report_intermediate_result(acc)` to report the intermedian result after finish each epoch.
* Use `nni.report_intermediate_result(acc)` to report the final result before the trial end.

*Implemented code directory: [main.py][9]*

You can also use your previous code directly, refer to [How to define a trial][5] for modify.

**Config**

Here is the example of running this experiment on local(with multiple GPUs):

code directory: [examples/trials/cifar10_pytorch/config.yml][6]

Here is the example of running this experiment on OpenPAI:

code directory: [examples/trials/cifar10_pytorch/config_pai.yml][7]

*The complete examples we have implemented: [examples/trials/cifar10_pytorch/][1]*

Chi Song's avatar
Chi Song committed
68
#### Launch the experiment
Yan Ni's avatar
Yan Ni committed
69
70
71

We are ready for the experiment, let's now **run the config.yml file from your command line to start the experiment**.

Chi Song's avatar
Chi Song committed
72
73
```bash
nnictl create --config nni/examples/trials/cifar10_pytorch/config.yml
Yan Ni's avatar
Yan Ni committed
74
75
76
77
78
79
```

[1]: https://github.com/Microsoft/nni/tree/master/examples/trials/cifar10_pytorch
[2]: https://pytorch.org/
[3]: https://www.cs.toronto.edu/~kriz/cifar.html
[4]: https://github.com/Microsoft/nni/tree/master/examples/trials/cifar10_pytorch
Yan Ni's avatar
Yan Ni committed
80
[5]: https://github.com/Microsoft/nni/blob/master/docs/Trials.md
Yan Ni's avatar
Yan Ni committed
81
82
83
84
[6]: https://github.com/Microsoft/nni/blob/master/examples/trials/cifar10_pytorch/config.yml
[7]: https://github.com/Microsoft/nni/blob/master/examples/trials/cifar10_pytorch/config_pai.yml
[8]: https://github.com/Microsoft/nni/blob/master/examples/trials/cifar10_pytorch/search_space.json
[9]: https://github.com/Microsoft/nni/blob/master/examples/trials/cifar10_pytorch/main.py