def _load_datasets(train_src, pre_trained_src, dev_src=None): ds_dev = None ds_train = SNLIDataset(train_src, pre_trained_src) if dev_src: ds_dev = SNLIDataset(dev_src) ds_dev.load_word_vocabulary(ds_train.word_vocabulary) return ds_train, ds_dev
def predict(self, dataset: SNLIDataset): self.model.eval() test_data_loader = DataLoader( dataset, batch_size=self._batch_size, collate_fn=dataset.collate_fn, shuffle=False ) with torch.no_grad(): loss_count = 0 good_pred = 0 all_pred = 0 results = [] len_data = len(test_data_loader) for batch_index, (p, h, pw, hw, pc, hc, label) in enumerate(test_data_loader): stdout.write("\r\r\r%d" % int(100 * (batch_index + 1) / len_data) + "%") stdout.flush() pw, pc, hw, hc, label = self._to_test_variables(pw, pc, hw, hc, label) output = self._model(pw, pc, hw, hc) # calculate accuracy and loss loss_count += self._loss_func(output, label) good_pred += sum([1 if i.item() == j.item() else 0 for i, j in zip(torch.argmax(output, dim=1), label)]) all_pred += label.shape[0] for premise, hypothesis, pred in zip(p, h, torch.argmax(output, dim=1)): results.append((dataset.label(pred.item()), " ".join(premise), " ".join(hypothesis))) loss = float(loss_count / len(test_data_loader)) accuracy = good_pred / all_pred print("loss=" + str(loss) + " ------ accuracy=" + str(accuracy)) return results, loss, accuracy
x = self._layer1(x) x = self._activation(x) x = self._output_layer(x) x = softmax(x, dim=1) return x if __name__ == "__main__": import os from utils.data_loader import SNLIDataset from utils.params import TRAIN_SRC, PRE_TRAINED_SRC, ChrLevelCnnParams, SequenceEncoderParams from torch.utils.data import DataLoader from utils.cnn_character_level_model import CharacterCnnEmbed from utils.bi_lstm_sequence_model import SequenceEncoderModel ds = SNLIDataset(os.path.join("..", TRAIN_SRC), os.path.join("..", PRE_TRAINED_SRC)) chr_params_ = ChrLevelCnnParams(chr_vocab_dim=ds.len_chars_vocab) word_params_ = SequenceEncoderParams(word_vocab_dim=ds.len_words_vocab, pre_trained=ds.word_embed_mx) top_layer_params_ = TopLayerParams() chr_model = CharacterCnnEmbed(chr_params_) seq_model = SequenceEncoderModel(word_params_) top_layer_model = TopLayerModel(top_layer_params_) dl = DataLoader(dataset=ds, batch_size=64, collate_fn=ds.collate_fn) for i, (p, h, pw, hw, pc, hc, label) in enumerate(dl): premise_v = seq_model(pw, chr_model(pc)) hypothesis_v = seq_model(hw, chr_model(hc)) out = top_layer_model(premise_v, hypothesis_v) e = 0
def _get_test_dataset(test_src, vocab): ds_test = SNLIDataset(test_src) ds_test.load_word_vocabulary(vocab) return ds_test
for premise, hypothesis, pred in zip(p, h, torch.argmax(output, dim=1)): results.append((dataset.label(pred.item()), " ".join(premise), " ".join(hypothesis))) loss = float(loss_count / len(test_data_loader)) accuracy = good_pred / all_pred print("loss=" + str(loss) + " ------ accuracy=" + str(accuracy)) return results, loss, accuracy if __name__ == "__main__": import os from utils.data_loader import SNLIDataset from utils.params import TRAIN_SRC, DEV_SRC, TEST_SRC, PRE_TRAINED_SRC, ChrLevelCnnParams, SequenceEncoderParams,\ TopLayerParams, SNLIFullModelParams # datasets ds_train = SNLIDataset(os.path.join("..", TRAIN_SRC), os.path.join("..", PRE_TRAINED_SRC)) ds_dev = SNLIDataset(os.path.join("..", DEV_SRC)) ds_test = SNLIDataset(os.path.join("..", TEST_SRC)) ds_dev.load_word_vocabulary(ds_train.word_vocabulary) ds_test.load_word_vocabulary(ds_train.word_vocabulary) # model model_params = SNLIFullModelParams(ChrLevelCnnParams(chr_vocab_dim=ds_train.len_chars_vocab), SequenceEncoderParams(word_vocab_dim=ds_train.len_words_vocab, pre_trained=ds_train.word_embed_mx), SequenceEncoderParams(word_vocab_dim=ds_train.len_words_vocab), TopLayerParams()) model_ = SNLIModel(model_params) # activator params_ = SNLIActivatorParams() activator = SNLIActivator(model_, params_, ds_train, ds_dev)