项目作者: netrack

项目描述 :
Metrics for Keras. DEPRECATED since Keras 2.3.0
高级语言: Python
项目地址: git://github.com/netrack/keras-metrics.git
创建时间: 2018-05-23T19:54:15Z
项目社区:https://github.com/netrack/keras-metrics

开源协议:MIT License

下载


Keras Metrics

Deprecation Warning

Since Keras version 2.3.0, it provides all metrics available in this package.
It’s preferrable to use metrics from the original Keras package.

This package will be maintained for older version of Keras (<2.3.0).

Build Status

This package provides metrics for evaluation of Keras classification models.
The metrics are safe to use for batch-based model evaluation.

Installation

To install the package from the PyPi repository you can execute the following
command:

  1. pip install keras-metrics

Usage

The usage of the package is simple:

  1. import keras
  2. import keras_metrics as km
  3. model = models.Sequential()
  4. model.add(keras.layers.Dense(1, activation="sigmoid", input_dim=2))
  5. model.add(keras.layers.Dense(1, activation="softmax"))
  6. model.compile(optimizer="sgd",
  7. loss="binary_crossentropy",
  8. metrics=[km.binary_precision(), km.binary_recall()])

Similar configuration for multi-label binary crossentropy:

  1. import keras
  2. import keras_metrics as km
  3. model = models.Sequential()
  4. model.add(keras.layers.Dense(1, activation="sigmoid", input_dim=2))
  5. model.add(keras.layers.Dense(2, activation="softmax"))
  6. # Calculate precision for the second label.
  7. precision = km.binary_precision(label=1)
  8. # Calculate recall for the first label.
  9. recall = km.binary_recall(label=0)
  10. model.compile(optimizer="sgd",
  11. loss="binary_crossentropy",
  12. metrics=[precision, recall])

Keras metrics package also supports metrics for categorical crossentropy and
sparse categorical crossentropy:

  1. import keras_metrics as km
  2. c_precision = km.categorical_precision()
  3. sc_precision = km.sparse_categorical_precision()
  4. # ...

Tensorflow Keras

Tensorflow library provides the keras package as parts of its API, in
order to use keras_metrics with Tensorflow Keras, you are advised to
perform model training with initialized global variables:

  1. import numpy as np
  2. import keras_metrics as km
  3. import tensorflow as tf
  4. import tensorflow.keras as keras
  5. model = keras.Sequential()
  6. model.add(keras.layers.Dense(1, activation="softmax"))
  7. model.compile(optimizer="sgd",
  8. loss="binary_crossentropy",
  9. metrics=[km.binary_true_positive()])
  10. x = np.array([[0], [1], [0], [1]])
  11. y = np.array([1, 0, 1, 0])
  12. # Wrap model.fit into the session with global
  13. # variables initialization.
  14. with tf.Session() as s:
  15. s.run(tf.global_variables_initializer())
  16. model.fit(x=x, y=y)