This repository has been archived by the owner on Sep 18, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
aca75e8
commit bcb53a7
Showing
20 changed files
with
334 additions
and
47 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
# TextNAS | ||
|
||
## 介绍 | ||
|
||
这是论文 [TextNAS: A Neural Architecture Search Space tailored for Text Representation](https://arxiv.org/pdf/1912.10729.pdf) 中 TextNAS 算法的实现。 TextNAS 是用于文本表示的神经网络架构搜索算法,具体来说,TextNAS 基于由适配各种自然语言任务的操作符所组成的新的搜索空间,TextNAS 还支持单个网络中的多路径集成,来平衡网络的宽度和深度。 | ||
|
||
TextNAS 的搜索空间包含: | ||
|
||
* 过滤器尺寸为 1, 3, 5, 7 的一维卷积操作 | ||
* 循环操作符(双向 GRU) | ||
* 自注意操作符 | ||
* 池化操作符(最大值、平均值) | ||
|
||
遵循 ENAS 算法,TextNAS 也用了参数共享来加速搜索速度,并采用了强化学习的 Controller 来进行架构采样和生成。 参考 TextNAS 论文了解更多细节。 | ||
|
||
## 准备 | ||
|
||
准备词向量和 SST 数据集,并按如下结构放到 data 目录中: | ||
|
||
``` | ||
textnas | ||
├── data | ||
│ ├── sst | ||
│ │ └── trees | ||
│ │ ├── dev.txt | ||
│ │ ├── test.txt | ||
│ │ └── train.txt | ||
│ └── glove.840B.300d.txt | ||
├── dataloader.py | ||
├── model.py | ||
├── ops.py | ||
├── README.md | ||
├── search.py | ||
└── utils.py | ||
``` | ||
|
||
以下链接有助于查找和下载相应的数据集: | ||
|
||
* [GloVe: Global Vectors for Word Representation](https://nlp.stanford.edu/projects/glove/) | ||
* [glove.840B.300d.txt](http://nlp.stanford.edu/data/glove.840B.300d.zip) | ||
* [Recursive Deep Models for Semantic Compositionality Over a Sentiment Treebank](https://nlp.stanford.edu/sentiment/) | ||
* [trainDevTestTrees_PTB.zip](https://nlp.stanford.edu/sentiment/trainDevTestTrees_PTB.zip) | ||
|
||
## 示例 | ||
|
||
### 搜索空间 | ||
|
||
[示例代码](https://github.com/microsoft/nni/tree/master/examples/nas/textnas) | ||
|
||
```bash | ||
#如果未克隆 NNI 代码。 如果代码已被克隆,请忽略此行并直接进入代码目录。 | ||
git clone https://github.com/Microsoft/nni.git | ||
|
||
# 搜索最佳网络结构 | ||
cd examples/nas/textnas | ||
|
||
# 查看搜索的更多选项 | ||
python3 search.py -h | ||
``` | ||
|
||
在每个搜索 Epoch 后,会直接测试 10 个采样的结构。 10 个 Epoch 后的性能预计为 40% - 42%。 | ||
|
||
默认情况下,20 个采样结构会被导出到 `checkpoints` 目录中,以便进行下一步处理。 | ||
|
||
### 重新训练 | ||
|
||
```bash | ||
#如果未克隆 NNI 代码。 如果代码已被克隆,请忽略此行并直接进入代码目录。 | ||
git clone https://github.com/Microsoft/nni.git | ||
|
||
# 搜索最佳网络结构 | ||
cd examples/nas/textnas | ||
|
||
# 默认在 sst-2 上训练 | ||
sh run_retrain.sh | ||
``` | ||
|
||
## 参考 | ||
|
||
TextNAS 直接使用了 EnasTrainer,参考 [ENAS](./ENAS.md) 了解 Trainer 的 API。 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
# NAS 可视化(测试版) | ||
|
||
## 内置 Trainer 支持 | ||
|
||
当前,仅 ENAS 和 DARTS 支持可视化。 [ENAS](./ENAS.md) 和 [DARTS](./DARTS.md) 的示例演示了如何在代码中启用可视化,其需要在 `trainer.train()` 前添加代码。 | ||
|
||
```python | ||
trainer.enable_visualization() | ||
``` | ||
|
||
此代码会在当前目录中创建新目录 `logs/<current_time_stamp>`,并创建两个新文件 `graph.json` 和 `log`。 | ||
|
||
不必等到程序运行完后,再启动 NAS 界面,但需要确保这两个文件产生后,再启动。 启动 NAS 界面: | ||
|
||
```bash | ||
nnictl webui nas --logdir logs/<current_time_stamp> --port <port> | ||
``` | ||
|
||
## 可视化定制的 Trainer | ||
|
||
如果要定制 Trainer,参考[文档](./Advanced.md#extend-the-ability-of-one-shot-trainers)。 | ||
|
||
需要对已有 Trainer 代码做两处改动来支持可视化: | ||
|
||
1. 在训练前导出图: | ||
|
||
```python | ||
vis_graph = self.mutator.graph(inputs) | ||
# `inputs` 是模型的虚拟输入。 例如,torch.randn((1, 3, 32, 32)).cuda() | ||
# 如果模型有多个输入,则要使用 tuple。 | ||
with open("/path/to/your/logdir/graph.json", "w") as f: | ||
json.dump(vis_graph, f) | ||
``` | ||
|
||
2. 记录选择的 Choice。 可以每个 Epoch,批处理或任何频率下做次记录。 | ||
|
||
```python | ||
def __init__(self): | ||
# ... | ||
self.status_writer = open("/path/to/your/logdir/log", "w") # create a writer | ||
|
||
def train(self): | ||
# ... | ||
print(json.dumps(self.mutator.status()), file=self.status_writer, flush=True) # 保存状态 | ||
``` | ||
|
||
如果继承 `Trainer`,实现定制的 Trainer。 NNI 提供了 `enable_visualization()` 和 `_write_graph_status()` 来简化可视化。 只需要在开始前调用 `trainer.enable_visualization()`,并在每次要记录日志前调用 `trainer._write_graph_status()`。 注意,这两个 API 还处于试用阶段,未来可能会有所更改。 | ||
|
||
最后,启动 NAS 界面: | ||
|
||
```bash | ||
nnictl webui nas --logdir /path/to/your/logdir | ||
``` | ||
|
||
## NAS 界面预览 | ||
|
||
![](../../img/nasui-1.png) | ||
|
||
![](../../img/nasui-2.png) | ||
|
||
## 局限性 | ||
|
||
* NAS 可视化仅适用于 PyTorch >=1.4。PyTorch 1.3.1 无法正常工作。 | ||
* 其依赖于 PyTorch 对 tensorboard 导出图的支持,即依赖于 `torch.jit`。 如果模型不支持 `jit`,也无法使用。 | ||
* 在加载中等大小,但有许多 Choice 的模型时(如 DARTS 的搜索空间),会遇到性能问题。 | ||
|
||
## 反馈 | ||
|
||
NAS UI 目前是测试版。 欢迎提交反馈。 [这里](https://github.com/microsoft/nni/pull/2085)列出了 NAS UI 接下来的工作。 欢迎直接评论,如果有其它建议,也可以[提交新问题](https://github.com/microsoft/nni/issues/new?template=enhancement.md)。 |
Oops, something went wrong.