前提・実現したいこと
Transformersで感情を識別するモデルを作りたい
発生している問題・エラーメッセージ
ロスが下がっていない、実験結果も与えたテキストによらず似たようなアウトプットが出る。
恐らく、学習していないのではないかと思われる。
Epoch Training Loss Validation Loss Runtime Samples Per Second 1 No log 0.622909 0.594900 3.362000 2 No log 0.622928 0.631200 3.169000 3 No log 0.622966 0.610100 3.278000 4 No log 0.623018 0.590800 3.385000 5 No log 0.623088 0.625500 3.198000 6 No log 0.623177 0.614500 3.255000 7 No log 0.623285 0.604700 3.308000 8 No log 0.623407 0.599800 3.335000 9 No log 0.623546 0.609700 3.280000 10 No log 0.623706 0.604200 3.310000```
該当のソースコード
python
1!pip install transformers[ja]==4.3.3 torch==1.8.1 sentencepiece==0.1.91 2 3from google.colab import drive 4import pandas as pd 5 6drive.mount('/content/drive') 7 8training_data = pd.read_csv('/content/drive/MyDrive/Texts/negaposi.csv') 9training_data.head() 10 11print(len(training_data["query"].unique())) 12training_data[["title", "label"]].groupby("label").count() 13 14 15from sklearn.model_selection import train_test_split 16train_queries, val_queries, train_docs, val_docs, train_labels, val_labels = train_test_split( 17 training_data["query"].tolist(), 18 training_data["title"].tolist(), 19 training_data["label"].tolist(), 20 test_size=.2 21) 22 23from transformers import BertJapaneseTokenizer, BertForMaskedLM, pipeline 24 25model_name = 'cl-tohoku/bert-base-japanese-whole-word-masking' 26tokenizer = BertJapaneseTokenizer.from_pretrained(model_name) 27 28train_encodings = tokenizer(train_queries, train_docs, truncation=True, padding='max_length', max_length=128) 29val_encodings = tokenizer(val_queries, val_docs, truncation=True, padding='max_length', max_length=128) 30 31import torch 32 33class NPDataset(torch.utils.data.Dataset): 34 def __init__(self, encodings, labels): 35 self.encodings = encodings 36 self.labels = labels 37 38 def __getitem__(self, idx): 39 item = {key: torch.tensor(val[idx]) for key, val in self.encodings.items()} 40 item['labels'] = torch.tensor(self.labels[idx]) 41 return item 42 43 def __len__(self): 44 return len(self.labels) 45 46train_dataset = NPDataset(train_encodings, train_labels) 47val_dataset = NPDataset(val_encodings, val_labels) 48 49model_name = "cl-tohoku/bert-base-japanese-whole-word-masking" 50from transformers import BertForSequenceClassification 51model = BertForSequenceClassification.from_pretrained(model_name) 52 53for param in model.base_model.parameters(): 54 param.requires_grad = False 55 56from transformers import Trainer, TrainingArguments 57 58training_args = TrainingArguments( 59 output_dir='/content/drive/MyDrive/Models/', # output directory 60 evaluation_strategy="epoch", # Evaluation is done at the end of each epoch. 61 num_train_epochs=10, # total number of training epochs 62 per_device_train_batch_size=16, # batch size per device during training 63 per_device_eval_batch_size=64, # batch size for evaluation 64 warmup_steps=500, # number of warmup steps for learning rate scheduler 65 weight_decay=0.01, # strength of weight decay 66 save_total_limit=1, # limit the total amount of checkpoints. Deletes the older checkpoints. 67) 68 69 70trainer = Trainer( 71 model=model, # the instantiated ???? Transformers model to be trained 72 args=training_args, # training arguments, defined above 73 train_dataset=train_dataset, # training dataset 74 eval_dataset=val_dataset # evaluation dataset 75) 76 77trainer.train() 78 79# 実験 80nlp = pipeline("sentiment-analysis",model=model,tokenizer=tokenizer) 81nlp("楽しかった")
試したこと
nlp("楽しかった")
nlp("悲しかった")
補足情報(FW/ツールのバージョンなど)
ここにより詳細な情報を記載してください。
あなたの回答
tips
プレビュー