Upgrade to Pro — share decks privately, control downloads, hide ads and more …

AllenNLPを使った開発

 AllenNLPを使った開発

Yasufumi Taniguchi

July 29, 2019
Tweet

More Decks by Yasufumi Taniguchi

Other Decks in Programming

Transcript

  1. 1. パラメータ管理 python -m torch.distributed.launch --nproc_per_node=8 ./examples/run_squad.py ¥ --model_type bert

    ¥ --model_name_or_path bert-large-uncased-whole-word-masking ¥ --do_train ¥ --do_eval ¥ --do_lower_case ¥ --train_file $SQUAD_DIR/train-v1.1.json ¥ --predict_file $SQUAD_DIR/dev-v1.1.json ¥ --learning_rate 3e-5 ¥ --num_train_epochs 2 ¥ --max_seq_length 384 ¥ --doc_stride 128 ¥ --output_dir ../models/wwm_uncased_finetuned_squad/ ¥ --per_gpu_eval_batch_size=3 ¥ --per_gpu_train_batch_size=3 ¥
  2. 2. Modelの役割 tensor([88, 19, 226, 3, 151, 25, 18, 14,

    3, 1, 273, 34, 17, 37, 26, 0]) Model {“loss”: tensor([0.1234])}
  3. 3. DatasetReaderの実装 @overrides def text_to_instance(self, string, label): fields = {}

    tokens = self._tokenizer.tokenize(string) fields['tokens'] = TextField(tokens, self._token_indexers) fields['label'] = LabelField(label, skip_indexing=True) return Instance(fields) • トークナイゼーション • インデックス化
  4. 3. DatasetReaderの実装 • ⽂書のトークナイズを⾏う • トークナイザは設定ファイルで指定する @overrides def text_to_instance(self, string,

    label): fields = {} tokens = self._tokenizer.tokenize(string) fields['tokens'] = TextField(tokens, self._token_indexers) fields['label'] = LabelField(label, skip_indexing=True) return Instance(fields)
  5. 3. DatasetReaderの実装 • トークナイズした⽂書からTextFieldを⽣成 • インデックス化はTextFieldが実⾏ @overrides def text_to_instance(self, string,

    label): fields = {} tokens = self._tokenizer.tokenize(string) fields['tokens'] = TextField(tokens, self._token_indexers) fields['label'] = LabelField(label, skip_indexing=True) return Instance(fields)
  6. 3. Modelの実装 def forward(self, tokens, label): embedded_text = self._text_field_embbeder(tokens) mask

    = get_text_field_mask(tokens).float() encoded_text = self._dropout( self._seq2vec_encoder(embedded_text, mask=mask) ) logits = self._classification_layer(encoded_text) probs = F.softmax(logits, dim=1) loss = self._loss(logits, label.long().view(-1)) return {“loss”: loss} • ⼊⼒からロスを計算 • dict形式で値を返却
  7. 3. 学習 allennlp train –include-package allennlp_imdb ¥ -s /path/to/serialization ¥

    training_config/base_cpu.jsonnet • 設定ファイルを指定して実験を⾏う
  8. 3. 学習 allennlp train –include-package allennlp_imdb ¥ -s /path/to/serialization ¥

    -o ‘{“trainer”: {“cuda_device”: 0}}’ training_config/base_cpu.jsonnet • 設定ファイルを指定して実験を⾏う • 設定の上書きも可能