args.save_dir, datetime.datetime.now().strftime('%Y-%m-%d_%H-%M-%S')) print("\nParameters:") for attr, value in sorted(args.__dict__.items()): print("\t{}={}".format(attr.upper(), value)) # model if args.model_type == 'textcnn': cnn = model.CNN_Text(args) elif args.model_type == 'gatecnn': print("current model is gatecnn") cnn = model.GatedCNN(args) elif args.model_type == 'lstm': print("current model is lstm") cnn = model.LSTMClassifier(args) elif args.model_type == 'gatecnn_tanh': print("current model is gatecnn with tanh") cnn = model.GatedCNN_tanh(args) else: cnn = model.CNN_Text(args) if args.snapshot is not None: print('\nLoading model from {}...'.format(args.snapshot)) cnn.load_state_dict(torch.load(args.snapshot)) if args.cuda: torch.cuda.set_device(args.device) cnn = cnn.cuda() # train or predict
def train(train_loader, batch_size, vocab_size, bidirectional=None): """Run the training loop. Parameters ---------- train_loader : DataLoader The training dataset in -Loader format. batch_size : int vocab_size : int Returns ------- LSTMClassifier The trained LSTM. """ EMBEDDING_DIM = 32 HIDDEN_DIM = 128 if bidirectional: LSTM = model.BiLSTMClassifier( EMBEDDING_DIM, HIDDEN_DIM, vocab_size, batch_size) else: LSTM = model.LSTMClassifier( EMBEDDING_DIM, HIDDEN_DIM, vocab_size, batch_size) if use_cuda: LSTM = LSTM.cuda() loss_function = nn.NLLLoss() optimizer = optim.SGD(LSTM.parameters(), lr=0.1) print_every = 1 n_epochs = 1 for epoch in range(1, n_epochs+1): start = time.time() epoch_loss = 0 for data in train_loader: if use_cuda: sentence = Variable(data["sentence"].cuda()) label = Variable(data["language"].cuda()) else: sentence = Variable(data["sentence"]) label = Variable(data["language"]) LSTM.zero_grad() LSTM.hidden = LSTM.init_hidden() pred = LSTM(sentence) loss = loss_function(pred, label) epoch_loss += loss.data loss.backward() optimizer.step() if epoch % print_every == 0: loss_avg = epoch_loss / print_every print("%s (%d %d%%) %.4f" % ( utils.time_since(start, epoch / n_epochs), epoch, epoch / n_epochs * 100, loss_avg)) return LSTM
def prepare_wordvec(seq): word_vector = [] for w in seq: w = spell(w) if w in word_to_ix: word_vector.append(word_to_ix[w]) else: word_vector.append(word_to_ix["none"]) c = len(word_vector) word_vector = np.array(word_vector) word_vector = word_vector.reshape(c, 300) vec = Variable(torch.from_numpy(word_vector)) return vec model = model.LSTMClassifier() model.load_state_dict(torch.load("./trained_model/sentiment.pt")) model.eval() print("fasttext loaded. Please enter your feedback\n") while 1: feedback = input() if len(feedback) == 0: continue else: test = utils.normalizeString(feedback) length = len(test.split()) test = prepare_wordvec(test.split()) test = test.view(1, length, 300) k = nn.utils.rnn.pack_padded_sequence(test, [length], batch_first=True) tag_scores = model(k, 1)
import data from torchvision import transforms from torchvision import datasets from torch.utils.data import DataLoader import model torch.manual_seed(1) trainids, trainseqs, trainlabels = data.load_train_data() testids, testseqs = data.load_test_data() word_to_ix = data.load_ix_dics() train_loader = data.create_dataset(trainids, trainseqs, trainlabels, word_to_ix, True, model.BATCH_SIZE) test_loader = data.create_dataset(testids, testseqs, [], word_to_ix, False, model.BATCH_SIZE) lstmmodel = model.LSTMClassifier(model.EMBEDDING_DIM, model.HIDDEN_DIM, len(word_to_ix)) loss_function = nn.NLLLoss() optimizer = optim.Adam(lstmmodel.parameters(), lr=0.001) for epoch in range(model.EPOCH_NUM): total_loss = 0 for ids, seqs, labels, lengths in train_loader: lstmmodel.zero_grad() lstmmodel.hidden = lstmmodel.init_hidden() tag_score = lstmmodel(seqs,lengths) loss = loss_function(tag_score, labels)
d_train,d_test = data.getCharDataset() m = model.CharacterLevelCNN() ADAMOP = fastNLP.Adam(lr=0.001,weight_decay=0,betas=(0.9,0.999)) trainner = Trainer( train_data=d_train, model=m, n_epochs=100, batch_size=128, use_cuda=True, check_code_level=0, optimizer=ADAMOP, dev_data=d_test, metrics=core.metrics.AccuracyMetric(target="label") ) trainner.train() if RNN: d_train,d_test,embedding = data.getWordDataset() m = model.LSTMClassifier(32,20,256,400001,200,embedding) ADAMOP = fastNLP.Adam(lr=0.001,weight_decay=0,betas=(0.9,0.999)) trainner = Trainer( train_data=d_train, model=m, n_epochs=100, batch_size=32, use_cuda=True, check_code_level=-1, optimizer=ADAMOP, dev_data=d_test, metrics=core.metrics.AccuracyMetric(target="label") ) trainner.train()