示例#1
0
    params = {'dim': (input_size,),
              'batch_size': config["training"]["batch_size"],
              'n_classes': config["data"]["n_classes"],
              'shuffle': True}

    # Datasets
    with open(config["data"]["save_ratings"], 'rb') as fp:
        labels = pickle.load(fp)

    # Generators
    training_generator = DataGenerator(train_indices, labels, config["data"]["save_reviews"], **params)
    validation_generator = DataGenerator(valid_indices, labels, config["data"]["save_reviews"], **params)

    # Define model
    model = CharCNN(input_size=input_size,
                    alphabet_size=alphabet_size,
                     embedding_size=config["char_cnn"]["embedding_size"],
                     conv_layers=config["char_cnn"]["conv_layers"],
                     fully_connected_layers=config["char_cnn"]["fully_connected_layers"],
                     n_classes=config["data"]["n_classes"],
                     threshold=config["char_cnn"]["threshold"],
                     dropout_p=config["char_cnn"]["dropout_p"],
                     optimizer=config["char_cnn"]["optimizer"],
                     loss=config["char_cnn"]["loss"])
    # Train model
    model.train(training_gen=training_generator,
                validation_gen=validation_generator,
                epochs=config["training"]["epochs"],
                batch_size=config["training"]["batch_size"],
                log_freq=config["training"]["log_freq"])
示例#2
0
    dataTest = Data(list(zip(contents_test, labels_test)), alphabet,
                    input_size)
    test_data, test_labels = dataTest.convert_data()

    # Initialize the model
    model = CharCNN(input_sz=config["data"]["input_size"],
                    alphabet_sz=config["data"]["alphabet_size"],
                    emb_sz=config["char_cnn_zhang"]["embedding_size"],
                    conv_layers=config["char_cnn_zhang"]["conv_layers"],
                    fc_layers=[],
                    threshold=config["char_cnn_zhang"]["threshold"],
                    dropout_p=config["char_cnn_zhang"]["dropout_p"],
                    optimizer=config["char_cnn_zhang"]["optimizer"],
                    loss=config["char_cnn_zhang"]["loss"])

    # Train
    model.train(train_inputs=train_data,
                train_labels=train_labels,
                val_inputs=val_data,
                val_labels=val_labels,
                epochs=config["training"]["epochs"],
                bs=config["training"]["batch_size"])

    # Evaluate
    results = model.test(test_data, test_labels, bs=128)
    model.test_model(test_data, test_labels, bs=128)
    print(results)

    # Save the model
    model.model.save('model.h5')