コード例 #1
0
from models import BiLSTM
from utils import aspects_sent_convert, read_file_lines, write_file_lines
from import_dy import dy
from random_inits import np

# Use a sentence level classifier to automatically label sentences.
if __name__ == "__main__":
    label_files = sys.argv[1].split(',')
    lines = list()
    for label_file in label_files:
        lines += read_file_lines(label_file)
    model = dy.ParameterCollection()

    W2I = pickle.load(open("models/SentenceLevelBiLSTM" + "/Model.meta", "rb"))
    network = BiLSTM(1, 300, 150, W2I, model)
    network.load_model("models/SentenceLevelBiLSTM/Model")
    new_samples = list()

    for i in range(0, len(lines), 1000):
        print str(i) + " Sentences Labeled"

        samples = aspects_sent_convert(lines[i:i + 1000])
        for sample in samples:
            dy.renew_cg()
            sent = sample.sent
            h = network(sent[0])
            dist = network.classify(h)
            prediction = np.argmax(dist.npvalue())

            sample_str = sent[0] + u"<->" + str(prediction)
コード例 #2
0
                command_line_file.write(arg + "\n")
            command_line_file.close()
        else:
            print "Model Folder Already exists."
            sys.exit(1)
        network.save_meta(model_path + "/Model.meta")
        trainer = dy.AdamTrainer(model)

        dists = np.load(args.dist_path)
        xr_train(network,
                 trainer,
                 train,
                 dev,
                 args.epochs,
                 30,
                 model_path,
                 dists,
                 T_value=args.T,
                 batch_size=args.batch_size)
    network.load_model(model_path + "/Model")
    acc, loss, f1 = aspect_test(test, network, 30)
    test_acc = "Test Accuarcy:" + str(acc)
    test_loss = "Test Loss:" + str(loss)
    test_f1 = "Test F1:" + str(f1)
    print test_acc
    print test_loss
    print test_f1
    test_results_file = open(model_path + '/Results/test_results.txt', 'w')
    test_results_file.write(test_acc + "\n" + test_loss + "\n" + test_f1)
    test_results_file.close()
コード例 #3
0
    dynet_config.set(random_seed=args.seed)
    from train_funcs import *

    dyparams = dy.DynetParams()
    dyparams.set_autobatch(True)
    from models import BiLSTM, LSTMAtt

    model = dy.ParameterCollection()
    model_params = model
    W2I, vecs = pickle.load(
        open("models/" + args.pretrained_model + "/Model.meta", "rb")), None

    # Create the base bilstm network, which will be pretrained.
    base_network = BiLSTM(1, 300, 150, W2I, model, vecs)
    # Load the pretrained bilstm network
    base_network.load_model("models/" + args.pretrained_model + "/Model")
    # Add attention weights to the pretrained bilstm network.
    network = LSTMAtt(base_network, model, 300, 150)
    if not os.path.exists(model_path):
        os.makedirs(model_path)
        os.makedirs(model_path + "/Results")
        command_line_file = open(model_path + "/Command_Lines.txt", 'w')
        for arg in sys.argv:
            command_line_file.write(arg + "\n")
        command_line_file.close()
    else:
        print "Model Folder Already exists."
        sys.exit(1)
    network.save_meta(model_path + "/Model.meta")
    trainer = dy.AdamTrainer(model)
    # Finetune the model.