def calc_val_loss(val): loss = [] for i in range(len(val)): sentence = val[i][0] tags = val[i][1] sentence_in = prepare_sentence(sentence, word_to_ix) targets = prepare_tags(tags, tag_to_ix) nll = model.neg_log_likelihood(sentence_in, targets) loss.append(nll.item()) return sum(loss) / len(loss)
features_dim = len(features[0][0]) print("how many additional features: ", features_dim) EMBEDDING_DIM = 16 HIDDEN_DIM = 16 MAX_CHARS = 10 print("len(char_to_ix): ", len(char_to_ix)) print("len(word_to_ix): ", len(word_to_ix)) print("features_dim: ", features_dim) model = CNN_BLSTM_CRF(len(char_to_ix), MAX_CHARS, len(word_to_ix), tag_to_ix, features_dim, EMBEDDING_DIM, HIDDEN_DIM, False) model.load_state_dict(torch.load("out/lstm_crf_char/epoch20.hdf5")) # predict on val set true_tags = [] pred_tags = [] with torch.no_grad(): for i in range(len(examples)): precheck_sent = prepare_sentence(examples[i][0], word_to_ix) precheck_tags = prepare_tags(examples[i][1], tag_to_ix) precheck_features = prepare_features(features[i]) precheck_chars = prepare_char_level_sentence(examples[i][0], char_to_ix, MAX_CHARS) predicts = model(precheck_sent, precheck_features, precheck_chars) print("After training: {}\n ".format(predicts)) true_tags.extend(precheck_tags.tolist()) pred_tags.extend(predicts[1]) print(classification_report(true_tags, pred_tags))
for epoch in range( EPOCHES ): # again, normally you would NOT do 300 epochs, it is toy data print("epoch %d" % epoch) # generate new order of index idx = np.random.choice(NUM_TO_TRAIN, replace=False, size=NUM_TO_TRAIN) for i in idx: sentence = train[i][0] tags = train[i][1] # Step 1. Remember that Pytorch accumulates gradients. # We need to clear them out before each instance model.zero_grad() # Step 2. Get our inputs ready for the network, that is, # turn them into Tensors of word indices. sentence_in = prepare_sentence(sentence, word_to_ix) targets = prepare_tags(tags, tag_to_ix) features_in = prepare_features(features_train[i]) # Step 3. Run our forward pass. loss = model.neg_log_likelihood(sentence_in, targets, features_in) # if i % 10 == 0: print("loss: ", loss) train_loss.append(loss.item()) # Step 4. Compute the loss, gradients, and update the parameters by # calling optimizer.step() loss.backward() optimizer.step() # print average loss after each iteration val_loss = calc_val_loss(val)