Skip to main content

Tensorflow Keras implementation of CORAL ordinal regression output layer, loss, activation, and metrics

Project description

Ordinal regression in Tensorflow Keras

Tensorflow Keras implementation of ordinal regression (aka ordinal classification) using consistent rank logits (CORAL) by Cao, Mirjalili, & Raschka (2019).

This package includes:

  • Ordinal output layer: CoralOrdinal()
  • Ordinal loss function: OrdinalCrossEntropy()
  • Ordinal error metric: MeanAbsoluteErrorLabels()
  • Ordinal activation function: ordinal_softmax()

This is a work in progress, so please post any issues to the issue queue. The package was developed as part of the Berkeley D-Lab's hate speech measurement project and paper (Kennedy et al. 2020).

Acknowledgments: Many thanks to Sebastian Raschka for the help in porting from the PyTorch source repository.

Key pending items:

  • Function docstrings
  • Docs
  • Tests

Installation

Install the stable version via pip:

pip install coral-ordinal

Install the most recent code on GitHub via pip:

pip install git+https://github.com/ck37/coral-ordinal/

Dependencies

This package relies on Python 3.6+, Tensorflow 2.2+, and numpy.

Example

This is a quick example to show a basic model implementation. With actual data one would also want to specify the input shape.

import coral_ordinal as coral
model = tf.keras.Sequential()
model.add(tf.keras.layers.Dense(32, activation = "relu"))
model.add(coral.CoralOrdinal(num_classes = 5)) # Ordinal variable has 5 labels, 0 through 4.
model.compile(loss = coral.OrdinalCrossEntropy(), metrics = [coral.MeanAbsoluteErrorLabels])

See this colab notebook for extended examples of ordinal regression with MNIST (multilayer perceptron) and Amazon reviews (universal sentence encoder).

Note that the minimum value of the ordinal variable needs to be 0. If your labeled data ranges from 1 to 5, you will need to subtract 1 so that it is scaled to be 0 to 4.

References

Cao, W., Mirjalili, V., & Raschka, S. (2019). Consistent rank logits for ordinal regression with convolutional neural networks. arXiv preprint arXiv:1901.07884, 6.

Kennedy, C., Bacon, G., Sahn, A., Broege, N., & von Vacano, C. (2020). Constructing interval latent variables via Rasch measurement and multitask deep learning: a hate speech application. https://hatespeech.berkeley.edu

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

coral-ordinal-0.1.5.tar.gz (6.1 kB view details)

Uploaded Source

Built Distribution

coral_ordinal-0.1.5-py3-none-any.whl (9.0 kB view details)

Uploaded Python 3

File details

Details for the file coral-ordinal-0.1.5.tar.gz.

File metadata

  • Download URL: coral-ordinal-0.1.5.tar.gz
  • Upload date:
  • Size: 6.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.24.0 setuptools/47.3.1 requests-toolbelt/0.9.1 tqdm/4.46.1 CPython/3.7.7

File hashes

Hashes for coral-ordinal-0.1.5.tar.gz
Algorithm Hash digest
SHA256 64a733d671db69365aa6189a29fbac3d1e820516ef04ebe5fbb1a50855c892be
MD5 f70c9cca9a73e209c11b38d476d97428
BLAKE2b-256 7dd7b4f74d355709c8a85a1e621cad1ceffa4f7074009de66a65cedf368ca12c

See more details on using hashes here.

File details

Details for the file coral_ordinal-0.1.5-py3-none-any.whl.

File metadata

  • Download URL: coral_ordinal-0.1.5-py3-none-any.whl
  • Upload date:
  • Size: 9.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.24.0 setuptools/47.3.1 requests-toolbelt/0.9.1 tqdm/4.46.1 CPython/3.7.7

File hashes

Hashes for coral_ordinal-0.1.5-py3-none-any.whl
Algorithm Hash digest
SHA256 be65b221dffcd231bc895ed3bde00ccc068005bfa05e1b1ae57d5097b9de1cd9
MD5 89cd83026c303677ed6aa74ce940c3c0
BLAKE2b-256 2308deadeeb4e7571a9f07d355ddd4bf13c74f7bd781232701f11df25b3b868b

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page