def eval_net(): '''eval net''' if config.dataset == 'MR': instance = MovieReview(root_dir=config.data_path, maxlen=config.word_len, split=0.9) elif config.dataset == 'SUBJ': instance = Subjectivity(root_dir=config.data_path, maxlen=config.word_len, split=0.9) elif config.dataset == 'SST2': instance = SST2(root_dir=config.data_path, maxlen=config.word_len, split=0.9) device_target = config.device_target context.set_context(mode=context.GRAPH_MODE, device_target=config.device_target) if device_target == "Ascend": context.set_context(device_id=get_device_id()) dataset = instance.create_test_dataset(batch_size=config.batch_size) loss = nn.SoftmaxCrossEntropyWithLogits(sparse=True) net = TextCNN(vocab_len=instance.get_dict_len(), word_len=config.word_len, num_classes=config.num_classes, vec_length=config.vec_length) opt = nn.Adam(filter(lambda x: x.requires_grad, net.get_parameters()), learning_rate=0.001, weight_decay=float(config.weight_decay)) param_dict = load_checkpoint(config.checkpoint_file_path) print("load checkpoint from [{}].".format(config.checkpoint_file_path)) load_param_into_net(net, param_dict) net.set_train(False) model = Model(net, loss_fn=loss, optimizer=opt, metrics={'acc': Accuracy()}) acc = model.eval(dataset) print("accuracy: ", acc)
help="device target") parser.add_argument('--dataset', type=str, default='MR', choices=['MR', 'SUBJ', 'SST2'], help='dataset name.') args = parser.parse_args() context.set_context(mode=context.GRAPH_MODE, device_target=args.device_target, device_id=args.device_id) if __name__ == '__main__': if args.dataset == 'MR': cfg = cfg_mr instance = MovieReview(root_dir=cfg.data_path, maxlen=cfg.word_len, split=0.9) elif args.dataset == 'SUBJ': cfg = cfg_subj instance = Subjectivity(root_dir=cfg.data_path, maxlen=cfg.word_len, split=0.9) elif args.dataset == 'SST2': cfg = cfg_sst2 instance = SST2(root_dir=cfg.data_path, maxlen=cfg.word_len, split=0.9) else: raise ValueError("dataset is not support.") net = TextCNN(vocab_len=instance.get_dict_len(), word_len=cfg.word_len, num_classes=cfg.num_classes, vec_length=cfg.vec_length) param_dict = load_checkpoint(args.ckpt_file) load_param_into_net(net, param_dict) input_arr = Tensor(np.ones([cfg.batch_size, cfg.word_len], np.int32)) export(net, input_arr, file_name=args.file_name, file_format=args.file_format)
def train_net(): '''train net''' # set context context.set_context(mode=context.GRAPH_MODE, device_target=config.device_target) context.set_context(device_id=get_device_id()) if config.dataset == 'MR': instance = MovieReview(root_dir=config.data_path, maxlen=config.word_len, split=0.9) elif config.dataset == 'SUBJ': instance = Subjectivity(root_dir=config.data_path, maxlen=config.word_len, split=0.9) elif config.dataset == 'SST2': instance = SST2(root_dir=config.data_path, maxlen=config.word_len, split=0.9) dataset = instance.create_train_dataset(batch_size=config.batch_size, epoch_size=config.epoch_size) batch_num = dataset.get_dataset_size() base_lr = float(config.base_lr) learning_rate = [] warm_up = [ base_lr / math.floor(config.epoch_size / 5) * (i + 1) for _ in range(batch_num) for i in range(math.floor(config.epoch_size / 5)) ] shrink = [ base_lr / (16 * (i + 1)) for _ in range(batch_num) for i in range(math.floor(config.epoch_size * 3 / 5)) ] normal_run = [ base_lr for _ in range(batch_num) for i in range(config.epoch_size - math.floor(config.epoch_size / 5) - math.floor(config.epoch_size * 2 / 5)) ] learning_rate = learning_rate + warm_up + normal_run + shrink net = TextCNN(vocab_len=instance.get_dict_len(), word_len=config.word_len, num_classes=config.num_classes, vec_length=config.vec_length) # Continue training if set pre_trained to be True if config.pre_trained: param_dict = load_checkpoint(config.checkpoint_path) load_param_into_net(net, param_dict) opt = nn.Adam(filter(lambda x: x.requires_grad, net.get_parameters()), \ learning_rate=learning_rate, weight_decay=float(config.weight_decay)) loss = SoftmaxCrossEntropyExpand(sparse=True) model = Model(net, loss_fn=loss, optimizer=opt, metrics={'acc': Accuracy()}) config_ck = CheckpointConfig( save_checkpoint_steps=int(config.epoch_size * batch_num / 2), keep_checkpoint_max=config.keep_checkpoint_max) time_cb = TimeMonitor(data_size=batch_num) ckpt_save_dir = os.path.join(config.output_path, config.checkpoint_path) ckpoint_cb = ModelCheckpoint(prefix="train_textcnn", directory=ckpt_save_dir, config=config_ck) loss_cb = LossMonitor() model.train(config.epoch_size, dataset, callbacks=[time_cb, ckpoint_cb, loss_cb]) print("train success")