Skip to main content

Keras Activations and Gradients

Project description

Keract: Keras Activations + Gradients

pip install keract

You have just found a (easy) way to get the activations (outputs) and gradients for each layer of your Keras model (LSTM, conv nets...).

API

Get activations (outputs of each layer)

from keract import get_activations
get_activations(model, x)

Inputs are:

  • model is a keras.models.Model object.
  • x is a numpy array to feed to the model as input. In the case of multi-input, x is of type List. We use the Keras convention (as used in predict, fit...).

The output is a dictionary containing the activations for each layer of model for the input x:

{
  'conv2d_1/Relu:0': np.array(...),
  'conv2d_2/Relu:0': np.array(...),
  ...,
  'dense_2/Softmax:0': np.array(...)
}

The key is the name of the layer and the value is the corresponding output of the layer for the given input x.

Get gradients of weights

  • model is a keras.models.Model object.
  • x Input data (numpy array). Keras convention.
  • y: Labels (numpy array). Keras convention.
from keract import get_gradients_of_trainable_weights
get_gradients_of_trainable_weights(model, x, y)

The output is a dictionary mapping each trainable weight to the values of its gradients (regarding x and y).

Get gradients of get_gradients_of_activations

  • model is a keras.models.Model object.
  • x Input data (numpy array). Keras convention.
  • y: Labels (numpy array). Keras convention.
from keract import get_gradients_of_activations
get_gradients_of_activations(model, x, y)

The output is a dictionary mapping each layer to the values of its gradients (regarding x and y).

Examples

Examples are provided for:

  • keras.models.Sequential - mnist.py
  • keras.models.Model - multi_inputs.py
  • Recurrent networks - recurrent.py

In the case of MNIST with LeNet, we are able to fetch the activations for a batch of size 128:

conv2d_1/Relu:0
(128, 26, 26, 32)

conv2d_2/Relu:0
(128, 24, 24, 64)

max_pooling2d_1/MaxPool:0
(128, 12, 12, 64)

dropout_1/cond/Merge:0
(128, 12, 12, 64)

flatten_1/Reshape:0
(128, 9216)

dense_1/Relu:0
(128, 128)

dropout_2/cond/Merge:0
(128, 128)

dense_2/Softmax:0
(128, 10)

We can even visualise some of them.


A random seven from MNIST


Activation map of CONV1 of LeNet


Activation map of FC1 of LeNet


Activation map of Softmax of LeNet. Yes it's a seven!

Repo views (since 2018/10/31)

HitCount

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

keract-2.1.1.tar.gz (3.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

keract-2.1.1-py2.py3-none-any.whl (7.7 kB view details)

Uploaded Python 2Python 3

File details

Details for the file keract-2.1.1.tar.gz.

File metadata

  • Download URL: keract-2.1.1.tar.gz
  • Upload date:
  • Size: 3.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.12.1 pkginfo/1.4.2 requests/2.21.0 setuptools/39.0.1 requests-toolbelt/0.8.0 tqdm/4.28.1 CPython/3.6.2

File hashes

Hashes for keract-2.1.1.tar.gz
Algorithm Hash digest
SHA256 fc960c5877401968b4e25b4c58c246c334fc4fb488932feff3ff48f597527ac7
MD5 162b4ebd69cb3e3ee116749e57d0bd39
BLAKE2b-256 87ee5eadc338ca9ccde36321ef8f22a4287d601bbf367e93d3cc2abc97a39f9f

See more details on using hashes here.

File details

Details for the file keract-2.1.1-py2.py3-none-any.whl.

File metadata

  • Download URL: keract-2.1.1-py2.py3-none-any.whl
  • Upload date:
  • Size: 7.7 kB
  • Tags: Python 2, Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.12.1 pkginfo/1.4.2 requests/2.21.0 setuptools/39.0.1 requests-toolbelt/0.8.0 tqdm/4.28.1 CPython/3.6.2

File hashes

Hashes for keract-2.1.1-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 7579ed06467a2443ce8b85e88e9c67584c1991ea9de7e61034eb6d69677370cd
MD5 75825e2b8b92ea6b8c6ff2355a7d45a2
BLAKE2b-256 5159275a98570b5446dded5fe4cbf38204c9485777b13ffc78d156e871de7fc7

See more details on using hashes here.

Supported by

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