################################################ if model_name == 'SIMPLE': model_cfg = TrainingConfigManager(path_man.Doc() + 'config.json') else: model_cfg = TrainingConfigManager('../run/runConfig.json') modelParams = model_cfg.modelParams() dataset = SeqFileDataset(path_man.FileData(), path_man.FileSeqLen(), N=N) dataloader = DataLoader(dataset, batch_size=N, collate_fn=batchSequenceWithoutPad) if model_name != 'Random': state_dict = t.load(path_man.Model() + '_v%s.0' % version) word_matrix = state_dict['Embedding.weight'] else: word_matrix = t.Tensor( np.load(path_man.WordEmbedMatrix(), allow_pickle=True)) loss_fn = t.nn.NLLLoss().cuda() if model_name == 'SIMPLE': model = SIMPLE(word_matrix, **modelParams) model.load_state_dict(state_dict) elif model_name == 'FT': model = FT(class_n, loss_fn, word_matrix, **modelParams) model.load_state_dict(state_dict) elif model_name == 'Random': model = FT(class_n, loss_fn, word_matrix, **modelParams)
train_task = ImpEpisodeTask(k, qk, n, N, train_dataset, cuda=True, expand=expand, parallel=modelParams['data_parallel_devices']) val_task = ImpEpisodeTask(k, qk, n, N, val_dataset, cuda=True, expand=expand, parallel=modelParams['data_parallel_devices']) else: train_task = ProtoEpisodeTask(k, qk, n, N, train_dataset, cuda=True, expand=expand, parallel=modelParams['data_parallel_devices']) val_task = ProtoEpisodeTask(k, qk, n, N, val_dataset, cuda=True, expand=expand, parallel=modelParams['data_parallel_devices']) stat = TrainStatManager(model_save_path=train_path_manager.Model(), stat_save_path=train_path_manager.Doc(), train_report_iter=ValCycle, criteria=criteria) if RecordGradient: types.append('line') titles.append('gradient') xlabels.append('iterations') ylabels.append('gradient norm') legends.append(['Encoder Gradient']) if UseVisdom: plot = VisdomPlot(env_title='train monitoring', types=types, titles=titles,
test_task = ProtoEpisodeTask(k, qk, n, N, test_dataset, cuda=True, expand=expand) stat = TestStatManager() ################################################ #----------------------模型定义和初始化------------------ ################################################ printState('init model...') state_dict = t.load(test_path_manager.Model()) if model_type in ADAPTED_MODELS: word_matrix = state_dict['Learner.Embedding.weight'] else: word_matrix = state_dict['Embedding.weight'] loss = t.nn.NLLLoss().cuda() if loss_func == 'nll' else t.nn.MSELoss().cuda() if model_type == 'FEAT': model = FEAT(pretrained_matrix=word_matrix, **modelParams) else: raise ValueError('不支持的模型类型:', model_type) model.load_state_dict(state_dict) model = model.cuda()
qk, n, N, test_dataset, cuda=True, expand=expand) stat = TestStatManager() ################################################ #----------------------模型定义和初始化------------------ ################################################ printState('init model...') if not MODEL_RANDOM_STATE: state_dict = t.load(test_path_manager.Model(type=cfg.loadBest())) if model_type in ADAPTED_MODELS: word_matrix = state_dict['Learner.Embedding.weight'] else: word_matrix = state_dict['Embedding.weight'] else: word_matrix = t.Tensor( np.load(test_path_manager.WordEmbedMatrix(), allow_pickle=True)) print("loading done...") loss = t.nn.NLLLoss().cuda() if loss_func == 'nll' else t.nn.MSELoss().cuda() if model_type == 'ProtoNet': model = ProtoNet(pretrained_matrix=word_matrix, **modelParams) elif model_type == 'InductionNet': model = InductionNet(pretrained_matrix=word_matrix, **modelParams)