strategy.md 6.6 KB
Newer Older
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 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248
## hub.strategy

在PaddleHub中,Strategy类封装了一系列适用于迁移学习的Fine-tune策略。Strategy包含了对预训练参数使用什么学习率变化策略,使用哪种类型的优化器,使用什么类型的正则化等。

### Class `hub.finetune.strategy.AdamWeightDecayStrategy`

```python
hub.AdamWeightDecayStrategy(
    learning_rate=1e-4,
    lr_scheduler="linear_decay",
    warmup_proportion=0.0,
    weight_decay=0.01,
    optimizer_name="adam")
```

基于Adam优化器的学习率衰减策略

**参数**

* learning_rate: 全局学习率,默认为1e-4
* lr_scheduler: 学习率调度方法,默认为"linear_decay"
* warmup_proportion: warmup所占比重
* weight_decay: 学习率衰减率
* optimizer_name: 优化器名称,默认为adam

**返回**

`AdamWeightDecayStrategy`

**示例**

```python
...
strategy = hub.AdamWeightDecayStrategy()

config = hub.RunConfig(
    use_cuda=True,
    num_epoch=10,
    batch_size=32,
    checkpoint_dir="hub_finetune_ckpt",
    strategy=strategy)
```

### Class `hub.finetune.strategy.DefaultFinetuneStrategy`

```python
hub.DefaultFinetuneStrategy(
    learning_rate=1e-4,
    optimizer_name="adam",
    regularization_coeff=1e-3)
```

默认的Finetune策略,该策略会对预训练参数增加L2正则作为惩罚因子

**参数**

* learning_rate: 全局学习率。默认为1e-4
* optimizer_name: 优化器名称。默认adam
* regularization_coeff: 正则化的λ参数。默认为1e-3

**返回**

`DefaultFinetuneStrategy`

**示例**

```python
...
strategy = hub.DefaultFinetuneStrategy()

config = hub.RunConfig(
    use_cuda=True,
    num_epoch=10,
    batch_size=32,
    checkpoint_dir="hub_finetune_ckpt",
    strategy=strategy)
```

### Class `hub.finetune.strategy.L2SPFinetuneStrategy`

```python
hub.L2SPFinetuneStrategy(
    learning_rate=1e-4,
    optimizer_name="adam",
    regularization_coeff=1e-3)
```

使用L2SP正则作为惩罚因子的Finetune策略

**参数**

* learning_rate: 全局学习率。默认为1e-4
* optimizer_name: 优化器名称。默认为adam
* regularization_coeff: 正则化的λ参数。默认为1e-3

**返回**

`L2SPFinetuneStrategy`

**示例**

```python
...
strategy = hub.L2SPFinetuneStrategy()

config = hub.RunConfig(
    use_cuda=True,
    num_epoch=10,
    batch_size=32,
    checkpoint_dir="hub_finetune_ckpt",
    strategy=strategy)
```

### Class `hub.finetune.strategy.ULMFiTStrategy`

```python
hub.ULMFiTStrategy(
    learning_rate=1e-4,
    optimizer_name="adam",
    cut_fraction=0.1,
  	ratio=32,
  	dis_blocks=3,
  	factor=2.6,
  	frz_blocks=3)
```

该策略实现了[ULMFiT](https://arxiv.org/abs/1801.06146)论文中提出的三种策略:Slanted triangular learning rates, Discriminative fine-tuning, Gradual unfreezing。

- Slanted triangular learning rates是一种学习率先上升再下降的策略,如下图所示:
<div align=center><img src="https://github.com/PaddlePaddle/PaddleHub/wiki/images/slanted.png" width="50%" height="50%"></div>

- Discriminative fine-tuning是一种学习率逐层递减的策略,通过该策略可以减缓底层的更新速度。
- Gradual unfreezing是一种逐层解冻的策略,通过该策略可以优先更新上层,再慢慢解冻下层参与更新。

**参数**

* learning_rate: 全局学习率。默认为1e-4。
* optimizer_name: 优化器名称。默认为adam。
* cut_fraction: 设置Slanted triangular learning rates学习率上升的步数在整个训练总步数中的比例,对应论文中Slanted triangular learning rates中的cut_frac。默认为0.1,如果设置为0,则不采用Slanted triangular learning rates。
* ratio: 设置Slanted triangular learning rates下降的最小学习率与上升的最大学习率的比例关系,默认为32,表示最小学习率是最大学习率的1/32。
* dis_blocks: 设置 Discriminative fine-tuning中的块数。由于预训练模型中没有记录op的层数,Paddlehub通过op的前后关系推测op所在的层次,这会导致诸如LSTM这类计算单元的op会被当作是不同层的op。为了不使层次划分太细,我们将层次进行了分块,用块的概念代替原论文中层的概念,通过设置dis_blocks即可设置块的个数。默认为3,如果设置为0,则不采用Discriminative fine-tuning。
* factor: 设置Discriminative fine-tuning的衰减率。默认为2.6,表示下一层的学习率是上一层的1/2.6。
* frz_blocks: 设置Gradual unfreezing中的块数。块的概念同“dis_blocks”中介绍的概念。

**返回**

`ULMFiTStrategy`

**示例**

```python
...
strategy = hub.ULMFiTStrategy()

config = hub.RunConfig(
    use_cuda=True,
    num_epoch=10,
    batch_size=32,
    checkpoint_dir="hub_finetune_ckpt",
    strategy=strategy)
```

### Class `hub.finetune.strategy.CombinedStrategy`

```python
hub.CombinedStrategy(
    optimizer_name="adam",
    learning_rate=1e-4,
    scheduler=None,
    regularization=None,
    clip=None)
```

Paddlehub中的基类策略,上文的所有策略都基于该策略,通过该策略可以设置所有策略参数。

**参数**

* optimizer_name: 优化器名称,默认为adam。
* learning_rate: 全局学习率,默认为1e-4。
* scheduler: 学习率调度方法,默认为None,此时不改变任何默认学习率调度方法参数,不采取任何学习率调度方法,即:
```python
scheduler = {
    "warmup": 0.0,
    "linear_decay": {
        "start_point": 1.0,
        "end_learning_rate": 0.0,
    },
    "noam_decay": False,
    "discriminative": {
        "blocks": 0,
        "factor": 2.6
    },
    "gradual_unfreeze": 0,
    "slanted_triangle": {
        "cut_fraction": 0.0,
        "ratio": 32
    }
}
```
* regularization: 正则方法,默认为None,此时不改变任何默认正则方法参数,不采取任何正则方法,即:

```python
regularization = {
    "L2": 0.0,
    "L2SP": 0.0,
    "weight_decay": 0.0,
}
```
* clip: 梯度裁剪方法,默认为None,此时不改变任何默认正则方法参数,不采取任何梯度裁剪方法,即:
```python
clip = {
    "GlobalNorm": 0.0,
    "Norm": 0.0
}
```

**返回**

`CombinedStrategy`

**示例**

```python
...
# Parameters not specified will remain default
scheduler = {
  "discriminative": {
    "blocks": 3,
    "factor": 2.6
  }
}
# Parameters not specified will remain default
regularization = {"L2": 1e-3}
# Parameters not specified will remain default
clip = {"GlobalNorm": 1.0}
strategy = hub.CombinedStrategy(
  scheduler = scheduler,
	regularization = regularization,
	clip = clip
)

config = hub.RunConfig(
    use_cuda=True,
    num_epoch=10,
    batch_size=32,
    checkpoint_dir="hub_finetune_ckpt",
    strategy=strategy)
```