728x90
๋ฐ์ํ
์ด๋ฒ์๋ Huggingface์์ ์ ๊ณตํ๋ Transformers์ ๋ํ์ฌ ์์๋ณด๊ณ ์ ํฉ๋๋ค.
https://huggingface.co/docs/transformers/index
- ์์ฐ์ด ์ฒ๋ฆฌ ๊ด๋ จ ์ฌ๋ฌ ๋ผ์ด๋ธ๋ฌ๋ฆฌ๊ฐ ์์ง๋ง Transformer๋ฅผ ํ์ฉํ ์์ฐ์ด ์ฒ๋ฆฌ task์์ ๊ฐ์ฅ ๋ง์ด ํ์ฉ๋๊ณ ์๋ ๋ผ์ด๋ธ๋ฌ๋ฆฌ๋ transformers์ ๋๋ค.
- pytorch version์ BERT๋ฅผ ๊ฐ์ฅ ๋จผ์ ๊ตฌํํ๋ฉฐ ์ฃผ๋ชฉ๋ฐ์๋ huggingface๋ ํ์ฌ transformer๊ธฐ๋ฐ์ ๋ค์ํ ๋ชจ๋ธ๋ค์ ๊ตฌํ ๋ฐ ๊ณต๊ฐํ๋ฉฐ ๋ง์ ์ฃผ๋ชฉ์ ๋ฐ๊ณ ์์ต๋๋ค.(์๋ ์ฃผ์์์ ๋ค์ํ ๋ชจ๋ธ๋ค์ ํ์ธ ๊ฐ๋ฅํฉ๋๋ค)
- https://huggingface.co/models
- ์ ์๋ ๋ชจ๋ธ ์ด์ธ์๋ custom model์ ์ ๋ก๋ํ์ฌ ์ฌ์ฉํ ์ ์์ต๋๋ค.
- Configuration- ๋ชจ๋ธ ๋ช ์ธ, ๋ชจ๋ธ์ ์ ๋ณด https://huggingface.co/transformers/main_classes/configuration.html
- AutoConfig์์๋ ๋ค์ํ ๋ชจ๋ธ์ configuration (ํ๊ฒฝ ์ค์ )์ string tag๋ฅผ ์ด์ฉํด ์ฝ๊ฒ loadํ ์ ์์ต๋๋ค.
- ๊ฐ Config์๋ ํด๋น ๋ชจ๋ธ architecture์ task์ ํ์ํ ๋ค์ํ ์ ๋ณด(architecture ์ข ๋ฅ, ๋ ์ด์ด ์, hidden unit size, hyperparameter)๋ฅผ ๋ด๊ณ ์์ต๋๋ค.
- https://huggingface.co/models ์์ ํด๋น ๋ชจ๋ธ๋ค์ name tag๋ฅผ ํ์ธํ ์ ์์ต๋๋ค.
- ์๋ ์์์ ๊ฒฝ์ฐ - https://huggingface.co/bert-base-uncased
from transformers import AutoConfig
config = AutoConfig.from_pretrained('bert-base-uncased')
config
#Result
BertConfig {
"_name_or_path": "bert-base-uncased",
"architectures": [
"BertForMaskedLM"
],
"attention_probs_dropout_prob": 0.1,
"classifier_dropout": null,
"gradient_checkpointing": false,
"hidden_act": "gelu",
"hidden_dropout_prob": 0.1,
"hidden_size": 768,
"initializer_range": 0.02,
"intermediate_size": 3072,
"layer_norm_eps": 1e-12,
"max_position_embeddings": 512,
"model_type": "bert",
"num_attention_heads": 12,
"num_hidden_layers": 12,
"pad_token_id": 0,
"position_embedding_type": "absolute",
"transformers_version": "4.25.1",
"type_vocab_size": 2,
"use_cache": true,
"vocab_size": 30522
}
gpt_config = AutoConfig.from_pretrained('gpt2')
gpt_config
#Result
GPT2Config {
"_name_or_path": "gpt2",
"activation_function": "gelu_new",
"architectures": [
"GPT2LMHeadModel"
],
"attn_pdrop": 0.1,
"bos_token_id": 50256,
"embd_pdrop": 0.1,
"eos_token_id": 50256,
"initializer_range": 0.02,
"layer_norm_epsilon": 1e-05,
"model_type": "gpt2",
"n_ctx": 1024,
"n_embd": 768,
"n_head": 12,
"n_inner": null,
"n_layer": 12,
"n_positions": 1024,
"reorder_and_upcast_attn": false,
"resid_pdrop": 0.1,
"scale_attn_by_inverse_layer_idx": false,
"scale_attn_weights": true,
"summary_activation": null,
"summary_first_dropout": 0.1,
"summary_proj_to_labels": true,
"summary_type": "cls_index",
"summary_use_proj": true,
"task_specific_params": {
"text-generation": {
"do_sample": true,
"max_length": 50
}
},
"transformers_version": "4.25.1",
"use_cache": true,
"vocab_size": 50257
}
print(config.vocab_size)
30522
config_dict = config.to_dict()
config_dict
#Result
{'return_dict': True,
'output_hidden_states': False,
'output_attentions': False,
'torchscript': False,
'torch_dtype': None,
'use_bfloat16': False,
'tf_legacy_loss': False,
'pruned_heads': {},
'tie_word_embeddings': True,
'is_encoder_decoder': False,
'is_decoder': False,
'cross_attention_hidden_size': None,
'add_cross_attention': False,
'tie_encoder_decoder': False,
'max_length': 20,
'min_length': 0,
'do_sample': False,
'early_stopping': False,
'num_beams': 1,
'num_beam_groups': 1,
'diversity_penalty': 0.0,
'temperature': 1.0,
'top_k': 50,
'top_p': 1.0,
'typical_p': 1.0,
'repetition_penalty': 1.0,
'length_penalty': 1.0,
'no_repeat_ngram_size': 0,
'encoder_no_repeat_ngram_size': 0,
'bad_words_ids': None,
'num_return_sequences': 1,
'chunk_size_feed_forward': 0,
'output_scores': False,
'return_dict_in_generate': False,
'forced_bos_token_id': None,
'forced_eos_token_id': None,
'remove_invalid_values': False,
'exponential_decay_length_penalty': None,
'suppress_tokens': None,
'begin_suppress_tokens': None,
'architectures': ['BertForMaskedLM'],
'finetuning_task': None,
'id2label': {0: 'LABEL_0', 1: 'LABEL_1'},
'label2id': {'LABEL_0': 0, 'LABEL_1': 1},
'tokenizer_class': None,
'prefix': None,
'bos_token_id': None,
'pad_token_id': 0,
'eos_token_id': None,
'sep_token_id': None,
'decoder_start_token_id': None,
'task_specific_params': None,
'problem_type': None,
'_name_or_path': 'bert-base-uncased',
'transformers_version': '4.25.1',
'gradient_checkpointing': False,
'model_type': 'bert',
'vocab_size': 30522,
'hidden_size': 768,
'num_hidden_layers': 12,
'num_attention_heads': 12,
'hidden_act': 'gelu',
'intermediate_size': 3072,
'hidden_dropout_prob': 0.1,
'attention_probs_dropout_prob': 0.1,
'max_position_embeddings': 512,
'type_vocab_size': 2,
'initializer_range': 0.02,
'layer_norm_eps': 1e-12,
'position_embedding_type': 'absolute',
'use_cache': True,
'classifier_dropout': None}
from transformers import BertConfig
bertconfig = BertConfig.from_pretrained('bert-base-uncased')
bert_in_gpt2_config = BertConfig.from_pretrained('gpt2')
#You are using a model of type gpt2 to instantiate a model of type bert. This is not supported for all configurations of models and can yield errors.
Model: https://github.com/huggingface/transformers/tree/master/src/transformers/models
- Transformers์์๋ transformer๊ธฐ๋ฐ์ ๋ชจ๋ธ architecture๋ฅผ ๊ตฌํํด๋์์ต๋๋ค.
- ์ต๊ทผ์๋ https://arxiv.org/abs/2010.11929์ ๊ฐ์ด Vision task์์ ํ์ฉํ๋ transformer ๋ชจ๋ธ๋ค์ ์ถ๊ฐํ๋ฉฐ ๊ทธ ํ์ฅ์ฑ์ ๋ํด๊ฐ๊ณ ์์ต๋๋ค.
- ๋ชจ๋ธ architecture ๋ฟ๋ง ์๋๋ผ ๊ด๋ จ task์ ์ ์ฉ๊ฐ๋ฅํ ํํ์ ๊ตฌํ์ฒด๋ค์ด ์์ต๋๋ค.
- BERT ๊ตฌํ์ฒด์์ ์ ๊ณตํ๊ณ ์๋ class๋ฅผ ํ์ธํ๊ณ ํด๋น ๊ตฌ์กฐ๋ฅผ ์ด์ฉํด ํ์ตํ ๋ชจ๋ธ๋ค์ loadํด๋ณด๊ฒ ์ต๋๋ค
from transformers import BertForMaskedLM, BertForQuestionAnswering, BertForSequenceClassification, BertForTokenClassification, BertForMultipleChoice, BertModel
from transformers import AutoModel, AutoTokenizer, AutoConfig
bertmodel = AutoModel.from_pretrained('bert-base-uncased')
tokenizer = AutoTokenizer.from_pretrained('bert-base-uncased')
input = tokenizer('hi, my name is joy')
input
'input_ids': [101, 7632, 1010, 2026, 2171, 2003, 6569, 102], 'token_type_ids': [0, 0, 0, 0, 0, 0, 0, 0], 'attention_mask': [1, 1, 1, 1, 1, 1, 1, 1]}
bert_qa = BertForQuestionAnswering.from_pretrained('bert-base-uncased')
๋ฐ์ํ
'์์ฐ์ด ์ฒ๋ฆฌ > Today I learned :' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
์ธ์ด๋ชจ๋ธ GPT (1) | 2023.01.17 |
---|---|
๋ฒํธ๋ฅผ ํ์ฉํ ์ํ๋ฆฌ๋ทฐ ๋ถ๋ฅ (0) | 2023.01.16 |
ํธ๋์คํฌ๋จธ์ ์ดํ ์ ์ ๋ฆฌ --2 (0) | 2023.01.16 |
ํธ๋์คํฌ๋จธ์ ์ดํ ์ ์ ๋ฆฌ --1 (1) | 2023.01.15 |
transfomers ๋ฅผ ์ฌ์ฉํ ๊ฐ๋จํ ๋ถ๋ฅ ์์ (BertForSequenceClassification) (0) | 2023.01.12 |