def train(): train_args, model_args = ConfigSection(), ConfigSection() ConfigLoader.load_config(config_dir, {"text_class": train_args}) # load dataset print("Loading data...") ds_loader = ClassDatasetLoader(train_data_dir) data = ds_loader.load() print(data[0]) # pre-process data pre = ClassPreprocess() data_train = pre.run(data, pickle_path=save_dir) print("vocabulary size:", pre.vocab_size) print("number of classes:", pre.num_classes) model_args["num_classes"] = pre.num_classes model_args["vocab_size"] = pre.vocab_size # construct model print("Building model...") model = CNNText(model_args) # ConfigSaver().save_config(config_dir, {"text_class_model": model_args}) # train print("Training...") # 1 # trainer = ClassificationTrainer(train_args) # 2 trainer = ClassificationTrainer(epochs=train_args["epochs"], batch_size=train_args["batch_size"], validate=train_args["validate"], use_cuda=train_args["use_cuda"], pickle_path=save_dir, save_best_dev=train_args["save_best_dev"], model_name=model_name, loss=Loss("cross_entropy"), optimizer=Optimizer("SGD", lr=0.001, momentum=0.9)) trainer.train(model, data_train) print("Training finished!") saver = ModelSaver(os.path.join(save_dir, model_name)) saver.save_pytorch(model) print("Model saved!")
def train(): train_args, model_args = ConfigSection(), ConfigSection() ConfigLoader.load_config(config_dir, {"text_class": train_args}) # load dataset print("Loading data...") data = TextClassifyDataSet(load_func=ClassDataSetLoader.load) data.load(train_data_dir) print("vocabulary size:", len(data.word_vocab)) print("number of classes:", len(data.label_vocab)) save_pickle(data.word_vocab, save_dir, "word2id.pkl") save_pickle(data.label_vocab, save_dir, "label2id.pkl") model_args["num_classes"] = len(data.label_vocab) model_args["vocab_size"] = len(data.word_vocab) # construct model print("Building model...") model = CNNText(model_args) # train print("Training...") trainer = ClassificationTrainer(epochs=train_args["epochs"], batch_size=train_args["batch_size"], validate=train_args["validate"], use_cuda=train_args["use_cuda"], pickle_path=save_dir, save_best_dev=train_args["save_best_dev"], model_name=model_name, loss=Loss("cross_entropy"), optimizer=Optimizer("SGD", lr=0.001, momentum=0.9)) trainer.train(model, data) print("Training finished!") saver = ModelSaver(os.path.join(save_dir, model_name)) saver.save_pytorch(model) print("Model saved!")
# construct model model_args = { 'num_classes': n_classes, 'vocab_size': vocab_size } model = ClassificationModel(num_classes=n_classes, vocab_size=vocab_size) # construct trainer train_args = { "epochs": 3, "batch_size": 16, "pickle_path": data_dir, "validate": False, "save_best_dev": False, "model_saved_path": None, "use_cuda": True, "loss": Loss("cross_entropy"), "optimizer": Optimizer("Adam", lr=0.001) } trainer = ClassificationTrainer(**train_args) # start training trainer.train(model, train_data=train_set, dev_data=dev_set) # predict using model data_infer = [x[0] for x in data] infer = ClassificationInfer(data_dir) labels_pred = infer.predict(model.cpu(), data_infer) print(labels_pred)
dim=attention_unit, num_vec=attention_hops) self.mlp = MLP( size_layer=[lstm_hidden_size * 2 * attention_hops, nfc, class_num]) def forward(self, x): x_emb = self.embedding(x) output = self.lstm(x_emb) after_attention, penalty = self.attention(output, x) after_attention = after_attention.view(after_attention.size(0), -1) output = self.mlp(after_attention) return output def loss(self, predict, ground_truth): print("predict:%s; g:%s" % (str(predict.size()), str(ground_truth.size()))) print(ground_truth) return F.cross_entropy(predict, ground_truth) train_args = ConfigSection() ConfigLoader("good path").load_config('config.cfg', {"train": train_args}) train_args['vocab'] = len(word2index) trainer = ClassificationTrainer(**train_args.data) # for k in train_args.__dict__.keys(): # print(k, train_args[k]) model = SELF_ATTENTION_YELP_CLASSIFICATION(train_args) trainer.train(model, train_data, dev_data)