有没有更加简便加载模型的方法
Created by: zhangxu3486432
可以使用 task.load_parameters
加载 best_model,
但是构建 task
实例过于繁琐,只是预测任务的话,会加载很多不必要的参数,有没有更加简便的方法?
import paddlehub as hub
from paddlehub.dataset.base_nlp_dataset import BaseNLPDataset
class TrafficNews(BaseNLPDataset):
def __init__(self):
# 数据集存放位置
self.dataset_dir = "/home/traffic_news/dataset"
super(TrafficNews, self).__init__(
base_path=self.dataset_dir,
train_file="train.tsv",
dev_file="valid.tsv",
test_file="test.tsv",
train_file_with_header=True,
dev_file_with_header=True,
test_file_with_header=True,
# 数据集类别集合
label_list=['1','2','3','4']
)
dataset = TrafficNews()
module = hub.Module(name='chinese-electra-base')
reader = hub.reader.ClassifyReader(
dataset=dataset,
vocab_path=module.get_vocab_path(),
sp_model_path=module.get_spm_path(),
word_dict_path=module.get_word_dict_path(),
max_seq_len=128
)
inputs, outputs, program = module.context(trainable=True, max_seq_len=128)
# Use "pooled_output" for classification tasks on an entire sentence.
pooled_output = outputs["pooled_output"]
feed_list = [
inputs["input_ids"].name,
inputs["position_ids"].name,
inputs["segment_ids"].name,
inputs["input_mask"].name,
]
strategy = hub.AdamWeightDecayStrategy(
weight_decay=0.01,
warmup_proportion=0.1,
learning_rate=1e-4
)
config = hub.RunConfig(
use_cuda=False,
num_epoch=10,
checkpoint_dir="hub_electra",
batch_size=16,
eval_interval=50,
strategy=strategy
)
task = hub.TextClassifierTask(
data_reader=reader,
feature=pooled_output,
feed_list=feed_list,
num_classes=dataset.num_labels,
config=config
)
task.load_parameters('hub_electra/best_model/')