Python keras 模块,metrics() 实例源码

我们从Python开源项目中,提取了以下3个代码示例,用于说明如何使用keras.metrics()

项目:dsde-deep-learning    作者:broadinstitute    | 项目源码 | 文件源码
def logistic_regression():
    train, test, valid = load_data('mnist.pkl.gz')

    epochs = 3200
    num_labels = 10
    train_y = make_one_hot(train[1], num_labels)
    valid_y = make_one_hot(valid[1], num_labels)
    test_y = make_one_hot(test[1], num_labels)

    logistic_model = Sequential()
    logistic_model.add(Dense(10, activation='softmax', input_dim=784, name='mnist_templates'))
    logistic_model.compile(loss='categorical_crossentropy', optimizer='sgd', metrics=['accuracy'])
    logistic_model.summary()
    templates = logistic_model.layers[0].get_weights()[0]
    plot_templates(templates, 0)
    print('weights shape:', templates.shape)

    for e in range(epochs):
        trainidx = random.sample(range(0, train[0].shape[0]), 8192)
        x_batch = train[0][trainidx,:]
        y_batch = train_y[trainidx]
        logistic_model.train_on_batch(x_batch, y_batch)
        if e % 5 == 0:
            plot_templates(logistic_model.layers[0].get_weights()[0], e)

    print('Test set loss and accuracy:', logistic_model.evaluate(test[0], test_y))
项目:dsde-deep-learning    作者:broadinstitute    | 项目源码 | 文件源码
def multilayer_perceptron():
    train, test, valid = load_data('mnist.pkl.gz')

    num_labels = 10
    train_y = make_one_hot(train[1], num_labels)
    valid_y = make_one_hot(valid[1], num_labels)
    test_y = make_one_hot(test[1], num_labels)

    mlp_model = Sequential()
    mlp_model.add(Dense(300, activation='relu', input_dim=784))
    mlp_model.add(Dense(10, activation='softmax'))
    mlp_model.compile(loss='categorical_crossentropy', optimizer='sgd', metrics=['accuracy'])
    mlp_model.fit(train[0], train_y, validation_data=(valid[0],valid_y), batch_size=32, epochs=10)
    print('Test set loss and accuracy:', mlp_model.evaluate(test[0], test_y))
项目:deeputil    作者:Avkash    | 项目源码 | 文件源码
def import_keras_model_config_and_weight_and_compile(model_config, model_weights,
                                                     model_loss_weights="none",
                                                     sample_weight_mode="none",
                                                     model_loss="categorical_crossentropy",
                                                     model_optimizer="rmsprop",
                                                     model_metrics=["acc"],
                                                     show_info=True
                                                     ):
    """
    This function loads a model config and weights from disk and then compile it from given parameters
    model_config:
    model_weights:
    model_weights_mode:
    loss:
    optimizer:
    metrics:
    :return: model (Keras Model)
    """
    model_local = Model

    #assert model_config
    #assert model_weights
    #assert sample_weight_mode
    #assert model_loss_weights

    # Check if given loss is part of keras.losses
    utils.helper_functions.show_print_message("Losses: " + model_loss, show_info)
    if model_loss not in definitions.Definitions.keras_losses:
        utils.helper_functions.show_print_message("Error: The given loss function is not a keras loss function.", show_info)
        return model_local

    # Check if given optimizer is part of keras.optimizer
    utils.helper_functions.show_print_message("Optimizers: " + model_optimizer, show_info)
    if model_optimizer not in definitions.Definitions.keras_optimizers:
        utils.helper_functions.show_print_message("Error: The given optimizer is not a keras optimizer.", show_info)
        return model_local

    # Check if given metrics is part of keras.metrics
    utils.helper_functions.show_print_message("Metrics: " + str(model_metrics), show_info)
    len(model_metrics)

    for i in range(len(model_metrics)):
        if model_metrics[i] not in definitions.Definitions.keras_metrics:
            utils.helper_functions.show_print_message("Error: The given metrics is not a keras metrics.", show_info)
            return model_local

    model_local = import_keras_model_json_from_disk(model_config, show_info)
    model_local = import_keras_model_weights_from_disk(model_local, model_weights, show_info)
    model_local.compile(loss=model_loss,
              optimizer=model_optimizer,
              metrics=model_metrics)
    utils.helper_functions.show_print_message("Model config and weight import is done along with compile!", show_info)
    return model_local