├── keras
├── legacy
│ └── __init__.py
├── wrappers
│ └── __init__.py
├── preprocessing
│ └── __init__.py
├── objectives.py
├── engine
│ ├── topology.py
│ └── __init__.py
├── datasets
│ ├── __init__.py
│ ├── mnist.py
│ ├── cifar.py
│ ├── cifar10.py
│ ├── cifar100.py
│ ├── boston_housing.py
│ ├── fashion_mnist.py
│ ├── reuters.py
│ └── imdb.py
├── applications
│ └── __init__.py
├── __init__.py
├── utils
│ ├── __init__.py
│ ├── np_utils.py
│ ├── io_utils.py
│ └── vis_utils.py
├── layers
│ └── __init__.py
├── regularizers.py
├── metrics.py
├── backend
│ ├── __init__.py
│ └── common.py
└── losses.py
├── setup.cfg
├── docker
├── theanorc
├── Makefile
├── README.md
└── Dockerfile
├── docs
├── templates
│ ├── preprocessing
│ │ ├── image.md
│ │ └── text.md
│ ├── index.md
│ ├── models
│ │ ├── sequential.md
│ │ ├── model.md
│ │ └── about-keras-models.md
│ ├── visualization.md
│ ├── constraints.md
│ ├── activations.md
│ ├── initializers.md
│ ├── layers
│ │ ├── about-keras-layers.md
│ │ └── writing-your-own-keras-layers.md
│ ├── optimizers.md
│ ├── losses.md
│ ├── regularizers.md
│ ├── metrics.md
│ ├── scikit-learn-api.md
│ ├── callbacks.md
│ └── backend.md
├── README.md
└── mkdocs.yml
├── MANIFEST.in
├── .gitignore
├── pytest.ini
├── .coveragerc
├── .github
└── stale.yml
├── ISSUE_TEMPLATE.md
├── tests
├── keras
│ ├── utils
│ │ ├── vis_utils_test.py
│ │ ├── np_utils_test.py
│ │ ├── layer_utils_test.py
│ │ ├── conv_utils_test.py
│ │ ├── io_utils_test.py
│ │ └── generic_utils_test.py
│ ├── layers
│ │ ├── noise_test.py
│ │ ├── advanced_activations_test.py
│ │ ├── embeddings_test.py
│ │ └── local_test.py
│ ├── legacy
│ │ └── layers_test.py
│ ├── constraints_test.py
│ ├── preprocessing
│ │ └── text_test.py
│ ├── regularizers_test.py
│ └── initializers_test.py
├── test_loss_masking.py
├── integration_tests
│ ├── test_image_data_tasks.py
│ ├── test_datasets.py
│ └── test_vector_data_tasks.py
└── test_dynamic_trainability.py
├── examples
├── imdb_bidirectional_lstm.py
├── mnist_mlp.py
├── imdb_lstm.py
├── reuters_mlp.py
├── imdb_cnn_lstm.py
├── imdb_cnn.py
├── mnist_cnn.py
├── mnist_irnn.py
├── mnist_hierarchical_rnn.py
├── antirectifier.py
├── lstm_text_generation.py
├── mnist_dataset_api.py
├── mnist_sklearn_wrapper.py
├── mnist_transfer_cnn.py
├── cifar10_cnn.py
├── README.md
├── conv_filter_visualization.py
└── mnist_siamese.py
├── LICENSE
├── setup.py
└── .travis.yml
/keras/legacy/__init__.py:
--------------------------------------------------------------------------------
1 |
--------------------------------------------------------------------------------
/keras/wrappers/__init__.py:
--------------------------------------------------------------------------------
1 |
--------------------------------------------------------------------------------
/setup.cfg:
--------------------------------------------------------------------------------
1 | [metadata]
2 | description-file = README.md
--------------------------------------------------------------------------------
/docker/theanorc:
--------------------------------------------------------------------------------
1 | [global]
2 | floatX = float32
3 | optimizer=None
4 | device = cuda
5 |
6 |
--------------------------------------------------------------------------------
/docs/templates/preprocessing/image.md:
--------------------------------------------------------------------------------
1 |
2 | # Image Preprocessing
3 |
4 | {{autogenerated}}
5 |
--------------------------------------------------------------------------------
/docs/templates/preprocessing/text.md:
--------------------------------------------------------------------------------
1 |
2 | ### Text Preprocessing
3 |
4 | {{autogenerated}}
5 |
--------------------------------------------------------------------------------
/keras/preprocessing/__init__.py:
--------------------------------------------------------------------------------
1 | from . import image
2 | from . import sequence
3 | from . import text
4 |
--------------------------------------------------------------------------------
/MANIFEST.in:
--------------------------------------------------------------------------------
1 | include LICENSE
2 | include README.md
3 | include CONTRIBUTING.md
4 | graft docs
5 | graft examples
6 | graft tests
7 |
--------------------------------------------------------------------------------
/keras/objectives.py:
--------------------------------------------------------------------------------
1 | """Legacy objectives module.
2 |
3 | Only kept for backwards API compatibility.
4 | """
5 | from __future__ import absolute_import
6 | from .losses import *
7 |
--------------------------------------------------------------------------------
/docs/templates/index.md:
--------------------------------------------------------------------------------
1 | # Keras: The Python Deep Learning library
2 |
3 |
4 |
5 | {{autogenerated}}
--------------------------------------------------------------------------------
/keras/engine/topology.py:
--------------------------------------------------------------------------------
1 | """This module is deprecated, but kept around for backwards compatibility.
2 | """
3 | from .base_layer import Layer, Node, InputSpec
4 | from .input_layer import Input, InputLayer
5 | from .network import Network, get_source_inputs
6 |
--------------------------------------------------------------------------------
/keras/datasets/__init__.py:
--------------------------------------------------------------------------------
1 | from __future__ import absolute_import
2 |
3 | from . import mnist
4 | from . import imdb
5 | from . import reuters
6 | from . import cifar10
7 | from . import cifar100
8 | from . import boston_housing
9 | from . import fashion_mnist
10 |
--------------------------------------------------------------------------------
/keras/engine/__init__.py:
--------------------------------------------------------------------------------
1 | # note: `Node` is an internal class,
2 | # it isn't meant to be used by Keras users.
3 | from .input_layer import Input
4 | from .input_layer import InputLayer
5 | from .base_layer import InputSpec
6 | from .base_layer import Layer
7 | from .network import get_source_inputs
8 | from .training import Model
9 |
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | *.DS_Store
2 | *.pyc
3 | *.swp
4 | temp/*
5 | dist/*
6 | build/*
7 | keras/datasets/data/*
8 | keras/datasets/temp/*
9 | docs/site/*
10 | docs/theme/*
11 | docs/sources/*
12 | tags
13 | Keras.egg-info
14 | examples/img/*
15 |
16 | # test-related
17 | .coverage
18 | .cache
19 | .pytest_cache
20 |
21 | # developer environments
22 | .idea
23 | .vscode
24 |
--------------------------------------------------------------------------------
/docs/templates/models/sequential.md:
--------------------------------------------------------------------------------
1 | # The Sequential model API
2 |
3 | To get started, read [this guide to the Keras Sequential model](/getting-started/sequential-model-guide).
4 |
5 | ## Useful attributes of Model
6 |
7 | - `model.layers` is a list of the layers added to the model.
8 |
9 |
10 | ----
11 |
12 | ## Sequential model methods
13 |
14 | {{autogenerated}}
--------------------------------------------------------------------------------
/keras/applications/__init__.py:
--------------------------------------------------------------------------------
1 | from .vgg16 import VGG16
2 | from .vgg19 import VGG19
3 | from .resnet50 import ResNet50
4 | from .inception_v3 import InceptionV3
5 | from .inception_resnet_v2 import InceptionResNetV2
6 | from .xception import Xception
7 | from .mobilenet import MobileNet
8 | from .mobilenetv2 import MobileNetV2
9 | from .densenet import DenseNet121, DenseNet169, DenseNet201
10 | from .nasnet import NASNetMobile, NASNetLarge
11 |
--------------------------------------------------------------------------------
/docs/README.md:
--------------------------------------------------------------------------------
1 | # Keras Documentation
2 |
3 | The source for Keras documentation is in this directory under `sources/`.
4 | Our documentation uses extended Markdown, as implemented by [MkDocs](http://mkdocs.org).
5 |
6 | ## Building the documentation
7 |
8 | - install MkDocs: `pip install mkdocs`
9 | - `cd` to the `docs/` folder and run:
10 | - `python autogen.py`
11 | - `mkdocs serve` # Starts a local webserver: [localhost:8000](localhost:8000)
12 | - `mkdocs build` # Builds a static site in "site" directory
13 |
--------------------------------------------------------------------------------
/pytest.ini:
--------------------------------------------------------------------------------
1 | # Configuration of py.test
2 | [pytest]
3 | addopts=-v
4 | -n 2
5 | --durations=20
6 |
7 | # Do not run tests in the build folder
8 | norecursedirs= build
9 |
10 | # PEP-8 The following are ignored:
11 | # E501 line too long (82 > 79 characters)
12 | # E402 module level import not at top of file - temporary measure to continue adding ros python packaged in sys.path
13 | # E731 do not assign a lambda expression, use a def
14 |
15 | pep8ignore=* E501 \
16 | * E402 \
17 | * E731 \
18 |
19 |
--------------------------------------------------------------------------------
/.coveragerc:
--------------------------------------------------------------------------------
1 | [report]
2 | # Regexes for lines to exclude from consideration
3 | exclude_lines =
4 | os.remove
5 | except ImportError
6 | # Don't complain if tests don't hit defensive assertion code:
7 | raise ImportError
8 | raise NotImplementedError
9 |
10 | # Don't complain if legacy support codes are not performed:
11 | if original_keras_version == '1':
12 |
13 | fail_under = 85
14 | show_missing = True
15 | omit =
16 | keras/applications/*
17 | keras/datasets/*
18 | keras/layers/cudnn_recurrent.py
19 | keras/legacy/*
20 | keras/utils/multi_gpu_utils.py
21 |
--------------------------------------------------------------------------------
/keras/__init__.py:
--------------------------------------------------------------------------------
1 | from __future__ import absolute_import
2 |
3 | from . import utils
4 | from . import activations
5 | from . import applications
6 | from . import backend
7 | from . import datasets
8 | from . import engine
9 | from . import layers
10 | from . import preprocessing
11 | from . import wrappers
12 | from . import callbacks
13 | from . import constraints
14 | from . import initializers
15 | from . import metrics
16 | from . import models
17 | from . import losses
18 | from . import optimizers
19 | from . import regularizers
20 |
21 | # Also importable from root
22 | from .layers import Input
23 | from .models import Model
24 | from .models import Sequential
25 |
26 | __version__ = '2.1.6'
27 |
--------------------------------------------------------------------------------
/.github/stale.yml:
--------------------------------------------------------------------------------
1 | # Configuration for probot-stale - https://github.com/probot/stale
2 |
3 | # Number of days of inactivity before an Issue or Pull Request becomes stale
4 | daysUntilStale: 90
5 | # Number of days of inactivity before a stale Issue or Pull Request is closed
6 | daysUntilClose: 30
7 | # Issues or Pull Requests with these labels will never be considered stale
8 | exemptLabels:
9 | - bug
10 | - Announcement
11 | - help wanted
12 | - To investigate
13 | # Label to use when marking as stale
14 | staleLabel: stale
15 | # Comment to post when marking as stale. Set to `false` to disable
16 | markComment: >
17 | This issue has been automatically marked as stale because it has not had
18 | recent activity. It will be closed after 30 days if no further activity
19 | occurs, but feel free to re-open a closed issue if needed.
20 |
--------------------------------------------------------------------------------
/docs/templates/visualization.md:
--------------------------------------------------------------------------------
1 |
2 | ## Model visualization
3 |
4 | The `keras.utils.vis_utils` module provides utility functions to plot
5 | a Keras model (using `graphviz`).
6 |
7 | This will plot a graph of the model and save it to a file:
8 | ```python
9 | from keras.utils import plot_model
10 | plot_model(model, to_file='model.png')
11 | ```
12 |
13 | `plot_model` takes two optional arguments:
14 |
15 | - `show_shapes` (defaults to False) controls whether output shapes are shown in the graph.
16 | - `show_layer_names` (defaults to True) controls whether layer names are shown in the graph.
17 |
18 | You can also directly obtain the `pydot.Graph` object and render it yourself,
19 | for example to show it in an ipython notebook :
20 | ```python
21 | from IPython.display import SVG
22 | from keras.utils.vis_utils import model_to_dot
23 |
24 | SVG(model_to_dot(model).create(prog='dot', format='svg'))
25 | ```
26 |
--------------------------------------------------------------------------------
/docs/templates/constraints.md:
--------------------------------------------------------------------------------
1 | ## Usage of constraints
2 |
3 | Functions from the `constraints` module allow setting constraints (eg. non-negativity) on network parameters during optimization.
4 |
5 | The penalties are applied on a per-layer basis. The exact API will depend on the layer, but the layers `Dense`, `Conv1D`, `Conv2D` and `Conv3D` have a unified API.
6 |
7 | These layers expose 2 keyword arguments:
8 |
9 | - `kernel_constraint` for the main weights matrix
10 | - `bias_constraint` for the bias.
11 |
12 |
13 | ```python
14 | from keras.constraints import max_norm
15 | model.add(Dense(64, kernel_constraint=max_norm(2.)))
16 | ```
17 |
18 | ## Available constraints
19 |
20 | - __max_norm(max_value=2, axis=0)__: maximum-norm constraint
21 | - __non_neg()__: non-negativity constraint
22 | - __unit_norm(axis=0)__: unit-norm constraint
23 | - __min_max_norm(min_value=0.0, max_value=1.0, rate=1.0, axis=0)__: minimum/maximum-norm constraint
24 |
--------------------------------------------------------------------------------
/keras/datasets/mnist.py:
--------------------------------------------------------------------------------
1 | """MNIST handwritten digits dataset.
2 | """
3 | from __future__ import absolute_import
4 | from __future__ import division
5 | from __future__ import print_function
6 |
7 | from ..utils.data_utils import get_file
8 | import numpy as np
9 |
10 |
11 | def load_data(path='mnist.npz'):
12 | """Loads the MNIST dataset.
13 |
14 | # Arguments
15 | path: path where to cache the dataset locally
16 | (relative to ~/.keras/datasets).
17 |
18 | # Returns
19 | Tuple of Numpy arrays: `(x_train, y_train), (x_test, y_test)`.
20 | """
21 | path = get_file(path,
22 | origin='https://s3.amazonaws.com/img-datasets/mnist.npz',
23 | file_hash='8a61469f7ea1b51cbae51d4f78837e45')
24 | f = np.load(path)
25 | x_train, y_train = f['x_train'], f['y_train']
26 | x_test, y_test = f['x_test'], f['y_test']
27 | f.close()
28 | return (x_train, y_train), (x_test, y_test)
29 |
--------------------------------------------------------------------------------
/keras/utils/__init__.py:
--------------------------------------------------------------------------------
1 | from __future__ import absolute_import
2 | from . import np_utils
3 | from . import generic_utils
4 | from . import data_utils
5 | from . import io_utils
6 | from . import conv_utils
7 |
8 | # Globally-importable utils.
9 | from .io_utils import HDF5Matrix
10 | from .data_utils import get_file
11 | from .data_utils import Sequence
12 | from .data_utils import GeneratorEnqueuer
13 | from .data_utils import OrderedEnqueuer
14 | from .generic_utils import CustomObjectScope
15 | from .generic_utils import custom_object_scope
16 | from .generic_utils import get_custom_objects
17 | from .generic_utils import serialize_keras_object
18 | from .generic_utils import deserialize_keras_object
19 | from .generic_utils import Progbar
20 | from .layer_utils import convert_all_kernels_in_model
21 | from .layer_utils import print_summary
22 | from .vis_utils import plot_model
23 | from .np_utils import to_categorical
24 | from .np_utils import normalize
25 | from .multi_gpu_utils import multi_gpu_model
26 |
--------------------------------------------------------------------------------
/docker/Makefile:
--------------------------------------------------------------------------------
1 | help:
2 | @cat Makefile
3 |
4 | DATA?="${HOME}/Data"
5 | GPU?=0
6 | DOCKER_FILE=Dockerfile
7 | DOCKER=GPU=$(GPU) nvidia-docker
8 | BACKEND=tensorflow
9 | PYTHON_VERSION?=3.6
10 | CUDA_VERSION?=9.0
11 | CUDNN_VERSION?=7
12 | TEST=tests/
13 | SRC?=$(shell dirname `pwd`)
14 |
15 | build:
16 | docker build -t keras --build-arg python_version=$(PYTHON_VERSION) --build-arg cuda_version=$(CUDA_VERSION) --build-arg cudnn_version=$(CUDNN_VERSION) -f $(DOCKER_FILE) .
17 |
18 | bash: build
19 | $(DOCKER) run -it -v $(SRC):/src/workspace -v $(DATA):/data --env KERAS_BACKEND=$(BACKEND) keras bash
20 |
21 | ipython: build
22 | $(DOCKER) run -it -v $(SRC):/src/workspace -v $(DATA):/data --env KERAS_BACKEND=$(BACKEND) keras ipython
23 |
24 | notebook: build
25 | $(DOCKER) run -it -v $(SRC):/src/workspace -v $(DATA):/data --net=host --env KERAS_BACKEND=$(BACKEND) keras
26 |
27 | test: build
28 | $(DOCKER) run -it -v $(SRC):/src/workspace -v $(DATA):/data --env KERAS_BACKEND=$(BACKEND) keras py.test $(TEST)
29 |
30 |
--------------------------------------------------------------------------------
/docs/templates/models/model.md:
--------------------------------------------------------------------------------
1 | # Model class API
2 |
3 | In the functional API, given some input tensor(s) and output tensor(s), you can instantiate a `Model` via:
4 |
5 | ```python
6 | from keras.models import Model
7 | from keras.layers import Input, Dense
8 |
9 | a = Input(shape=(32,))
10 | b = Dense(32)(a)
11 | model = Model(inputs=a, outputs=b)
12 | ```
13 |
14 | This model will include all layers required in the computation of `b` given `a`.
15 |
16 | In the case of multi-input or multi-output models, you can use lists as well:
17 |
18 | ```python
19 | model = Model(inputs=[a1, a2], outputs=[b1, b2, b3])
20 | ```
21 |
22 | For a detailed introduction of what `Model` can do, read [this guide to the Keras functional API](/getting-started/functional-api-guide).
23 |
24 | ## Useful attributes of Model
25 |
26 | - `model.layers` is a flattened list of the layers comprising the model graph.
27 | - `model.inputs` is the list of input tensors.
28 | - `model.outputs` is the list of output tensors.
29 |
30 | ## Methods
31 |
32 | {{autogenerated}}
33 |
--------------------------------------------------------------------------------
/docs/templates/activations.md:
--------------------------------------------------------------------------------
1 |
2 | ## Usage of activations
3 |
4 | Activations can either be used through an `Activation` layer, or through the `activation` argument supported by all forward layers:
5 |
6 | ```python
7 | from keras.layers import Activation, Dense
8 |
9 | model.add(Dense(64))
10 | model.add(Activation('tanh'))
11 | ```
12 |
13 | This is equivalent to:
14 |
15 | ```python
16 | model.add(Dense(64, activation='tanh'))
17 | ```
18 |
19 | You can also pass an element-wise TensorFlow/Theano/CNTK function as an activation:
20 |
21 | ```python
22 | from keras import backend as K
23 |
24 | model.add(Dense(64, activation=K.tanh))
25 | ```
26 |
27 | ## Available activations
28 |
29 | {{autogenerated}}
30 |
31 | ## On "Advanced Activations"
32 |
33 | Activations that are more complex than a simple TensorFlow/Theano/CNTK function (eg. learnable activations, which maintain a state) are available as [Advanced Activation layers](layers/advanced-activations.md), and can be found in the module `keras.layers.advanced_activations`. These include `PReLU` and `LeakyReLU`.
34 |
--------------------------------------------------------------------------------
/ISSUE_TEMPLATE.md:
--------------------------------------------------------------------------------
1 | Please make sure that the boxes below are checked before you submit your issue. If your issue is an implementation question, please ask your question on [StackOverflow](http://stackoverflow.com/questions/tagged/keras) or [join the Keras Slack channel](https://keras-slack-autojoin.herokuapp.com/) and ask there instead of filing a GitHub issue.
2 |
3 | Thank you!
4 |
5 | - [ ] Check that you are up-to-date with the master branch of Keras. You can update with:
6 | pip install git+git://github.com/keras-team/keras.git --upgrade --no-deps
7 |
8 | - [ ] If running on TensorFlow, check that you are up-to-date with the latest version. The installation instructions can be found [here](https://www.tensorflow.org/get_started/os_setup).
9 |
10 | - [ ] If running on Theano, check that you are up-to-date with the master branch of Theano. You can update with:
11 | pip install git+git://github.com/Theano/Theano.git --upgrade --no-deps
12 |
13 | - [ ] Provide a link to a GitHub Gist of a Python script that can reproduce your issue (or just copy the script here if it is short).
14 |
--------------------------------------------------------------------------------
/tests/keras/utils/vis_utils_test.py:
--------------------------------------------------------------------------------
1 | import pytest
2 | import os
3 | import sys
4 | import numpy as np
5 | from keras.layers import Conv2D
6 | from keras.layers import Dense
7 | from keras.layers import Flatten
8 | from keras.layers import LSTM
9 | from keras.layers import TimeDistributed
10 | from keras.models import Sequential
11 | from keras.utils import vis_utils
12 |
13 |
14 | def test_plot_model():
15 | model = Sequential()
16 | model.add(Conv2D(filters=2, kernel_size=(2, 3), input_shape=(3, 5, 5), name='conv'))
17 | model.add(Flatten(name='flat'))
18 | model.add(Dense(5, name='dense1'))
19 | vis_utils.plot_model(model, to_file='model1.png', show_layer_names=False)
20 | os.remove('model1.png')
21 |
22 | model = Sequential()
23 | model.add(LSTM(16, return_sequences=True, input_shape=(2, 3), name='lstm'))
24 | model.add(TimeDistributed(Dense(5, name='dense2')))
25 | vis_utils.plot_model(model, to_file='model2.png', show_shapes=True)
26 | os.remove('model2.png')
27 |
28 |
29 | if __name__ == '__main__':
30 | pytest.main([__file__])
31 |
--------------------------------------------------------------------------------
/keras/datasets/cifar.py:
--------------------------------------------------------------------------------
1 | # -*- coding: utf-8 -*-
2 | """Utilities common to CIFAR10 and CIFAR100 datasets.
3 | """
4 | from __future__ import absolute_import
5 | from __future__ import division
6 | from __future__ import print_function
7 |
8 | import sys
9 | from six.moves import cPickle
10 |
11 |
12 | def load_batch(fpath, label_key='labels'):
13 | """Internal utility for parsing CIFAR data.
14 |
15 | # Arguments
16 | fpath: path the file to parse.
17 | label_key: key for label data in the retrieve
18 | dictionary.
19 |
20 | # Returns
21 | A tuple `(data, labels)`.
22 | """
23 | with open(fpath, 'rb') as f:
24 | if sys.version_info < (3,):
25 | d = cPickle.load(f)
26 | else:
27 | d = cPickle.load(f, encoding='bytes')
28 | # decode utf8
29 | d_decoded = {}
30 | for k, v in d.items():
31 | d_decoded[k.decode('utf8')] = v
32 | d = d_decoded
33 | data = d['data']
34 | labels = d[label_key]
35 |
36 | data = data.reshape(data.shape[0], 3, 32, 32)
37 | return data, labels
38 |
--------------------------------------------------------------------------------
/tests/keras/layers/noise_test.py:
--------------------------------------------------------------------------------
1 | import pytest
2 | from keras.utils.test_utils import layer_test
3 | from keras.utils.test_utils import keras_test
4 | from keras.layers import noise
5 | from keras import backend as K
6 |
7 |
8 | @keras_test
9 | @pytest.mark.skipif((K.backend() == 'cntk'),
10 | reason="cntk does not support it yet")
11 | def test_GaussianNoise():
12 | layer_test(noise.GaussianNoise,
13 | kwargs={'stddev': 1.},
14 | input_shape=(3, 2, 3))
15 |
16 |
17 | @keras_test
18 | @pytest.mark.skipif((K.backend() == 'cntk'),
19 | reason="cntk does not support it yet")
20 | def test_GaussianDropout():
21 | layer_test(noise.GaussianDropout,
22 | kwargs={'rate': 0.5},
23 | input_shape=(3, 2, 3))
24 |
25 |
26 | @keras_test
27 | @pytest.mark.skipif((K.backend() == 'cntk'),
28 | reason="cntk does not support it yet")
29 | def test_AlphaDropout():
30 | layer_test(noise.AlphaDropout,
31 | kwargs={'rate': 0.1},
32 | input_shape=(3, 2, 3))
33 |
34 |
35 | if __name__ == '__main__':
36 | pytest.main([__file__])
37 |
--------------------------------------------------------------------------------
/tests/keras/layers/advanced_activations_test.py:
--------------------------------------------------------------------------------
1 | import pytest
2 | from keras.utils.test_utils import layer_test
3 | from keras.utils.test_utils import keras_test
4 | from keras import layers
5 |
6 |
7 | @keras_test
8 | def test_leaky_relu():
9 | for alpha in [0., .5, -1.]:
10 | layer_test(layers.LeakyReLU, kwargs={'alpha': alpha},
11 | input_shape=(2, 3, 4))
12 |
13 |
14 | @keras_test
15 | def test_prelu():
16 | layer_test(layers.PReLU, kwargs={},
17 | input_shape=(2, 3, 4))
18 |
19 |
20 | @keras_test
21 | def test_prelu_share():
22 | layer_test(layers.PReLU, kwargs={'shared_axes': 1},
23 | input_shape=(2, 3, 4))
24 |
25 |
26 | @keras_test
27 | def test_elu():
28 | for alpha in [0., .5, -1.]:
29 | layer_test(layers.ELU, kwargs={'alpha': alpha},
30 | input_shape=(2, 3, 4))
31 |
32 |
33 | @keras_test
34 | def test_thresholded_relu():
35 | layer_test(layers.ThresholdedReLU, kwargs={'theta': 0.5},
36 | input_shape=(2, 3, 4))
37 |
38 |
39 | @keras_test
40 | def test_softmax():
41 | for axis in [1, -1]:
42 | layer_test(layers.Softmax, kwargs={'axis': axis},
43 | input_shape=(2, 3, 4))
44 |
45 |
46 | if __name__ == '__main__':
47 | pytest.main([__file__])
48 |
--------------------------------------------------------------------------------
/tests/keras/layers/embeddings_test.py:
--------------------------------------------------------------------------------
1 | import pytest
2 | from keras.utils.test_utils import layer_test, keras_test
3 | from keras.layers.embeddings import Embedding
4 | import keras.backend as K
5 |
6 |
7 | @keras_test
8 | def test_embedding():
9 | layer_test(Embedding,
10 | kwargs={'output_dim': 4, 'input_dim': 10, 'input_length': 2},
11 | input_shape=(3, 2),
12 | input_dtype='int32',
13 | expected_output_dtype=K.floatx())
14 | layer_test(Embedding,
15 | kwargs={'output_dim': 4, 'input_dim': 10, 'mask_zero': True},
16 | input_shape=(3, 2),
17 | input_dtype='int32',
18 | expected_output_dtype=K.floatx())
19 | layer_test(Embedding,
20 | kwargs={'output_dim': 4, 'input_dim': 10, 'mask_zero': True},
21 | input_shape=(3, 2, 5),
22 | input_dtype='int32',
23 | expected_output_dtype=K.floatx())
24 | layer_test(Embedding,
25 | kwargs={'output_dim': 4, 'input_dim': 10, 'mask_zero': True, 'input_length': (None, 5)},
26 | input_shape=(3, 2, 5),
27 | input_dtype='int32',
28 | expected_output_dtype=K.floatx())
29 |
30 |
31 | if __name__ == '__main__':
32 | pytest.main([__file__])
33 |
--------------------------------------------------------------------------------
/tests/keras/utils/np_utils_test.py:
--------------------------------------------------------------------------------
1 | """Tests for functions in np_utils.py.
2 | """
3 | import numpy as np
4 | import pytest
5 | from keras.utils import to_categorical
6 |
7 |
8 | def test_to_categorical():
9 | num_classes = 5
10 | shapes = [(1,), (3,), (4, 3), (5, 4, 3), (3, 1), (3, 2, 1)]
11 | expected_shapes = [(1, num_classes),
12 | (3, num_classes),
13 | (4, 3, num_classes),
14 | (5, 4, 3, num_classes),
15 | (3, num_classes),
16 | (3, 2, num_classes)]
17 | labels = [np.random.randint(0, num_classes, shape) for shape in shapes]
18 | one_hots = [to_categorical(label, num_classes) for label in labels]
19 | for label, one_hot, expected_shape in zip(labels,
20 | one_hots,
21 | expected_shapes):
22 | # Check shape
23 | assert one_hot.shape == expected_shape
24 | # Make sure there are only 0s and 1s
25 | assert np.array_equal(one_hot, one_hot.astype(bool))
26 | # Make sure there is exactly one 1 in a row
27 | assert np.all(one_hot.sum(axis=-1) == 1)
28 | # Get original labels back from one hots
29 | assert np.all(np.argmax(one_hot, -1).reshape(label.shape) == label)
30 |
31 |
32 | if __name__ == '__main__':
33 | pytest.main([__file__])
34 |
--------------------------------------------------------------------------------
/docs/templates/initializers.md:
--------------------------------------------------------------------------------
1 | ## Usage of initializers
2 |
3 | Initializations define the way to set the initial random weights of Keras layers.
4 |
5 | The keyword arguments used for passing initializers to layers will depend on the layer. Usually it is simply `kernel_initializer` and `bias_initializer`:
6 |
7 | ```python
8 | model.add(Dense(64,
9 | kernel_initializer='random_uniform',
10 | bias_initializer='zeros'))
11 | ```
12 |
13 | ## Available initializers
14 |
15 | The following built-in initializers are available as part of the `keras.initializers` module:
16 |
17 | {{autogenerated}}
18 |
19 |
20 | An initializer may be passed as a string (must match one of the available initializers above), or as a callable:
21 |
22 | ```python
23 | from keras import initializers
24 |
25 | model.add(Dense(64, kernel_initializer=initializers.random_normal(stddev=0.01)))
26 |
27 | # also works; will use the default parameters.
28 | model.add(Dense(64, kernel_initializer='random_normal'))
29 | ```
30 |
31 |
32 | ## Using custom initializers
33 |
34 | If passing a custom callable, then it must take the argument `shape` (shape of the variable to initialize) and `dtype` (dtype of generated values):
35 |
36 | ```python
37 | from keras import backend as K
38 |
39 | def my_init(shape, dtype=None):
40 | return K.random_normal(shape, dtype=dtype)
41 |
42 | model.add(Dense(64, kernel_initializer=my_init))
43 | ```
44 |
--------------------------------------------------------------------------------
/docs/templates/layers/about-keras-layers.md:
--------------------------------------------------------------------------------
1 | # About Keras layers
2 |
3 | All Keras layers have a number of methods in common:
4 |
5 | - `layer.get_weights()`: returns the weights of the layer as a list of Numpy arrays.
6 | - `layer.set_weights(weights)`: sets the weights of the layer from a list of Numpy arrays (with the same shapes as the output of `get_weights`).
7 | - `layer.get_config()`: returns a dictionary containing the configuration of the layer. The layer can be reinstantiated from its config via:
8 |
9 | ```python
10 | layer = Dense(32)
11 | config = layer.get_config()
12 | reconstructed_layer = Dense.from_config(config)
13 | ```
14 |
15 | Or:
16 |
17 | ```python
18 | from keras import layers
19 |
20 | config = layer.get_config()
21 | layer = layers.deserialize({'class_name': layer.__class__.__name__,
22 | 'config': config})
23 | ```
24 |
25 | If a layer has a single node (i.e. if it isn't a shared layer), you can get its input tensor, output tensor, input shape and output shape via:
26 |
27 | - `layer.input`
28 | - `layer.output`
29 | - `layer.input_shape`
30 | - `layer.output_shape`
31 |
32 | If the layer has multiple nodes (see: [the concept of layer node and shared layers](/getting-started/functional-api-guide/#the-concept-of-layer-node)), you can use the following methods:
33 |
34 | - `layer.get_input_at(node_index)`
35 | - `layer.get_output_at(node_index)`
36 | - `layer.get_input_shape_at(node_index)`
37 | - `layer.get_output_shape_at(node_index)`
--------------------------------------------------------------------------------
/docs/templates/optimizers.md:
--------------------------------------------------------------------------------
1 |
2 | ## Usage of optimizers
3 |
4 | An optimizer is one of the two arguments required for compiling a Keras model:
5 |
6 | ```python
7 | from keras import optimizers
8 |
9 | model = Sequential()
10 | model.add(Dense(64, kernel_initializer='uniform', input_shape=(10,)))
11 | model.add(Activation('softmax'))
12 |
13 | sgd = optimizers.SGD(lr=0.01, decay=1e-6, momentum=0.9, nesterov=True)
14 | model.compile(loss='mean_squared_error', optimizer=sgd)
15 | ```
16 |
17 | You can either instantiate an optimizer before passing it to `model.compile()` , as in the above example, or you can call it by its name. In the latter case, the default parameters for the optimizer will be used.
18 |
19 | ```python
20 | # pass optimizer by name: default parameters will be used
21 | model.compile(loss='mean_squared_error', optimizer='sgd')
22 | ```
23 |
24 | ---
25 |
26 | ## Parameters common to all Keras optimizers
27 |
28 | The parameters `clipnorm` and `clipvalue` can be used with all optimizers to control gradient clipping:
29 |
30 | ```python
31 | from keras import optimizers
32 |
33 | # All parameter gradients will be clipped to
34 | # a maximum norm of 1.
35 | sgd = optimizers.SGD(lr=0.01, clipnorm=1.)
36 | ```
37 |
38 | ```python
39 | from keras import optimizers
40 |
41 | # All parameter gradients will be clipped to
42 | # a maximum value of 0.5 and
43 | # a minimum value of -0.5.
44 | sgd = optimizers.SGD(lr=0.01, clipvalue=0.5)
45 | ```
46 |
47 | ---
48 |
49 | {{autogenerated}}
50 |
--------------------------------------------------------------------------------
/tests/test_loss_masking.py:
--------------------------------------------------------------------------------
1 | import numpy as np
2 | import pytest
3 |
4 | from keras.models import Sequential
5 | from keras.engine.training_utils import weighted_masked_objective
6 | from keras.layers import TimeDistributed, Masking, Dense
7 | from keras.utils.test_utils import keras_test
8 | from keras import losses
9 | from keras import backend as K
10 |
11 |
12 | @keras_test
13 | def test_masking():
14 | np.random.seed(1337)
15 | x = np.array([[[1], [1]],
16 | [[0], [0]]])
17 | model = Sequential()
18 | model.add(Masking(mask_value=0, input_shape=(2, 1)))
19 | model.add(TimeDistributed(Dense(1, kernel_initializer='one')))
20 | model.compile(loss='mse', optimizer='sgd')
21 | y = np.array([[[1], [1]],
22 | [[1], [1]]])
23 | loss = model.train_on_batch(x, y)
24 | assert loss == 0
25 |
26 |
27 | @keras_test
28 | def test_loss_masking():
29 | weighted_loss = weighted_masked_objective(losses.get('mae'))
30 | shape = (3, 4, 2)
31 | x = np.arange(24).reshape(shape)
32 | y = 2 * x
33 |
34 | # Normally the trailing 1 is added by standardize_weights
35 | weights = np.ones((3,))
36 | mask = np.ones((3, 4))
37 | mask[1, 0] = 0
38 |
39 | out = K.eval(weighted_loss(K.variable(x),
40 | K.variable(y),
41 | K.variable(weights),
42 | K.variable(mask)))
43 |
44 |
45 | if __name__ == '__main__':
46 | pytest.main([__file__])
47 |
--------------------------------------------------------------------------------
/keras/datasets/cifar10.py:
--------------------------------------------------------------------------------
1 | """CIFAR10 small images classification dataset.
2 | """
3 | from __future__ import absolute_import
4 | from __future__ import division
5 | from __future__ import print_function
6 |
7 | from .cifar import load_batch
8 | from ..utils.data_utils import get_file
9 | from .. import backend as K
10 | import numpy as np
11 | import os
12 |
13 |
14 | def load_data():
15 | """Loads CIFAR10 dataset.
16 |
17 | # Returns
18 | Tuple of Numpy arrays: `(x_train, y_train), (x_test, y_test)`.
19 | """
20 | dirname = 'cifar-10-batches-py'
21 | origin = 'https://www.cs.toronto.edu/~kriz/cifar-10-python.tar.gz'
22 | path = get_file(dirname, origin=origin, untar=True)
23 |
24 | num_train_samples = 50000
25 |
26 | x_train = np.empty((num_train_samples, 3, 32, 32), dtype='uint8')
27 | y_train = np.empty((num_train_samples,), dtype='uint8')
28 |
29 | for i in range(1, 6):
30 | fpath = os.path.join(path, 'data_batch_' + str(i))
31 | (x_train[(i - 1) * 10000: i * 10000, :, :, :],
32 | y_train[(i - 1) * 10000: i * 10000]) = load_batch(fpath)
33 |
34 | fpath = os.path.join(path, 'test_batch')
35 | x_test, y_test = load_batch(fpath)
36 |
37 | y_train = np.reshape(y_train, (len(y_train), 1))
38 | y_test = np.reshape(y_test, (len(y_test), 1))
39 |
40 | if K.image_data_format() == 'channels_last':
41 | x_train = x_train.transpose(0, 2, 3, 1)
42 | x_test = x_test.transpose(0, 2, 3, 1)
43 |
44 | return (x_train, y_train), (x_test, y_test)
45 |
--------------------------------------------------------------------------------
/keras/datasets/cifar100.py:
--------------------------------------------------------------------------------
1 | """CIFAR100 small images classification dataset.
2 | """
3 | from __future__ import absolute_import
4 | from __future__ import division
5 | from __future__ import print_function
6 |
7 | from .cifar import load_batch
8 | from ..utils.data_utils import get_file
9 | from .. import backend as K
10 | import numpy as np
11 | import os
12 |
13 |
14 | def load_data(label_mode='fine'):
15 | """Loads CIFAR100 dataset.
16 |
17 | # Arguments
18 | label_mode: one of "fine", "coarse".
19 |
20 | # Returns
21 | Tuple of Numpy arrays: `(x_train, y_train), (x_test, y_test)`.
22 |
23 | # Raises
24 | ValueError: in case of invalid `label_mode`.
25 | """
26 | if label_mode not in ['fine', 'coarse']:
27 | raise ValueError('`label_mode` must be one of `"fine"`, `"coarse"`.')
28 |
29 | dirname = 'cifar-100-python'
30 | origin = 'https://www.cs.toronto.edu/~kriz/cifar-100-python.tar.gz'
31 | path = get_file(dirname, origin=origin, untar=True)
32 |
33 | fpath = os.path.join(path, 'train')
34 | x_train, y_train = load_batch(fpath, label_key=label_mode + '_labels')
35 |
36 | fpath = os.path.join(path, 'test')
37 | x_test, y_test = load_batch(fpath, label_key=label_mode + '_labels')
38 |
39 | y_train = np.reshape(y_train, (len(y_train), 1))
40 | y_test = np.reshape(y_test, (len(y_test), 1))
41 |
42 | if K.image_data_format() == 'channels_last':
43 | x_train = x_train.transpose(0, 2, 3, 1)
44 | x_test = x_test.transpose(0, 2, 3, 1)
45 |
46 | return (x_train, y_train), (x_test, y_test)
47 |
--------------------------------------------------------------------------------
/tests/keras/legacy/layers_test.py:
--------------------------------------------------------------------------------
1 | import pytest
2 |
3 | from keras.utils.test_utils import keras_test
4 | from keras.utils.test_utils import layer_test
5 | from keras.legacy import layers as legacy_layers
6 | from keras import regularizers
7 | from keras import constraints
8 |
9 |
10 | @keras_test
11 | def test_highway():
12 | layer_test(legacy_layers.Highway,
13 | kwargs={},
14 | input_shape=(3, 2))
15 |
16 | layer_test(legacy_layers.Highway,
17 | kwargs={'W_regularizer': regularizers.l2(0.01),
18 | 'b_regularizer': regularizers.l1(0.01),
19 | 'activity_regularizer': regularizers.l2(0.01),
20 | 'W_constraint': constraints.MaxNorm(1),
21 | 'b_constraint': constraints.MaxNorm(1)},
22 | input_shape=(3, 2))
23 |
24 |
25 | @keras_test
26 | def test_maxout_dense():
27 | layer_test(legacy_layers.MaxoutDense,
28 | kwargs={'output_dim': 3},
29 | input_shape=(3, 2))
30 |
31 | layer_test(legacy_layers.MaxoutDense,
32 | kwargs={'output_dim': 3,
33 | 'W_regularizer': regularizers.l2(0.01),
34 | 'b_regularizer': regularizers.l1(0.01),
35 | 'activity_regularizer': regularizers.l2(0.01),
36 | 'W_constraint': constraints.MaxNorm(1),
37 | 'b_constraint': constraints.MaxNorm(1)},
38 | input_shape=(3, 2))
39 |
40 |
41 | if __name__ == '__main__':
42 | pytest.main([__file__])
43 |
--------------------------------------------------------------------------------
/keras/datasets/boston_housing.py:
--------------------------------------------------------------------------------
1 | """Boston housing price regression dataset.
2 | """
3 | from __future__ import absolute_import
4 | from __future__ import division
5 | from __future__ import print_function
6 |
7 | from ..utils.data_utils import get_file
8 | import numpy as np
9 |
10 |
11 | def load_data(path='boston_housing.npz', test_split=0.2, seed=113):
12 | """Loads the Boston Housing dataset.
13 |
14 | # Arguments
15 | path: path where to cache the dataset locally
16 | (relative to ~/.keras/datasets).
17 | test_split: fraction of the data to reserve as test set.
18 | seed: Random seed for shuffling the data
19 | before computing the test split.
20 |
21 | # Returns
22 | Tuple of Numpy arrays: `(x_train, y_train), (x_test, y_test)`.
23 | """
24 | assert 0 <= test_split < 1
25 | path = get_file(path,
26 | origin='https://s3.amazonaws.com/keras-datasets/boston_housing.npz',
27 | file_hash='f553886a1f8d56431e820c5b82552d9d95cfcb96d1e678153f8839538947dff5')
28 | f = np.load(path)
29 | x = f['x']
30 | y = f['y']
31 | f.close()
32 |
33 | np.random.seed(seed)
34 | indices = np.arange(len(x))
35 | np.random.shuffle(indices)
36 | x = x[indices]
37 | y = y[indices]
38 |
39 | x_train = np.array(x[:int(len(x) * (1 - test_split))])
40 | y_train = np.array(y[:int(len(x) * (1 - test_split))])
41 | x_test = np.array(x[int(len(x) * (1 - test_split)):])
42 | y_test = np.array(y[int(len(x) * (1 - test_split)):])
43 | return (x_train, y_train), (x_test, y_test)
44 |
--------------------------------------------------------------------------------
/docs/templates/losses.md:
--------------------------------------------------------------------------------
1 |
2 | ## Usage of loss functions
3 |
4 | A loss function (or objective function, or optimization score function) is one of the two parameters required to compile a model:
5 |
6 | ```python
7 | model.compile(loss='mean_squared_error', optimizer='sgd')
8 | ```
9 |
10 | ```python
11 | from keras import losses
12 |
13 | model.compile(loss=losses.mean_squared_error, optimizer='sgd')
14 | ```
15 |
16 | You can either pass the name of an existing loss function, or pass a TensorFlow/Theano symbolic function that returns a scalar for each data-point and takes the following two arguments:
17 |
18 | - __y_true__: True labels. TensorFlow/Theano tensor.
19 | - __y_pred__: Predictions. TensorFlow/Theano tensor of the same shape as y_true.
20 |
21 | The actual optimized objective is the mean of the output array across all datapoints.
22 |
23 | For a few examples of such functions, check out the [losses source](https://github.com/keras-team/keras/blob/master/keras/losses.py).
24 |
25 | ## Available loss functions
26 |
27 | {{autogenerated}}
28 |
29 | ----
30 |
31 | **Note**: when using the `categorical_crossentropy` loss, your targets should be in categorical format (e.g. if you have 10 classes, the target for each sample should be a 10-dimensional vector that is all-zeros except for a 1 at the index corresponding to the class of the sample). In order to convert *integer targets* into *categorical targets*, you can use the Keras utility `to_categorical`:
32 |
33 | ```python
34 | from keras.utils.np_utils import to_categorical
35 |
36 | categorical_labels = to_categorical(int_labels, num_classes=None)
37 | ```
38 |
--------------------------------------------------------------------------------
/keras/datasets/fashion_mnist.py:
--------------------------------------------------------------------------------
1 | """Fashion-MNIST dataset.
2 | """
3 | from __future__ import absolute_import
4 | from __future__ import division
5 | from __future__ import print_function
6 |
7 | import gzip
8 | import os
9 |
10 | from ..utils.data_utils import get_file
11 | import numpy as np
12 |
13 |
14 | def load_data():
15 | """Loads the Fashion-MNIST dataset.
16 |
17 | # Returns
18 | Tuple of Numpy arrays: `(x_train, y_train), (x_test, y_test)`.
19 | """
20 | dirname = os.path.join('datasets', 'fashion-mnist')
21 | base = 'http://fashion-mnist.s3-website.eu-central-1.amazonaws.com/'
22 | files = ['train-labels-idx1-ubyte.gz', 'train-images-idx3-ubyte.gz',
23 | 't10k-labels-idx1-ubyte.gz', 't10k-images-idx3-ubyte.gz']
24 |
25 | paths = []
26 | for fname in files:
27 | paths.append(get_file(fname,
28 | origin=base + fname,
29 | cache_subdir=dirname))
30 |
31 | with gzip.open(paths[0], 'rb') as lbpath:
32 | y_train = np.frombuffer(lbpath.read(), np.uint8, offset=8)
33 |
34 | with gzip.open(paths[1], 'rb') as imgpath:
35 | x_train = np.frombuffer(imgpath.read(), np.uint8,
36 | offset=16).reshape(len(y_train), 28, 28)
37 |
38 | with gzip.open(paths[2], 'rb') as lbpath:
39 | y_test = np.frombuffer(lbpath.read(), np.uint8, offset=8)
40 |
41 | with gzip.open(paths[3], 'rb') as imgpath:
42 | x_test = np.frombuffer(imgpath.read(), np.uint8,
43 | offset=16).reshape(len(y_test), 28, 28)
44 |
45 | return (x_train, y_train), (x_test, y_test)
46 |
--------------------------------------------------------------------------------
/examples/imdb_bidirectional_lstm.py:
--------------------------------------------------------------------------------
1 | '''Trains a Bidirectional LSTM on the IMDB sentiment classification task.
2 |
3 | Output after 4 epochs on CPU: ~0.8146
4 | Time per epoch on CPU (Core i7): ~150s.
5 | '''
6 |
7 | from __future__ import print_function
8 | import numpy as np
9 |
10 | from keras.preprocessing import sequence
11 | from keras.models import Sequential
12 | from keras.layers import Dense, Dropout, Embedding, LSTM, Bidirectional
13 | from keras.datasets import imdb
14 |
15 |
16 | max_features = 20000
17 | # cut texts after this number of words
18 | # (among top max_features most common words)
19 | maxlen = 100
20 | batch_size = 32
21 |
22 | print('Loading data...')
23 | (x_train, y_train), (x_test, y_test) = imdb.load_data(num_words=max_features)
24 | print(len(x_train), 'train sequences')
25 | print(len(x_test), 'test sequences')
26 |
27 | print('Pad sequences (samples x time)')
28 | x_train = sequence.pad_sequences(x_train, maxlen=maxlen)
29 | x_test = sequence.pad_sequences(x_test, maxlen=maxlen)
30 | print('x_train shape:', x_train.shape)
31 | print('x_test shape:', x_test.shape)
32 | y_train = np.array(y_train)
33 | y_test = np.array(y_test)
34 |
35 | model = Sequential()
36 | model.add(Embedding(max_features, 128, input_length=maxlen))
37 | model.add(Bidirectional(LSTM(64)))
38 | model.add(Dropout(0.5))
39 | model.add(Dense(1, activation='sigmoid'))
40 |
41 | # try using different optimizers and different optimizer configs
42 | model.compile('adam', 'binary_crossentropy', metrics=['accuracy'])
43 |
44 | print('Train...')
45 | model.fit(x_train, y_train,
46 | batch_size=batch_size,
47 | epochs=4,
48 | validation_data=[x_test, y_test])
49 |
--------------------------------------------------------------------------------
/keras/utils/np_utils.py:
--------------------------------------------------------------------------------
1 | """Numpy-related utilities."""
2 | from __future__ import absolute_import
3 | from __future__ import division
4 | from __future__ import print_function
5 |
6 | import numpy as np
7 |
8 |
9 | def to_categorical(y, num_classes=None):
10 | """Converts a class vector (integers) to binary class matrix.
11 |
12 | E.g. for use with categorical_crossentropy.
13 |
14 | # Arguments
15 | y: class vector to be converted into a matrix
16 | (integers from 0 to num_classes).
17 | num_classes: total number of classes.
18 |
19 | # Returns
20 | A binary matrix representation of the input.
21 | """
22 | y = np.array(y, dtype='int')
23 | input_shape = y.shape
24 | if input_shape and input_shape[-1] == 1 and len(input_shape) > 1:
25 | input_shape = tuple(input_shape[:-1])
26 | y = y.ravel()
27 | if not num_classes:
28 | num_classes = np.max(y) + 1
29 | n = y.shape[0]
30 | categorical = np.zeros((n, num_classes), dtype=np.float32)
31 | categorical[np.arange(n), y] = 1
32 | output_shape = input_shape + (num_classes,)
33 | categorical = np.reshape(categorical, output_shape)
34 | return categorical
35 |
36 |
37 | def normalize(x, axis=-1, order=2):
38 | """Normalizes a Numpy array.
39 |
40 | # Arguments
41 | x: Numpy array to normalize.
42 | axis: axis along which to normalize.
43 | order: Normalization order (e.g. 2 for L2 norm).
44 |
45 | # Returns
46 | A normalized copy of the array.
47 | """
48 | l2 = np.atleast_1d(np.linalg.norm(x, order, axis))
49 | l2[l2 == 0] = 1
50 | return x / np.expand_dims(l2, axis)
51 |
--------------------------------------------------------------------------------
/docs/templates/regularizers.md:
--------------------------------------------------------------------------------
1 | ## Usage of regularizers
2 |
3 | Regularizers allow to apply penalties on layer parameters or layer activity during optimization. These penalties are incorporated in the loss function that the network optimizes.
4 |
5 | The penalties are applied on a per-layer basis. The exact API will depend on the layer, but the layers `Dense`, `Conv1D`, `Conv2D` and `Conv3D` have a unified API.
6 |
7 | These layers expose 3 keyword arguments:
8 |
9 | - `kernel_regularizer`: instance of `keras.regularizers.Regularizer`
10 | - `bias_regularizer`: instance of `keras.regularizers.Regularizer`
11 | - `activity_regularizer`: instance of `keras.regularizers.Regularizer`
12 |
13 |
14 | ## Example
15 |
16 | ```python
17 | from keras import regularizers
18 | model.add(Dense(64, input_dim=64,
19 | kernel_regularizer=regularizers.l2(0.01),
20 | activity_regularizer=regularizers.l1(0.01)))
21 | ```
22 |
23 | ## Available penalties
24 |
25 | ```python
26 | keras.regularizers.l1(0.)
27 | keras.regularizers.l2(0.)
28 | keras.regularizers.l1_l2(0.)
29 | ```
30 |
31 | ## Developing new regularizers
32 |
33 | Any function that takes in a weight matrix and returns a loss contribution tensor can be used as a regularizer, e.g.:
34 |
35 | ```python
36 | from keras import backend as K
37 |
38 | def l1_reg(weight_matrix):
39 | return 0.01 * K.sum(K.abs(weight_matrix))
40 |
41 | model.add(Dense(64, input_dim=64,
42 | kernel_regularizer=l1_reg))
43 | ```
44 |
45 | Alternatively, you can write your regularizers in an object-oriented way;
46 | see the [keras/regularizers.py](https://github.com/keras-team/keras/blob/master/keras/regularizers.py) module for examples.
--------------------------------------------------------------------------------
/docs/templates/metrics.md:
--------------------------------------------------------------------------------
1 |
2 | ## Usage of metrics
3 |
4 | A metric is a function that is used to judge the performance of your model. Metric functions are to be supplied in the `metrics` parameter when a model is compiled.
5 |
6 | ```python
7 | model.compile(loss='mean_squared_error',
8 | optimizer='sgd',
9 | metrics=['mae', 'acc'])
10 | ```
11 |
12 | ```python
13 | from keras import metrics
14 |
15 | model.compile(loss='mean_squared_error',
16 | optimizer='sgd',
17 | metrics=[metrics.mae, metrics.categorical_accuracy])
18 | ```
19 |
20 | A metric function is similar to a [loss function](/losses), except that the results from evaluating a metric are not used when training the model.
21 |
22 | You can either pass the name of an existing metric, or pass a Theano/TensorFlow symbolic function (see [Custom metrics](#custom-metrics)).
23 |
24 | #### Arguments
25 | - __y_true__: True labels. Theano/TensorFlow tensor.
26 | - __y_pred__: Predictions. Theano/TensorFlow tensor of the same shape as y_true.
27 |
28 | #### Returns
29 | Single tensor value representing the mean of the output array across all
30 | datapoints.
31 |
32 | ----
33 |
34 | ## Available metrics
35 |
36 |
37 | {{autogenerated}}
38 |
39 | ----
40 |
41 | ## Custom metrics
42 |
43 | Custom metrics can be passed at the compilation step. The
44 | function would need to take `(y_true, y_pred)` as arguments and return
45 | a single tensor value.
46 |
47 | ```python
48 | import keras.backend as K
49 |
50 | def mean_pred(y_true, y_pred):
51 | return K.mean(y_pred)
52 |
53 | model.compile(optimizer='rmsprop',
54 | loss='binary_crossentropy',
55 | metrics=['accuracy', mean_pred])
56 | ```
57 |
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | COPYRIGHT
2 |
3 | All contributions by François Chollet:
4 | Copyright (c) 2015 - 2018, François Chollet.
5 | All rights reserved.
6 |
7 | All contributions by Google:
8 | Copyright (c) 2015 - 2018, Google, Inc.
9 | All rights reserved.
10 |
11 | All contributions by Microsoft:
12 | Copyright (c) 2017 - 2018, Microsoft, Inc.
13 | All rights reserved.
14 |
15 | All other contributions:
16 | Copyright (c) 2015 - 2018, the respective contributors.
17 | All rights reserved.
18 |
19 | Each contributor holds copyright over their respective contributions.
20 | The project versioning (Git) records all such contribution source information.
21 |
22 | LICENSE
23 |
24 | The MIT License (MIT)
25 |
26 | Permission is hereby granted, free of charge, to any person obtaining a copy
27 | of this software and associated documentation files (the "Software"), to deal
28 | in the Software without restriction, including without limitation the rights
29 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
30 | copies of the Software, and to permit persons to whom the Software is
31 | furnished to do so, subject to the following conditions:
32 |
33 | The above copyright notice and this permission notice shall be included in all
34 | copies or substantial portions of the Software.
35 |
36 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
37 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
38 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
39 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
40 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
41 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
42 | SOFTWARE.
43 |
44 |
--------------------------------------------------------------------------------
/examples/mnist_mlp.py:
--------------------------------------------------------------------------------
1 | '''Trains a simple deep NN on the MNIST dataset.
2 |
3 | Gets to 98.40% test accuracy after 20 epochs
4 | (there is *a lot* of margin for parameter tuning).
5 | 2 seconds per epoch on a K520 GPU.
6 | '''
7 |
8 | from __future__ import print_function
9 |
10 | import keras
11 | from keras.datasets import mnist
12 | from keras.models import Sequential
13 | from keras.layers import Dense, Dropout
14 | from keras.optimizers import RMSprop
15 |
16 | batch_size = 128
17 | num_classes = 10
18 | epochs = 20
19 |
20 | # the data, split between train and test sets
21 | (x_train, y_train), (x_test, y_test) = mnist.load_data()
22 |
23 | x_train = x_train.reshape(60000, 784)
24 | x_test = x_test.reshape(10000, 784)
25 | x_train = x_train.astype('float32')
26 | x_test = x_test.astype('float32')
27 | x_train /= 255
28 | x_test /= 255
29 | print(x_train.shape[0], 'train samples')
30 | print(x_test.shape[0], 'test samples')
31 |
32 | # convert class vectors to binary class matrices
33 | y_train = keras.utils.to_categorical(y_train, num_classes)
34 | y_test = keras.utils.to_categorical(y_test, num_classes)
35 |
36 | model = Sequential()
37 | model.add(Dense(512, activation='relu', input_shape=(784,)))
38 | model.add(Dropout(0.2))
39 | model.add(Dense(512, activation='relu'))
40 | model.add(Dropout(0.2))
41 | model.add(Dense(num_classes, activation='softmax'))
42 |
43 | model.summary()
44 |
45 | model.compile(loss='categorical_crossentropy',
46 | optimizer=RMSprop(),
47 | metrics=['accuracy'])
48 |
49 | history = model.fit(x_train, y_train,
50 | batch_size=batch_size,
51 | epochs=epochs,
52 | verbose=1,
53 | validation_data=(x_test, y_test))
54 | score = model.evaluate(x_test, y_test, verbose=0)
55 | print('Test loss:', score[0])
56 | print('Test accuracy:', score[1])
57 |
--------------------------------------------------------------------------------
/keras/layers/__init__.py:
--------------------------------------------------------------------------------
1 | from __future__ import absolute_import
2 |
3 | from ..utils.generic_utils import deserialize_keras_object
4 | from ..engine.base_layer import Layer
5 | from ..engine import Input
6 | from ..engine import InputLayer
7 | from ..engine.base_layer import InputSpec
8 | from .merge import *
9 | from .core import *
10 | from .convolutional import *
11 | from .pooling import *
12 | from .local import *
13 | from .recurrent import *
14 | from .cudnn_recurrent import *
15 | from .normalization import *
16 | from .embeddings import *
17 | from .noise import *
18 | from .advanced_activations import *
19 | from .wrappers import *
20 | from .convolutional_recurrent import *
21 | from ..legacy.layers import *
22 |
23 |
24 | def serialize(layer):
25 | """Serialize a layer.
26 |
27 | # Arguments
28 | layer: a Layer object.
29 |
30 | # Returns
31 | dictionary with config.
32 | """
33 | return {'class_name': layer.__class__.__name__,
34 | 'config': layer.get_config()}
35 |
36 |
37 | def deserialize(config, custom_objects=None):
38 | """Instantiate a layer from a config dictionary.
39 |
40 | # Arguments
41 | config: dict of the form {'class_name': str, 'config': dict}
42 | custom_objects: dict mapping class names (or function names)
43 | of custom (non-Keras) objects to class/functions
44 |
45 | # Returns
46 | Layer instance (may be Model, Sequential, Layer...)
47 | """
48 | from .. import models
49 | globs = globals() # All layers.
50 | globs['Model'] = models.Model
51 | globs['Sequential'] = models.Sequential
52 | return deserialize_keras_object(config,
53 | module_objects=globs,
54 | custom_objects=custom_objects,
55 | printable_module_name='layer')
56 |
--------------------------------------------------------------------------------
/tests/integration_tests/test_image_data_tasks.py:
--------------------------------------------------------------------------------
1 | from __future__ import print_function
2 | import numpy as np
3 | import pytest
4 |
5 | from keras.utils.test_utils import get_test_data, keras_test
6 | from keras.models import Sequential
7 | from keras import layers
8 | from keras.utils.np_utils import to_categorical
9 |
10 |
11 | @keras_test
12 | def test_image_classification():
13 | np.random.seed(1337)
14 | input_shape = (16, 16, 3)
15 | (x_train, y_train), (x_test, y_test) = get_test_data(num_train=500,
16 | num_test=200,
17 | input_shape=input_shape,
18 | classification=True,
19 | num_classes=4)
20 | y_train = to_categorical(y_train)
21 | y_test = to_categorical(y_test)
22 |
23 | model = Sequential([
24 | layers.Conv2D(filters=8, kernel_size=3,
25 | activation='relu',
26 | input_shape=input_shape),
27 | layers.MaxPooling2D(pool_size=2),
28 | layers.Conv2D(filters=4, kernel_size=(3, 3),
29 | activation='relu', padding='same'),
30 | layers.GlobalAveragePooling2D(),
31 | layers.Dense(y_test.shape[-1], activation='softmax')
32 | ])
33 | model.compile(loss='categorical_crossentropy',
34 | optimizer='rmsprop',
35 | metrics=['accuracy'])
36 | model.summary()
37 | history = model.fit(x_train, y_train, epochs=10, batch_size=16,
38 | validation_data=(x_test, y_test),
39 | verbose=0)
40 | assert history.history['val_acc'][-1] > 0.75
41 | config = model.get_config()
42 | model = Sequential.from_config(config)
43 |
44 |
45 | if __name__ == '__main__':
46 | pytest.main([__file__])
47 |
--------------------------------------------------------------------------------
/docker/README.md:
--------------------------------------------------------------------------------
1 | # Using Keras via Docker
2 |
3 | This directory contains `Dockerfile` to make it easy to get up and running with
4 | Keras via [Docker](http://www.docker.com/).
5 |
6 | ## Installing Docker
7 |
8 | General installation instructions are
9 | [on the Docker site](https://docs.docker.com/installation/), but we give some
10 | quick links here:
11 |
12 | * [OSX](https://docs.docker.com/installation/mac/): [docker toolbox](https://www.docker.com/toolbox)
13 | * [ubuntu](https://docs.docker.com/installation/ubuntulinux/)
14 |
15 | ## Running the container
16 |
17 | We are using `Makefile` to simplify docker commands within make commands.
18 |
19 | Build the container and start a Jupyter Notebook
20 |
21 | $ make notebook
22 |
23 | Build the container and start an iPython shell
24 |
25 | $ make ipython
26 |
27 | Build the container and start a bash
28 |
29 | $ make bash
30 |
31 | For GPU support install NVIDIA drivers (ideally latest) and
32 | [nvidia-docker](https://github.com/NVIDIA/nvidia-docker). Run using
33 |
34 | $ make notebook GPU=0 # or [ipython, bash]
35 |
36 | Switch between Theano and TensorFlow
37 |
38 | $ make notebook BACKEND=theano
39 | $ make notebook BACKEND=tensorflow
40 |
41 | Mount a volume for external data sets
42 |
43 | $ make DATA=~/mydata
44 |
45 | Prints all make tasks
46 |
47 | $ make help
48 |
49 | You can change Theano parameters by editing `/docker/theanorc`.
50 |
51 |
52 | Note: If you would have a problem running nvidia-docker you may try the old way
53 | we have used. But it is not recommended. If you find a bug in the nvidia-docker report
54 | it there please and try using the nvidia-docker as described above.
55 |
56 | $ export CUDA_SO=$(\ls /usr/lib/x86_64-linux-gnu/libcuda.* | xargs -I{} echo '-v {}:{}')
57 | $ export DEVICES=$(\ls /dev/nvidia* | xargs -I{} echo '--device {}:{}')
58 | $ docker run -it -p 8888:8888 $CUDA_SO $DEVICES gcr.io/tensorflow/tensorflow:latest-gpu
59 |
--------------------------------------------------------------------------------
/docs/templates/layers/writing-your-own-keras-layers.md:
--------------------------------------------------------------------------------
1 | # Writing your own Keras layers
2 |
3 | For simple, stateless custom operations, you are probably better off using `layers.core.Lambda` layers. But for any custom operation that has trainable weights, you should implement your own layer.
4 |
5 | Here is the skeleton of a Keras layer, **as of Keras 2.0** (if you have an older version, please upgrade). There are only three methods you need to implement:
6 |
7 | - `build(input_shape)`: this is where you will define your weights. This method must set `self.built = True` at the end, which can be done by calling `super([Layer], self).build()`.
8 | - `call(x)`: this is where the layer's logic lives. Unless you want your layer to support masking, you only have to care about the first argument passed to `call`: the input tensor.
9 | - `compute_output_shape(input_shape)`: in case your layer modifies the shape of its input, you should specify here the shape transformation logic. This allows Keras to do automatic shape inference.
10 |
11 | ```python
12 | from keras import backend as K
13 | from keras.engine.topology import Layer
14 | import numpy as np
15 |
16 | class MyLayer(Layer):
17 |
18 | def __init__(self, output_dim, **kwargs):
19 | self.output_dim = output_dim
20 | super(MyLayer, self).__init__(**kwargs)
21 |
22 | def build(self, input_shape):
23 | # Create a trainable weight variable for this layer.
24 | self.kernel = self.add_weight(name='kernel',
25 | shape=(input_shape[1], self.output_dim),
26 | initializer='uniform',
27 | trainable=True)
28 | super(MyLayer, self).build(input_shape) # Be sure to call this at the end
29 |
30 | def call(self, x):
31 | return K.dot(x, self.kernel)
32 |
33 | def compute_output_shape(self, input_shape):
34 | return (input_shape[0], self.output_dim)
35 | ```
36 |
37 | The existing Keras layers provide examples of how to implement almost anything. Never hesitate to read the source code!
38 |
--------------------------------------------------------------------------------
/examples/imdb_lstm.py:
--------------------------------------------------------------------------------
1 | '''Trains an LSTM model on the IMDB sentiment classification task.
2 |
3 | The dataset is actually too small for LSTM to be of any advantage
4 | compared to simpler, much faster methods such as TF-IDF + LogReg.
5 |
6 | # Notes
7 |
8 | - RNNs are tricky. Choice of batch size is important,
9 | choice of loss and optimizer is critical, etc.
10 | Some configurations won't converge.
11 |
12 | - LSTM loss decrease patterns during training can be quite different
13 | from what you see with CNNs/MLPs/etc.
14 | '''
15 | from __future__ import print_function
16 |
17 | from keras.preprocessing import sequence
18 | from keras.models import Sequential
19 | from keras.layers import Dense, Embedding
20 | from keras.layers import LSTM
21 | from keras.datasets import imdb
22 |
23 | max_features = 20000
24 | maxlen = 80 # cut texts after this number of words (among top max_features most common words)
25 | batch_size = 32
26 |
27 | print('Loading data...')
28 | (x_train, y_train), (x_test, y_test) = imdb.load_data(num_words=max_features)
29 | print(len(x_train), 'train sequences')
30 | print(len(x_test), 'test sequences')
31 |
32 | print('Pad sequences (samples x time)')
33 | x_train = sequence.pad_sequences(x_train, maxlen=maxlen)
34 | x_test = sequence.pad_sequences(x_test, maxlen=maxlen)
35 | print('x_train shape:', x_train.shape)
36 | print('x_test shape:', x_test.shape)
37 |
38 | print('Build model...')
39 | model = Sequential()
40 | model.add(Embedding(max_features, 128))
41 | model.add(LSTM(128, dropout=0.2, recurrent_dropout=0.2))
42 | model.add(Dense(1, activation='sigmoid'))
43 |
44 | # try using different optimizers and different optimizer configs
45 | model.compile(loss='binary_crossentropy',
46 | optimizer='adam',
47 | metrics=['accuracy'])
48 |
49 | print('Train...')
50 | model.fit(x_train, y_train,
51 | batch_size=batch_size,
52 | epochs=15,
53 | validation_data=(x_test, y_test))
54 | score, acc = model.evaluate(x_test, y_test,
55 | batch_size=batch_size)
56 | print('Test score:', score)
57 | print('Test accuracy:', acc)
58 |
--------------------------------------------------------------------------------
/docs/mkdocs.yml:
--------------------------------------------------------------------------------
1 | site_name: Keras Documentation
2 | theme: readthedocs
3 | # theme_dir: theme
4 | docs_dir: sources
5 | repo_url: http://github.com/keras-team/keras
6 | site_url: http://keras.io/
7 | site_description: 'Documentation for Keras, the Python Deep Learning library.'
8 |
9 | dev_addr: '0.0.0.0:8000'
10 | google_analytics: ['UA-61785484-1', 'keras.io']
11 |
12 | pages:
13 | - Home: index.md
14 | - Why use Keras: why-use-keras.md
15 | - Getting started:
16 | - Guide to the Sequential model: getting-started/sequential-model-guide.md
17 | - Guide to the Functional API: getting-started/functional-api-guide.md
18 | - FAQ: getting-started/faq.md
19 | - Models:
20 | - About Keras models: models/about-keras-models.md
21 | - Sequential: models/sequential.md
22 | - Model (functional API): models/model.md
23 | - Layers:
24 | - About Keras layers: layers/about-keras-layers.md
25 | - Core Layers: layers/core.md
26 | - Convolutional Layers: layers/convolutional.md
27 | - Pooling Layers: layers/pooling.md
28 | - Locally-connected Layers: layers/local.md
29 | - Recurrent Layers: layers/recurrent.md
30 | - Embedding Layers: layers/embeddings.md
31 | - Merge Layers: layers/merge.md
32 | - Advanced Activations Layers: layers/advanced-activations.md
33 | - Normalization Layers: layers/normalization.md
34 | - Noise layers: layers/noise.md
35 | - Layer wrappers: layers/wrappers.md
36 | - Writing your own Keras layers: layers/writing-your-own-keras-layers.md
37 | - Preprocessing:
38 | - Sequence Preprocessing: preprocessing/sequence.md
39 | - Text Preprocessing: preprocessing/text.md
40 | - Image Preprocessing: preprocessing/image.md
41 | - Losses: losses.md
42 | - Metrics: metrics.md
43 | - Optimizers: optimizers.md
44 | - Activations: activations.md
45 | - Callbacks: callbacks.md
46 | - Datasets: datasets.md
47 | - Applications: applications.md
48 | - Backend: backend.md
49 | - Initializers: initializers.md
50 | - Regularizers: regularizers.md
51 | - Constraints: constraints.md
52 | - Visualization: visualization.md
53 | - Scikit-learn API: scikit-learn-api.md
54 | - Utils: utils.md
55 | - Contributing: contributing.md
56 |
--------------------------------------------------------------------------------
/examples/reuters_mlp.py:
--------------------------------------------------------------------------------
1 | '''Trains and evaluate a simple MLP
2 | on the Reuters newswire topic classification task.
3 | '''
4 | from __future__ import print_function
5 |
6 | import numpy as np
7 | import keras
8 | from keras.datasets import reuters
9 | from keras.models import Sequential
10 | from keras.layers import Dense, Dropout, Activation
11 | from keras.preprocessing.text import Tokenizer
12 |
13 | max_words = 1000
14 | batch_size = 32
15 | epochs = 5
16 |
17 | print('Loading data...')
18 | (x_train, y_train), (x_test, y_test) = reuters.load_data(num_words=max_words,
19 | test_split=0.2)
20 | print(len(x_train), 'train sequences')
21 | print(len(x_test), 'test sequences')
22 |
23 | num_classes = np.max(y_train) + 1
24 | print(num_classes, 'classes')
25 |
26 | print('Vectorizing sequence data...')
27 | tokenizer = Tokenizer(num_words=max_words)
28 | x_train = tokenizer.sequences_to_matrix(x_train, mode='binary')
29 | x_test = tokenizer.sequences_to_matrix(x_test, mode='binary')
30 | print('x_train shape:', x_train.shape)
31 | print('x_test shape:', x_test.shape)
32 |
33 | print('Convert class vector to binary class matrix '
34 | '(for use with categorical_crossentropy)')
35 | y_train = keras.utils.to_categorical(y_train, num_classes)
36 | y_test = keras.utils.to_categorical(y_test, num_classes)
37 | print('y_train shape:', y_train.shape)
38 | print('y_test shape:', y_test.shape)
39 |
40 | print('Building model...')
41 | model = Sequential()
42 | model.add(Dense(512, input_shape=(max_words,)))
43 | model.add(Activation('relu'))
44 | model.add(Dropout(0.5))
45 | model.add(Dense(num_classes))
46 | model.add(Activation('softmax'))
47 |
48 | model.compile(loss='categorical_crossentropy',
49 | optimizer='adam',
50 | metrics=['accuracy'])
51 |
52 | history = model.fit(x_train, y_train,
53 | batch_size=batch_size,
54 | epochs=epochs,
55 | verbose=1,
56 | validation_split=0.1)
57 | score = model.evaluate(x_test, y_test,
58 | batch_size=batch_size, verbose=1)
59 | print('Test score:', score[0])
60 | print('Test accuracy:', score[1])
61 |
--------------------------------------------------------------------------------
/docker/Dockerfile:
--------------------------------------------------------------------------------
1 | ARG cuda_version=9.0
2 | ARG cudnn_version=7
3 | FROM nvidia/cuda:${cuda_version}-cudnn${cudnn_version}-devel
4 |
5 | # Install system packages
6 | RUN apt-get update && apt-get install -y --no-install-recommends \
7 | bzip2 \
8 | g++ \
9 | git \
10 | graphviz \
11 | libgl1-mesa-glx \
12 | libhdf5-dev \
13 | openmpi-bin \
14 | wget && \
15 | rm -rf /var/lib/apt/lists/*
16 |
17 | # Install conda
18 | ENV CONDA_DIR /opt/conda
19 | ENV PATH $CONDA_DIR/bin:$PATH
20 |
21 | RUN wget --quiet --no-check-certificate https://repo.continuum.io/miniconda/Miniconda3-4.2.12-Linux-x86_64.sh && \
22 | echo "c59b3dd3cad550ac7596e0d599b91e75d88826db132e4146030ef471bb434e9a *Miniconda3-4.2.12-Linux-x86_64.sh" | sha256sum -c - && \
23 | /bin/bash /Miniconda3-4.2.12-Linux-x86_64.sh -f -b -p $CONDA_DIR && \
24 | rm Miniconda3-4.2.12-Linux-x86_64.sh && \
25 | echo export PATH=$CONDA_DIR/bin:'$PATH' > /etc/profile.d/conda.sh
26 |
27 | # Install Python packages and keras
28 | ENV NB_USER keras
29 | ENV NB_UID 1000
30 |
31 | RUN useradd -m -s /bin/bash -N -u $NB_UID $NB_USER && \
32 | chown $NB_USER $CONDA_DIR -R && \
33 | mkdir -p /src && \
34 | chown $NB_USER /src
35 |
36 | USER $NB_USER
37 |
38 | ARG python_version=3.6
39 |
40 | RUN conda install -y python=${python_version} && \
41 | pip install --upgrade pip && \
42 | pip install \
43 | sklearn_pandas \
44 | tensorflow-gpu && \
45 | pip install https://cntk.ai/PythonWheel/GPU/cntk-2.1-cp36-cp36m-linux_x86_64.whl && \
46 | conda install \
47 | bcolz \
48 | h5py \
49 | matplotlib \
50 | mkl \
51 | nose \
52 | notebook \
53 | Pillow \
54 | pandas \
55 | pygpu \
56 | pyyaml \
57 | scikit-learn \
58 | six \
59 | theano && \
60 | git clone git://github.com/keras-team/keras.git /src && pip install -e /src[tests] && \
61 | pip install git+git://github.com/keras-team/keras.git && \
62 | conda clean -yt
63 |
64 | ADD theanorc /home/keras/.theanorc
65 |
66 | ENV PYTHONPATH='/src/:$PYTHONPATH'
67 |
68 | WORKDIR /src
69 |
70 | EXPOSE 8888
71 |
72 | CMD jupyter notebook --port=8888 --ip=0.0.0.0
73 |
74 |
--------------------------------------------------------------------------------
/docs/templates/scikit-learn-api.md:
--------------------------------------------------------------------------------
1 | # Wrappers for the Scikit-Learn API
2 |
3 | You can use `Sequential` Keras models (single-input only) as part of your Scikit-Learn workflow via the wrappers found at `keras.wrappers.scikit_learn.py`.
4 |
5 | There are two wrappers available:
6 |
7 | `keras.wrappers.scikit_learn.KerasClassifier(build_fn=None, **sk_params)`, which implements the Scikit-Learn classifier interface,
8 |
9 | `keras.wrappers.scikit_learn.KerasRegressor(build_fn=None, **sk_params)`, which implements the Scikit-Learn regressor interface.
10 |
11 | ### Arguments
12 |
13 | - __build_fn__: callable function or class instance
14 | - __sk_params__: model parameters & fitting parameters
15 |
16 | `build_fn` should construct, compile and return a Keras model, which
17 | will then be used to fit/predict. One of the following
18 | three values could be passed to `build_fn`:
19 |
20 | 1. A function
21 | 2. An instance of a class that implements the `__call__` method
22 | 3. None. This means you implement a class that inherits from either
23 | `KerasClassifier` or `KerasRegressor`. The `__call__` method of the
24 | present class will then be treated as the default `build_fn`.
25 |
26 | `sk_params` takes both model parameters and fitting parameters. Legal model
27 | parameters are the arguments of `build_fn`. Note that like all other
28 | estimators in scikit-learn, `build_fn` should provide default values for
29 | its arguments, so that you could create the estimator without passing any
30 | values to `sk_params`.
31 |
32 | `sk_params` could also accept parameters for calling `fit`, `predict`,
33 | `predict_proba`, and `score` methods (e.g., `epochs`, `batch_size`).
34 | fitting (predicting) parameters are selected in the following order:
35 |
36 | 1. Values passed to the dictionary arguments of
37 | `fit`, `predict`, `predict_proba`, and `score` methods
38 | 2. Values passed to `sk_params`
39 | 3. The default values of the `keras.models.Sequential`
40 | `fit`, `predict`, `predict_proba` and `score` methods
41 |
42 | When using scikit-learn's `grid_search` API, legal tunable parameters are
43 | those you could pass to `sk_params`, including fitting parameters.
44 | In other words, you could use `grid_search` to search for the best
45 | `batch_size` or `epochs` as well as the model parameters.
46 |
--------------------------------------------------------------------------------
/examples/imdb_cnn_lstm.py:
--------------------------------------------------------------------------------
1 | '''Train a recurrent convolutional network on the IMDB sentiment
2 | classification task.
3 |
4 | Gets to 0.8498 test accuracy after 2 epochs. 41s/epoch on K520 GPU.
5 | '''
6 | from __future__ import print_function
7 |
8 | from keras.preprocessing import sequence
9 | from keras.models import Sequential
10 | from keras.layers import Dense, Dropout, Activation
11 | from keras.layers import Embedding
12 | from keras.layers import LSTM
13 | from keras.layers import Conv1D, MaxPooling1D
14 | from keras.datasets import imdb
15 |
16 | # Embedding
17 | max_features = 20000
18 | maxlen = 100
19 | embedding_size = 128
20 |
21 | # Convolution
22 | kernel_size = 5
23 | filters = 64
24 | pool_size = 4
25 |
26 | # LSTM
27 | lstm_output_size = 70
28 |
29 | # Training
30 | batch_size = 30
31 | epochs = 2
32 |
33 | '''
34 | Note:
35 | batch_size is highly sensitive.
36 | Only 2 epochs are needed as the dataset is very small.
37 | '''
38 |
39 | print('Loading data...')
40 | (x_train, y_train), (x_test, y_test) = imdb.load_data(num_words=max_features)
41 | print(len(x_train), 'train sequences')
42 | print(len(x_test), 'test sequences')
43 |
44 | print('Pad sequences (samples x time)')
45 | x_train = sequence.pad_sequences(x_train, maxlen=maxlen)
46 | x_test = sequence.pad_sequences(x_test, maxlen=maxlen)
47 | print('x_train shape:', x_train.shape)
48 | print('x_test shape:', x_test.shape)
49 |
50 | print('Build model...')
51 |
52 | model = Sequential()
53 | model.add(Embedding(max_features, embedding_size, input_length=maxlen))
54 | model.add(Dropout(0.25))
55 | model.add(Conv1D(filters,
56 | kernel_size,
57 | padding='valid',
58 | activation='relu',
59 | strides=1))
60 | model.add(MaxPooling1D(pool_size=pool_size))
61 | model.add(LSTM(lstm_output_size))
62 | model.add(Dense(1))
63 | model.add(Activation('sigmoid'))
64 |
65 | model.compile(loss='binary_crossentropy',
66 | optimizer='adam',
67 | metrics=['accuracy'])
68 |
69 | print('Train...')
70 | model.fit(x_train, y_train,
71 | batch_size=batch_size,
72 | epochs=epochs,
73 | validation_data=(x_test, y_test))
74 | score, acc = model.evaluate(x_test, y_test, batch_size=batch_size)
75 | print('Test score:', score)
76 | print('Test accuracy:', acc)
77 |
--------------------------------------------------------------------------------
/tests/keras/layers/local_test.py:
--------------------------------------------------------------------------------
1 | import pytest
2 |
3 | from keras.utils.test_utils import layer_test
4 | from keras.utils.test_utils import keras_test
5 | from keras.layers import local
6 |
7 |
8 | @keras_test
9 | def test_locallyconnected_1d():
10 | num_samples = 2
11 | num_steps = 8
12 | input_dim = 5
13 | filter_length = 3
14 | filters = 4
15 | padding = 'valid'
16 | strides = 1
17 |
18 | layer_test(local.LocallyConnected1D,
19 | kwargs={'filters': filters,
20 | 'kernel_size': filter_length,
21 | 'padding': padding,
22 | 'kernel_regularizer': 'l2',
23 | 'bias_regularizer': 'l2',
24 | 'activity_regularizer': 'l2',
25 | 'strides': strides},
26 | input_shape=(num_samples, num_steps, input_dim))
27 |
28 |
29 | @keras_test
30 | def test_locallyconnected_2d():
31 | num_samples = 5
32 | filters = 3
33 | stack_size = 4
34 | num_row = 6
35 | num_col = 8
36 | padding = 'valid'
37 |
38 | for strides in [(1, 1), (2, 2)]:
39 | layer_test(local.LocallyConnected2D,
40 | kwargs={'filters': filters,
41 | 'kernel_size': 3,
42 | 'padding': padding,
43 | 'kernel_regularizer': 'l2',
44 | 'bias_regularizer': 'l2',
45 | 'activity_regularizer': 'l2',
46 | 'strides': strides,
47 | 'data_format': 'channels_last'},
48 | input_shape=(num_samples, num_row, num_col, stack_size))
49 |
50 | layer_test(local.LocallyConnected2D,
51 | kwargs={'filters': filters,
52 | 'kernel_size': (3, 3),
53 | 'padding': padding,
54 | 'kernel_regularizer': 'l2',
55 | 'bias_regularizer': 'l2',
56 | 'activity_regularizer': 'l2',
57 | 'strides': strides,
58 | 'data_format': 'channels_first'},
59 | input_shape=(num_samples, stack_size, num_row, num_col))
60 |
61 |
62 | if __name__ == '__main__':
63 | pytest.main([__file__])
64 |
--------------------------------------------------------------------------------
/setup.py:
--------------------------------------------------------------------------------
1 | from setuptools import setup
2 | from setuptools import find_packages
3 |
4 | long_description = '''
5 | Keras is a high-level neural networks API,
6 | written in Python and capable of running on top of
7 | TensorFlow, CNTK, or Theano.
8 |
9 | Use Keras if you need a deep learning library that:
10 |
11 | - Allows for easy and fast prototyping
12 | (through user friendliness, modularity, and extensibility).
13 | - Supports both convolutional networks and recurrent networks,
14 | as well as combinations of the two.
15 | - Runs seamlessly on CPU and GPU.
16 |
17 | Read the documentation at: https://keras.io/
18 |
19 | For a detailed overview of what makes Keras special, see:
20 | https://keras.io/why-use-keras/
21 |
22 | Keras is compatible with Python 2.7-3.6
23 | and is distributed under the MIT liense.
24 | '''
25 |
26 | setup(name='Keras',
27 | version='2.1.6',
28 | description='Deep Learning for humans',
29 | long_description=long_description,
30 | author='Francois Chollet',
31 | author_email='francois.chollet@gmail.com',
32 | url='https://github.com/keras-team/keras',
33 | download_url='https://github.com/keras-team/keras/tarball/2.1.6',
34 | license='MIT',
35 | install_requires=['numpy>=1.9.1',
36 | 'scipy>=0.14',
37 | 'six>=1.9.0',
38 | 'pyyaml',
39 | 'h5py'],
40 | extras_require={
41 | 'visualize': ['pydot>=1.2.4'],
42 | 'tests': ['pytest',
43 | 'pytest-pep8',
44 | 'pytest-xdist',
45 | 'pytest-cov',
46 | 'pandas',
47 | 'requests'],
48 | },
49 | classifiers=[
50 | 'Development Status :: 5 - Production/Stable',
51 | 'Intended Audience :: Developers',
52 | 'Intended Audience :: Education',
53 | 'Intended Audience :: Science/Research',
54 | 'License :: OSI Approved :: MIT License',
55 | 'Programming Language :: Python :: 2',
56 | 'Programming Language :: Python :: 2.7',
57 | 'Programming Language :: Python :: 3',
58 | 'Programming Language :: Python :: 3.6',
59 | 'Topic :: Software Development :: Libraries',
60 | 'Topic :: Software Development :: Libraries :: Python Modules'
61 | ],
62 | packages=find_packages())
63 |
--------------------------------------------------------------------------------
/examples/imdb_cnn.py:
--------------------------------------------------------------------------------
1 | '''This example demonstrates the use of Convolution1D for text classification.
2 |
3 | Gets to 0.89 test accuracy after 2 epochs.
4 | 90s/epoch on Intel i5 2.4Ghz CPU.
5 | 10s/epoch on Tesla K40 GPU.
6 | '''
7 | from __future__ import print_function
8 |
9 | from keras.preprocessing import sequence
10 | from keras.models import Sequential
11 | from keras.layers import Dense, Dropout, Activation
12 | from keras.layers import Embedding
13 | from keras.layers import Conv1D, GlobalMaxPooling1D
14 | from keras.datasets import imdb
15 |
16 | # set parameters:
17 | max_features = 5000
18 | maxlen = 400
19 | batch_size = 32
20 | embedding_dims = 50
21 | filters = 250
22 | kernel_size = 3
23 | hidden_dims = 250
24 | epochs = 2
25 |
26 | print('Loading data...')
27 | (x_train, y_train), (x_test, y_test) = imdb.load_data(num_words=max_features)
28 | print(len(x_train), 'train sequences')
29 | print(len(x_test), 'test sequences')
30 |
31 | print('Pad sequences (samples x time)')
32 | x_train = sequence.pad_sequences(x_train, maxlen=maxlen)
33 | x_test = sequence.pad_sequences(x_test, maxlen=maxlen)
34 | print('x_train shape:', x_train.shape)
35 | print('x_test shape:', x_test.shape)
36 |
37 | print('Build model...')
38 | model = Sequential()
39 |
40 | # we start off with an efficient embedding layer which maps
41 | # our vocab indices into embedding_dims dimensions
42 | model.add(Embedding(max_features,
43 | embedding_dims,
44 | input_length=maxlen))
45 | model.add(Dropout(0.2))
46 |
47 | # we add a Convolution1D, which will learn filters
48 | # word group filters of size filter_length:
49 | model.add(Conv1D(filters,
50 | kernel_size,
51 | padding='valid',
52 | activation='relu',
53 | strides=1))
54 | # we use max pooling:
55 | model.add(GlobalMaxPooling1D())
56 |
57 | # We add a vanilla hidden layer:
58 | model.add(Dense(hidden_dims))
59 | model.add(Dropout(0.2))
60 | model.add(Activation('relu'))
61 |
62 | # We project onto a single unit output layer, and squash it with a sigmoid:
63 | model.add(Dense(1))
64 | model.add(Activation('sigmoid'))
65 |
66 | model.compile(loss='binary_crossentropy',
67 | optimizer='adam',
68 | metrics=['accuracy'])
69 | model.fit(x_train, y_train,
70 | batch_size=batch_size,
71 | epochs=epochs,
72 | validation_data=(x_test, y_test))
73 |
--------------------------------------------------------------------------------
/docs/templates/models/about-keras-models.md:
--------------------------------------------------------------------------------
1 | # About Keras models
2 |
3 | There are two types of models available in Keras: [the Sequential model](/models/sequential) and [the Model class used with functional API](/models/model).
4 |
5 | These models have a number of methods in common:
6 |
7 | - `model.summary()`: prints a summary representation of your model. Shortcut for [utils.print_summary](/utils/#print_summary)
8 | - `model.get_config()`: returns a dictionary containing the configuration of the model. The model can be reinstantiated from its config via:
9 | ```python
10 | config = model.get_config()
11 | model = Model.from_config(config)
12 | # or, for Sequential:
13 | model = Sequential.from_config(config)
14 | ```
15 |
16 | - `model.get_weights()`: returns a list of all weight tensors in the model, as Numpy arrays.
17 | - `model.set_weights(weights)`: sets the values of the weights of the model, from a list of Numpy arrays. The arrays in the list should have the same shape as those returned by `get_weights()`.
18 | - `model.to_json()`: returns a representation of the model as a JSON string. Note that the representation does not include the weights, only the architecture. You can reinstantiate the same model (with reinitialized weights) from the JSON string via:
19 | ```python
20 | from keras.models import model_from_json
21 |
22 | json_string = model.to_json()
23 | model = model_from_json(json_string)
24 | ```
25 | - `model.to_yaml()`: returns a representation of the model as a YAML string. Note that the representation does not include the weights, only the architecture. You can reinstantiate the same model (with reinitialized weights) from the YAML string via:
26 | ```python
27 | from keras.models import model_from_yaml
28 |
29 | yaml_string = model.to_yaml()
30 | model = model_from_yaml(yaml_string)
31 | ```
32 | - `model.save_weights(filepath)`: saves the weights of the model as a HDF5 file.
33 | - `model.load_weights(filepath, by_name=False)`: loads the weights of the model from a HDF5 file (created by `save_weights`). By default, the architecture is expected to be unchanged. To load weights into a different architecture (with some layers in common), use `by_name=True` to load only those layers with the same name.
34 |
35 | Note: Please also see [How can I install HDF5 or h5py to save my models in Keras?](/getting-started/faq/#how-can-i-install-HDF5-or-h5py-to-save-my-models-in-Keras) in the FAQ for instructions on how to install `h5py`.
--------------------------------------------------------------------------------
/keras/regularizers.py:
--------------------------------------------------------------------------------
1 | """Built-in regularizers.
2 | """
3 | from __future__ import absolute_import
4 | from __future__ import division
5 | from __future__ import print_function
6 |
7 | import six
8 | from . import backend as K
9 | from .utils.generic_utils import serialize_keras_object
10 | from .utils.generic_utils import deserialize_keras_object
11 |
12 |
13 | class Regularizer(object):
14 | """Regularizer base class.
15 | """
16 |
17 | def __call__(self, x):
18 | return 0.
19 |
20 | @classmethod
21 | def from_config(cls, config):
22 | return cls(**config)
23 |
24 |
25 | class L1L2(Regularizer):
26 | """Regularizer for L1 and L2 regularization.
27 |
28 | # Arguments
29 | l1: Float; L1 regularization factor.
30 | l2: Float; L2 regularization factor.
31 | """
32 |
33 | def __init__(self, l1=0., l2=0.):
34 | self.l1 = K.cast_to_floatx(l1)
35 | self.l2 = K.cast_to_floatx(l2)
36 |
37 | def __call__(self, x):
38 | regularization = 0.
39 | if self.l1:
40 | regularization += K.sum(self.l1 * K.abs(x))
41 | if self.l2:
42 | regularization += K.sum(self.l2 * K.square(x))
43 | return regularization
44 |
45 | def get_config(self):
46 | return {'l1': float(self.l1),
47 | 'l2': float(self.l2)}
48 |
49 |
50 | # Aliases.
51 |
52 |
53 | def l1(l=0.01):
54 | return L1L2(l1=l)
55 |
56 |
57 | def l2(l=0.01):
58 | return L1L2(l2=l)
59 |
60 |
61 | def l1_l2(l1=0.01, l2=0.01):
62 | return L1L2(l1=l1, l2=l2)
63 |
64 |
65 | def serialize(regularizer):
66 | return serialize_keras_object(regularizer)
67 |
68 |
69 | def deserialize(config, custom_objects=None):
70 | return deserialize_keras_object(config,
71 | module_objects=globals(),
72 | custom_objects=custom_objects,
73 | printable_module_name='regularizer')
74 |
75 |
76 | def get(identifier):
77 | if identifier is None:
78 | return None
79 | if isinstance(identifier, dict):
80 | return deserialize(identifier)
81 | elif isinstance(identifier, six.string_types):
82 | config = {'class_name': str(identifier), 'config': {}}
83 | return deserialize(config)
84 | elif callable(identifier):
85 | return identifier
86 | else:
87 | raise ValueError('Could not interpret regularizer identifier: ' +
88 | str(identifier))
89 |
--------------------------------------------------------------------------------
/docs/templates/callbacks.md:
--------------------------------------------------------------------------------
1 | ## Usage of callbacks
2 |
3 | A callback is a set of functions to be applied at given stages of the training procedure. You can use callbacks to get a view on internal states and statistics of the model during training. You can pass a list of callbacks (as the keyword argument `callbacks`) to the `.fit()` method of the `Sequential` or `Model` classes. The relevant methods of the callbacks will then be called at each stage of the training.
4 |
5 | ---
6 |
7 | {{autogenerated}}
8 |
9 | ---
10 |
11 |
12 | # Create a callback
13 |
14 | You can create a custom callback by extending the base class `keras.callbacks.Callback`. A callback has access to its associated model through the class property `self.model`.
15 |
16 | Here's a simple example saving a list of losses over each batch during training:
17 | ```python
18 | class LossHistory(keras.callbacks.Callback):
19 | def on_train_begin(self, logs={}):
20 | self.losses = []
21 |
22 | def on_batch_end(self, batch, logs={}):
23 | self.losses.append(logs.get('loss'))
24 | ```
25 |
26 | ---
27 |
28 | ### Example: recording loss history
29 |
30 | ```python
31 | class LossHistory(keras.callbacks.Callback):
32 | def on_train_begin(self, logs={}):
33 | self.losses = []
34 |
35 | def on_batch_end(self, batch, logs={}):
36 | self.losses.append(logs.get('loss'))
37 |
38 | model = Sequential()
39 | model.add(Dense(10, input_dim=784, kernel_initializer='uniform'))
40 | model.add(Activation('softmax'))
41 | model.compile(loss='categorical_crossentropy', optimizer='rmsprop')
42 |
43 | history = LossHistory()
44 | model.fit(x_train, y_train, batch_size=128, epochs=20, verbose=0, callbacks=[history])
45 |
46 | print(history.losses)
47 | # outputs
48 | '''
49 | [0.66047596406559383, 0.3547245744908703, ..., 0.25953155204159617, 0.25901699725311789]
50 | '''
51 | ```
52 |
53 | ---
54 |
55 | ### Example: model checkpoints
56 |
57 | ```python
58 | from keras.callbacks import ModelCheckpoint
59 |
60 | model = Sequential()
61 | model.add(Dense(10, input_dim=784, kernel_initializer='uniform'))
62 | model.add(Activation('softmax'))
63 | model.compile(loss='categorical_crossentropy', optimizer='rmsprop')
64 |
65 | '''
66 | saves the model weights after each epoch if the validation loss decreased
67 | '''
68 | checkpointer = ModelCheckpoint(filepath='/tmp/weights.hdf5', verbose=1, save_best_only=True)
69 | model.fit(x_train, y_train, batch_size=128, epochs=20, verbose=0, validation_data=(X_test, Y_test), callbacks=[checkpointer])
70 | ```
71 |
--------------------------------------------------------------------------------
/examples/mnist_cnn.py:
--------------------------------------------------------------------------------
1 | '''Trains a simple convnet on the MNIST dataset.
2 |
3 | Gets to 99.25% test accuracy after 12 epochs
4 | (there is still a lot of margin for parameter tuning).
5 | 16 seconds per epoch on a GRID K520 GPU.
6 | '''
7 |
8 | from __future__ import print_function
9 | import keras
10 | from keras.datasets import mnist
11 | from keras.models import Sequential
12 | from keras.layers import Dense, Dropout, Flatten
13 | from keras.layers import Conv2D, MaxPooling2D
14 | from keras import backend as K
15 |
16 | batch_size = 128
17 | num_classes = 10
18 | epochs = 12
19 |
20 | # input image dimensions
21 | img_rows, img_cols = 28, 28
22 |
23 | # the data, split between train and test sets
24 | (x_train, y_train), (x_test, y_test) = mnist.load_data()
25 |
26 | if K.image_data_format() == 'channels_first':
27 | x_train = x_train.reshape(x_train.shape[0], 1, img_rows, img_cols)
28 | x_test = x_test.reshape(x_test.shape[0], 1, img_rows, img_cols)
29 | input_shape = (1, img_rows, img_cols)
30 | else:
31 | x_train = x_train.reshape(x_train.shape[0], img_rows, img_cols, 1)
32 | x_test = x_test.reshape(x_test.shape[0], img_rows, img_cols, 1)
33 | input_shape = (img_rows, img_cols, 1)
34 |
35 | x_train = x_train.astype('float32')
36 | x_test = x_test.astype('float32')
37 | x_train /= 255
38 | x_test /= 255
39 | print('x_train shape:', x_train.shape)
40 | print(x_train.shape[0], 'train samples')
41 | print(x_test.shape[0], 'test samples')
42 |
43 | # convert class vectors to binary class matrices
44 | y_train = keras.utils.to_categorical(y_train, num_classes)
45 | y_test = keras.utils.to_categorical(y_test, num_classes)
46 |
47 | model = Sequential()
48 | model.add(Conv2D(32, kernel_size=(3, 3),
49 | activation='relu',
50 | input_shape=input_shape))
51 | model.add(Conv2D(64, (3, 3), activation='relu'))
52 | model.add(MaxPooling2D(pool_size=(2, 2)))
53 | model.add(Dropout(0.25))
54 | model.add(Flatten())
55 | model.add(Dense(128, activation='relu'))
56 | model.add(Dropout(0.5))
57 | model.add(Dense(num_classes, activation='softmax'))
58 |
59 | model.compile(loss=keras.losses.categorical_crossentropy,
60 | optimizer=keras.optimizers.Adadelta(),
61 | metrics=['accuracy'])
62 |
63 | model.fit(x_train, y_train,
64 | batch_size=batch_size,
65 | epochs=epochs,
66 | verbose=1,
67 | validation_data=(x_test, y_test))
68 | score = model.evaluate(x_test, y_test, verbose=0)
69 | print('Test loss:', score[0])
70 | print('Test accuracy:', score[1])
71 |
--------------------------------------------------------------------------------
/examples/mnist_irnn.py:
--------------------------------------------------------------------------------
1 | '''This is a reproduction of the IRNN experiment
2 | with pixel-by-pixel sequential MNIST in
3 | "A Simple Way to Initialize Recurrent Networks of Rectified Linear Units"
4 | by Quoc V. Le, Navdeep Jaitly, Geoffrey E. Hinton
5 |
6 | arxiv:1504.00941v2 [cs.NE] 7 Apr 2015
7 | http://arxiv.org/pdf/1504.00941v2.pdf
8 |
9 | Optimizer is replaced with RMSprop which yields more stable and steady
10 | improvement.
11 |
12 | Reaches 0.93 train/test accuracy after 900 epochs
13 | (which roughly corresponds to 1687500 steps in the original paper.)
14 | '''
15 |
16 | from __future__ import print_function
17 |
18 | import keras
19 | from keras.datasets import mnist
20 | from keras.models import Sequential
21 | from keras.layers import Dense, Activation
22 | from keras.layers import SimpleRNN
23 | from keras import initializers
24 | from keras.optimizers import RMSprop
25 |
26 | batch_size = 32
27 | num_classes = 10
28 | epochs = 200
29 | hidden_units = 100
30 |
31 | learning_rate = 1e-6
32 | clip_norm = 1.0
33 |
34 | # the data, split between train and test sets
35 | (x_train, y_train), (x_test, y_test) = mnist.load_data()
36 |
37 | x_train = x_train.reshape(x_train.shape[0], -1, 1)
38 | x_test = x_test.reshape(x_test.shape[0], -1, 1)
39 | x_train = x_train.astype('float32')
40 | x_test = x_test.astype('float32')
41 | x_train /= 255
42 | x_test /= 255
43 | print('x_train shape:', x_train.shape)
44 | print(x_train.shape[0], 'train samples')
45 | print(x_test.shape[0], 'test samples')
46 |
47 | # convert class vectors to binary class matrices
48 | y_train = keras.utils.to_categorical(y_train, num_classes)
49 | y_test = keras.utils.to_categorical(y_test, num_classes)
50 |
51 | print('Evaluate IRNN...')
52 | model = Sequential()
53 | model.add(SimpleRNN(hidden_units,
54 | kernel_initializer=initializers.RandomNormal(stddev=0.001),
55 | recurrent_initializer=initializers.Identity(gain=1.0),
56 | activation='relu',
57 | input_shape=x_train.shape[1:]))
58 | model.add(Dense(num_classes))
59 | model.add(Activation('softmax'))
60 | rmsprop = RMSprop(lr=learning_rate)
61 | model.compile(loss='categorical_crossentropy',
62 | optimizer=rmsprop,
63 | metrics=['accuracy'])
64 |
65 | model.fit(x_train, y_train,
66 | batch_size=batch_size,
67 | epochs=epochs,
68 | verbose=1,
69 | validation_data=(x_test, y_test))
70 |
71 | scores = model.evaluate(x_test, y_test, verbose=0)
72 | print('IRNN test score:', scores[0])
73 | print('IRNN test accuracy:', scores[1])
74 |
--------------------------------------------------------------------------------
/tests/keras/utils/layer_utils_test.py:
--------------------------------------------------------------------------------
1 | import pytest
2 | import numpy as np
3 | from numpy.testing import assert_allclose
4 | from keras import backend as K
5 | from keras.layers import Conv2D
6 | from keras.layers import Dense
7 | from keras.layers import Flatten
8 | from keras.models import Sequential
9 | from keras.utils import layer_utils
10 | from keras.utils.test_utils import keras_test
11 |
12 |
13 | @keras_test
14 | def test_convert_weights():
15 | def get_model(shape, data_format):
16 | model = Sequential()
17 | model.add(Conv2D(filters=2,
18 | kernel_size=(4, 3),
19 | input_shape=shape,
20 | data_format=data_format))
21 | model.add(Flatten())
22 | model.add(Dense(5))
23 | return model
24 |
25 | for data_format in ['channels_first', 'channels_last']:
26 | if data_format == 'channels_first':
27 | shape = (3, 5, 5)
28 | target_shape = (5, 5, 3)
29 | prev_shape = (2, 3, 2)
30 | flip = lambda x: np.flip(np.flip(x, axis=2), axis=3)
31 | transpose = lambda x: np.transpose(x, (0, 2, 3, 1))
32 | target_data_format = 'channels_last'
33 | elif data_format == 'channels_last':
34 | shape = (5, 5, 3)
35 | target_shape = (3, 5, 5)
36 | prev_shape = (2, 2, 3)
37 | flip = lambda x: np.flip(np.flip(x, axis=1), axis=2)
38 | transpose = lambda x: np.transpose(x, (0, 3, 1, 2))
39 | target_data_format = 'channels_first'
40 |
41 | model1 = get_model(shape, data_format)
42 | model2 = get_model(target_shape, target_data_format)
43 | conv = K.function([model1.input], [model1.layers[0].output])
44 |
45 | x = np.random.random((1,) + shape)
46 |
47 | # Test equivalence of convert_all_kernels_in_model
48 | convout1 = conv([x])[0]
49 | layer_utils.convert_all_kernels_in_model(model1)
50 | convout2 = flip(conv([flip(x)])[0])
51 |
52 | assert_allclose(convout1, convout2, atol=1e-5)
53 |
54 | # Test equivalence of convert_dense_weights_data_format
55 | out1 = model1.predict(x)
56 | layer_utils.convert_dense_weights_data_format(model1.layers[2], prev_shape, target_data_format)
57 | for (src, dst) in zip(model1.layers, model2.layers):
58 | dst.set_weights(src.get_weights())
59 | out2 = model2.predict(transpose(x))
60 |
61 | assert_allclose(out1, out2, atol=1e-5)
62 |
63 |
64 | if __name__ == '__main__':
65 | pytest.main([__file__])
66 |
--------------------------------------------------------------------------------
/keras/metrics.py:
--------------------------------------------------------------------------------
1 | """Built-in metrics.
2 | """
3 | from __future__ import absolute_import
4 | from __future__ import division
5 | from __future__ import print_function
6 |
7 | import six
8 | from . import backend as K
9 | from .losses import mean_squared_error
10 | from .losses import mean_absolute_error
11 | from .losses import mean_absolute_percentage_error
12 | from .losses import mean_squared_logarithmic_error
13 | from .losses import hinge
14 | from .losses import logcosh
15 | from .losses import squared_hinge
16 | from .losses import categorical_crossentropy
17 | from .losses import sparse_categorical_crossentropy
18 | from .losses import binary_crossentropy
19 | from .losses import kullback_leibler_divergence
20 | from .losses import poisson
21 | from .losses import cosine_proximity
22 | from .utils.generic_utils import deserialize_keras_object
23 | from .utils.generic_utils import serialize_keras_object
24 |
25 |
26 | def binary_accuracy(y_true, y_pred):
27 | return K.mean(K.equal(y_true, K.round(y_pred)), axis=-1)
28 |
29 |
30 | def categorical_accuracy(y_true, y_pred):
31 | return K.cast(K.equal(K.argmax(y_true, axis=-1),
32 | K.argmax(y_pred, axis=-1)),
33 | K.floatx())
34 |
35 |
36 | def sparse_categorical_accuracy(y_true, y_pred):
37 | return K.cast(K.equal(K.max(y_true, axis=-1),
38 | K.cast(K.argmax(y_pred, axis=-1), K.floatx())),
39 | K.floatx())
40 |
41 |
42 | def top_k_categorical_accuracy(y_true, y_pred, k=5):
43 | return K.mean(K.in_top_k(y_pred, K.argmax(y_true, axis=-1), k), axis=-1)
44 |
45 |
46 | def sparse_top_k_categorical_accuracy(y_true, y_pred, k=5):
47 | return K.mean(K.in_top_k(y_pred, K.cast(K.max(y_true, axis=-1), 'int32'), k), axis=-1)
48 |
49 |
50 | # Aliases
51 |
52 | mse = MSE = mean_squared_error
53 | mae = MAE = mean_absolute_error
54 | mape = MAPE = mean_absolute_percentage_error
55 | msle = MSLE = mean_squared_logarithmic_error
56 | cosine = cosine_proximity
57 |
58 |
59 | def serialize(metric):
60 | return serialize_keras_object(metric)
61 |
62 |
63 | def deserialize(config, custom_objects=None):
64 | return deserialize_keras_object(config,
65 | module_objects=globals(),
66 | custom_objects=custom_objects,
67 | printable_module_name='metric function')
68 |
69 |
70 | def get(identifier):
71 | if isinstance(identifier, dict):
72 | config = {'class_name': str(identifier), 'config': {}}
73 | return deserialize(config)
74 | elif isinstance(identifier, six.string_types):
75 | return deserialize(str(identifier))
76 | elif callable(identifier):
77 | return identifier
78 | else:
79 | raise ValueError('Could not interpret '
80 | 'metric function identifier:', identifier)
81 |
--------------------------------------------------------------------------------
/tests/keras/constraints_test.py:
--------------------------------------------------------------------------------
1 | import pytest
2 | import numpy as np
3 | from numpy.testing import assert_allclose
4 |
5 | from keras import backend as K
6 | from keras import constraints
7 | from keras.utils.test_utils import keras_test
8 |
9 |
10 | def get_test_values():
11 | return [0.1, 0.5, 3, 8, 1e-7]
12 |
13 |
14 | def get_example_array():
15 | np.random.seed(3537)
16 | example_array = np.random.random((100, 100)) * 100. - 50.
17 | example_array[0, 0] = 0. # 0 could possibly cause trouble
18 | return example_array
19 |
20 |
21 | def test_serialization():
22 | all_activations = ['max_norm', 'non_neg',
23 | 'unit_norm', 'min_max_norm']
24 | for name in all_activations:
25 | fn = constraints.get(name)
26 | ref_fn = getattr(constraints, name)()
27 | assert fn.__class__ == ref_fn.__class__
28 | config = constraints.serialize(fn)
29 | fn = constraints.deserialize(config)
30 | assert fn.__class__ == ref_fn.__class__
31 |
32 |
33 | @keras_test
34 | def test_max_norm():
35 | array = get_example_array()
36 | for m in get_test_values():
37 | norm_instance = constraints.max_norm(m)
38 | normed = norm_instance(K.variable(array))
39 | assert(np.all(K.eval(normed) < m))
40 |
41 | # a more explicit example
42 | norm_instance = constraints.max_norm(2.0)
43 | x = np.array([[0, 0, 0], [1.0, 0, 0], [3, 0, 0], [3, 3, 3]]).T
44 | x_normed_target = np.array([[0, 0, 0], [1.0, 0, 0],
45 | [2.0, 0, 0],
46 | [2. / np.sqrt(3),
47 | 2. / np.sqrt(3),
48 | 2. / np.sqrt(3)]]).T
49 | x_normed_actual = K.eval(norm_instance(K.variable(x)))
50 | assert_allclose(x_normed_actual, x_normed_target, rtol=1e-05)
51 |
52 |
53 | @keras_test
54 | def test_non_neg():
55 | non_neg_instance = constraints.non_neg()
56 | normed = non_neg_instance(K.variable(get_example_array()))
57 | assert(np.all(np.min(K.eval(normed), axis=1) == 0.))
58 |
59 |
60 | @keras_test
61 | def test_unit_norm():
62 | unit_norm_instance = constraints.unit_norm()
63 | normalized = unit_norm_instance(K.variable(get_example_array()))
64 | norm_of_normalized = np.sqrt(np.sum(K.eval(normalized) ** 2, axis=0))
65 | # In the unit norm constraint, it should be equal to 1.
66 | difference = norm_of_normalized - 1.
67 | largest_difference = np.max(np.abs(difference))
68 | assert(np.abs(largest_difference) < 10e-5)
69 |
70 |
71 | @keras_test
72 | def test_min_max_norm():
73 | array = get_example_array()
74 | for m in get_test_values():
75 | norm_instance = constraints.min_max_norm(min_value=m, max_value=m * 2)
76 | normed = norm_instance(K.variable(array))
77 | value = K.eval(normed)
78 | l2 = np.sqrt(np.sum(np.square(value), axis=0))
79 | assert not l2[l2 < m]
80 | assert not l2[l2 > m * 2 + 1e-5]
81 |
82 |
83 | if __name__ == '__main__':
84 | pytest.main([__file__])
85 |
--------------------------------------------------------------------------------
/tests/keras/utils/conv_utils_test.py:
--------------------------------------------------------------------------------
1 | import pytest
2 | import numpy as np
3 | from keras.utils import conv_utils
4 |
5 |
6 | def test_normalize_tuple():
7 | assert conv_utils.normalize_tuple(5, 2, 'kernel_size') == (5, 5)
8 | assert conv_utils.normalize_tuple([7, 9], 2, 'kernel_size') == (7, 9)
9 |
10 | with pytest.raises(ValueError):
11 | conv_utils.normalize_tuple(None, 2, 'kernel_size')
12 | with pytest.raises(ValueError):
13 | conv_utils.normalize_tuple([2, 3, 4], 2, 'kernel_size')
14 | with pytest.raises(ValueError):
15 | conv_utils.normalize_tuple(['str', 'impossible'], 2, 'kernel_size')
16 |
17 |
18 | def test_invalid_data_format():
19 | with pytest.raises(ValueError):
20 | conv_utils.normalize_data_format('channels_middle')
21 |
22 |
23 | def test_invalid_padding():
24 | with pytest.raises(ValueError):
25 | conv_utils.normalize_padding('diagonal')
26 |
27 |
28 | def test_invalid_convert_kernel():
29 | with pytest.raises(ValueError):
30 | conv_utils.convert_kernel(np.zeros((10, 20)))
31 |
32 |
33 | def test_conv_output_length():
34 | assert conv_utils.conv_output_length(None, 7, 'same', 1) is None
35 | assert conv_utils.conv_output_length(224, 7, 'same', 1) == 224
36 | assert conv_utils.conv_output_length(224, 7, 'same', 2) == 112
37 | assert conv_utils.conv_output_length(32, 5, 'valid', 1) == 28
38 | assert conv_utils.conv_output_length(32, 5, 'valid', 2) == 14
39 | assert conv_utils.conv_output_length(32, 5, 'causal', 1) == 32
40 | assert conv_utils.conv_output_length(32, 5, 'causal', 2) == 16
41 | assert conv_utils.conv_output_length(32, 5, 'full', 1) == 36
42 | assert conv_utils.conv_output_length(32, 5, 'full', 2) == 18
43 |
44 | with pytest.raises(AssertionError):
45 | conv_utils.conv_output_length(32, 5, 'diagonal', 2)
46 |
47 |
48 | def test_conv_input_length():
49 | assert conv_utils.conv_input_length(None, 7, 'same', 1) is None
50 | assert conv_utils.conv_input_length(112, 7, 'same', 1) == 112
51 | assert conv_utils.conv_input_length(112, 7, 'same', 2) == 223
52 | assert conv_utils.conv_input_length(28, 5, 'valid', 1) == 32
53 | assert conv_utils.conv_input_length(14, 5, 'valid', 2) == 31
54 | assert conv_utils.conv_input_length(36, 5, 'full', 1) == 32
55 | assert conv_utils.conv_input_length(18, 5, 'full', 2) == 31
56 |
57 | with pytest.raises(AssertionError):
58 | conv_utils.conv_output_length(18, 5, 'diagonal', 2)
59 |
60 |
61 | def test_deconv_length():
62 | assert conv_utils.deconv_length(None, 1, 7, 'same') is None
63 | assert conv_utils.deconv_length(224, 1, 7, 'same') == 224
64 | assert conv_utils.deconv_length(224, 2, 7, 'same') == 448
65 | assert conv_utils.deconv_length(32, 1, 5, 'valid') == 36
66 | assert conv_utils.deconv_length(32, 2, 5, 'valid') == 67
67 | assert conv_utils.deconv_length(32, 1, 5, 'full') == 28
68 | assert conv_utils.deconv_length(32, 2, 5, 'full') == 59
69 |
70 |
71 | if __name__ == '__main__':
72 | pytest.main([__file__])
73 |
--------------------------------------------------------------------------------
/tests/integration_tests/test_datasets.py:
--------------------------------------------------------------------------------
1 | from __future__ import print_function
2 | import pytest
3 | import time
4 | import random
5 | from keras.datasets import cifar10
6 | from keras.datasets import cifar100
7 | from keras.datasets import reuters
8 | from keras.datasets import imdb
9 | from keras.datasets import mnist
10 | from keras.datasets import boston_housing
11 | from keras.datasets import fashion_mnist
12 |
13 |
14 | def test_cifar():
15 | # only run data download tests 20% of the time
16 | # to speed up frequent testing
17 | random.seed(time.time())
18 | if random.random() > 0.8:
19 | (x_train, y_train), (x_test, y_test) = cifar10.load_data()
20 | assert len(x_train) == len(y_train) == 50000
21 | assert len(x_test) == len(y_test) == 10000
22 | (x_train, y_train), (x_test, y_test) = cifar100.load_data('fine')
23 | assert len(x_train) == len(y_train) == 50000
24 | assert len(x_test) == len(y_test) == 10000
25 | (x_train, y_train), (x_test, y_test) = cifar100.load_data('coarse')
26 | assert len(x_train) == len(y_train) == 50000
27 | assert len(x_test) == len(y_test) == 10000
28 |
29 |
30 | def test_reuters():
31 | # only run data download tests 20% of the time
32 | # to speed up frequent testing
33 | random.seed(time.time())
34 | if random.random() > 0.8:
35 | (x_train, y_train), (x_test, y_test) = reuters.load_data()
36 | assert len(x_train) == len(y_train)
37 | assert len(x_test) == len(y_test)
38 | assert len(x_train) + len(x_test) == 11228
39 | (x_train, y_train), (x_test, y_test) = reuters.load_data(maxlen=10)
40 | assert len(x_train) == len(y_train)
41 | assert len(x_test) == len(y_test)
42 | word_index = reuters.get_word_index()
43 | assert isinstance(word_index, dict)
44 |
45 |
46 | def test_mnist():
47 | # only run data download tests 20% of the time
48 | # to speed up frequent testing
49 | random.seed(time.time())
50 | if random.random() > 0.8:
51 | (x_train, y_train), (x_test, y_test) = mnist.load_data()
52 | assert len(x_train) == len(y_train) == 60000
53 | assert len(x_test) == len(y_test) == 10000
54 |
55 |
56 | def test_imdb():
57 | # only run data download tests 20% of the time
58 | # to speed up frequent testing
59 | random.seed(time.time())
60 | if random.random() > 0.8:
61 | (x_train, y_train), (x_test, y_test) = imdb.load_data()
62 | (x_train, y_train), (x_test, y_test) = imdb.load_data(maxlen=40)
63 | assert len(x_train) == len(y_train)
64 | assert len(x_test) == len(y_test)
65 | word_index = imdb.get_word_index()
66 | assert isinstance(word_index, dict)
67 |
68 |
69 | def test_boston_housing():
70 | # only run data download tests 20% of the time
71 | # to speed up frequent testing
72 | random.seed(time.time())
73 | if random.random() > 0.8:
74 | (x_train, y_train), (x_test, y_test) = boston_housing.load_data()
75 | assert len(x_train) == len(y_train)
76 | assert len(x_test) == len(y_test)
77 |
78 |
79 | def test_fashion_mnist():
80 | # only run data download tests 20% of the time
81 | # to speed up frequent testing
82 | random.seed(time.time())
83 | if random.random() > 0.8:
84 | (x_train, y_train), (x_test, y_test) = fashion_mnist.load_data()
85 | assert len(x_train) == len(y_train) == 60000
86 | assert len(x_test) == len(y_test) == 10000
87 |
88 |
89 | if __name__ == '__main__':
90 | pytest.main([__file__])
91 |
--------------------------------------------------------------------------------
/examples/mnist_hierarchical_rnn.py:
--------------------------------------------------------------------------------
1 | """Example of using Hierarchical RNN (HRNN) to classify MNIST digits.
2 |
3 | HRNNs can learn across multiple levels
4 | of temporal hierarchy over a complex sequence.
5 | Usually, the first recurrent layer of an HRNN
6 | encodes a sentence (e.g. of word vectors)
7 | into a sentence vector.
8 | The second recurrent layer then encodes a sequence of
9 | such vectors (encoded by the first layer) into a document vector.
10 | This document vector is considered to preserve both
11 | the word-level and sentence-level structure of the context.
12 |
13 | # References
14 |
15 | - [A Hierarchical Neural Autoencoder for Paragraphs and Documents](https://arxiv.org/abs/1506.01057)
16 | Encodes paragraphs and documents with HRNN.
17 | Results have shown that HRNN outperforms standard
18 | RNNs and may play some role in more sophisticated generation tasks like
19 | summarization or question answering.
20 | - [Hierarchical recurrent neural network for skeleton based action recognition](http://ieeexplore.ieee.org/stamp/stamp.jsp?tp=&arnumber=7298714)
21 | Achieved state-of-the-art results on
22 | skeleton based action recognition with 3 levels
23 | of bidirectional HRNN combined with fully connected layers.
24 |
25 | In the below MNIST example the first LSTM layer first encodes every
26 | column of pixels of shape (28, 1) to a column vector of shape (128,).
27 | The second LSTM layer encodes then these 28 column vectors of shape (28, 128)
28 | to a image vector representing the whole image.
29 | A final Dense layer is added for prediction.
30 |
31 | After 5 epochs: train acc: 0.9858, val acc: 0.9864
32 | """
33 | from __future__ import print_function
34 |
35 | import keras
36 | from keras.datasets import mnist
37 | from keras.models import Model
38 | from keras.layers import Input, Dense, TimeDistributed
39 | from keras.layers import LSTM
40 |
41 | # Training parameters.
42 | batch_size = 32
43 | num_classes = 10
44 | epochs = 5
45 |
46 | # Embedding dimensions.
47 | row_hidden = 128
48 | col_hidden = 128
49 |
50 | # The data, split between train and test sets.
51 | (x_train, y_train), (x_test, y_test) = mnist.load_data()
52 |
53 | # Reshapes data to 4D for Hierarchical RNN.
54 | x_train = x_train.reshape(x_train.shape[0], 28, 28, 1)
55 | x_test = x_test.reshape(x_test.shape[0], 28, 28, 1)
56 | x_train = x_train.astype('float32')
57 | x_test = x_test.astype('float32')
58 | x_train /= 255
59 | x_test /= 255
60 | print('x_train shape:', x_train.shape)
61 | print(x_train.shape[0], 'train samples')
62 | print(x_test.shape[0], 'test samples')
63 |
64 | # Converts class vectors to binary class matrices.
65 | y_train = keras.utils.to_categorical(y_train, num_classes)
66 | y_test = keras.utils.to_categorical(y_test, num_classes)
67 |
68 | row, col, pixel = x_train.shape[1:]
69 |
70 | # 4D input.
71 | x = Input(shape=(row, col, pixel))
72 |
73 | # Encodes a row of pixels using TimeDistributed Wrapper.
74 | encoded_rows = TimeDistributed(LSTM(row_hidden))(x)
75 |
76 | # Encodes columns of encoded rows.
77 | encoded_columns = LSTM(col_hidden)(encoded_rows)
78 |
79 | # Final predictions and model.
80 | prediction = Dense(num_classes, activation='softmax')(encoded_columns)
81 | model = Model(x, prediction)
82 | model.compile(loss='categorical_crossentropy',
83 | optimizer='rmsprop',
84 | metrics=['accuracy'])
85 |
86 | # Training.
87 | model.fit(x_train, y_train,
88 | batch_size=batch_size,
89 | epochs=epochs,
90 | verbose=1,
91 | validation_data=(x_test, y_test))
92 |
93 | # Evaluation.
94 | scores = model.evaluate(x_test, y_test, verbose=0)
95 | print('Test loss:', scores[0])
96 | print('Test accuracy:', scores[1])
97 |
--------------------------------------------------------------------------------
/examples/antirectifier.py:
--------------------------------------------------------------------------------
1 | '''The example demonstrates how to write custom layers for Keras.
2 |
3 | We build a custom activation layer called 'Antirectifier',
4 | which modifies the shape of the tensor that passes through it.
5 | We need to specify two methods: `compute_output_shape` and `call`.
6 |
7 | Note that the same result can also be achieved via a Lambda layer.
8 |
9 | Because our custom layer is written with primitives from the Keras
10 | backend (`K`), our code can run both on TensorFlow and Theano.
11 | '''
12 |
13 | from __future__ import print_function
14 | import keras
15 | from keras.models import Sequential
16 | from keras import layers
17 | from keras.datasets import mnist
18 | from keras import backend as K
19 |
20 |
21 | class Antirectifier(layers.Layer):
22 | '''This is the combination of a sample-wise
23 | L2 normalization with the concatenation of the
24 | positive part of the input with the negative part
25 | of the input. The result is a tensor of samples that are
26 | twice as large as the input samples.
27 |
28 | It can be used in place of a ReLU.
29 |
30 | # Input shape
31 | 2D tensor of shape (samples, n)
32 |
33 | # Output shape
34 | 2D tensor of shape (samples, 2*n)
35 |
36 | # Theoretical justification
37 | When applying ReLU, assuming that the distribution
38 | of the previous output is approximately centered around 0.,
39 | you are discarding half of your input. This is inefficient.
40 |
41 | Antirectifier allows to return all-positive outputs like ReLU,
42 | without discarding any data.
43 |
44 | Tests on MNIST show that Antirectifier allows to train networks
45 | with twice less parameters yet with comparable
46 | classification accuracy as an equivalent ReLU-based network.
47 | '''
48 |
49 | def compute_output_shape(self, input_shape):
50 | shape = list(input_shape)
51 | assert len(shape) == 2 # only valid for 2D tensors
52 | shape[-1] *= 2
53 | return tuple(shape)
54 |
55 | def call(self, inputs):
56 | inputs -= K.mean(inputs, axis=1, keepdims=True)
57 | inputs = K.l2_normalize(inputs, axis=1)
58 | pos = K.relu(inputs)
59 | neg = K.relu(-inputs)
60 | return K.concatenate([pos, neg], axis=1)
61 |
62 | # global parameters
63 | batch_size = 128
64 | num_classes = 10
65 | epochs = 40
66 |
67 | # the data, split between train and test sets
68 | (x_train, y_train), (x_test, y_test) = mnist.load_data()
69 |
70 | x_train = x_train.reshape(60000, 784)
71 | x_test = x_test.reshape(10000, 784)
72 | x_train = x_train.astype('float32')
73 | x_test = x_test.astype('float32')
74 | x_train /= 255
75 | x_test /= 255
76 | print(x_train.shape[0], 'train samples')
77 | print(x_test.shape[0], 'test samples')
78 |
79 | # convert class vectors to binary class matrices
80 | y_train = keras.utils.to_categorical(y_train, num_classes)
81 | y_test = keras.utils.to_categorical(y_test, num_classes)
82 |
83 | # build the model
84 | model = Sequential()
85 | model.add(layers.Dense(256, input_shape=(784,)))
86 | model.add(Antirectifier())
87 | model.add(layers.Dropout(0.1))
88 | model.add(layers.Dense(256))
89 | model.add(Antirectifier())
90 | model.add(layers.Dropout(0.1))
91 | model.add(layers.Dense(num_classes))
92 | model.add(layers.Activation('softmax'))
93 |
94 | # compile the model
95 | model.compile(loss='categorical_crossentropy',
96 | optimizer='rmsprop',
97 | metrics=['accuracy'])
98 |
99 | # train the model
100 | model.fit(x_train, y_train,
101 | batch_size=batch_size,
102 | epochs=epochs,
103 | verbose=1,
104 | validation_data=(x_test, y_test))
105 |
106 | # next, compare with an equivalent network
107 | # with2x bigger Dense layers and ReLU
108 |
--------------------------------------------------------------------------------
/tests/keras/preprocessing/text_test.py:
--------------------------------------------------------------------------------
1 | # -*- coding: utf-8 -*-
2 |
3 | import numpy as np
4 | import pytest
5 |
6 | from keras.preprocessing.text import Tokenizer, one_hot, hashing_trick, text_to_word_sequence
7 |
8 |
9 | def test_one_hot():
10 | text = 'The cat sat on the mat.'
11 | encoded = one_hot(text, 5)
12 | assert len(encoded) == 6
13 | assert np.max(encoded) <= 4
14 | assert np.min(encoded) >= 0
15 |
16 |
17 | def test_hashing_trick_hash():
18 | text = 'The cat sat on the mat.'
19 | encoded = hashing_trick(text, 5)
20 | assert len(encoded) == 6
21 | assert np.max(encoded) <= 4
22 | assert np.min(encoded) >= 1
23 |
24 |
25 | def test_hashing_trick_md5():
26 | text = 'The cat sat on the mat.'
27 | encoded = hashing_trick(text, 5, hash_function='md5')
28 | assert len(encoded) == 6
29 | assert np.max(encoded) <= 4
30 | assert np.min(encoded) >= 1
31 |
32 |
33 | def test_tokenizer():
34 | texts = ['The cat sat on the mat.',
35 | 'The dog sat on the log.',
36 | 'Dogs and cats living together.']
37 | tokenizer = Tokenizer(num_words=10)
38 | tokenizer.fit_on_texts(texts)
39 |
40 | sequences = []
41 | for seq in tokenizer.texts_to_sequences_generator(texts):
42 | sequences.append(seq)
43 | assert np.max(np.max(sequences)) < 10
44 | assert np.min(np.min(sequences)) == 1
45 |
46 | tokenizer.fit_on_sequences(sequences)
47 |
48 | for mode in ['binary', 'count', 'tfidf', 'freq']:
49 | matrix = tokenizer.texts_to_matrix(texts, mode)
50 |
51 |
52 | def test_sequential_fit():
53 | texts = ['The cat sat on the mat.',
54 | 'The dog sat on the log.',
55 | 'Dogs and cats living together.']
56 | word_sequences = [
57 | ['The', 'cat', 'is', 'sitting'],
58 | ['The', 'dog', 'is', 'standing']
59 | ]
60 |
61 | tokenizer = Tokenizer()
62 | tokenizer.fit_on_texts(texts)
63 | tokenizer.fit_on_texts(word_sequences)
64 |
65 | assert tokenizer.document_count == 5
66 |
67 | tokenizer.texts_to_matrix(texts)
68 | tokenizer.texts_to_matrix(word_sequences)
69 |
70 |
71 | def test_text_to_word_sequence():
72 | text = 'hello! ? world!'
73 | assert text_to_word_sequence(text) == ['hello', 'world']
74 |
75 |
76 | def test_text_to_word_sequence_multichar_split():
77 | text = 'hello!stop?world!'
78 | assert text_to_word_sequence(text, split='stop') == ['hello', 'world']
79 |
80 |
81 | def test_text_to_word_sequence_unicode():
82 | text = u'ali! veli? kırk dokuz elli'
83 | assert text_to_word_sequence(text) == [u'ali', u'veli', u'kırk', u'dokuz', u'elli']
84 |
85 |
86 | def test_text_to_word_sequence_unicode_multichar_split():
87 | text = u'ali!stopveli?stopkırkstopdokuzstopelli'
88 | assert text_to_word_sequence(text, split='stop') == [u'ali', u'veli', u'kırk', u'dokuz', u'elli']
89 |
90 |
91 | def test_tokenizer_unicode():
92 | texts = [u'ali veli kırk dokuz elli', u'ali veli kırk dokuz elli veli kırk dokuz']
93 | tokenizer = Tokenizer(num_words=5)
94 | tokenizer.fit_on_texts(texts)
95 |
96 | assert len(tokenizer.word_counts) == 5
97 |
98 |
99 | def test_tokenizer_oov_flag():
100 | """
101 | Test of Out of Vocabulary (OOV) flag in Tokenizer
102 | """
103 | x_train = ['This text has only known words']
104 | x_test = ['This text has some unknown words'] # 2 OOVs: some, unknown
105 |
106 | # Default, without OOV flag
107 | tokenizer = Tokenizer()
108 | tokenizer.fit_on_texts(x_train)
109 | x_test_seq = tokenizer.texts_to_sequences(x_test)
110 | assert len(x_test_seq[0]) == 4 # discards 2 OOVs
111 |
112 | # With OOV feature
113 | tokenizer = Tokenizer(oov_token='')
114 | tokenizer.fit_on_texts(x_train)
115 | x_test_seq = tokenizer.texts_to_sequences(x_test)
116 | assert len(x_test_seq[0]) == 6 # OOVs marked in place
117 |
118 |
119 | if __name__ == '__main__':
120 | pytest.main([__file__])
121 |
--------------------------------------------------------------------------------
/examples/lstm_text_generation.py:
--------------------------------------------------------------------------------
1 | '''Example script to generate text from Nietzsche's writings.
2 |
3 | At least 20 epochs are required before the generated text
4 | starts sounding coherent.
5 |
6 | It is recommended to run this script on GPU, as recurrent
7 | networks are quite computationally intensive.
8 |
9 | If you try this script on new data, make sure your corpus
10 | has at least ~100k characters. ~1M is better.
11 | '''
12 |
13 | from __future__ import print_function
14 | from keras.callbacks import LambdaCallback
15 | from keras.models import Sequential
16 | from keras.layers import Dense, Activation
17 | from keras.layers import LSTM
18 | from keras.optimizers import RMSprop
19 | from keras.utils.data_utils import get_file
20 | import numpy as np
21 | import random
22 | import sys
23 | import io
24 |
25 | path = get_file('nietzsche.txt', origin='https://s3.amazonaws.com/text-datasets/nietzsche.txt')
26 | with io.open(path, encoding='utf-8') as f:
27 | text = f.read().lower()
28 | print('corpus length:', len(text))
29 |
30 | chars = sorted(list(set(text)))
31 | print('total chars:', len(chars))
32 | char_indices = dict((c, i) for i, c in enumerate(chars))
33 | indices_char = dict((i, c) for i, c in enumerate(chars))
34 |
35 | # cut the text in semi-redundant sequences of maxlen characters
36 | maxlen = 40
37 | step = 3
38 | sentences = []
39 | next_chars = []
40 | for i in range(0, len(text) - maxlen, step):
41 | sentences.append(text[i: i + maxlen])
42 | next_chars.append(text[i + maxlen])
43 | print('nb sequences:', len(sentences))
44 |
45 | print('Vectorization...')
46 | x = np.zeros((len(sentences), maxlen, len(chars)), dtype=np.bool)
47 | y = np.zeros((len(sentences), len(chars)), dtype=np.bool)
48 | for i, sentence in enumerate(sentences):
49 | for t, char in enumerate(sentence):
50 | x[i, t, char_indices[char]] = 1
51 | y[i, char_indices[next_chars[i]]] = 1
52 |
53 |
54 | # build the model: a single LSTM
55 | print('Build model...')
56 | model = Sequential()
57 | model.add(LSTM(128, input_shape=(maxlen, len(chars))))
58 | model.add(Dense(len(chars)))
59 | model.add(Activation('softmax'))
60 |
61 | optimizer = RMSprop(lr=0.01)
62 | model.compile(loss='categorical_crossentropy', optimizer=optimizer)
63 |
64 |
65 | def sample(preds, temperature=1.0):
66 | # helper function to sample an index from a probability array
67 | preds = np.asarray(preds).astype('float64')
68 | preds = np.log(preds) / temperature
69 | exp_preds = np.exp(preds)
70 | preds = exp_preds / np.sum(exp_preds)
71 | probas = np.random.multinomial(1, preds, 1)
72 | return np.argmax(probas)
73 |
74 |
75 | def on_epoch_end(epoch, logs):
76 | # Function invoked at end of each epoch. Prints generated text.
77 | print()
78 | print('----- Generating text after Epoch: %d' % epoch)
79 |
80 | start_index = random.randint(0, len(text) - maxlen - 1)
81 | for diversity in [0.2, 0.5, 1.0, 1.2]:
82 | print('----- diversity:', diversity)
83 |
84 | generated = ''
85 | sentence = text[start_index: start_index + maxlen]
86 | generated += sentence
87 | print('----- Generating with seed: "' + sentence + '"')
88 | sys.stdout.write(generated)
89 |
90 | for i in range(400):
91 | x_pred = np.zeros((1, maxlen, len(chars)))
92 | for t, char in enumerate(sentence):
93 | x_pred[0, t, char_indices[char]] = 1.
94 |
95 | preds = model.predict(x_pred, verbose=0)[0]
96 | next_index = sample(preds, diversity)
97 | next_char = indices_char[next_index]
98 |
99 | generated += next_char
100 | sentence = sentence[1:] + next_char
101 |
102 | sys.stdout.write(next_char)
103 | sys.stdout.flush()
104 | print()
105 |
106 | print_callback = LambdaCallback(on_epoch_end=on_epoch_end)
107 |
108 | model.fit(x, y,
109 | batch_size=128,
110 | epochs=60,
111 | callbacks=[print_callback])
112 |
--------------------------------------------------------------------------------
/tests/test_dynamic_trainability.py:
--------------------------------------------------------------------------------
1 | from __future__ import absolute_import
2 | from __future__ import print_function
3 | import pytest
4 |
5 | from keras.utils.test_utils import keras_test
6 | from keras.models import Model, Sequential
7 | from keras.layers import Dense, Input
8 |
9 |
10 | @keras_test
11 | def test_layer_trainability_switch():
12 | # with constructor argument, in Sequential
13 | model = Sequential()
14 | model.add(Dense(2, trainable=False, input_dim=1))
15 | assert model.trainable_weights == []
16 |
17 | # by setting the `trainable` argument, in Sequential
18 | model = Sequential()
19 | layer = Dense(2, input_dim=1)
20 | model.add(layer)
21 | assert model.trainable_weights == layer.trainable_weights
22 | layer.trainable = False
23 | assert model.trainable_weights == []
24 |
25 | # with constructor argument, in Model
26 | x = Input(shape=(1,))
27 | y = Dense(2, trainable=False)(x)
28 | model = Model(x, y)
29 | assert model.trainable_weights == []
30 |
31 | # by setting the `trainable` argument, in Model
32 | x = Input(shape=(1,))
33 | layer = Dense(2)
34 | y = layer(x)
35 | model = Model(x, y)
36 | assert model.trainable_weights == layer.trainable_weights
37 | layer.trainable = False
38 | assert model.trainable_weights == []
39 |
40 |
41 | @keras_test
42 | def test_model_trainability_switch():
43 | # a non-trainable model has no trainable weights
44 | x = Input(shape=(1,))
45 | y = Dense(2)(x)
46 | model = Model(x, y)
47 | model.trainable = False
48 | assert model.trainable_weights == []
49 |
50 | # same for Sequential
51 | model = Sequential()
52 | model.add(Dense(2, input_dim=1))
53 | model.trainable = False
54 | assert model.trainable_weights == []
55 |
56 |
57 | @keras_test
58 | def test_nested_model_trainability():
59 | # a Sequential inside a Model
60 | inner_model = Sequential()
61 | inner_model.add(Dense(2, input_dim=1))
62 |
63 | x = Input(shape=(1,))
64 | y = inner_model(x)
65 | outer_model = Model(x, y)
66 | assert outer_model.trainable_weights == inner_model.trainable_weights
67 | inner_model.trainable = False
68 | assert outer_model.trainable_weights == []
69 | inner_model.trainable = True
70 | inner_model.layers[-1].trainable = False
71 | assert outer_model.trainable_weights == []
72 |
73 | # a Sequential inside a Sequential
74 | inner_model = Sequential()
75 | inner_model.add(Dense(2, input_dim=1))
76 | outer_model = Sequential()
77 | outer_model.add(inner_model)
78 | assert outer_model.trainable_weights == inner_model.trainable_weights
79 | inner_model.trainable = False
80 | assert outer_model.trainable_weights == []
81 | inner_model.trainable = True
82 | inner_model.layers[-1].trainable = False
83 | assert outer_model.trainable_weights == []
84 |
85 | # a Model inside a Model
86 | x = Input(shape=(1,))
87 | y = Dense(2)(x)
88 | inner_model = Model(x, y)
89 | x = Input(shape=(1,))
90 | y = inner_model(x)
91 | outer_model = Model(x, y)
92 | assert outer_model.trainable_weights == inner_model.trainable_weights
93 | inner_model.trainable = False
94 | assert outer_model.trainable_weights == []
95 | inner_model.trainable = True
96 | inner_model.layers[-1].trainable = False
97 | assert outer_model.trainable_weights == []
98 |
99 | # a Model inside a Sequential
100 | x = Input(shape=(1,))
101 | y = Dense(2)(x)
102 | inner_model = Model(x, y)
103 | outer_model = Sequential()
104 | outer_model.add(inner_model)
105 | assert outer_model.trainable_weights == inner_model.trainable_weights
106 | inner_model.trainable = False
107 | assert outer_model.trainable_weights == []
108 | inner_model.trainable = True
109 | inner_model.layers[-1].trainable = False
110 | assert outer_model.trainable_weights == []
111 |
112 |
113 | if __name__ == '__main__':
114 | pytest.main([__file__])
115 |
--------------------------------------------------------------------------------
/tests/integration_tests/test_vector_data_tasks.py:
--------------------------------------------------------------------------------
1 | from __future__ import print_function
2 | import pytest
3 |
4 | from keras.utils.test_utils import get_test_data, keras_test
5 | from keras.models import Sequential
6 | from keras import layers
7 | import keras
8 | from keras.utils.np_utils import to_categorical
9 |
10 | num_classes = 2
11 |
12 |
13 | @keras_test
14 | def test_vector_classification():
15 | '''
16 | Classify random float vectors into 2 classes with logistic regression
17 | using 2 layer neural network with ReLU hidden units.
18 | '''
19 | (x_train, y_train), (x_test, y_test) = get_test_data(num_train=500,
20 | num_test=200,
21 | input_shape=(20,),
22 | classification=True,
23 | num_classes=num_classes)
24 | y_train = to_categorical(y_train)
25 | y_test = to_categorical(y_test)
26 |
27 | # Test with Sequential API
28 | model = Sequential([
29 | layers.Dense(16, input_shape=(x_train.shape[-1],), activation='relu'),
30 | layers.Dense(8),
31 | layers.Activation('relu'),
32 | layers.Dense(num_classes, activation='softmax')
33 | ])
34 | model.compile(loss='categorical_crossentropy',
35 | optimizer='rmsprop',
36 | metrics=['accuracy'])
37 | model.summary()
38 | history = model.fit(x_train, y_train, epochs=15, batch_size=16,
39 | validation_data=(x_test, y_test),
40 | verbose=0)
41 | assert(history.history['val_acc'][-1] > 0.8)
42 | config = model.get_config()
43 | model = Sequential.from_config(config)
44 |
45 |
46 | @keras_test
47 | def test_vector_classification_functional():
48 | (x_train, y_train), (x_test, y_test) = get_test_data(num_train=500,
49 | num_test=200,
50 | input_shape=(20,),
51 | classification=True,
52 | num_classes=num_classes)
53 | # Test with functional API
54 | inputs = layers.Input(shape=(x_train.shape[-1],))
55 | x = layers.Dense(16, activation=keras.activations.relu)(inputs)
56 | x = layers.Dense(8)(x)
57 | x = layers.Activation('relu')(x)
58 | outputs = layers.Dense(num_classes, activation='softmax')(x)
59 | model = keras.models.Model(inputs, outputs)
60 | model.compile(loss=keras.losses.sparse_categorical_crossentropy,
61 | optimizer=keras.optimizers.RMSprop(),
62 | metrics=['acc'])
63 | history = model.fit(x_train, y_train, epochs=15, batch_size=16,
64 | validation_data=(x_test, y_test),
65 | verbose=0)
66 | assert(history.history['val_acc'][-1] > 0.8)
67 |
68 |
69 | @keras_test
70 | def test_vector_regression():
71 | '''
72 | Perform float data prediction (regression) using 2 layer MLP
73 | with tanh and sigmoid activations.
74 | '''
75 | (x_train, y_train), (x_test, y_test) = get_test_data(num_train=500,
76 | num_test=200,
77 | input_shape=(20,),
78 | output_shape=(num_classes,),
79 | classification=False)
80 |
81 | model = Sequential([
82 | layers.Dense(16, input_shape=(x_train.shape[-1],), activation='tanh'),
83 | layers.Dense(num_classes)
84 | ])
85 |
86 | model.compile(loss='hinge', optimizer='adagrad')
87 | history = model.fit(x_train, y_train, epochs=20, batch_size=16,
88 | validation_data=(x_test, y_test), verbose=0)
89 | assert (history.history['val_loss'][-1] < 0.9)
90 |
91 |
92 | if __name__ == '__main__':
93 | pytest.main([__file__])
94 |
--------------------------------------------------------------------------------
/tests/keras/regularizers_test.py:
--------------------------------------------------------------------------------
1 | import pytest
2 |
3 | from keras.models import Sequential, Model
4 | from keras.layers import Dense, Input, Average
5 | from keras.utils import np_utils
6 | from keras.utils import test_utils
7 | from keras import regularizers
8 | from keras import backend as K
9 |
10 | data_dim = 5
11 | num_classes = 2
12 | batch_size = 10
13 |
14 |
15 | def get_data():
16 | (x_train, y_train), _ = test_utils.get_test_data(
17 | num_train=batch_size,
18 | num_test=batch_size,
19 | input_shape=(data_dim,),
20 | classification=True,
21 | num_classes=num_classes)
22 | y_train = np_utils.to_categorical(y_train, num_classes)
23 |
24 | return x_train, y_train
25 |
26 |
27 | def create_model(kernel_regularizer=None, activity_regularizer=None):
28 | model = Sequential()
29 | model.add(Dense(num_classes,
30 | kernel_regularizer=kernel_regularizer,
31 | activity_regularizer=activity_regularizer,
32 | input_shape=(data_dim,)))
33 | return model
34 |
35 |
36 | def create_multi_input_model_from(layer1, layer2):
37 | input_1 = Input(shape=(data_dim,))
38 | input_2 = Input(shape=(data_dim,))
39 | out1 = layer1(input_1)
40 | out2 = layer2(input_2)
41 | out = Average()([out1, out2])
42 | model = Model([input_1, input_2], out)
43 | model.add_loss(K.mean(out2))
44 | model.add_loss(1)
45 | model.add_loss(1)
46 | return model
47 |
48 |
49 | def test_kernel_regularization():
50 | x_train, y_train = get_data()
51 | for reg in [regularizers.l1(),
52 | regularizers.l2(),
53 | regularizers.l1_l2()]:
54 | model = create_model(kernel_regularizer=reg)
55 | model.compile(loss='categorical_crossentropy', optimizer='sgd')
56 | assert len(model.losses) == 1
57 | model.train_on_batch(x_train, y_train)
58 |
59 |
60 | def test_activity_regularization():
61 | x_train, y_train = get_data()
62 | for reg in [regularizers.l1(), regularizers.l2()]:
63 | model = create_model(activity_regularizer=reg)
64 | model.compile(loss='categorical_crossentropy', optimizer='sgd')
65 | assert len(model.losses) == 1
66 | model.train_on_batch(x_train, y_train)
67 |
68 |
69 | def test_regularization_shared_layer():
70 | dense_layer = Dense(num_classes,
71 | kernel_regularizer=regularizers.l1(),
72 | activity_regularizer=regularizers.l1())
73 |
74 | model = create_multi_input_model_from(dense_layer, dense_layer)
75 | model.compile(loss='categorical_crossentropy', optimizer='sgd')
76 | assert len(model.losses) == 6
77 |
78 |
79 | def test_regularization_shared_model():
80 | dense_layer = Dense(num_classes,
81 | kernel_regularizer=regularizers.l1(),
82 | activity_regularizer=regularizers.l1())
83 |
84 | input_tensor = Input(shape=(data_dim,))
85 | dummy_model = Model(input_tensor, dense_layer(input_tensor))
86 |
87 | model = create_multi_input_model_from(dummy_model, dummy_model)
88 | model.compile(loss='categorical_crossentropy', optimizer='sgd')
89 | assert len(model.losses) == 6
90 |
91 |
92 | def test_regularization_shared_layer_in_different_models():
93 | shared_dense = Dense(num_classes,
94 | kernel_regularizer=regularizers.l1(),
95 | activity_regularizer=regularizers.l1())
96 | models = []
97 | for _ in range(2):
98 | input_tensor = Input(shape=(data_dim,))
99 | unshared_dense = Dense(num_classes, kernel_regularizer=regularizers.l1())
100 | out = unshared_dense(shared_dense(input_tensor))
101 | models.append(Model(input_tensor, out))
102 |
103 | model = create_multi_input_model_from(*models)
104 | model.compile(loss='categorical_crossentropy', optimizer='sgd')
105 | assert len(model.losses) == 8
106 |
107 |
108 | if __name__ == '__main__':
109 | pytest.main([__file__])
110 |
--------------------------------------------------------------------------------
/examples/mnist_dataset_api.py:
--------------------------------------------------------------------------------
1 | '''MNIST classification with TensorFlow's Dataset API.
2 |
3 | Introduced in TensorFlow 1.3, the Dataset API is now the
4 | standard method for loading data into TensorFlow models.
5 | A Dataset is a sequence of elements, which are themselves
6 | composed of tf.Tensor components. For more details, see:
7 | https://www.tensorflow.org/programmers_guide/datasets
8 |
9 | To use this with Keras, we make a dataset out of elements
10 | of the form (input batch, output batch). From there, we
11 | create a one-shot iterator and a graph node corresponding
12 | to its get_next() method. Its components are then provided
13 | to the network's Input layer and the Model.compile() method,
14 | respectively.
15 |
16 | This example is intended to closely follow the
17 | mnist_tfrecord.py example.
18 | '''
19 | import numpy as np
20 | import os
21 | import tempfile
22 |
23 | import keras
24 | from keras import backend as K
25 | from keras import layers
26 | from keras.datasets import mnist
27 |
28 | import tensorflow as tf
29 |
30 |
31 | if K.backend() != 'tensorflow':
32 | raise RuntimeError('This example can only run with the TensorFlow backend,'
33 | ' because it requires the Datset API, which is not'
34 | ' supported on other platforms.')
35 |
36 |
37 | def cnn_layers(inputs):
38 | x = layers.Conv2D(32, (3, 3),
39 | activation='relu', padding='valid')(inputs)
40 | x = layers.MaxPooling2D(pool_size=(2, 2))(x)
41 | x = layers.Conv2D(64, (3, 3), activation='relu')(x)
42 | x = layers.MaxPooling2D(pool_size=(2, 2))(x)
43 | x = layers.Flatten()(x)
44 | x = layers.Dense(512, activation='relu')(x)
45 | x = layers.Dropout(0.5)(x)
46 | predictions = layers.Dense(num_classes,
47 | activation='softmax',
48 | name='x_train_out')(x)
49 | return predictions
50 |
51 |
52 | batch_size = 128
53 | buffer_size = 10000
54 | steps_per_epoch = int(np.ceil(60000 / float(batch_size))) # = 469
55 | epochs = 5
56 | num_classes = 10
57 |
58 | (x_train, y_train), (x_test, y_test) = mnist.load_data()
59 | x_train = x_train.astype(np.float32) / 255
60 | x_train = np.expand_dims(x_train, -1)
61 | y_train = tf.one_hot(y_train, num_classes)
62 |
63 | # Create the dataset and its associated one-shot iterator.
64 | dataset = tf.data.Dataset.from_tensor_slices((x_train, y_train))
65 | dataset = dataset.repeat()
66 | dataset = dataset.shuffle(buffer_size)
67 | dataset = dataset.batch(batch_size)
68 | iterator = dataset.make_one_shot_iterator()
69 |
70 | # Model creation using tensors from the get_next() graph node.
71 | inputs, targets = iterator.get_next()
72 | model_input = layers.Input(tensor=inputs)
73 | model_output = cnn_layers(model_input)
74 | train_model = keras.models.Model(inputs=model_input, outputs=model_output)
75 |
76 | train_model.compile(optimizer=keras.optimizers.RMSprop(lr=2e-3, decay=1e-5),
77 | loss='categorical_crossentropy',
78 | metrics=['accuracy'],
79 | target_tensors=[targets])
80 | train_model.summary()
81 |
82 | train_model.fit(epochs=epochs,
83 | steps_per_epoch=steps_per_epoch)
84 |
85 | # Save the model weights.
86 | weight_path = os.path.join(tempfile.gettempdir(), 'saved_wt.h5')
87 | train_model.save_weights(weight_path)
88 |
89 | # Clean up the TF session.
90 | K.clear_session()
91 |
92 | # Second session to test loading trained model without tensors.
93 | x_test = x_test.astype(np.float32)
94 | x_test = np.expand_dims(x_test, -1)
95 |
96 | x_test_inp = layers.Input(shape=x_test.shape[1:])
97 | test_out = cnn_layers(x_test_inp)
98 | test_model = keras.models.Model(inputs=x_test_inp, outputs=test_out)
99 |
100 | test_model.load_weights(weight_path)
101 | test_model.compile(optimizer='rmsprop',
102 | loss='sparse_categorical_crossentropy',
103 | metrics=['accuracy'])
104 | test_model.summary()
105 |
106 | loss, acc = test_model.evaluate(x_test, y_test, num_classes)
107 | print('\nTest accuracy: {0}'.format(acc))
108 |
--------------------------------------------------------------------------------
/examples/mnist_sklearn_wrapper.py:
--------------------------------------------------------------------------------
1 | '''Example of how to use sklearn wrapper
2 |
3 | Builds simple CNN models on MNIST and uses sklearn's GridSearchCV to find best model
4 | '''
5 |
6 | from __future__ import print_function
7 |
8 | import keras
9 | from keras.datasets import mnist
10 | from keras.models import Sequential
11 | from keras.layers import Dense, Dropout, Activation, Flatten
12 | from keras.layers import Conv2D, MaxPooling2D
13 | from keras.wrappers.scikit_learn import KerasClassifier
14 | from keras import backend as K
15 | from sklearn.grid_search import GridSearchCV
16 |
17 |
18 | num_classes = 10
19 |
20 | # input image dimensions
21 | img_rows, img_cols = 28, 28
22 |
23 | # load training data and do basic data normalization
24 | (x_train, y_train), (x_test, y_test) = mnist.load_data()
25 |
26 | if K.image_data_format() == 'channels_first':
27 | x_train = x_train.reshape(x_train.shape[0], 1, img_rows, img_cols)
28 | x_test = x_test.reshape(x_test.shape[0], 1, img_rows, img_cols)
29 | input_shape = (1, img_rows, img_cols)
30 | else:
31 | x_train = x_train.reshape(x_train.shape[0], img_rows, img_cols, 1)
32 | x_test = x_test.reshape(x_test.shape[0], img_rows, img_cols, 1)
33 | input_shape = (img_rows, img_cols, 1)
34 |
35 | x_train = x_train.astype('float32')
36 | x_test = x_test.astype('float32')
37 | x_train /= 255
38 | x_test /= 255
39 |
40 | # convert class vectors to binary class matrices
41 | y_train = keras.utils.to_categorical(y_train, num_classes)
42 | y_test = keras.utils.to_categorical(y_test, num_classes)
43 |
44 |
45 | def make_model(dense_layer_sizes, filters, kernel_size, pool_size):
46 | '''Creates model comprised of 2 convolutional layers followed by dense layers
47 |
48 | dense_layer_sizes: List of layer sizes.
49 | This list has one number for each layer
50 | filters: Number of convolutional filters in each convolutional layer
51 | kernel_size: Convolutional kernel size
52 | pool_size: Size of pooling area for max pooling
53 | '''
54 |
55 | model = Sequential()
56 | model.add(Conv2D(filters, kernel_size,
57 | padding='valid',
58 | input_shape=input_shape))
59 | model.add(Activation('relu'))
60 | model.add(Conv2D(filters, kernel_size))
61 | model.add(Activation('relu'))
62 | model.add(MaxPooling2D(pool_size=pool_size))
63 | model.add(Dropout(0.25))
64 |
65 | model.add(Flatten())
66 | for layer_size in dense_layer_sizes:
67 | model.add(Dense(layer_size))
68 | model.add(Activation('relu'))
69 | model.add(Dropout(0.5))
70 | model.add(Dense(num_classes))
71 | model.add(Activation('softmax'))
72 |
73 | model.compile(loss='categorical_crossentropy',
74 | optimizer='adadelta',
75 | metrics=['accuracy'])
76 |
77 | return model
78 |
79 | dense_size_candidates = [[32], [64], [32, 32], [64, 64]]
80 | my_classifier = KerasClassifier(make_model, batch_size=32)
81 | validator = GridSearchCV(my_classifier,
82 | param_grid={'dense_layer_sizes': dense_size_candidates,
83 | # epochs is avail for tuning even when not
84 | # an argument to model building function
85 | 'epochs': [3, 6],
86 | 'filters': [8],
87 | 'kernel_size': [3],
88 | 'pool_size': [2]},
89 | scoring='neg_log_loss',
90 | n_jobs=1)
91 | validator.fit(x_train, y_train)
92 |
93 | print('The parameters of the best model are: ')
94 | print(validator.best_params_)
95 |
96 | # validator.best_estimator_ returns sklearn-wrapped version of best model.
97 | # validator.best_estimator_.model returns the (unwrapped) keras model
98 | best_model = validator.best_estimator_.model
99 | metric_names = best_model.metrics_names
100 | metric_values = best_model.evaluate(x_test, y_test)
101 | for metric, value in zip(metric_names, metric_values):
102 | print(metric, ': ', value)
103 |
--------------------------------------------------------------------------------
/examples/mnist_transfer_cnn.py:
--------------------------------------------------------------------------------
1 | '''Transfer learning toy example.
2 |
3 | 1 - Train a simple convnet on the MNIST dataset the first 5 digits [0..4].
4 | 2 - Freeze convolutional layers and fine-tune dense layers
5 | for the classification of digits [5..9].
6 |
7 | Get to 99.8% test accuracy after 5 epochs
8 | for the first five digits classifier
9 | and 99.2% for the last five digits after transfer + fine-tuning.
10 | '''
11 |
12 | from __future__ import print_function
13 |
14 | import datetime
15 | import keras
16 | from keras.datasets import mnist
17 | from keras.models import Sequential
18 | from keras.layers import Dense, Dropout, Activation, Flatten
19 | from keras.layers import Conv2D, MaxPooling2D
20 | from keras import backend as K
21 |
22 | now = datetime.datetime.now
23 |
24 | batch_size = 128
25 | num_classes = 5
26 | epochs = 5
27 |
28 | # input image dimensions
29 | img_rows, img_cols = 28, 28
30 | # number of convolutional filters to use
31 | filters = 32
32 | # size of pooling area for max pooling
33 | pool_size = 2
34 | # convolution kernel size
35 | kernel_size = 3
36 |
37 | if K.image_data_format() == 'channels_first':
38 | input_shape = (1, img_rows, img_cols)
39 | else:
40 | input_shape = (img_rows, img_cols, 1)
41 |
42 |
43 | def train_model(model, train, test, num_classes):
44 | x_train = train[0].reshape((train[0].shape[0],) + input_shape)
45 | x_test = test[0].reshape((test[0].shape[0],) + input_shape)
46 | x_train = x_train.astype('float32')
47 | x_test = x_test.astype('float32')
48 | x_train /= 255
49 | x_test /= 255
50 | print('x_train shape:', x_train.shape)
51 | print(x_train.shape[0], 'train samples')
52 | print(x_test.shape[0], 'test samples')
53 |
54 | # convert class vectors to binary class matrices
55 | y_train = keras.utils.to_categorical(train[1], num_classes)
56 | y_test = keras.utils.to_categorical(test[1], num_classes)
57 |
58 | model.compile(loss='categorical_crossentropy',
59 | optimizer='adadelta',
60 | metrics=['accuracy'])
61 |
62 | t = now()
63 | model.fit(x_train, y_train,
64 | batch_size=batch_size,
65 | epochs=epochs,
66 | verbose=1,
67 | validation_data=(x_test, y_test))
68 | print('Training time: %s' % (now() - t))
69 | score = model.evaluate(x_test, y_test, verbose=0)
70 | print('Test score:', score[0])
71 | print('Test accuracy:', score[1])
72 |
73 |
74 | # the data, split between train and test sets
75 | (x_train, y_train), (x_test, y_test) = mnist.load_data()
76 |
77 | # create two datasets one with digits below 5 and one with 5 and above
78 | x_train_lt5 = x_train[y_train < 5]
79 | y_train_lt5 = y_train[y_train < 5]
80 | x_test_lt5 = x_test[y_test < 5]
81 | y_test_lt5 = y_test[y_test < 5]
82 |
83 | x_train_gte5 = x_train[y_train >= 5]
84 | y_train_gte5 = y_train[y_train >= 5] - 5
85 | x_test_gte5 = x_test[y_test >= 5]
86 | y_test_gte5 = y_test[y_test >= 5] - 5
87 |
88 | # define two groups of layers: feature (convolutions) and classification (dense)
89 | feature_layers = [
90 | Conv2D(filters, kernel_size,
91 | padding='valid',
92 | input_shape=input_shape),
93 | Activation('relu'),
94 | Conv2D(filters, kernel_size),
95 | Activation('relu'),
96 | MaxPooling2D(pool_size=pool_size),
97 | Dropout(0.25),
98 | Flatten(),
99 | ]
100 |
101 | classification_layers = [
102 | Dense(128),
103 | Activation('relu'),
104 | Dropout(0.5),
105 | Dense(num_classes),
106 | Activation('softmax')
107 | ]
108 |
109 | # create complete model
110 | model = Sequential(feature_layers + classification_layers)
111 |
112 | # train model for 5-digit classification [0..4]
113 | train_model(model,
114 | (x_train_lt5, y_train_lt5),
115 | (x_test_lt5, y_test_lt5), num_classes)
116 |
117 | # freeze feature layers and rebuild model
118 | for l in feature_layers:
119 | l.trainable = False
120 |
121 | # transfer: train dense layers for new classification task [5..9]
122 | train_model(model,
123 | (x_train_gte5, y_train_gte5),
124 | (x_test_gte5, y_test_gte5), num_classes)
125 |
--------------------------------------------------------------------------------
/keras/datasets/reuters.py:
--------------------------------------------------------------------------------
1 | # -*- coding: utf-8 -*-
2 | """Reuters topic classification dataset.
3 | """
4 | from __future__ import absolute_import
5 | from __future__ import division
6 | from __future__ import print_function
7 |
8 | from ..utils.data_utils import get_file
9 | from ..preprocessing.sequence import _remove_long_seq
10 | import numpy as np
11 | import json
12 | import warnings
13 |
14 |
15 | def load_data(path='reuters.npz', num_words=None, skip_top=0,
16 | maxlen=None, test_split=0.2, seed=113,
17 | start_char=1, oov_char=2, index_from=3, **kwargs):
18 | """Loads the Reuters newswire classification dataset.
19 |
20 | # Arguments
21 | path: where to cache the data (relative to `~/.keras/dataset`).
22 | num_words: max number of words to include. Words are ranked
23 | by how often they occur (in the training set) and only
24 | the most frequent words are kept
25 | skip_top: skip the top N most frequently occurring words
26 | (which may not be informative).
27 | maxlen: truncate sequences after this length.
28 | test_split: Fraction of the dataset to be used as test data.
29 | seed: random seed for sample shuffling.
30 | start_char: The start of a sequence will be marked with this character.
31 | Set to 1 because 0 is usually the padding character.
32 | oov_char: words that were cut out because of the `num_words`
33 | or `skip_top` limit will be replaced with this character.
34 | index_from: index actual words with this index and higher.
35 |
36 | # Returns
37 | Tuple of Numpy arrays: `(x_train, y_train), (x_test, y_test)`.
38 |
39 | Note that the 'out of vocabulary' character is only used for
40 | words that were present in the training set but are not included
41 | because they're not making the `num_words` cut here.
42 | Words that were not seen in the training set but are in the test set
43 | have simply been skipped.
44 | """
45 | # Legacy support
46 | if 'nb_words' in kwargs:
47 | warnings.warn('The `nb_words` argument in `load_data` '
48 | 'has been renamed `num_words`.')
49 | num_words = kwargs.pop('nb_words')
50 | if kwargs:
51 | raise TypeError('Unrecognized keyword arguments: ' + str(kwargs))
52 |
53 | path = get_file(path,
54 | origin='https://s3.amazonaws.com/text-datasets/reuters.npz',
55 | file_hash='87aedbeb0cb229e378797a632c1997b6')
56 | with np.load(path) as f:
57 | xs, labels = f['x'], f['y']
58 |
59 | np.random.seed(seed)
60 | indices = np.arange(len(xs))
61 | np.random.shuffle(indices)
62 | xs = xs[indices]
63 | labels = labels[indices]
64 |
65 | if start_char is not None:
66 | xs = [[start_char] + [w + index_from for w in x] for x in xs]
67 | elif index_from:
68 | xs = [[w + index_from for w in x] for x in xs]
69 |
70 | if maxlen:
71 | xs, labels = _remove_long_seq(maxlen, xs, labels)
72 |
73 | if not num_words:
74 | num_words = max([max(x) for x in xs])
75 |
76 | # by convention, use 2 as OOV word
77 | # reserve 'index_from' (=3 by default) characters:
78 | # 0 (padding), 1 (start), 2 (OOV)
79 | if oov_char is not None:
80 | xs = [[w if skip_top <= w < num_words else oov_char for w in x] for x in xs]
81 | else:
82 | xs = [[w for w in x if skip_top <= w < num_words] for x in xs]
83 |
84 | idx = int(len(xs) * (1 - test_split))
85 | x_train, y_train = np.array(xs[:idx]), np.array(labels[:idx])
86 | x_test, y_test = np.array(xs[idx:]), np.array(labels[idx:])
87 |
88 | return (x_train, y_train), (x_test, y_test)
89 |
90 |
91 | def get_word_index(path='reuters_word_index.json'):
92 | """Retrieves the dictionary mapping word indices back to words.
93 |
94 | # Arguments
95 | path: where to cache the data (relative to `~/.keras/dataset`).
96 |
97 | # Returns
98 | The word index dictionary.
99 | """
100 | path = get_file(path,
101 | origin='https://s3.amazonaws.com/text-datasets/reuters_word_index.json',
102 | file_hash='4d44cc38712099c9e383dc6e5f11a921')
103 | f = open(path)
104 | data = json.load(f)
105 | f.close()
106 | return data
107 |
--------------------------------------------------------------------------------
/keras/backend/__init__.py:
--------------------------------------------------------------------------------
1 | from __future__ import absolute_import
2 | from __future__ import print_function
3 | import os
4 | import json
5 | import sys
6 | import importlib
7 | from .common import epsilon
8 | from .common import floatx
9 | from .common import set_epsilon
10 | from .common import set_floatx
11 | from .common import cast_to_floatx
12 | from .common import image_data_format
13 | from .common import set_image_data_format
14 |
15 | # Set Keras base dir path given KERAS_HOME env variable, if applicable.
16 | # Otherwise either ~/.keras or /tmp.
17 | if 'KERAS_HOME' in os.environ:
18 | _keras_dir = os.environ.get('KERAS_HOME')
19 | else:
20 | _keras_base_dir = os.path.expanduser('~')
21 | if not os.access(_keras_base_dir, os.W_OK):
22 | _keras_base_dir = '/tmp'
23 | _keras_dir = os.path.join(_keras_base_dir, '.keras')
24 |
25 | # Default backend: TensorFlow.
26 | _BACKEND = 'tensorflow'
27 |
28 | # Attempt to read Keras config file.
29 | _config_path = os.path.expanduser(os.path.join(_keras_dir, 'keras.json'))
30 | if os.path.exists(_config_path):
31 | try:
32 | with open(_config_path) as f:
33 | _config = json.load(f)
34 | except ValueError:
35 | _config = {}
36 | _floatx = _config.get('floatx', floatx())
37 | assert _floatx in {'float16', 'float32', 'float64'}
38 | _epsilon = _config.get('epsilon', epsilon())
39 | assert isinstance(_epsilon, float)
40 | _backend = _config.get('backend', _BACKEND)
41 | _image_data_format = _config.get('image_data_format',
42 | image_data_format())
43 | assert _image_data_format in {'channels_last', 'channels_first'}
44 |
45 | set_floatx(_floatx)
46 | set_epsilon(_epsilon)
47 | set_image_data_format(_image_data_format)
48 | _BACKEND = _backend
49 |
50 | # Save config file, if possible.
51 | if not os.path.exists(_keras_dir):
52 | try:
53 | os.makedirs(_keras_dir)
54 | except OSError:
55 | # Except permission denied and potential race conditions
56 | # in multi-threaded environments.
57 | pass
58 |
59 | if not os.path.exists(_config_path):
60 | _config = {
61 | 'floatx': floatx(),
62 | 'epsilon': epsilon(),
63 | 'backend': _BACKEND,
64 | 'image_data_format': image_data_format()
65 | }
66 | try:
67 | with open(_config_path, 'w') as f:
68 | f.write(json.dumps(_config, indent=4))
69 | except IOError:
70 | # Except permission denied.
71 | pass
72 |
73 | # Set backend based on KERAS_BACKEND flag, if applicable.
74 | if 'KERAS_BACKEND' in os.environ:
75 | _backend = os.environ['KERAS_BACKEND']
76 | _BACKEND = _backend
77 |
78 | # Import backend functions.
79 | if _BACKEND == 'cntk':
80 | sys.stderr.write('Using CNTK backend\n')
81 | from .cntk_backend import *
82 | elif _BACKEND == 'theano':
83 | sys.stderr.write('Using Theano backend.\n')
84 | from .theano_backend import *
85 | elif _BACKEND == 'tensorflow':
86 | sys.stderr.write('Using TensorFlow backend.\n')
87 | from .tensorflow_backend import *
88 | else:
89 | # Try and load external backend.
90 | try:
91 | backend_module = importlib.import_module(_BACKEND)
92 | entries = backend_module.__dict__
93 | # Check if valid backend.
94 | # Module is a valid backend if it has the required entries.
95 | required_entries = ['placeholder', 'variable', 'function']
96 | for e in required_entries:
97 | if e not in entries:
98 | raise ValueError('Invalid backend. Missing required entry : ' + e)
99 | namespace = globals()
100 | for k, v in entries.items():
101 | # Make sure we don't override any entries from common, such as epsilon.
102 | if k not in namespace:
103 | namespace[k] = v
104 | sys.stderr.write('Using ' + _BACKEND + ' backend.\n')
105 | except ImportError:
106 | raise ValueError('Unable to import backend : ' + str(_BACKEND))
107 |
108 |
109 | def backend():
110 | """Publicly accessible method
111 | for determining the current backend.
112 |
113 | # Returns
114 | String, the name of the backend Keras is currently using.
115 |
116 | # Example
117 | ```python
118 | >>> keras.backend.backend()
119 | 'tensorflow'
120 | ```
121 | """
122 | return _BACKEND
123 |
--------------------------------------------------------------------------------
/keras/losses.py:
--------------------------------------------------------------------------------
1 | """Built-in loss functions.
2 | """
3 | from __future__ import absolute_import
4 | from __future__ import division
5 | from __future__ import print_function
6 |
7 | import six
8 | from . import backend as K
9 | from .utils.generic_utils import deserialize_keras_object
10 | from .utils.generic_utils import serialize_keras_object
11 |
12 |
13 | def mean_squared_error(y_true, y_pred):
14 | return K.mean(K.square(y_pred - y_true), axis=-1)
15 |
16 |
17 | def mean_absolute_error(y_true, y_pred):
18 | return K.mean(K.abs(y_pred - y_true), axis=-1)
19 |
20 |
21 | def mean_absolute_percentage_error(y_true, y_pred):
22 | diff = K.abs((y_true - y_pred) / K.clip(K.abs(y_true),
23 | K.epsilon(),
24 | None))
25 | return 100. * K.mean(diff, axis=-1)
26 |
27 |
28 | def mean_squared_logarithmic_error(y_true, y_pred):
29 | first_log = K.log(K.clip(y_pred, K.epsilon(), None) + 1.)
30 | second_log = K.log(K.clip(y_true, K.epsilon(), None) + 1.)
31 | return K.mean(K.square(first_log - second_log), axis=-1)
32 |
33 |
34 | def squared_hinge(y_true, y_pred):
35 | return K.mean(K.square(K.maximum(1. - y_true * y_pred, 0.)), axis=-1)
36 |
37 |
38 | def hinge(y_true, y_pred):
39 | return K.mean(K.maximum(1. - y_true * y_pred, 0.), axis=-1)
40 |
41 |
42 | def categorical_hinge(y_true, y_pred):
43 | pos = K.sum(y_true * y_pred, axis=-1)
44 | neg = K.max((1. - y_true) * y_pred, axis=-1)
45 | return K.maximum(0., neg - pos + 1.)
46 |
47 |
48 | def logcosh(y_true, y_pred):
49 | """Logarithm of the hyperbolic cosine of the prediction error.
50 |
51 | `log(cosh(x))` is approximately equal to `(x ** 2) / 2` for small `x` and
52 | to `abs(x) - log(2)` for large `x`. This means that 'logcosh' works mostly
53 | like the mean squared error, but will not be so strongly affected by the
54 | occasional wildly incorrect prediction.
55 |
56 | # Arguments
57 | y_true: tensor of true targets.
58 | y_pred: tensor of predicted targets.
59 |
60 | # Returns
61 | Tensor with one scalar loss entry per sample.
62 | """
63 | def _logcosh(x):
64 | return x + K.softplus(-2. * x) - K.log(2.)
65 | return K.mean(_logcosh(y_pred - y_true), axis=-1)
66 |
67 |
68 | def categorical_crossentropy(y_true, y_pred):
69 | return K.categorical_crossentropy(y_true, y_pred)
70 |
71 |
72 | def sparse_categorical_crossentropy(y_true, y_pred):
73 | return K.sparse_categorical_crossentropy(y_true, y_pred)
74 |
75 |
76 | def binary_crossentropy(y_true, y_pred):
77 | return K.mean(K.binary_crossentropy(y_true, y_pred), axis=-1)
78 |
79 |
80 | def kullback_leibler_divergence(y_true, y_pred):
81 | y_true = K.clip(y_true, K.epsilon(), 1)
82 | y_pred = K.clip(y_pred, K.epsilon(), 1)
83 | return K.sum(y_true * K.log(y_true / y_pred), axis=-1)
84 |
85 |
86 | def poisson(y_true, y_pred):
87 | return K.mean(y_pred - y_true * K.log(y_pred + K.epsilon()), axis=-1)
88 |
89 |
90 | def cosine_proximity(y_true, y_pred):
91 | y_true = K.l2_normalize(y_true, axis=-1)
92 | y_pred = K.l2_normalize(y_pred, axis=-1)
93 | return -K.sum(y_true * y_pred, axis=-1)
94 |
95 |
96 | # Aliases.
97 |
98 | mse = MSE = mean_squared_error
99 | mae = MAE = mean_absolute_error
100 | mape = MAPE = mean_absolute_percentage_error
101 | msle = MSLE = mean_squared_logarithmic_error
102 | kld = KLD = kullback_leibler_divergence
103 | cosine = cosine_proximity
104 |
105 |
106 | def serialize(loss):
107 | return serialize_keras_object(loss)
108 |
109 |
110 | def deserialize(name, custom_objects=None):
111 | return deserialize_keras_object(name,
112 | module_objects=globals(),
113 | custom_objects=custom_objects,
114 | printable_module_name='loss function')
115 |
116 |
117 | def get(identifier):
118 | if identifier is None:
119 | return None
120 | if isinstance(identifier, six.string_types):
121 | identifier = str(identifier)
122 | return deserialize(identifier)
123 | if isinstance(identifier, dict):
124 | return deserialize(identifier)
125 | elif callable(identifier):
126 | return identifier
127 | else:
128 | raise ValueError('Could not interpret '
129 | 'loss function identifier:', identifier)
130 |
--------------------------------------------------------------------------------
/examples/cifar10_cnn.py:
--------------------------------------------------------------------------------
1 | '''Train a simple deep CNN on the CIFAR10 small images dataset.
2 |
3 | It gets to 75% validation accuracy in 25 epochs, and 79% after 50 epochs.
4 | (it's still underfitting at that point, though).
5 | '''
6 |
7 | from __future__ import print_function
8 | import keras
9 | from keras.datasets import cifar10
10 | from keras.preprocessing.image import ImageDataGenerator
11 | from keras.models import Sequential
12 | from keras.layers import Dense, Dropout, Activation, Flatten
13 | from keras.layers import Conv2D, MaxPooling2D
14 | import os
15 |
16 | batch_size = 32
17 | num_classes = 10
18 | epochs = 100
19 | data_augmentation = True
20 | num_predictions = 20
21 | save_dir = os.path.join(os.getcwd(), 'saved_models')
22 | model_name = 'keras_cifar10_trained_model.h5'
23 |
24 | # The data, split between train and test sets:
25 | (x_train, y_train), (x_test, y_test) = cifar10.load_data()
26 | print('x_train shape:', x_train.shape)
27 | print(x_train.shape[0], 'train samples')
28 | print(x_test.shape[0], 'test samples')
29 |
30 | # Convert class vectors to binary class matrices.
31 | y_train = keras.utils.to_categorical(y_train, num_classes)
32 | y_test = keras.utils.to_categorical(y_test, num_classes)
33 |
34 | model = Sequential()
35 | model.add(Conv2D(32, (3, 3), padding='same',
36 | input_shape=x_train.shape[1:]))
37 | model.add(Activation('relu'))
38 | model.add(Conv2D(32, (3, 3)))
39 | model.add(Activation('relu'))
40 | model.add(MaxPooling2D(pool_size=(2, 2)))
41 | model.add(Dropout(0.25))
42 |
43 | model.add(Conv2D(64, (3, 3), padding='same'))
44 | model.add(Activation('relu'))
45 | model.add(Conv2D(64, (3, 3)))
46 | model.add(Activation('relu'))
47 | model.add(MaxPooling2D(pool_size=(2, 2)))
48 | model.add(Dropout(0.25))
49 |
50 | model.add(Flatten())
51 | model.add(Dense(512))
52 | model.add(Activation('relu'))
53 | model.add(Dropout(0.5))
54 | model.add(Dense(num_classes))
55 | model.add(Activation('softmax'))
56 |
57 | # initiate RMSprop optimizer
58 | opt = keras.optimizers.rmsprop(lr=0.0001, decay=1e-6)
59 |
60 | # Let's train the model using RMSprop
61 | model.compile(loss='categorical_crossentropy',
62 | optimizer=opt,
63 | metrics=['accuracy'])
64 |
65 | x_train = x_train.astype('float32')
66 | x_test = x_test.astype('float32')
67 | x_train /= 255
68 | x_test /= 255
69 |
70 | if not data_augmentation:
71 | print('Not using data augmentation.')
72 | model.fit(x_train, y_train,
73 | batch_size=batch_size,
74 | epochs=epochs,
75 | validation_data=(x_test, y_test),
76 | shuffle=True)
77 | else:
78 | print('Using real-time data augmentation.')
79 | # This will do preprocessing and realtime data augmentation:
80 | datagen = ImageDataGenerator(
81 | featurewise_center=False, # set input mean to 0 over the dataset
82 | samplewise_center=False, # set each sample mean to 0
83 | featurewise_std_normalization=False, # divide inputs by std of the dataset
84 | samplewise_std_normalization=False, # divide each input by its std
85 | zca_whitening=False, # apply ZCA whitening
86 | rotation_range=0, # randomly rotate images in the range (degrees, 0 to 180)
87 | width_shift_range=0.1, # randomly shift images horizontally (fraction of total width)
88 | height_shift_range=0.1, # randomly shift images vertically (fraction of total height)
89 | horizontal_flip=True, # randomly flip images
90 | vertical_flip=False) # randomly flip images
91 |
92 | # Compute quantities required for feature-wise normalization
93 | # (std, mean, and principal components if ZCA whitening is applied).
94 | datagen.fit(x_train)
95 |
96 | # Fit the model on the batches generated by datagen.flow().
97 | model.fit_generator(datagen.flow(x_train, y_train,
98 | batch_size=batch_size),
99 | epochs=epochs,
100 | validation_data=(x_test, y_test),
101 | workers=4)
102 |
103 | # Save model and weights
104 | if not os.path.isdir(save_dir):
105 | os.makedirs(save_dir)
106 | model_path = os.path.join(save_dir, model_name)
107 | model.save(model_path)
108 | print('Saved trained model at %s ' % model_path)
109 |
110 | # Score trained model.
111 | scores = model.evaluate(x_test, y_test, verbose=1)
112 | print('Test loss:', scores[0])
113 | print('Test accuracy:', scores[1])
114 |
--------------------------------------------------------------------------------
/tests/keras/utils/io_utils_test.py:
--------------------------------------------------------------------------------
1 | '''Tests for functions in io_utils.py.
2 | '''
3 | import os
4 | import sys
5 | import pytest
6 | from keras.models import Sequential
7 | from keras.layers import Dense
8 | from keras.utils.io_utils import HDF5Matrix
9 | from keras.utils.io_utils import ask_to_proceed_with_overwrite
10 | import numpy as np
11 | import six
12 | import warnings
13 | import h5py
14 | try:
15 | from unittest.mock import patch
16 | except:
17 | from mock import patch
18 |
19 |
20 | @pytest.fixture
21 | def in_tmpdir(tmpdir):
22 | """Runs a function in a temporary directory.
23 |
24 | Checks that the directory is empty afterwards.
25 | """
26 | with tmpdir.as_cwd():
27 | yield None
28 | assert not tmpdir.listdir()
29 |
30 |
31 | def create_dataset(h5_path='test.h5'):
32 | X = np.random.randn(200, 10).astype('float32')
33 | y = np.random.randint(0, 2, size=(200, 1))
34 | with h5py.File(h5_path, 'w') as f:
35 | # Creating dataset to store features
36 | X_dset = f.create_dataset('my_data', (200, 10), dtype='f')
37 | X_dset[:] = X
38 | # Creating dataset to store labels
39 | y_dset = f.create_dataset('my_labels', (200, 1), dtype='i')
40 | y_dset[:] = y
41 |
42 |
43 | def test_io_utils(in_tmpdir):
44 | '''Tests the HDF5Matrix code using the sample from @jfsantos at
45 | https://gist.github.com/jfsantos/e2ef822c744357a4ed16ec0c885100a3
46 | '''
47 | h5_path = 'test.h5'
48 | create_dataset(h5_path)
49 |
50 | # Instantiating HDF5Matrix for the training set, which is a slice of the first 150 elements
51 | X_train = HDF5Matrix(h5_path, 'my_data', start=0, end=150)
52 | y_train = HDF5Matrix(h5_path, 'my_labels', start=0, end=150)
53 |
54 | # Likewise for the test set
55 | X_test = HDF5Matrix(h5_path, 'my_data', start=150, end=200)
56 | y_test = HDF5Matrix(h5_path, 'my_labels', start=150, end=200)
57 |
58 | # HDF5Matrix behave more or less like Numpy matrices with regards to indexing
59 | assert y_train.shape == (150, 1), 'HDF5Matrix shape should match input array'
60 | # But they do not support negative indices, so don't try print(X_train[-1])
61 |
62 | assert y_train.dtype == np.dtype('i'), 'HDF5Matrix dtype should match input array'
63 | assert y_train.ndim == 2, 'HDF5Matrix ndim should match input array'
64 | assert y_train.size == 150, 'HDF5Matrix ndim should match input array'
65 |
66 | model = Sequential()
67 | model.add(Dense(64, input_shape=(10,), activation='relu'))
68 | model.add(Dense(1, activation='sigmoid'))
69 |
70 | model.compile(loss='binary_crossentropy', optimizer='sgd')
71 |
72 | # Note: you have to use shuffle='batch' or False with HDF5Matrix
73 | model.fit(X_train, y_train, batch_size=32, shuffle='batch', verbose=False)
74 | # test that evalutation and prediction don't crash and return reasonable results
75 | out_pred = model.predict(X_test, batch_size=32, verbose=False)
76 | out_eval = model.evaluate(X_test, y_test, batch_size=32, verbose=False)
77 |
78 | assert out_pred.shape == (50, 1), 'Prediction shape does not match'
79 | assert out_eval.shape == (), 'Shape of evaluation does not match'
80 | assert out_eval > 0, 'Evaluation value does not meet criteria: {}'.format(out_eval)
81 |
82 | # test slicing for shortened array
83 | assert len(X_train[0:]) == len(X_train), 'Incorrect shape for sliced data'
84 |
85 | # test __getitem__
86 | with pytest.raises(IndexError):
87 | X_train[1000]
88 | with pytest.raises(IndexError):
89 | X_train[1000:1001]
90 | with pytest.raises(IndexError):
91 | X_train[[1000, 1001]]
92 | with pytest.raises(IndexError):
93 | X_train[np.array([1000])]
94 | with pytest.raises(IndexError):
95 | X_train[None]
96 | assert (X_train[0] == X_train[:1][0]).all()
97 | assert (X_train[[0, 1]] == X_train[:2]).all()
98 | assert (X_train[np.array([0, 1])] == X_train[:2]).all()
99 |
100 | # test normalizer
101 | normalizer = lambda x: x + 1
102 | normalized_X_train = HDF5Matrix(h5_path, 'my_data', start=0, end=150, normalizer=normalizer)
103 | assert np.isclose(normalized_X_train[0][0], X_train[0][0] + 1)
104 |
105 | os.remove(h5_path)
106 |
107 |
108 | def test_ask_to_proceed_with_overwrite():
109 | with patch('six.moves.input') as mock:
110 | mock.return_value = 'y'
111 | assert ask_to_proceed_with_overwrite('/tmp/not_exists')
112 |
113 | mock.return_value = 'n'
114 | assert not ask_to_proceed_with_overwrite('/tmp/not_exists')
115 |
116 |
117 | if __name__ == '__main__':
118 | pytest.main([__file__])
119 |
--------------------------------------------------------------------------------
/examples/README.md:
--------------------------------------------------------------------------------
1 | # Keras examples directory
2 |
3 | ## Vision models examples
4 |
5 | [mnist_mlp.py](mnist_mlp.py)
6 | Trains a simple deep multi-layer perceptron on the MNIST dataset.
7 |
8 | [mnist_cnn.py](mnist_cnn.py)
9 | Trains a simple convnet on the MNIST dataset.
10 |
11 | [cifar10_cnn.py](cifar10_cnn.py)
12 | Trains a simple deep CNN on the CIFAR10 small images dataset.
13 |
14 | [cifar10_resnet.py](cifar10_resnet.py)
15 | Trains a ResNet on the CIFAR10 small images dataset.
16 |
17 | [conv_lstm.py](conv_lstm.py)
18 | Demonstrates the use of a convolutional LSTM network.
19 |
20 | [image_ocr.py](image_ocr.py)
21 | Trains a convolutional stack followed by a recurrent stack and a CTC logloss function to perform optical character recognition (OCR).
22 |
23 | [mnist_acgan.py](mnist_acgan.py)
24 | Implementation of AC-GAN (Auxiliary Classifier GAN) on the MNIST dataset
25 |
26 | [mnist_hierarchical_rnn.py](mnist_hierarchical_rnn.py)
27 | Trains a Hierarchical RNN (HRNN) to classify MNIST digits.
28 |
29 | [mnist_siamese.py](mnist_siamese.py)
30 | Trains a Siamese multi-layer perceptron on pairs of digits from the MNIST dataset.
31 |
32 | [mnist_swwae.py](mnist_swwae.py)
33 | Trains a Stacked What-Where AutoEncoder built on residual blocks on the MNIST dataset.
34 |
35 | [mnist_transfer_cnn.py](mnist_transfer_cnn.py)
36 | Transfer learning toy example.
37 |
38 | ----
39 |
40 | ## Text & sequences examples
41 |
42 | [addition_rnn.py](addition_rnn.py)
43 | Implementation of sequence to sequence learning for performing addition of two numbers (as strings).
44 |
45 | [babi_rnn.py](babi_rnn.py)
46 | Trains a two-branch recurrent network on the bAbI dataset for reading comprehension.
47 |
48 | [babi_memnn.py](babi_memnn.py)
49 | Trains a memory network on the bAbI dataset for reading comprehension.
50 |
51 | [imdb_bidirectional_lstm.py](imdb_bidirectional_lstm.py)
52 | Trains a Bidirectional LSTM on the IMDB sentiment classification task.
53 |
54 | [imdb_cnn.py](imdb_cnn.py)
55 | Demonstrates the use of Convolution1D for text classification.
56 |
57 | [imdb_cnn_lstm.py](imdb_cnn_lstm.py)
58 | Trains a convolutional stack followed by a recurrent stack network on the IMDB sentiment classification task.
59 |
60 | [imdb_fasttext.py](imdb_fasttext.py)
61 | Trains a FastText model on the IMDB sentiment classification task.
62 |
63 | [imdb_lstm.py](imdb_lstm.py)
64 | Trains an LSTM model on the IMDB sentiment classification task.
65 |
66 | [lstm_stateful.py](lstm_stateful.py)
67 | Demonstrates how to use stateful RNNs to model long sequences efficiently.
68 |
69 | [pretrained_word_embeddings.py](pretrained_word_embeddings.py)
70 | Loads pre-trained word embeddings (GloVe embeddings) into a frozen Keras Embedding layer, and uses it to train a text classification model on the 20 Newsgroup dataset.
71 |
72 | [reuters_mlp.py](reuters_mlp.py)
73 | Trains and evaluate a simple MLP on the Reuters newswire topic classification task.
74 |
75 | ----
76 |
77 | ## Generative models examples
78 |
79 | [lstm_text_generation.py](lstm_text_generation.py)
80 | Generates text from Nietzsche's writings.
81 |
82 | [conv_filter_visualization.py](conv_filter_visualization.py)
83 | Visualization of the filters of VGG16, via gradient ascent in input space.
84 |
85 | [deep_dream.py](deep_dream.py)
86 | Deep Dreams in Keras.
87 |
88 | [neural_doodle.py](neural_doodle.py)
89 | Neural doodle.
90 |
91 | [neural_style_transfer.py](neural_style_transfer.py)
92 | Neural style transfer.
93 |
94 | [variational_autoencoder.py](variational_autoencoder.py)
95 | Demonstrates how to build a variational autoencoder.
96 |
97 | [variational_autoencoder_deconv.py](variational_autoencoder_deconv.py)
98 | Demonstrates how to build a variational autoencoder with Keras using deconvolution layers.
99 |
100 | ----
101 |
102 | ## Examples demonstrating specific Keras functionality
103 |
104 | [antirectifier.py](antirectifier.py)
105 | Demonstrates how to write custom layers for Keras.
106 |
107 | [mnist_sklearn_wrapper.py](mnist_sklearn_wrapper.py)
108 | Demonstrates how to use the sklearn wrapper.
109 |
110 | [mnist_irnn.py](mnist_irnn.py)
111 | Reproduction of the IRNN experiment with pixel-by-pixel sequential MNIST in "A Simple Way to Initialize Recurrent Networks of Rectified Linear Units" by Le et al.
112 |
113 | [mnist_net2net.py](mnist_net2net.py)
114 | Reproduction of the Net2Net experiment with MNIST in "Net2Net: Accelerating Learning via Knowledge Transfer".
115 |
116 | [reuters_mlp_relu_vs_selu.py](reuters_mlp_relu_vs_selu.py)
117 | Compares self-normalizing MLPs with regular MLPs.
118 |
119 | [mnist_tfrecord.py](mnist_tfrecord.py)
120 | MNIST dataset with TFRecords, the standard TensorFlow data format.
121 |
122 | [mnist_dataset_api.py](mnist_dataset_api.py)
123 | MNIST dataset with TensorFlow's Dataset API.
--------------------------------------------------------------------------------
/examples/conv_filter_visualization.py:
--------------------------------------------------------------------------------
1 | '''Visualization of the filters of VGG16, via gradient ascent in input space.
2 |
3 | This script can run on CPU in a few minutes.
4 |
5 | Results example: http://i.imgur.com/4nj4KjN.jpg
6 | '''
7 | from __future__ import print_function
8 |
9 | import numpy as np
10 | import time
11 | from keras.preprocessing.image import save_img
12 | from keras.applications import vgg16
13 | from keras import backend as K
14 |
15 | # dimensions of the generated pictures for each filter.
16 | img_width = 128
17 | img_height = 128
18 |
19 | # the name of the layer we want to visualize
20 | # (see model definition at keras/applications/vgg16.py)
21 | layer_name = 'block5_conv1'
22 |
23 | # util function to convert a tensor into a valid image
24 |
25 |
26 | def deprocess_image(x):
27 | # normalize tensor: center on 0., ensure std is 0.1
28 | x -= x.mean()
29 | x /= (x.std() + K.epsilon())
30 | x *= 0.1
31 |
32 | # clip to [0, 1]
33 | x += 0.5
34 | x = np.clip(x, 0, 1)
35 |
36 | # convert to RGB array
37 | x *= 255
38 | if K.image_data_format() == 'channels_first':
39 | x = x.transpose((1, 2, 0))
40 | x = np.clip(x, 0, 255).astype('uint8')
41 | return x
42 |
43 |
44 | # build the VGG16 network with ImageNet weights
45 | model = vgg16.VGG16(weights='imagenet', include_top=False)
46 | print('Model loaded.')
47 |
48 | model.summary()
49 |
50 | # this is the placeholder for the input images
51 | input_img = model.input
52 |
53 | # get the symbolic outputs of each "key" layer (we gave them unique names).
54 | layer_dict = dict([(layer.name, layer) for layer in model.layers[1:]])
55 |
56 |
57 | def normalize(x):
58 | # utility function to normalize a tensor by its L2 norm
59 | return x / (K.sqrt(K.mean(K.square(x))) + K.epsilon())
60 |
61 |
62 | kept_filters = []
63 | for filter_index in range(200):
64 | # we only scan through the first 200 filters,
65 | # but there are actually 512 of them
66 | print('Processing filter %d' % filter_index)
67 | start_time = time.time()
68 |
69 | # we build a loss function that maximizes the activation
70 | # of the nth filter of the layer considered
71 | layer_output = layer_dict[layer_name].output
72 | if K.image_data_format() == 'channels_first':
73 | loss = K.mean(layer_output[:, filter_index, :, :])
74 | else:
75 | loss = K.mean(layer_output[:, :, :, filter_index])
76 |
77 | # we compute the gradient of the input picture wrt this loss
78 | grads = K.gradients(loss, input_img)[0]
79 |
80 | # normalization trick: we normalize the gradient
81 | grads = normalize(grads)
82 |
83 | # this function returns the loss and grads given the input picture
84 | iterate = K.function([input_img], [loss, grads])
85 |
86 | # step size for gradient ascent
87 | step = 1.
88 |
89 | # we start from a gray image with some random noise
90 | if K.image_data_format() == 'channels_first':
91 | input_img_data = np.random.random((1, 3, img_width, img_height))
92 | else:
93 | input_img_data = np.random.random((1, img_width, img_height, 3))
94 | input_img_data = (input_img_data - 0.5) * 20 + 128
95 |
96 | # we run gradient ascent for 20 steps
97 | for i in range(20):
98 | loss_value, grads_value = iterate([input_img_data])
99 | input_img_data += grads_value * step
100 |
101 | print('Current loss value:', loss_value)
102 | if loss_value <= 0.:
103 | # some filters get stuck to 0, we can skip them
104 | break
105 |
106 | # decode the resulting input image
107 | if loss_value > 0:
108 | img = deprocess_image(input_img_data[0])
109 | kept_filters.append((img, loss_value))
110 | end_time = time.time()
111 | print('Filter %d processed in %ds' % (filter_index, end_time - start_time))
112 |
113 | # we will stich the best 64 filters on a 8 x 8 grid.
114 | n = 8
115 |
116 | # the filters that have the highest loss are assumed to be better-looking.
117 | # we will only keep the top 64 filters.
118 | kept_filters.sort(key=lambda x: x[1], reverse=True)
119 | kept_filters = kept_filters[:n * n]
120 |
121 | # build a black picture with enough space for
122 | # our 8 x 8 filters of size 128 x 128, with a 5px margin in between
123 | margin = 5
124 | width = n * img_width + (n - 1) * margin
125 | height = n * img_height + (n - 1) * margin
126 | stitched_filters = np.zeros((width, height, 3))
127 |
128 | # fill the picture with our saved filters
129 | for i in range(n):
130 | for j in range(n):
131 | img, loss = kept_filters[i * n + j]
132 | stitched_filters[(img_width + margin) * i: (img_width + margin) * i + img_width,
133 | (img_height + margin) * j: (img_height + margin) * j + img_height, :] = img
134 |
135 | # save the result to disk
136 | save_img('stitched_filters_%dx%d.png' % (n, n), stitched_filters)
137 |
--------------------------------------------------------------------------------
/keras/datasets/imdb.py:
--------------------------------------------------------------------------------
1 | """IMDB sentiment classification dataset.
2 | """
3 | from __future__ import absolute_import
4 | from __future__ import division
5 | from __future__ import print_function
6 |
7 | from ..utils.data_utils import get_file
8 | from ..preprocessing.sequence import _remove_long_seq
9 | import numpy as np
10 | import json
11 | import warnings
12 |
13 |
14 | def load_data(path='imdb.npz', num_words=None, skip_top=0,
15 | maxlen=None, seed=113,
16 | start_char=1, oov_char=2, index_from=3, **kwargs):
17 | """Loads the IMDB dataset.
18 |
19 | # Arguments
20 | path: where to cache the data (relative to `~/.keras/dataset`).
21 | num_words: max number of words to include. Words are ranked
22 | by how often they occur (in the training set) and only
23 | the most frequent words are kept
24 | skip_top: skip the top N most frequently occurring words
25 | (which may not be informative).
26 | maxlen: sequences longer than this will be filtered out.
27 | seed: random seed for sample shuffling.
28 | start_char: The start of a sequence will be marked with this character.
29 | Set to 1 because 0 is usually the padding character.
30 | oov_char: words that were cut out because of the `num_words`
31 | or `skip_top` limit will be replaced with this character.
32 | index_from: index actual words with this index and higher.
33 |
34 | # Returns
35 | Tuple of Numpy arrays: `(x_train, y_train), (x_test, y_test)`.
36 |
37 | # Raises
38 | ValueError: in case `maxlen` is so low
39 | that no input sequence could be kept.
40 |
41 | Note that the 'out of vocabulary' character is only used for
42 | words that were present in the training set but are not included
43 | because they're not making the `num_words` cut here.
44 | Words that were not seen in the training set but are in the test set
45 | have simply been skipped.
46 | """
47 | # Legacy support
48 | if 'nb_words' in kwargs:
49 | warnings.warn('The `nb_words` argument in `load_data` '
50 | 'has been renamed `num_words`.')
51 | num_words = kwargs.pop('nb_words')
52 | if kwargs:
53 | raise TypeError('Unrecognized keyword arguments: ' + str(kwargs))
54 |
55 | path = get_file(path,
56 | origin='https://s3.amazonaws.com/text-datasets/imdb.npz',
57 | file_hash='599dadb1135973df5b59232a0e9a887c')
58 | with np.load(path) as f:
59 | x_train, labels_train = f['x_train'], f['y_train']
60 | x_test, labels_test = f['x_test'], f['y_test']
61 |
62 | np.random.seed(seed)
63 | indices = np.arange(len(x_train))
64 | np.random.shuffle(indices)
65 | x_train = x_train[indices]
66 | labels_train = labels_train[indices]
67 |
68 | indices = np.arange(len(x_test))
69 | np.random.shuffle(indices)
70 | x_test = x_test[indices]
71 | labels_test = labels_test[indices]
72 |
73 | xs = np.concatenate([x_train, x_test])
74 | labels = np.concatenate([labels_train, labels_test])
75 |
76 | if start_char is not None:
77 | xs = [[start_char] + [w + index_from for w in x] for x in xs]
78 | elif index_from:
79 | xs = [[w + index_from for w in x] for x in xs]
80 |
81 | if maxlen:
82 | xs, labels = _remove_long_seq(maxlen, xs, labels)
83 | if not xs:
84 | raise ValueError('After filtering for sequences shorter than maxlen=' +
85 | str(maxlen) + ', no sequence was kept. '
86 | 'Increase maxlen.')
87 | if not num_words:
88 | num_words = max([max(x) for x in xs])
89 |
90 | # by convention, use 2 as OOV word
91 | # reserve 'index_from' (=3 by default) characters:
92 | # 0 (padding), 1 (start), 2 (OOV)
93 | if oov_char is not None:
94 | xs = [[w if (skip_top <= w < num_words) else oov_char for w in x] for x in xs]
95 | else:
96 | xs = [[w for w in x if skip_top <= w < num_words] for x in xs]
97 |
98 | idx = len(x_train)
99 | x_train, y_train = np.array(xs[:idx]), np.array(labels[:idx])
100 | x_test, y_test = np.array(xs[idx:]), np.array(labels[idx:])
101 |
102 | return (x_train, y_train), (x_test, y_test)
103 |
104 |
105 | def get_word_index(path='imdb_word_index.json'):
106 | """Retrieves the dictionary mapping word indices back to words.
107 |
108 | # Arguments
109 | path: where to cache the data (relative to `~/.keras/dataset`).
110 |
111 | # Returns
112 | The word index dictionary.
113 | """
114 | path = get_file(path,
115 | origin='https://s3.amazonaws.com/text-datasets/imdb_word_index.json',
116 | file_hash='bfafd718b763782e994055a2d397834f')
117 | with open(path) as f:
118 | return json.load(f)
119 |
--------------------------------------------------------------------------------
/examples/mnist_siamese.py:
--------------------------------------------------------------------------------
1 | '''Trains a Siamese MLP on pairs of digits from the MNIST dataset.
2 |
3 | It follows Hadsell-et-al.'06 [1] by computing the Euclidean distance on the
4 | output of the shared network and by optimizing the contrastive loss (see paper
5 | for mode details).
6 |
7 | # References
8 |
9 | - Dimensionality Reduction by Learning an Invariant Mapping
10 | http://yann.lecun.com/exdb/publis/pdf/hadsell-chopra-lecun-06.pdf
11 |
12 | Gets to 97.2% test accuracy after 20 epochs.
13 | 2 seconds per epoch on a Titan X Maxwell GPU
14 | '''
15 | from __future__ import absolute_import
16 | from __future__ import print_function
17 | import numpy as np
18 |
19 | import random
20 | from keras.datasets import mnist
21 | from keras.models import Model
22 | from keras.layers import Input, Flatten, Dense, Dropout, Lambda
23 | from keras.optimizers import RMSprop
24 | from keras import backend as K
25 |
26 | num_classes = 10
27 | epochs = 20
28 |
29 |
30 | def euclidean_distance(vects):
31 | x, y = vects
32 | return K.sqrt(K.maximum(K.sum(K.square(x - y), axis=1, keepdims=True), K.epsilon()))
33 |
34 |
35 | def eucl_dist_output_shape(shapes):
36 | shape1, shape2 = shapes
37 | return (shape1[0], 1)
38 |
39 |
40 | def contrastive_loss(y_true, y_pred):
41 | '''Contrastive loss from Hadsell-et-al.'06
42 | http://yann.lecun.com/exdb/publis/pdf/hadsell-chopra-lecun-06.pdf
43 | '''
44 | margin = 1
45 | return K.mean(y_true * K.square(y_pred) +
46 | (1 - y_true) * K.square(K.maximum(margin - y_pred, 0)))
47 |
48 |
49 | def create_pairs(x, digit_indices):
50 | '''Positive and negative pair creation.
51 | Alternates between positive and negative pairs.
52 | '''
53 | pairs = []
54 | labels = []
55 | n = min([len(digit_indices[d]) for d in range(num_classes)]) - 1
56 | for d in range(num_classes):
57 | for i in range(n):
58 | z1, z2 = digit_indices[d][i], digit_indices[d][i + 1]
59 | pairs += [[x[z1], x[z2]]]
60 | inc = random.randrange(1, num_classes)
61 | dn = (d + inc) % num_classes
62 | z1, z2 = digit_indices[d][i], digit_indices[dn][i]
63 | pairs += [[x[z1], x[z2]]]
64 | labels += [1, 0]
65 | return np.array(pairs), np.array(labels)
66 |
67 |
68 | def create_base_network(input_shape):
69 | '''Base network to be shared (eq. to feature extraction).
70 | '''
71 | input = Input(shape=input_shape)
72 | x = Flatten()(input)
73 | x = Dense(128, activation='relu')(x)
74 | x = Dropout(0.1)(x)
75 | x = Dense(128, activation='relu')(x)
76 | x = Dropout(0.1)(x)
77 | x = Dense(128, activation='relu')(x)
78 | return Model(input, x)
79 |
80 |
81 | def compute_accuracy(y_true, y_pred):
82 | '''Compute classification accuracy with a fixed threshold on distances.
83 | '''
84 | pred = y_pred.ravel() < 0.5
85 | return np.mean(pred == y_true)
86 |
87 |
88 | def accuracy(y_true, y_pred):
89 | '''Compute classification accuracy with a fixed threshold on distances.
90 | '''
91 | return K.mean(K.equal(y_true, K.cast(y_pred < 0.5, y_true.dtype)))
92 |
93 |
94 | # the data, split between train and test sets
95 | (x_train, y_train), (x_test, y_test) = mnist.load_data()
96 | x_train = x_train.astype('float32')
97 | x_test = x_test.astype('float32')
98 | x_train /= 255
99 | x_test /= 255
100 | input_shape = x_train.shape[1:]
101 |
102 | # create training+test positive and negative pairs
103 | digit_indices = [np.where(y_train == i)[0] for i in range(num_classes)]
104 | tr_pairs, tr_y = create_pairs(x_train, digit_indices)
105 |
106 | digit_indices = [np.where(y_test == i)[0] for i in range(num_classes)]
107 | te_pairs, te_y = create_pairs(x_test, digit_indices)
108 |
109 | # network definition
110 | base_network = create_base_network(input_shape)
111 |
112 | input_a = Input(shape=input_shape)
113 | input_b = Input(shape=input_shape)
114 |
115 | # because we re-use the same instance `base_network`,
116 | # the weights of the network
117 | # will be shared across the two branches
118 | processed_a = base_network(input_a)
119 | processed_b = base_network(input_b)
120 |
121 | distance = Lambda(euclidean_distance,
122 | output_shape=eucl_dist_output_shape)([processed_a, processed_b])
123 |
124 | model = Model([input_a, input_b], distance)
125 |
126 | # train
127 | rms = RMSprop()
128 | model.compile(loss=contrastive_loss, optimizer=rms, metrics=[accuracy])
129 | model.fit([tr_pairs[:, 0], tr_pairs[:, 1]], tr_y,
130 | batch_size=128,
131 | epochs=epochs,
132 | validation_data=([te_pairs[:, 0], te_pairs[:, 1]], te_y))
133 |
134 | # compute final accuracy on training and test sets
135 | y_pred = model.predict([tr_pairs[:, 0], tr_pairs[:, 1]])
136 | tr_acc = compute_accuracy(tr_y, y_pred)
137 | y_pred = model.predict([te_pairs[:, 0], te_pairs[:, 1]])
138 | te_acc = compute_accuracy(te_y, y_pred)
139 |
140 | print('* Accuracy on training set: %0.2f%%' % (100 * tr_acc))
141 | print('* Accuracy on test set: %0.2f%%' % (100 * te_acc))
142 |
--------------------------------------------------------------------------------
/.travis.yml:
--------------------------------------------------------------------------------
1 | sudo: required
2 | dist: trusty
3 | language: python
4 | matrix:
5 | include:
6 | - python: 2.7
7 | env: KERAS_BACKEND=tensorflow TEST_MODE=PEP8
8 | - python: 2.7
9 | env: KERAS_BACKEND=tensorflow TEST_MODE=INTEGRATION_TESTS
10 | - python: 3.6
11 | env: KERAS_BACKEND=tensorflow TEST_MODE=DOC
12 | - python: 2.7
13 | env: KERAS_BACKEND=tensorflow
14 | - python: 3.6
15 | env: KERAS_BACKEND=tensorflow
16 | - python: 2.7
17 | env: KERAS_BACKEND=theano THEANO_FLAGS=optimizer=fast_compile
18 | - python: 3.6
19 | env: KERAS_BACKEND=theano THEANO_FLAGS=optimizer=fast_compile
20 | - python: 2.7
21 | env: KERAS_BACKEND=cntk PYTHONWARNINGS=ignore
22 | - python: 3.6
23 | env: KERAS_BACKEND=cntk PYTHONWARNINGS=ignore
24 | install:
25 | # code below is taken from http://conda.pydata.org/docs/travis.html
26 | # We do this conditionally because it saves us some downloading if the
27 | # version is the same.
28 | - if [[ "$TRAVIS_PYTHON_VERSION" == "2.7" ]]; then
29 | wget https://repo.continuum.io/miniconda/Miniconda-latest-Linux-x86_64.sh -O miniconda.sh;
30 | else
31 | wget https://repo.continuum.io/miniconda/Miniconda3-latest-Linux-x86_64.sh -O miniconda.sh;
32 | fi
33 | - bash miniconda.sh -b -p $HOME/miniconda
34 | - export PATH="$HOME/miniconda/bin:$PATH"
35 | - hash -r
36 | - conda config --set always_yes yes --set changeps1 no
37 | - conda update -q conda
38 | # Useful for debugging any issues with conda
39 | - conda info -a
40 |
41 | - conda create -q -n test-environment python=$TRAVIS_PYTHON_VERSION pytest pandas
42 | - source activate test-environment
43 | - pip install --only-binary=numpy,scipy numpy nose scipy matplotlib h5py theano
44 | - conda install mkl mkl-service
45 |
46 | # set library path
47 | - export LD_LIBRARY_PATH=$HOME/miniconda/envs/test-environment/lib/:$LD_LIBRARY_PATH
48 |
49 | # install PIL for preprocessing tests
50 | - if [[ "$TRAVIS_PYTHON_VERSION" == "2.7" ]]; then
51 | conda install pil;
52 | elif [[ "$TRAVIS_PYTHON_VERSION" == "3.6" ]]; then
53 | conda install Pillow;
54 | fi
55 |
56 | - pip install -e .[tests]
57 |
58 | # install TensorFlow (CPU version).
59 | - pip install tensorflow==1.7
60 |
61 | # install cntk
62 | - if [[ "$TRAVIS_PYTHON_VERSION" == "2.7" ]]; then
63 | pip install https://cntk.ai/PythonWheel/CPU-Only/cntk-2.3.1-cp27-cp27mu-linux_x86_64.whl;
64 | elif [[ "$TRAVIS_PYTHON_VERSION" == "3.6" ]]; then
65 | pip install https://cntk.ai/PythonWheel/CPU-Only/cntk-2.3.1-cp36-cp36m-linux_x86_64.whl;
66 | fi
67 |
68 | # install pydot for visualization tests
69 | - conda install pydot graphviz
70 |
71 | # exclude different backends to measure a coverage for the designated backend only
72 | - if [[ "$KERAS_BACKEND" != "tensorflow" ]]; then
73 | echo ' keras/backend/tensorflow_backend.py' >> .coveragerc;
74 | fi
75 | - if [[ "$KERAS_BACKEND" != "theano" ]]; then
76 | echo ' keras/backend/theano_backend.py' >> .coveragerc;
77 | fi
78 | - if [[ "$KERAS_BACKEND" != "cntk" ]]; then
79 | echo ' keras/backend/cntk_backend.py' >> .coveragerc;
80 | fi
81 |
82 | # detect whether core files are changed or not
83 | - export CORE_CHANGED=False;
84 | - for entry in `git diff --name-only HEAD~1`; do if [[ "$entry" == "keras/backend/"* ]] || [[ "$entry" == "keras/engine/"* ]] || [[ "$entry" == "keras/layers/"* ]]; then export CORE_CHANGED=True; fi; done
85 | - export APP_CHANGED=False;
86 | - for entry in `git diff --name-only HEAD~1`; do if [[ "$entry" == "keras/applications/"* ]]; then export APP_CHANGED=True; fi; done
87 |
88 | #install open mpi
89 | - rm -rf ~/mpi
90 | - mkdir ~/mpi
91 | - pushd ~/mpi
92 | - wget http://cntk.ai/PythonWheel/ForKeras/depends/openmpi_1.10-3.zip
93 | - unzip ./openmpi_1.10-3.zip
94 | - sudo dpkg -i openmpi_1.10-3.deb
95 | - popd
96 |
97 | # command to run tests
98 | script:
99 | - export MKL_THREADING_LAYER="GNU"
100 | # run keras backend init to initialize backend config
101 | - python -c "import keras.backend"
102 | # create dataset directory to avoid concurrent directory creation at runtime
103 | - mkdir ~/.keras/datasets
104 | # set up keras backend
105 | - sed -i -e 's/"backend":[[:space:]]*"[^"]*/"backend":\ "'$KERAS_BACKEND'/g' ~/.keras/keras.json;
106 | - echo -e "Running tests with the following config:\n$(cat ~/.keras/keras.json)"
107 | - if [[ "$TEST_MODE" == "INTEGRATION_TESTS" ]]; then
108 | PYTHONPATH=$PWD:$PYTHONPATH py.test tests/integration_tests;
109 | elif [[ "$TEST_MODE" == "PEP8" ]]; then
110 | PYTHONPATH=$PWD:$PYTHONPATH py.test --pep8 -m pep8 -n0;
111 | elif [[ "$TEST_MODE" == "DOC" ]]; then
112 | PYTHONPATH=$PWD:$PYTHONPATH py.test tests/test_documentation.py;
113 | else
114 | PYTHONPATH=$PWD:$PYTHONPATH py.test tests/ --ignore=tests/integration_tests --ignore=tests/test_documentation.py --ignore=tests/keras/legacy/layers_test.py --cov-config .coveragerc --cov=keras tests/;
115 | fi
116 |
--------------------------------------------------------------------------------
/docs/templates/backend.md:
--------------------------------------------------------------------------------
1 | # Keras backends
2 |
3 | ## What is a "backend"?
4 |
5 | Keras is a model-level library, providing high-level building blocks for developing deep learning models. It does not handle itself low-level operations such as tensor products, convolutions and so on. Instead, it relies on a specialized, well-optimized tensor manipulation library to do so, serving as the "backend engine" of Keras. Rather than picking one single tensor library and making the implementation of Keras tied to that library, Keras handles the problem in a modular way, and several different backend engines can be plugged seamlessly into Keras.
6 |
7 | At this time, Keras has three backend implementations available: the **TensorFlow** backend, the **Theano** backend, and the **CNTK** backend.
8 |
9 | - [TensorFlow](http://www.tensorflow.org/) is an open-source symbolic tensor manipulation framework developed by Google.
10 | - [Theano](http://deeplearning.net/software/theano/) is an open-source symbolic tensor manipulation framework developed by LISA Lab at Université de Montréal.
11 | - [CNTK](https://www.microsoft.com/en-us/cognitive-toolkit/) is an open-source toolkit for deep learning developed by Microsoft.
12 |
13 | In the future, we are likely to add more backend options.
14 |
15 | ----
16 |
17 | ## Switching from one backend to another
18 |
19 | If you have run Keras at least once, you will find the Keras configuration file at:
20 |
21 | `$HOME/.keras/keras.json`
22 |
23 | If it isn't there, you can create it.
24 |
25 | **NOTE for Windows Users:** Please replace `$HOME` with `%USERPROFILE%`.
26 |
27 | The default configuration file looks like this:
28 |
29 | ```
30 | {
31 | "image_data_format": "channels_last",
32 | "epsilon": 1e-07,
33 | "floatx": "float32",
34 | "backend": "tensorflow"
35 | }
36 | ```
37 |
38 | Simply change the field `backend` to `"theano"`, `"tensorflow"`, or `"cntk"`, and Keras will use the new configuration next time you run any Keras code.
39 |
40 | You can also define the environment variable ``KERAS_BACKEND`` and this will
41 | override what is defined in your config file :
42 |
43 | ```bash
44 | KERAS_BACKEND=tensorflow python -c "from keras import backend"
45 | Using TensorFlow backend.
46 | ```
47 |
48 | ----
49 |
50 | ## keras.json details
51 |
52 |
53 | The `keras.json` configuration file contains the following settings:
54 |
55 | ```
56 | {
57 | "image_data_format": "channels_last",
58 | "epsilon": 1e-07,
59 | "floatx": "float32",
60 | "backend": "tensorflow"
61 | }
62 | ```
63 |
64 | You can change these settings by editing `$HOME/.keras/keras.json`.
65 |
66 | * `image_data_format`: String, either `"channels_last"` or `"channels_first"`. It specifies which data format convention Keras will follow. (`keras.backend.image_data_format()` returns it.)
67 | - For 2D data (e.g. image), `"channels_last"` assumes `(rows, cols, channels)` while `"channels_first"` assumes `(channels, rows, cols)`.
68 | - For 3D data, `"channels_last"` assumes `(conv_dim1, conv_dim2, conv_dim3, channels)` while `"channels_first"` assumes `(channels, conv_dim1, conv_dim2, conv_dim3)`.
69 | * `epsilon`: Float, a numeric fuzzing constant used to avoid dividing by zero in some operations.
70 | * `floatx`: String, `"float16"`, `"float32"`, or `"float64"`. Default float precision.
71 | * `backend`: String, `"tensorflow"`, `"theano"`, or `"cntk"`.
72 |
73 | ----
74 |
75 | ## Using the abstract Keras backend to write new code
76 |
77 | If you want the Keras modules you write to be compatible with both Theano (`th`) and TensorFlow (`tf`), you have to write them via the abstract Keras backend API. Here's an intro.
78 |
79 | You can import the backend module via:
80 | ```python
81 | from keras import backend as K
82 | ```
83 |
84 | The code below instantiates an input placeholder. It's equivalent to `tf.placeholder()` or `th.tensor.matrix()`, `th.tensor.tensor3()`, etc.
85 |
86 | ```python
87 | inputs = K.placeholder(shape=(2, 4, 5))
88 | # also works:
89 | inputs = K.placeholder(shape=(None, 4, 5))
90 | # also works:
91 | inputs = K.placeholder(ndim=3)
92 | ```
93 |
94 | The code below instantiates a variable. It's equivalent to `tf.Variable()` or `th.shared()`.
95 |
96 | ```python
97 | import numpy as np
98 | val = np.random.random((3, 4, 5))
99 | var = K.variable(value=val)
100 |
101 | # all-zeros variable:
102 | var = K.zeros(shape=(3, 4, 5))
103 | # all-ones:
104 | var = K.ones(shape=(3, 4, 5))
105 | ```
106 |
107 | Most tensor operations you will need can be done as you would in TensorFlow or Theano:
108 |
109 | ```python
110 | # Initializing Tensors with Random Numbers
111 | b = K.random_uniform_variable(shape=(3, 4), low=0, high=1) # Uniform distribution
112 | c = K.random_normal_variable(shape=(3, 4), mean=0, scale=1) # Gaussian distribution
113 | d = K.random_normal_variable(shape=(3, 4), mean=0, scale=1)
114 |
115 | # Tensor Arithmetic
116 | a = b + c * K.abs(d)
117 | c = K.dot(a, K.transpose(b))
118 | a = K.sum(b, axis=1)
119 | a = K.softmax(b)
120 | a = K.concatenate([b, c], axis=-1)
121 | # etc...
122 | ```
123 |
124 | ----
125 |
126 | ## Backend functions
127 |
128 |
129 | {{autogenerated}}
130 |
131 |
132 |
133 |
134 |
135 |
--------------------------------------------------------------------------------
/keras/backend/common.py:
--------------------------------------------------------------------------------
1 | from __future__ import absolute_import
2 | from __future__ import division
3 | from __future__ import print_function
4 |
5 | import numpy as np
6 |
7 | # the type of float to use throughout the session.
8 | _FLOATX = 'float32'
9 | _EPSILON = 1e-7
10 | _IMAGE_DATA_FORMAT = 'channels_last'
11 |
12 |
13 | def epsilon():
14 | """Returns the value of the fuzz factor used in numeric expressions.
15 |
16 | # Returns
17 | A float.
18 |
19 | # Example
20 | ```python
21 | >>> keras.backend.epsilon()
22 | 1e-07
23 | ```
24 | """
25 | return _EPSILON
26 |
27 |
28 | def set_epsilon(e):
29 | """Sets the value of the fuzz factor used in numeric expressions.
30 |
31 | # Arguments
32 | e: float. New value of epsilon.
33 |
34 | # Example
35 | ```python
36 | >>> from keras import backend as K
37 | >>> K.epsilon()
38 | 1e-07
39 | >>> K.set_epsilon(1e-05)
40 | >>> K.epsilon()
41 | 1e-05
42 | ```
43 | """
44 | global _EPSILON
45 | _EPSILON = e
46 |
47 |
48 | def floatx():
49 | """Returns the default float type, as a string.
50 | (e.g. 'float16', 'float32', 'float64').
51 |
52 | # Returns
53 | String, the current default float type.
54 |
55 | # Example
56 | ```python
57 | >>> keras.backend.floatx()
58 | 'float32'
59 | ```
60 | """
61 | return _FLOATX
62 |
63 |
64 | def set_floatx(floatx):
65 | """Sets the default float type.
66 |
67 | # Arguments
68 | floatx: String, 'float16', 'float32', or 'float64'.
69 |
70 | # Example
71 | ```python
72 | >>> from keras import backend as K
73 | >>> K.floatx()
74 | 'float32'
75 | >>> K.set_floatx('float16')
76 | >>> K.floatx()
77 | 'float16'
78 | ```
79 | """
80 | global _FLOATX
81 | if floatx not in {'float16', 'float32', 'float64'}:
82 | raise ValueError('Unknown floatx type: ' + str(floatx))
83 | _FLOATX = str(floatx)
84 |
85 |
86 | def cast_to_floatx(x):
87 | """Cast a Numpy array to the default Keras float type.
88 |
89 | # Arguments
90 | x: Numpy array.
91 |
92 | # Returns
93 | The same Numpy array, cast to its new type.
94 |
95 | # Example
96 | ```python
97 | >>> from keras import backend as K
98 | >>> K.floatx()
99 | 'float32'
100 | >>> arr = numpy.array([1.0, 2.0], dtype='float64')
101 | >>> arr.dtype
102 | dtype('float64')
103 | >>> new_arr = K.cast_to_floatx(arr)
104 | >>> new_arr
105 | array([ 1., 2.], dtype=float32)
106 | >>> new_arr.dtype
107 | dtype('float32')
108 | ```
109 | """
110 | return np.asarray(x, dtype=_FLOATX)
111 |
112 |
113 | def image_data_format():
114 | """Returns the default image data format convention ('channels_first' or 'channels_last').
115 |
116 | # Returns
117 | A string, either `'channels_first'` or `'channels_last'`
118 |
119 | # Example
120 | ```python
121 | >>> keras.backend.image_data_format()
122 | 'channels_first'
123 | ```
124 | """
125 | return _IMAGE_DATA_FORMAT
126 |
127 |
128 | def set_image_data_format(data_format):
129 | """Sets the value of the data format convention.
130 |
131 | # Arguments
132 | data_format: string. `'channels_first'` or `'channels_last'`.
133 |
134 | # Example
135 | ```python
136 | >>> from keras import backend as K
137 | >>> K.image_data_format()
138 | 'channels_first'
139 | >>> K.set_image_data_format('channels_last')
140 | >>> K.image_data_format()
141 | 'channels_last'
142 | ```
143 | """
144 | global _IMAGE_DATA_FORMAT
145 | if data_format not in {'channels_last', 'channels_first'}:
146 | raise ValueError('Unknown data_format:', data_format)
147 | _IMAGE_DATA_FORMAT = str(data_format)
148 |
149 |
150 | # Legacy methods
151 |
152 | def set_image_dim_ordering(dim_ordering):
153 | """Legacy setter for `image_data_format`.
154 |
155 | # Arguments
156 | dim_ordering: string. `tf` or `th`.
157 |
158 | # Example
159 | ```python
160 | >>> from keras import backend as K
161 | >>> K.image_data_format()
162 | 'channels_first'
163 | >>> K.set_image_data_format('channels_last')
164 | >>> K.image_data_format()
165 | 'channels_last'
166 | ```
167 |
168 | # Raises
169 | ValueError: if `dim_ordering` is invalid.
170 | """
171 | global _IMAGE_DATA_FORMAT
172 | if dim_ordering not in {'tf', 'th'}:
173 | raise ValueError('Unknown dim_ordering:', dim_ordering)
174 | if dim_ordering == 'th':
175 | data_format = 'channels_first'
176 | else:
177 | data_format = 'channels_last'
178 | _IMAGE_DATA_FORMAT = data_format
179 |
180 |
181 | def image_dim_ordering():
182 | """Legacy getter for `image_data_format`.
183 |
184 | # Returns
185 | string, one of `'th'`, `'tf'`
186 | """
187 | if _IMAGE_DATA_FORMAT == 'channels_first':
188 | return 'th'
189 | else:
190 | return 'tf'
191 |
--------------------------------------------------------------------------------
/tests/keras/utils/generic_utils_test.py:
--------------------------------------------------------------------------------
1 | import sys
2 | import pytest
3 | import numpy as np
4 | import marshal
5 | from keras.utils.generic_utils import custom_object_scope
6 | from keras.utils.generic_utils import has_arg
7 | from keras.utils.generic_utils import Progbar
8 | from keras.utils.generic_utils import func_dump
9 | from keras.utils.generic_utils import func_load
10 | from keras.utils.test_utils import keras_test
11 | from keras import activations
12 | from keras import regularizers
13 |
14 |
15 | @keras_test
16 | def test_progbar():
17 | values_s = [None,
18 | [['key1', 1], ['key2', 1e-4]],
19 | [['key3', 1], ['key2', 1e-4]]]
20 |
21 | for target in (len(values_s) - 1, None):
22 | for verbose in (0, 1, 2):
23 | bar = Progbar(target, width=30, verbose=verbose, interval=0.05)
24 | for current, values in enumerate(values_s):
25 | bar.update(current, values=values)
26 |
27 |
28 | def test_custom_objects_scope():
29 |
30 | def custom_fn():
31 | pass
32 |
33 | class CustomClass(object):
34 | pass
35 |
36 | with custom_object_scope({'CustomClass': CustomClass,
37 | 'custom_fn': custom_fn}):
38 | act = activations.get('custom_fn')
39 | assert act == custom_fn
40 | cl = regularizers.get('CustomClass')
41 | assert cl.__class__ == CustomClass
42 |
43 |
44 | @pytest.mark.parametrize('fn, name, accept_all, expected', [
45 | ('f(x)', 'x', False, True),
46 | ('f(x)', 'y', False, False),
47 | ('f(x)', 'y', True, False),
48 | ('f(x, y)', 'y', False, True),
49 | ('f(x, y=1)', 'y', False, True),
50 | ('f(x, **kwargs)', 'x', False, True),
51 | ('f(x, **kwargs)', 'y', False, False),
52 | ('f(x, **kwargs)', 'y', True, True),
53 | ('f(x, y=1, **kwargs)', 'y', False, True),
54 | # Keyword-only arguments (Python 3 only)
55 | ('f(x, *args, y=1)', 'y', False, True),
56 | ('f(x, *args, y=1)', 'z', True, False),
57 | ('f(x, *, y=1)', 'x', False, True),
58 | ('f(x, *, y=1)', 'y', False, True),
59 | # lambda
60 | (lambda x: x, 'x', False, True),
61 | (lambda x: x, 'y', False, False),
62 | (lambda x: x, 'y', True, False),
63 | ])
64 | def test_has_arg(fn, name, accept_all, expected):
65 | if isinstance(fn, str):
66 | context = dict()
67 | try:
68 | exec('def {}: pass'.format(fn), context)
69 | except SyntaxError:
70 | if sys.version_info >= (3,):
71 | raise
72 | pytest.skip('Function is not compatible with Python 2')
73 | context.pop('__builtins__', None) # Sometimes exec adds builtins to the context
74 | fn, = context.values()
75 |
76 | assert has_arg(fn, name, accept_all) is expected
77 |
78 |
79 | @pytest.mark.xfail(sys.version_info < (3, 3),
80 | reason='inspect API does not reveal positional-only arguments')
81 | def test_has_arg_positional_only():
82 | assert has_arg(pow, 'x') is False
83 |
84 |
85 | @pytest.mark.parametrize(
86 | 'test_function_type',
87 | ('simple function', 'closured function'))
88 | def test_func_dump_and_load(test_function_type):
89 |
90 | if test_function_type == 'simple function':
91 | def test_func():
92 | return r'\u'
93 |
94 | elif test_function_type == 'closured function':
95 | def get_test_func():
96 | x = r'\u'
97 |
98 | def test_func():
99 | return x
100 | return test_func
101 | test_func = get_test_func()
102 | else:
103 | raise Exception('Unknown test case for test_func_dump_and_load')
104 |
105 | serialized = func_dump(test_func)
106 | deserialized = func_load(serialized)
107 | assert deserialized.__code__ == test_func.__code__
108 | assert deserialized.__defaults__ == test_func.__defaults__
109 | assert deserialized.__closure__ == test_func.__closure__
110 |
111 |
112 | def test_func_dump_and_load_closure():
113 | y = 0
114 | test_func = lambda x: x + y
115 | serialized, _, closure = func_dump(test_func)
116 | deserialized = func_load(serialized, closure=closure)
117 | assert deserialized.__code__ == test_func.__code__
118 | assert deserialized.__defaults__ == test_func.__defaults__
119 | assert deserialized.__closure__ == test_func.__closure__
120 |
121 |
122 | @pytest.mark.parametrize(
123 | 'test_func', [activations.softmax, np.argmax, lambda x: x**2, lambda x: x])
124 | def test_func_dump_and_load_backwards_compat(test_func):
125 | # this test ensures that models serialized prior to version 2.1.2 can still be
126 | # deserialized
127 |
128 | # see https://github.com/evhub/keras/blob/2.1.1/keras/utils/generic_utils.py#L166
129 | serialized = marshal.dumps(test_func.__code__).decode('raw_unicode_escape')
130 |
131 | deserialized = func_load(serialized, defaults=test_func.__defaults__)
132 | assert deserialized.__code__ == test_func.__code__
133 | assert deserialized.__defaults__ == test_func.__defaults__
134 | assert deserialized.__closure__ == test_func.__closure__
135 |
136 | if __name__ == '__main__':
137 | pytest.main([__file__])
138 |
--------------------------------------------------------------------------------
/keras/utils/io_utils.py:
--------------------------------------------------------------------------------
1 | """Utilities related to disk I/O."""
2 | from __future__ import absolute_import
3 | from __future__ import division
4 | from __future__ import print_function
5 |
6 | import numpy as np
7 | from collections import defaultdict
8 |
9 | import six
10 | try:
11 | import h5py
12 | except ImportError:
13 | h5py = None
14 |
15 |
16 | class HDF5Matrix(object):
17 | """Representation of HDF5 dataset to be used instead of a Numpy array.
18 |
19 | # Example
20 |
21 | ```python
22 | x_data = HDF5Matrix('input/file.hdf5', 'data')
23 | model.predict(x_data)
24 | ```
25 |
26 | Providing `start` and `end` allows use of a slice of the dataset.
27 |
28 | Optionally, a normalizer function (or lambda) can be given. This will
29 | be called on every slice of data retrieved.
30 |
31 | # Arguments
32 | datapath: string, path to a HDF5 file
33 | dataset: string, name of the HDF5 dataset in the file specified
34 | in datapath
35 | start: int, start of desired slice of the specified dataset
36 | end: int, end of desired slice of the specified dataset
37 | normalizer: function to be called on data when retrieved
38 |
39 | # Returns
40 | An array-like HDF5 dataset.
41 | """
42 | refs = defaultdict(int)
43 |
44 | def __init__(self, datapath, dataset, start=0, end=None, normalizer=None):
45 | if h5py is None:
46 | raise ImportError('The use of HDF5Matrix requires '
47 | 'HDF5 and h5py installed.')
48 |
49 | if datapath not in list(self.refs.keys()):
50 | f = h5py.File(datapath)
51 | self.refs[datapath] = f
52 | else:
53 | f = self.refs[datapath]
54 | self.data = f[dataset]
55 | self.start = start
56 | if end is None:
57 | self.end = self.data.shape[0]
58 | else:
59 | self.end = end
60 | self.normalizer = normalizer
61 |
62 | def __len__(self):
63 | return self.end - self.start
64 |
65 | def __getitem__(self, key):
66 | if isinstance(key, slice):
67 | start, stop = key.start, key.stop
68 | if start is None:
69 | start = 0
70 | if stop is None:
71 | stop = self.shape[0]
72 | if stop + self.start <= self.end:
73 | idx = slice(start + self.start, stop + self.start)
74 | else:
75 | raise IndexError
76 | elif isinstance(key, (int, np.integer)):
77 | if key + self.start < self.end:
78 | idx = key + self.start
79 | else:
80 | raise IndexError
81 | elif isinstance(key, np.ndarray):
82 | if np.max(key) + self.start < self.end:
83 | idx = (self.start + key).tolist()
84 | else:
85 | raise IndexError
86 | elif isinstance(key, list):
87 | if max(key) + self.start < self.end:
88 | idx = [x + self.start for x in key]
89 | else:
90 | raise IndexError
91 | else:
92 | raise IndexError
93 | if self.normalizer is not None:
94 | return self.normalizer(self.data[idx])
95 | else:
96 | return self.data[idx]
97 |
98 | @property
99 | def shape(self):
100 | """Gets a numpy-style shape tuple giving the dataset dimensions.
101 |
102 | # Returns
103 | A numpy-style shape tuple.
104 | """
105 | return (self.end - self.start,) + self.data.shape[1:]
106 |
107 | @property
108 | def dtype(self):
109 | """Gets the datatype of the dataset.
110 |
111 | # Returns
112 | A numpy dtype string.
113 | """
114 | return self.data.dtype
115 |
116 | @property
117 | def ndim(self):
118 | """Gets the number of dimensions (rank) of the dataset.
119 |
120 | # Returns
121 | An integer denoting the number of dimensions (rank) of the dataset.
122 | """
123 | return self.data.ndim
124 |
125 | @property
126 | def size(self):
127 | """Gets the total dataset size (number of elements).
128 |
129 | # Returns
130 | An integer denoting the number of elements in the dataset.
131 | """
132 | return np.prod(self.shape)
133 |
134 |
135 | def ask_to_proceed_with_overwrite(filepath):
136 | """Produces a prompt asking about overwriting a file.
137 |
138 | # Arguments
139 | filepath: the path to the file to be overwritten.
140 |
141 | # Returns
142 | True if we can proceed with overwrite, False otherwise.
143 | """
144 | overwrite = six.moves.input('[WARNING] %s already exists - overwrite? '
145 | '[y/n]' % (filepath)).strip().lower()
146 | while overwrite not in ('y', 'n'):
147 | overwrite = six.moves.input('Enter "y" (overwrite) or "n" '
148 | '(cancel).').strip().lower()
149 | if overwrite == 'n':
150 | return False
151 | print('[TIP] Next time specify overwrite=True!')
152 | return True
153 |
--------------------------------------------------------------------------------
/tests/keras/initializers_test.py:
--------------------------------------------------------------------------------
1 | import pytest
2 | import numpy as np
3 |
4 | from keras import initializers
5 | from keras import backend as K
6 |
7 | # 2D tensor test fixture
8 | FC_SHAPE = (200, 100)
9 |
10 | # 4D convolution in th order. This shape has the same effective shape as FC_SHAPE
11 | CONV_SHAPE = (25, 25, 20, 20)
12 |
13 |
14 | def _runner(init, shape, target_mean=None, target_std=None,
15 | target_max=None, target_min=None):
16 | variable = K.variable(init(shape))
17 | output = K.get_value(variable)
18 | lim = 3e-2
19 | if target_std is not None:
20 | assert abs(output.std() - target_std) < lim
21 | if target_mean is not None:
22 | assert abs(output.mean() - target_mean) < lim
23 | if target_max is not None:
24 | assert abs(output.max() - target_max) < lim
25 | if target_min is not None:
26 | assert abs(output.min() - target_min) < lim
27 |
28 |
29 | @pytest.mark.parametrize('tensor_shape', [FC_SHAPE, CONV_SHAPE], ids=['FC', 'CONV'])
30 | def test_uniform(tensor_shape):
31 | _runner(initializers.RandomUniform(minval=-1, maxval=1), tensor_shape,
32 | target_mean=0., target_max=1, target_min=-1)
33 |
34 |
35 | @pytest.mark.parametrize('tensor_shape', [FC_SHAPE, CONV_SHAPE], ids=['FC', 'CONV'])
36 | def test_normal(tensor_shape):
37 | _runner(initializers.RandomNormal(mean=0, stddev=1), tensor_shape,
38 | target_mean=0., target_std=1)
39 |
40 |
41 | @pytest.mark.parametrize('tensor_shape', [FC_SHAPE, CONV_SHAPE], ids=['FC', 'CONV'])
42 | def test_truncated_normal(tensor_shape):
43 | _runner(initializers.TruncatedNormal(mean=0, stddev=1), tensor_shape,
44 | target_mean=0., target_max=2, target_min=-2)
45 |
46 |
47 | @pytest.mark.parametrize('tensor_shape', [FC_SHAPE, CONV_SHAPE], ids=['FC', 'CONV'])
48 | def test_constant(tensor_shape):
49 | _runner(initializers.Constant(2), tensor_shape,
50 | target_mean=2, target_max=2, target_min=2)
51 |
52 |
53 | @pytest.mark.parametrize('tensor_shape', [FC_SHAPE, CONV_SHAPE], ids=['FC', 'CONV'])
54 | def test_lecun_uniform(tensor_shape):
55 | fan_in, _ = initializers._compute_fans(tensor_shape)
56 | std = np.sqrt(1. / fan_in)
57 | _runner(initializers.lecun_uniform(), tensor_shape,
58 | target_mean=0., target_std=std)
59 |
60 |
61 | @pytest.mark.parametrize('tensor_shape', [FC_SHAPE, CONV_SHAPE], ids=['FC', 'CONV'])
62 | def test_glorot_uniform(tensor_shape):
63 | fan_in, fan_out = initializers._compute_fans(tensor_shape)
64 | std = np.sqrt(2. / (fan_in + fan_out))
65 | _runner(initializers.glorot_uniform(), tensor_shape,
66 | target_mean=0., target_std=std)
67 |
68 |
69 | @pytest.mark.parametrize('tensor_shape', [FC_SHAPE, CONV_SHAPE], ids=['FC', 'CONV'])
70 | def test_he_uniform(tensor_shape):
71 | fan_in, _ = initializers._compute_fans(tensor_shape)
72 | std = np.sqrt(2. / fan_in)
73 | _runner(initializers.he_uniform(), tensor_shape,
74 | target_mean=0., target_std=std)
75 |
76 |
77 | @pytest.mark.parametrize('tensor_shape', [FC_SHAPE, CONV_SHAPE], ids=['FC', 'CONV'])
78 | def test_lecun_normal(tensor_shape):
79 | fan_in, _ = initializers._compute_fans(tensor_shape)
80 | std = np.sqrt(1. / fan_in)
81 | _runner(initializers.lecun_normal(), tensor_shape,
82 | target_mean=0., target_std=std)
83 |
84 |
85 | @pytest.mark.parametrize('tensor_shape', [FC_SHAPE, CONV_SHAPE], ids=['FC', 'CONV'])
86 | def test_glorot_normal(tensor_shape):
87 | fan_in, fan_out = initializers._compute_fans(tensor_shape)
88 | std = np.sqrt(2. / (fan_in + fan_out))
89 | _runner(initializers.glorot_normal(), tensor_shape,
90 | target_mean=0., target_std=std)
91 |
92 |
93 | @pytest.mark.parametrize('tensor_shape', [FC_SHAPE, CONV_SHAPE], ids=['FC', 'CONV'])
94 | def test_he_normal(tensor_shape):
95 | fan_in, _ = initializers._compute_fans(tensor_shape)
96 | std = np.sqrt(2. / fan_in)
97 | _runner(initializers.he_normal(), tensor_shape,
98 | target_mean=0., target_std=std)
99 |
100 |
101 | @pytest.mark.parametrize('tensor_shape', [FC_SHAPE, CONV_SHAPE], ids=['FC', 'CONV'])
102 | def test_orthogonal(tensor_shape):
103 | _runner(initializers.orthogonal(), tensor_shape,
104 | target_mean=0.)
105 |
106 |
107 | @pytest.mark.parametrize('tensor_shape', [(100, 100), (1, 2, 3, 4)], ids=['FC', 'CONV'])
108 | def test_identity(tensor_shape):
109 | if len(tensor_shape) > 2:
110 | with pytest.raises(ValueError):
111 | _runner(initializers.identity(), tensor_shape,
112 | target_mean=1. / tensor_shape[0], target_max=1.)
113 | else:
114 | _runner(initializers.identity(), tensor_shape,
115 | target_mean=1. / tensor_shape[0], target_max=1.)
116 |
117 |
118 | @pytest.mark.parametrize('tensor_shape', [FC_SHAPE, CONV_SHAPE], ids=['FC', 'CONV'])
119 | def test_zero(tensor_shape):
120 | _runner(initializers.zeros(), tensor_shape,
121 | target_mean=0., target_max=0.)
122 |
123 |
124 | @pytest.mark.parametrize('tensor_shape', [FC_SHAPE, CONV_SHAPE], ids=['FC', 'CONV'])
125 | def test_one(tensor_shape):
126 | _runner(initializers.ones(), tensor_shape,
127 | target_mean=1., target_max=1.)
128 |
129 |
130 | if __name__ == '__main__':
131 | pytest.main([__file__])
132 |
--------------------------------------------------------------------------------
/keras/utils/vis_utils.py:
--------------------------------------------------------------------------------
1 | """Utilities related to model visualization."""
2 | from __future__ import absolute_import
3 | from __future__ import division
4 | from __future__ import print_function
5 |
6 | import os
7 |
8 | # `pydot` is an optional dependency,
9 | # see `extras_require` in `setup.py`.
10 | try:
11 | import pydot
12 | except ImportError:
13 | pydot = None
14 |
15 |
16 | def _check_pydot():
17 | """Raise errors if `pydot` or GraphViz unavailable."""
18 | if pydot is None:
19 | raise ImportError(
20 | 'Failed to import `pydot`. '
21 | 'Please install `pydot`. '
22 | 'For example with `pip install pydot`.')
23 | try:
24 | # Attempt to create an image of a blank graph
25 | # to check the pydot/graphviz installation.
26 | pydot.Dot.create(pydot.Dot())
27 | except OSError:
28 | raise OSError(
29 | '`pydot` failed to call GraphViz.'
30 | 'Please install GraphViz (https://www.graphviz.org/) '
31 | 'and ensure that its executables are in the $PATH.')
32 |
33 |
34 | def model_to_dot(model,
35 | show_shapes=False,
36 | show_layer_names=True,
37 | rankdir='TB'):
38 | """Convert a Keras model to dot format.
39 |
40 | # Arguments
41 | model: A Keras model instance.
42 | show_shapes: whether to display shape information.
43 | show_layer_names: whether to display layer names.
44 | rankdir: `rankdir` argument passed to PyDot,
45 | a string specifying the format of the plot:
46 | 'TB' creates a vertical plot;
47 | 'LR' creates a horizontal plot.
48 |
49 | # Returns
50 | A `pydot.Dot` instance representing the Keras model.
51 | """
52 | from ..layers.wrappers import Wrapper
53 | from ..models import Sequential
54 |
55 | _check_pydot()
56 | dot = pydot.Dot()
57 | dot.set('rankdir', rankdir)
58 | dot.set('concentrate', True)
59 | dot.set_node_defaults(shape='record')
60 |
61 | if isinstance(model, Sequential):
62 | if not model.built:
63 | model.build()
64 | model = model.model
65 | layers = model.layers
66 |
67 | # Create graph nodes.
68 | for layer in layers:
69 | layer_id = str(id(layer))
70 |
71 | # Append a wrapped layer's label to node's label, if it exists.
72 | layer_name = layer.name
73 | class_name = layer.__class__.__name__
74 | if isinstance(layer, Wrapper):
75 | layer_name = '{}({})'.format(layer_name, layer.layer.name)
76 | child_class_name = layer.layer.__class__.__name__
77 | class_name = '{}({})'.format(class_name, child_class_name)
78 |
79 | # Create node's label.
80 | if show_layer_names:
81 | label = '{}: {}'.format(layer_name, class_name)
82 | else:
83 | label = class_name
84 |
85 | # Rebuild the label as a table including input/output shapes.
86 | if show_shapes:
87 | try:
88 | outputlabels = str(layer.output_shape)
89 | except AttributeError:
90 | outputlabels = 'multiple'
91 | if hasattr(layer, 'input_shape'):
92 | inputlabels = str(layer.input_shape)
93 | elif hasattr(layer, 'input_shapes'):
94 | inputlabels = ', '.join(
95 | [str(ishape) for ishape in layer.input_shapes])
96 | else:
97 | inputlabels = 'multiple'
98 | label = '%s\n|{input:|output:}|{{%s}|{%s}}' % (label,
99 | inputlabels,
100 | outputlabels)
101 | node = pydot.Node(layer_id, label=label)
102 | dot.add_node(node)
103 |
104 | # Connect nodes with edges.
105 | for layer in layers:
106 | layer_id = str(id(layer))
107 | for i, node in enumerate(layer._inbound_nodes):
108 | node_key = layer.name + '_ib-' + str(i)
109 | if node_key in model._network_nodes:
110 | for inbound_layer in node.inbound_layers:
111 | inbound_layer_id = str(id(inbound_layer))
112 | layer_id = str(id(layer))
113 | dot.add_edge(pydot.Edge(inbound_layer_id, layer_id))
114 | return dot
115 |
116 |
117 | def plot_model(model,
118 | to_file='model.png',
119 | show_shapes=False,
120 | show_layer_names=True,
121 | rankdir='TB'):
122 | """Converts a Keras model to dot format and save to a file.
123 |
124 | # Arguments
125 | model: A Keras model instance
126 | to_file: File name of the plot image.
127 | show_shapes: whether to display shape information.
128 | show_layer_names: whether to display layer names.
129 | rankdir: `rankdir` argument passed to PyDot,
130 | a string specifying the format of the plot:
131 | 'TB' creates a vertical plot;
132 | 'LR' creates a horizontal plot.
133 | """
134 | dot = model_to_dot(model, show_shapes, show_layer_names, rankdir)
135 | _, extension = os.path.splitext(to_file)
136 | if not extension:
137 | extension = 'png'
138 | else:
139 | extension = extension[1:]
140 | dot.write(to_file, format=extension)
141 |
--------------------------------------------------------------------------------