阅读量:3
基于MindSpore通过GPT实现情感分类
导入数据集
import os import mindspore from mindnlp._legacy.engine import Evaluator, Trainer from mindnlp._legacy.engine.callbacks import BestModelCallback, CheckpointCallback from mindnlp._legacy.metrics import Accuracy from mindnlp.dataset import load_dataset from mindspore import nn from mindspore.dataset import GeneratorDataset, text, transforms imdb_ds = load_dataset("imdb", split=["train", "test"]) imdb_train = imdb_ds["train"] imdb_test = imdb_ds["test"]
对数据集进行预处理
import numpy as np def process_dataset(dataset, tokenizer, max_seq_len=512, batch_size=4, shuffle=False): is_ascend = mindspore.get_context("device_target") == "Ascend" def tokenize(text): if is_ascend: tokenized = tokenizer( text, padding="max_length", truncation=True, max_length=max_seq_len ) else: tokenized = tokenizer(text, truncation=True, max_length=max_seq_len) return tokenized["input_ids"], tokenized["attention_mask"] if shuffle: dataset = dataset.shuffle(batch_size) # map dataset dataset = dataset.map( operations=[tokenize], input_columns="text", output_columns=["input_ids", "attention_mask"], ) dataset = dataset.map( operations=transforms.TypeCast(mindspore.int32), input_columns="label", output_columns="labels", ) # batch dataset if is_ascend: dataset = dataset.batch(batch_size) else: dataset = dataset.padded_batch( batch_size, pad_info={ "input_ids": (None, tokenizer.pad_token_id), "attention_mask": (None, 0), }, ) return dataset
导入 tokenizer
from mindnlp.transformers import GPTTokenizer # tokenizer gpt_tokenizer = GPTTokenizer.from_pretrained("openai-gpt") # add sepcial token: <PAD> special_tokens_dict = { "bos_token": "<bos>", "eos_token": "<eos>", "pad_token": "<pad>", } num_added_toks = gpt_tokenizer.add_special_tokens(special_tokens_dict)
分割训练数据集
# split train dataset into train and valid datasets imdb_train, imdb_val = imdb_train.split([0.7, 0.3])
导入训练模型
from mindnlp.transformers import GPTForSequenceClassification from mindspore.experimental.optim import Adam # set bert config and define parameters for training model = GPTForSequenceClassification.from_pretrained('openai-gpt', num_labels=2) model.config.pad_token_id = gpt_tokenizer.pad_token_id model.resize_token_embeddings(model.config.vocab_size + 3) optimizer = nn.Adam(model.trainable_params(), learning_rate=2e-5) metric = Accuracy() # define callbacks to save checkpoints ckpoint_cb = CheckpointCallback(save_path='checkpoint', ckpt_name='gpt_imdb_finetune', epochs=1, keep_checkpoint_max=2) best_model_cb = BestModelCallback(save_path='checkpoint', ckpt_name='gpt_imdb_finetune_best', auto_load=True) trainer = Trainer(network=model, train_dataset=dataset_train, eval_dataset=dataset_train, metrics=metric, epochs=1, optimizer=optimizer, callbacks=[ckpoint_cb, best_model_cb], jit=False)
开始训练
trainer.run(tgt_columns="labels")
验证
evaluator = Evaluator(network=model, eval_dataset=dataset_test, metrics=metric) evaluator.run(tgt_columns="labels")