├── .coveragerc ├── .github ├── ISSUE_TEMPLATE │ ├── a--tensorflow-backend-users.md │ ├── b--theano-backend-users.md │ └── c--cntk-backend-users.md └── stale.yml ├── .gitignore ├── .travis.yml ├── CONTRIBUTING.md ├── ISSUE_TEMPLATE.md ├── LICENSE ├── MANIFEST.in ├── PULL_REQUEST_TEMPLATE.md ├── README.md ├── benchmark ├── README.md ├── __init__.py ├── benchmark_result │ ├── CNN_result.md │ ├── MemoryConsumption.png │ ├── RNN_result.md │ ├── cnn_inference_speed.png │ ├── lstm_Nietzsche_128.png │ ├── lstm_Nietzsche_32.png │ ├── lstm_Synthetic_128.png │ ├── lstm_Synthetic_32.png │ ├── lstm_Wikitext2_128.png │ ├── lstm_Wikitext2_32.png │ └── mxnet_backend_training_speed.png ├── scripts │ ├── __init__.py │ ├── benchmark_resnet.py │ ├── config.json │ ├── data_generator.py │ ├── logging_metrics.py │ ├── models │ │ ├── __init__.py │ │ ├── dataset_utils.py │ │ ├── lstm_synthetic.py │ │ ├── lstm_text_generation.py │ │ ├── model_config.py │ │ ├── resnet.py │ │ ├── resnet50_benchmark.py │ │ ├── resnet50_benchmark_tf_keras.py │ │ └── timehistory.py │ ├── run_benchmark.py │ ├── run_mxnet_backend.sh │ ├── run_tf_backend.sh │ └── run_tf_keras_backend.sh └── sparse │ └── linear_regression │ ├── inference_benchmark.md │ ├── keras_sparse_model.py │ ├── run_sparse_benchmark.py │ └── training_benchmark.md ├── docker ├── Dockerfile ├── Makefile ├── README.md └── theanorc ├── docs ├── README.md ├── __init__.py ├── autogen.py ├── mkdocs.yml ├── mxnet_backend │ ├── README.md │ ├── inference_with_aws_eia.md │ ├── installation.md │ ├── multi_gpu_training.md │ ├── performance_guide.md │ ├── save_mxnet_model.md │ ├── using_rnn_with_mxnet_backend.md │ └── using_sparse_data_with_mxnet_backend.md └── templates │ ├── activations.md │ ├── applications.md │ ├── backend.md │ ├── callbacks.md │ ├── constraints.md │ ├── datasets.md │ ├── getting-started │ ├── faq.md │ ├── functional-api-guide.md │ └── sequential-model-guide.md │ ├── index.md │ ├── initializers.md │ ├── layers │ ├── about-keras-layers.md │ └── writing-your-own-keras-layers.md │ ├── losses.md │ ├── metrics.md │ ├── models │ ├── about-keras-models.md │ ├── model.md │ └── sequential.md │ ├── optimizers.md │ ├── preprocessing │ ├── image.md │ └── text.md │ ├── regularizers.md │ ├── scikit-learn-api.md │ ├── visualization.md │ └── why-use-keras.md ├── examples ├── README.md ├── addition_rnn.py ├── antirectifier.py ├── babi_memnn.py ├── babi_rnn.py ├── cifar10_cnn.py ├── cifar10_cnn_capsule.py ├── cifar10_cnn_tfaugment2d.py ├── cifar10_mobilenet.py ├── cifar10_resnet.py ├── cifar10_resnet_multi_gpu.py ├── conv_filter_visualization.py ├── conv_lstm.py ├── deep_dream.py ├── image_ocr.py ├── imdb_bidirectional_lstm.py ├── imdb_cnn.py ├── imdb_cnn_lstm.py ├── imdb_fasttext.py ├── imdb_lstm.py ├── lstm_seq2seq.py ├── lstm_seq2seq_restore.py ├── lstm_stateful.py ├── lstm_text_generation.py ├── mnist_acgan.py ├── mnist_cnn.py ├── mnist_dataset_api.py ├── mnist_denoising_autoencoder.py ├── mnist_hierarchical_rnn.py ├── mnist_irnn.py ├── mnist_mlp.py ├── mnist_net2net.py ├── mnist_siamese.py ├── mnist_sklearn_wrapper.py ├── mnist_swwae.py ├── mnist_tfrecord.py ├── mnist_transfer_cnn.py ├── multi_hot_sparse_categorical_crossentropy.py ├── neural_doodle.py ├── neural_style_transfer.py ├── pretrained_word_embeddings.py ├── reuters_mlp.py ├── reuters_mlp_relu_vs_selu.py ├── tensorboard_embeddings_mnist.py ├── variational_autoencoder.py └── variational_autoencoder_deconv.py ├── keras ├── __init__.py ├── activations.py ├── applications │ ├── __init__.py │ ├── densenet.py │ ├── imagenet_utils.py │ ├── inception_resnet_v2.py │ ├── inception_v3.py │ ├── mobilenet.py │ ├── mobilenet_v2.py │ ├── mobilenetv2.py │ ├── nasnet.py │ ├── resnet50.py │ ├── vgg16.py │ ├── vgg19.py │ └── xception.py ├── backend │ ├── __init__.py │ ├── cntk_backend.py │ ├── common.py │ ├── mxnet_backend.py │ ├── tensorflow_backend.py │ └── theano_backend.py ├── callbacks.py ├── constraints.py ├── datasets │ ├── __init__.py │ ├── boston_housing.py │ ├── cifar.py │ ├── cifar10.py │ ├── cifar100.py │ ├── fashion_mnist.py │ ├── imdb.py │ ├── mnist.py │ └── reuters.py ├── engine │ ├── __init__.py │ ├── base_layer.py │ ├── input_layer.py │ ├── network.py │ ├── saving.py │ ├── sequential.py │ ├── topology.py │ ├── training.py │ ├── training_arrays.py │ ├── training_generator.py │ └── training_utils.py ├── initializers.py ├── layers │ ├── __init__.py │ ├── advanced_activations.py │ ├── convolutional.py │ ├── convolutional_recurrent.py │ ├── core.py │ ├── cudnn_recurrent.py │ ├── embeddings.py │ ├── local.py │ ├── merge.py │ ├── noise.py │ ├── normalization.py │ ├── pooling.py │ ├── recurrent.py │ └── wrappers.py ├── legacy │ ├── __init__.py │ ├── interfaces.py │ └── layers.py ├── losses.py ├── metrics.py ├── models.py ├── objectives.py ├── optimizers.py ├── preprocessing │ ├── __init__.py │ ├── image.py │ ├── sequence.py │ └── text.py ├── regularizers.py ├── utils │ ├── __init__.py │ ├── conv_utils.py │ ├── data_utils.py │ ├── generic_utils.py │ ├── io_utils.py │ ├── layer_utils.py │ ├── multi_gpu_utils.py │ ├── np_utils.py │ ├── test_utils.py │ └── vis_utils.py └── wrappers │ ├── __init__.py │ └── scikit_learn.py ├── keras_mxnet_ci ├── nightly-buildspec-python2.yml ├── nightly-buildspec-python3.yml ├── pr-buildspec-python2.yml └── pr-buildspec-python3.yml ├── pytest.ini ├── setup.cfg ├── setup.py └── tests ├── conftest.py ├── integration_tests ├── applications_test.py ├── imagenet_utils_test.py ├── preprocessing │ ├── image_test.py │ ├── sequence_test.py │ └── text_test.py ├── test_datasets.py ├── test_eia_integration.py ├── test_image_data_tasks.py ├── test_temporal_data_tasks.py ├── test_tensorflow_integration.py └── test_vector_data_tasks.py ├── keras ├── activations_test.py ├── backend │ ├── backend_test.py │ ├── mxnet_context_test.py │ ├── mxnet_operator_test.py │ ├── mxnet_sparse_test.py │ ├── mxnet_tf_model_test.py │ └── reference_operations.py ├── constraints_test.py ├── engine │ ├── test_topology.py │ └── test_training.py ├── initializers_test.py ├── layers │ ├── advanced_activations_test.py │ ├── convolutional_recurrent_test.py │ ├── convolutional_test.py │ ├── core_test.py │ ├── cudnn_recurrent_test.py │ ├── embeddings_test.py │ ├── local_test.py │ ├── merge_test.py │ ├── noise_test.py │ ├── normalization_test.py │ ├── pooling_test.py │ ├── recurrent_test.py │ └── wrappers_test.py ├── legacy │ ├── interface_test.py │ └── layers_test.py ├── losses_test.py ├── metrics_test.py ├── optimizers_test.py ├── regularizers_test.py ├── test_callbacks.py ├── test_sequential_model.py ├── utils │ ├── conv_utils_test.py │ ├── data_utils_test.py │ ├── generic_utils_test.py │ ├── io_utils_test.py │ ├── layer_utils_test.py │ ├── multi_gpu_test.py │ ├── np_utils_test.py │ └── vis_utils_test.py └── wrappers │ └── scikit_learn_test.py ├── test_doc_auto_generation.py ├── test_documentation.py ├── test_dynamic_trainability.py ├── test_loss_masking.py ├── test_loss_weighting.py ├── test_model_pickling.py ├── test_model_saving.py └── test_multiprocessing.py /.coveragerc: -------------------------------------------------------------------------------- 1 | [report] 2 | # Regexes for lines to exclude from consideration 3 | exclude_lines = 4 | pragma: no cover 5 | os.remove 6 | except ImportError 7 | # Don't complain if tests don't hit defensive assertion code: 8 | raise ImportError 9 | raise NotImplementedError 10 | 11 | # Don't complain if legacy support codes are not performed: 12 | if original_keras_version == '1': 13 | 14 | fail_under = 85 15 | show_missing = True 16 | omit = 17 | keras/applications/* 18 | keras/preprocessing/* 19 | keras/datasets/* 20 | keras/layers/cudnn_recurrent.py 21 | keras/legacy/* 22 | keras/utils/multi_gpu_utils.py 23 | -------------------------------------------------------------------------------- /.github/ISSUE_TEMPLATE/a--tensorflow-backend-users.md: -------------------------------------------------------------------------------- 1 | --- 2 | name: a) TensorFlow backend users 3 | about: Select this is you're using Keras with the TensorFlow backend (default). 4 | 5 | --- 6 | 7 | Please make sure that the boxes below are checked before you submit your issue. 8 | If your issue is an **implementation question**, please ask your question on [StackOverflow](http://stackoverflow.com/questions/tagged/keras) or [on the Keras Slack channel](https://keras-slack-autojoin.herokuapp.com/) instead of opening a GitHub issue. 9 | 10 | Thank you! 11 | 12 | - [ ] Check that you are up-to-date with the master branch of Keras. You can update with: 13 | `pip install git+git://github.com/keras-team/keras.git --upgrade --no-deps` 14 | 15 | - [ ] Check that your version of TensorFlow is up-to-date. The installation instructions can be found [here](https://www.tensorflow.org/get_started/os_setup). 16 | 17 | - [ ] 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). 18 | -------------------------------------------------------------------------------- /.github/ISSUE_TEMPLATE/b--theano-backend-users.md: -------------------------------------------------------------------------------- 1 | --- 2 | name: b) Theano backend users 3 | about: Select this if you're using Keras with the Theano backend. 4 | 5 | --- 6 | 7 | Please make sure that the boxes below are checked before you submit your issue. 8 | If your issue is an **implementation question**, please ask your question on [StackOverflow](http://stackoverflow.com/questions/tagged/keras) or [on the Keras Slack channel](https://keras-slack-autojoin.herokuapp.com/) instead of opening a GitHub issue. 9 | 10 | Thank you! 11 | 12 | - [ ] Check that you are up-to-date with the master branch of Keras. You can update with: 13 | `pip install git+git://github.com/keras-team/keras.git --upgrade --no-deps` 14 | 15 | - [ ] Check that you are up-to-date with the master branch of Theano. You can update with: 16 | `pip install git+git://github.com/Theano/Theano.git --upgrade --no-deps` 17 | 18 | - [ ] 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). 19 | -------------------------------------------------------------------------------- /.github/ISSUE_TEMPLATE/c--cntk-backend-users.md: -------------------------------------------------------------------------------- 1 | --- 2 | name: c) CNTK backend users 3 | about: Select this if you're using Keras with the CNTK backend. 4 | 5 | --- 6 | 7 | Please make sure that the boxes below are checked before you submit your issue. 8 | If your issue is an **implementation question**, please ask your question on [StackOverflow](http://stackoverflow.com/questions/tagged/keras) or [on the Keras Slack channel](https://keras-slack-autojoin.herokuapp.com/) instead of opening a GitHub issue. 9 | 10 | Thank you! 11 | 12 | - [ ] Check that you are up-to-date with the master branch of Keras. You can update with: 13 | `pip install git+git://github.com/keras-team/keras.git --upgrade --no-deps` 14 | 15 | - [ ] Check that your version of CNTK is up-to-date. 16 | 17 | - [ ] 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). 18 | -------------------------------------------------------------------------------- /.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 | -------------------------------------------------------------------------------- /.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 | -------------------------------------------------------------------------------- /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/awslabs/keras-apache-mxnet.git --upgrade --no-deps 7 | 8 | - [ ] If running on MXNet, check that you are up-to-date with the latest version. The installation 9 | instructions can be found [here](http://mxnet.incubator.apache.org/install/index.html?platform=Linux&language=Python&processor=CPU) 10 | - [ ] 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). 11 | 12 | - [ ] If running on Theano, check that you are up-to-date with the master branch of Theano. You can update with: 13 | pip install git+git://github.com/Theano/Theano.git --upgrade --no-deps 14 | 15 | - [ ] 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). 16 | -------------------------------------------------------------------------------- /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 contributions by Amazon: 16 | Copyright (c) 2017 Amazon.com, Inc. or its affiliates 17 | All rights reserved. 18 | 19 | All other contributions: 20 | Copyright (c) 2015 - 2018, the respective contributors. 21 | All rights reserved. 22 | 23 | Each contributor holds copyright over their respective contributions. 24 | The project versioning (Git) records all such contribution source information. 25 | 26 | LICENSE 27 | 28 | The MIT License (MIT) 29 | 30 | Permission is hereby granted, free of charge, to any person obtaining a copy 31 | of this software and associated documentation files (the "Software"), to deal 32 | in the Software without restriction, including without limitation the rights 33 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 34 | copies of the Software, and to permit persons to whom the Software is 35 | furnished to do so, subject to the following conditions: 36 | 37 | The above copyright notice and this permission notice shall be included in all 38 | copies or substantial portions of the Software. 39 | 40 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 41 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 42 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 43 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 44 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 45 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 46 | SOFTWARE. 47 | 48 | -------------------------------------------------------------------------------- /MANIFEST.in: -------------------------------------------------------------------------------- 1 | include LICENSE 2 | include README.md 3 | include CONTRIBUTING.md 4 | graft docs 5 | graft examples 6 | graft tests 7 | -------------------------------------------------------------------------------- /PULL_REQUEST_TEMPLATE.md: -------------------------------------------------------------------------------- 1 | ### Summary 2 | 3 | ### Related Issues 4 | 5 | ### PR Overview 6 | 7 | - [ ] This PR requires new unit tests [y/n] (make sure tests are included) 8 | - [ ] This PR requires to update the documentation [y/n] (make sure the docs are up-to-date) 9 | - [ ] This PR is backwards compatible [y/n] 10 | - [ ] This PR changes the current API [y/n] 11 | -------------------------------------------------------------------------------- /benchmark/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/awslabs/keras-apache-mxnet/5497ebd50a45ccc446b8944ebbe11fb7721a5533/benchmark/__init__.py -------------------------------------------------------------------------------- /benchmark/benchmark_result/MemoryConsumption.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/awslabs/keras-apache-mxnet/5497ebd50a45ccc446b8944ebbe11fb7721a5533/benchmark/benchmark_result/MemoryConsumption.png -------------------------------------------------------------------------------- /benchmark/benchmark_result/cnn_inference_speed.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/awslabs/keras-apache-mxnet/5497ebd50a45ccc446b8944ebbe11fb7721a5533/benchmark/benchmark_result/cnn_inference_speed.png -------------------------------------------------------------------------------- /benchmark/benchmark_result/lstm_Nietzsche_128.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/awslabs/keras-apache-mxnet/5497ebd50a45ccc446b8944ebbe11fb7721a5533/benchmark/benchmark_result/lstm_Nietzsche_128.png -------------------------------------------------------------------------------- /benchmark/benchmark_result/lstm_Nietzsche_32.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/awslabs/keras-apache-mxnet/5497ebd50a45ccc446b8944ebbe11fb7721a5533/benchmark/benchmark_result/lstm_Nietzsche_32.png -------------------------------------------------------------------------------- /benchmark/benchmark_result/lstm_Synthetic_128.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/awslabs/keras-apache-mxnet/5497ebd50a45ccc446b8944ebbe11fb7721a5533/benchmark/benchmark_result/lstm_Synthetic_128.png -------------------------------------------------------------------------------- /benchmark/benchmark_result/lstm_Synthetic_32.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/awslabs/keras-apache-mxnet/5497ebd50a45ccc446b8944ebbe11fb7721a5533/benchmark/benchmark_result/lstm_Synthetic_32.png -------------------------------------------------------------------------------- /benchmark/benchmark_result/lstm_Wikitext2_128.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/awslabs/keras-apache-mxnet/5497ebd50a45ccc446b8944ebbe11fb7721a5533/benchmark/benchmark_result/lstm_Wikitext2_128.png -------------------------------------------------------------------------------- /benchmark/benchmark_result/lstm_Wikitext2_32.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/awslabs/keras-apache-mxnet/5497ebd50a45ccc446b8944ebbe11fb7721a5533/benchmark/benchmark_result/lstm_Wikitext2_32.png -------------------------------------------------------------------------------- /benchmark/benchmark_result/mxnet_backend_training_speed.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/awslabs/keras-apache-mxnet/5497ebd50a45ccc446b8944ebbe11fb7721a5533/benchmark/benchmark_result/mxnet_backend_training_speed.png -------------------------------------------------------------------------------- /benchmark/scripts/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/awslabs/keras-apache-mxnet/5497ebd50a45ccc446b8944ebbe11fb7721a5533/benchmark/scripts/__init__.py -------------------------------------------------------------------------------- /benchmark/scripts/config.json: -------------------------------------------------------------------------------- 1 | { 2 | "cpu_config": { 3 | "cpu_num_cores": 32, 4 | "cpu_memory": 244, 5 | "cpu_memory_info": "GB", 6 | "platform_type": "AWS", 7 | "platform_machine_type": "p3.8xlarge", 8 | "gpus": 0, 9 | "gpu_platform": "NVIDIA Tesla V100" 10 | }, 11 | "gpu_config": { 12 | "cpu_num_cores": 32, 13 | "cpu_memory": 244, 14 | "cpu_memory_info": "GB", 15 | "platform_type": "AWS", 16 | "platform_machine_type": "p3.8xlarge", 17 | "gpus": 1, 18 | "gpu_platform": "NVIDIA Tesla V100" 19 | }, 20 | "4_gpu_config": { 21 | "cpu_num_cores": 32, 22 | "cpu_memory": 244, 23 | "cpu_memory_info": "GB", 24 | "platform_type": "AWS", 25 | "platform_machine_type": "p3.8xlarge", 26 | "gpus": 4, 27 | "gpu_platform": "NVIDIA Tesla V100" 28 | }, 29 | "8_gpu_config": { 30 | "cpu_num_cores": 64, 31 | "cpu_memory": 488, 32 | "cpu_memory_info": "GB", 33 | "platform_type": "AWS", 34 | "platform_machine_type": "p3.16xlarge", 35 | "gpus": 8, 36 | "gpu_platform": "NVIDIA Tesla V100" 37 | } 38 | } 39 | 40 | -------------------------------------------------------------------------------- /benchmark/scripts/data_generator.py: -------------------------------------------------------------------------------- 1 | """Random Data generator script 2 | Credit: 3 | Script Copied from TensorFlow Benchmark repo: 4 | https://github.com/tensorflow/benchmarks/blob/keras-benchmarks/scripts/keras_benchmarks/data_generator.py 5 | """ 6 | 7 | """ Generates input and label data for training models. """ 8 | import numpy as np 9 | 10 | 11 | def generate_img_input_data(input_shape, num_classes): 12 | """Generates training data and target labels. 13 | 14 | # Arguments 15 | input_shape: input shape in the following format 16 | (num_samples,channels, x, y) 17 | num_classes: number of classes that we want to classify the input 18 | 19 | # Returns 20 | numpy arrays: 'x_train, y_train' 21 | """ 22 | x_train = np.random.randint(0, 255, input_shape) 23 | y_train = np.random.randint(0, num_classes, (input_shape[0],)) 24 | 25 | return x_train, y_train 26 | 27 | 28 | def generate_text_input_data(input_shape): 29 | """Generates training data and target labels. 30 | 31 | # Arguments 32 | input_shape: input shape in the following format(num_samples, x, y) 33 | 34 | # Returns 35 | numpy arrays: 'x_train, y_train' where the value of the arrays are booleans. 36 | """ 37 | x_train = np.random.choice([True, False], input_shape) 38 | y_train = np.random.choice([True, False], (input_shape[0], input_shape[2])) 39 | 40 | return x_train, y_train 41 | -------------------------------------------------------------------------------- /benchmark/scripts/logging_metrics.py: -------------------------------------------------------------------------------- 1 | import keras 2 | 3 | 4 | class LoggingMetrics: 5 | """Callback that save metrics to a logfile. 6 | 7 | # Arguments 8 | history_callback: instance of `keras.callbacks.History`. 9 | Training parameters 10 | (eg. batch size, number of epochs, loss, acc). 11 | time_callback: instance of `keras.callbacks.Callback`. 12 | Training parameters 13 | (eg. time, time-step, speed). 14 | 15 | # Raises 16 | TypeError: In case of invalid object instance. 17 | """ 18 | 19 | def __init__(self, history_callback, time_callback): 20 | self.num_iteration = None 21 | self.metric_list = [] 22 | self.pattern_list = [] 23 | self.retrieve_metrics(history_callback, time_callback) 24 | 25 | def retrieve_metrics(self, history_callback, time_callback): 26 | if not isinstance(history_callback, keras.callbacks.History): 27 | raise TypeError('`history_callback` should be an instance of ' 28 | '`keras.callbacks.History`') 29 | if not isinstance(time_callback, keras.callbacks.Callback): 30 | raise TypeError('`time_callback` should be an instance of ' 31 | '`keras.callbacks.Callback`') 32 | 33 | if hasattr(history_callback, 'epoch'): 34 | self.metric_list.append(history_callback.epoch) 35 | self.pattern_list.append('[Epoch %d]\t') 36 | 37 | if hasattr(time_callback, 'times'): 38 | self.metric_list.append(time_callback.get_time()) 39 | self.metric_list.append(time_callback.get_time_step()) 40 | self.metric_list.append(time_callback.get_speed()) 41 | self.pattern_list.append('time: %s\t') 42 | self.pattern_list.append('time_step: %s\t') 43 | self.pattern_list.append('speed: %s\t') 44 | 45 | if 'loss' in history_callback.history: 46 | self.metric_list.append(history_callback.history['loss']) 47 | self.pattern_list.append('train_loss: %.4f\t') 48 | 49 | if 'acc' in history_callback.history: 50 | self.metric_list.append(history_callback.history['acc']) 51 | self.pattern_list.append('train_acc: %.4f\t') 52 | 53 | if 'val_loss' in history_callback.history: 54 | self.metric_list.append(history_callback.history['val_loss']) 55 | self.pattern_list.append('val_loss: %.4f\t') 56 | 57 | if 'val_acc' in history_callback.history: 58 | self.metric_list.append(history_callback.history['val_acc']) 59 | self.pattern_list.append('val_acc: %.4f\t') 60 | 61 | self.num_iteration = history_callback.params['epochs'] 62 | 63 | def get_metrics_index(self, idx): 64 | idx_metric_list = [] 65 | for metric in self.metric_list: 66 | idx_metric_list.append(metric[idx]) 67 | return tuple(idx_metric_list) 68 | 69 | def save_metrics_to_log(self, logging): 70 | pattern_str = ''.join(self.pattern_list) 71 | for i in range(self.num_iteration): 72 | logging.info(pattern_str % self.get_metrics_index(i)) 73 | -------------------------------------------------------------------------------- /benchmark/scripts/models/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/awslabs/keras-apache-mxnet/5497ebd50a45ccc446b8944ebbe11fb7721a5533/benchmark/scripts/models/__init__.py -------------------------------------------------------------------------------- /benchmark/scripts/models/dataset_utils.py: -------------------------------------------------------------------------------- 1 | """Dataset Utilities used in benchmark script 2 | """ 3 | 4 | import re 5 | import io 6 | import os 7 | 8 | from keras.utils.data_utils import get_file 9 | 10 | 11 | def get_dataset(dataset_name): 12 | """ 13 | Download and read the dataset into a string 14 | 15 | # Arguments 16 | dataset_name: dataset type 17 | 18 | # Returns 19 | A text string 20 | """ 21 | if dataset_name == 'nietzsche': 22 | path = get_file( 23 | 'nietzsche.txt', 24 | origin='https://s3.amazonaws.com/text-datasets/nietzsche.txt') 25 | with io.open(path, encoding='utf-8') as f: 26 | text = f.read().lower() 27 | return text 28 | elif dataset_name == 'wikitext2': 29 | path = get_file(fname='wikitext-2-raw.zip', extract=True, 30 | archive_format='zip', 31 | origin='https://s3.amazonaws.com/research.metamind.io/wikitext/wikitext-2-raw-v1.zip') 32 | path = path.split('.zip')[0] 33 | path = os.path.join(path, 'wiki.train.raw') 34 | text = '' 35 | with io.open(path, encoding='utf-8') as f: 36 | for line in f: 37 | line = " ".join(re.findall("[a-zA-Z0-9]+", line)) 38 | text += line.lower() 39 | return text 40 | else: 41 | raise ValueError('Only support `nietzsche` and `wikitext2` dataset. ' 42 | 'Please add the functionality here for your own ' 43 | 'dataset') 44 | -------------------------------------------------------------------------------- /benchmark/scripts/models/model_config.py: -------------------------------------------------------------------------------- 1 | """Model config 2 | Credit: 3 | Script modified from TensorFlow Benchmark repo: 4 | https://github.com/tensorflow/benchmarks/blob/keras-benchmarks/scripts/keras_benchmarks/models/model_config.py 5 | """ 6 | from models import resnet50_benchmark 7 | from models import resnet50_benchmark_tf_keras 8 | from models import lstm_synthetic 9 | from models import lstm_text_generation 10 | 11 | 12 | def get_model_config(model_name): 13 | if model_name == 'resnet50': 14 | return resnet50_benchmark.Resnet50Benchmark() 15 | 16 | if model_name == 'resnet50_tf_keras': 17 | return resnet50_benchmark_tf_keras.Resnet50Benchmark() 18 | 19 | if model_name == 'lstm_synthetic': 20 | return lstm_synthetic.LstmBenchmark() 21 | 22 | if model_name == 'lstm_nietzsche': 23 | return lstm_text_generation.LstmBenchmark('nietzsche') 24 | 25 | if model_name == 'lstm_wikitext2': 26 | return lstm_text_generation.LstmBenchmark('wikitext2') 27 | -------------------------------------------------------------------------------- /benchmark/scripts/models/resnet50_benchmark_tf_keras.py: -------------------------------------------------------------------------------- 1 | """Benchmark a Resnet50 model 2 | Credit: 3 | Script modified from TensorFlow Benchmark repo: 4 | https://github.com/tensorflow/benchmarks/blob/keras-benchmarks/scripts/keras_benchmarks/models/resnet50_benchmark.py 5 | """ 6 | 7 | from __future__ import print_function 8 | 9 | import keras 10 | 11 | from models import timehistory 12 | 13 | if keras.backend.backend() == 'tensorflow': 14 | import tensorflow as tf 15 | import tensorflow.contrib.eager as tfe 16 | import numpy as np 17 | 18 | 19 | def crossentropy_from_logits(y_true, y_pred): 20 | return tf.keras.backend.categorical_crossentropy(target=y_true, 21 | output=y_pred, 22 | from_logits=True) 23 | 24 | 25 | def device_and_data_format(): 26 | return ('/gpu:0', 'channels_first') if tfe.num_gpus() else ('/cpu:0', 27 | 'channels_last') 28 | 29 | 30 | class Resnet50Benchmark: 31 | 32 | def __init__(self): 33 | self.test_name = "resnet50_tf_keras" 34 | self.sample_type = "images" 35 | self.total_time = 0 36 | self.batch_size = 32 37 | self.epochs = 20 38 | self.num_samples = 1000 39 | self.test_type = 'tf.keras, eager_mode' 40 | 41 | def run_benchmark(self, gpus=0, inference=False, use_dataset_tensors=False, epochs=20): 42 | self.epochs = epochs 43 | print("Running model ", self.test_name) 44 | # tfe.enable_eager_execution() 45 | tf.keras.backend.set_learning_phase(True) 46 | 47 | input_shape = (self.num_samples, 3, 256, 256) 48 | num_classes = 1000 49 | 50 | x_train = np.random.randint(0, 255, input_shape) 51 | y_train = np.random.randint(0, num_classes, (input_shape[0],)) 52 | y_train = tf.keras.utils.to_categorical(y_train, num_classes) 53 | 54 | if tf.keras.backend.backend() == "tensorflow" and gpus >= 1: 55 | tf.keras.backend.set_image_data_format('channels_first') 56 | 57 | if tf.keras.backend.image_data_format() == 'channels_last': 58 | x_train = x_train.transpose(0, 2, 3, 1) 59 | print("data format is ", keras.backend.image_data_format()) 60 | x_train /= 255 61 | x_train = x_train.astype('float32') 62 | y_train = y_train.astype('float32') 63 | 64 | device, data_format = device_and_data_format() 65 | print(device) 66 | print(data_format) 67 | with tf.device(device): 68 | inputs = tf.keras.layers.Input(shape=(3, 256, 256)) 69 | outputs = tf.keras.applications.ResNet50(include_top=False, 70 | pooling='avg', 71 | weights=None)(inputs) 72 | predictions = tf.keras.layers.Dense(num_classes)(outputs) 73 | model = tf.keras.models.Model(inputs, predictions) 74 | model.compile(loss='categorical_crossentropy', 75 | optimizer=tf.train.RMSPropOptimizer(learning_rate=0.0001), 76 | metrics=['accuracy']) 77 | time_callback = timehistory.TimeHistory() 78 | model.fit(x_train, y_train, batch_size=self.batch_size, epochs=self.epochs, 79 | shuffle=True, callbacks=[time_callback]) 80 | 81 | self.total_time = 0 82 | print(time_callback.times) 83 | for i in range(1, self.epochs): 84 | self.total_time += time_callback.times[i] 85 | 86 | if tf.keras.backend.backend() == "tensorflow": 87 | tf.keras.backend.clear_session() 88 | -------------------------------------------------------------------------------- /benchmark/scripts/models/timehistory.py: -------------------------------------------------------------------------------- 1 | """ Utility class for accessing the first epoch time interval. 2 | Credit: 3 | Script modified from TensorFlow Benchmark repo: 4 | https://github.com/tensorflow/benchmarks/blob/keras-benchmarks/scripts/keras_benchmarks/models/timehistory.py 5 | """ 6 | import keras 7 | import time 8 | 9 | 10 | class TimeHistory(keras.callbacks.Callback): 11 | """Callback that extract execution time of every epoch, time-step, 12 | and speed in terms of sample per sec 13 | """ 14 | 15 | def __init__(self): 16 | super(TimeHistory, self).__init__() 17 | self.times = [] 18 | 19 | def on_train_begin(self, logs=None): 20 | self.times = [] 21 | 22 | def on_epoch_begin(self, batch, logs=None): 23 | self.epoch_time_start = time.time() 24 | 25 | def on_epoch_end(self, batch, logs=None): 26 | self.times.append(time.time() - self.epoch_time_start) 27 | 28 | def get_num_samples(self): 29 | if 'samples' in self.params: 30 | return self.params['samples'] 31 | elif 'steps' in self.params: 32 | return self.params['steps'] 33 | else: 34 | raise ValueError('Incorrect metric parameter') 35 | 36 | def __reformat(self, var): 37 | if var >= 1: 38 | var = '%.2f ' % var 39 | time_format = 'sec' 40 | elif var >= 1e-3: 41 | var = '%.2f ' % (var * 1e3) 42 | time_format = 'msec' 43 | else: 44 | var = '%.2f ' % (var * 1e6) 45 | time_format = 'usec' 46 | return var, time_format 47 | 48 | def get_time_step(self): 49 | time_list = [] 50 | num_samples = self.get_num_samples() 51 | for t in self.times: 52 | speed = t / num_samples 53 | speed, time_format = self.__reformat(speed) 54 | time_list.append(speed + time_format + '/step') 55 | return time_list 56 | 57 | def get_total_time(self): 58 | total_time = sum(self.times) 59 | total_time, time_format = self.__reformat(total_time) 60 | return total_time + time_format 61 | 62 | def get_time(self): 63 | time_list = [] 64 | for t in self.times: 65 | time, time_format = self.__reformat(t) 66 | time_list.append(time + time_format) 67 | return time_list 68 | 69 | def get_speed(self): 70 | samples_list = [] 71 | num_samples = self.get_num_samples() 72 | for t in self.times: 73 | sample_sec = num_samples / t 74 | sample_sec, time_format = self.__reformat(sample_sec) 75 | samples_list.append(sample_sec + 'samples/' + time_format) 76 | return samples_list 77 | -------------------------------------------------------------------------------- /benchmark/scripts/run_benchmark.py: -------------------------------------------------------------------------------- 1 | """ Main entry point for running benchmarks with different Keras backends. 2 | Credit: 3 | Script modified from TensorFlow Benchmark repo: 4 | https://github.com/tensorflow/benchmarks/blob/keras-benchmarks/scripts/keras_benchmarks/run_benchmark.py 5 | """ 6 | 7 | import argparse 8 | import json 9 | import keras 10 | import sys 11 | from models import model_config 12 | 13 | if keras.backend.backend() == "tensorflow": 14 | import tensorflow as tf 15 | if keras.backend.backend() == "mxnet": 16 | import mxnet 17 | 18 | parser = argparse.ArgumentParser() 19 | parser.add_argument('--pwd', 20 | help='The benchmark scripts dir') 21 | parser.add_argument('--inference', 22 | help='Benchmark inference only, use True or False') 23 | parser.add_argument('--mode', 24 | help='The benchmark can be run on cpu, gpu and multiple gpus.') 25 | parser.add_argument('--model_name', 26 | help='The name of the model that will be benchmarked.') 27 | parser.add_argument('--dry_run', type=bool, 28 | help='Flag to output metrics to the console instead of ' 29 | 'uploading metrics to BigQuery. This is useful when ' 30 | 'you are testing new models and do not want data ' 31 | 'corruption.') 32 | parser.add_argument('--epochs', default=20, 33 | help='Number of epochs') 34 | 35 | args = parser.parse_args() 36 | 37 | inference = False 38 | if args.inference: 39 | if args.inference not in ['True', 'False']: 40 | print('inference only accept True or False as parameter') 41 | sys.exit() 42 | 43 | if args.inference == 'True': 44 | inference = True 45 | 46 | if args.epochs: 47 | if not args.epochs.isdigit(): 48 | print('error: invalid int value: ', args.epochs) 49 | sys.exit() 50 | 51 | # Load the json config file for the requested mode. 52 | config_file = open(args.pwd + "/config.json", 'r') 53 | config_contents = config_file.read() 54 | config = json.loads(config_contents)[args.mode] 55 | 56 | 57 | def get_backend_version(): 58 | if keras.backend.backend() == "tensorflow": 59 | return tf.__version__ 60 | if keras.backend.backend() == "mxnet": 61 | return mxnet.__version__ 62 | return "undefined" 63 | 64 | 65 | model = model_config.get_model_config(args.model_name) 66 | 67 | use_dataset_tensors = False 68 | if args.epochs: 69 | model.run_benchmark(gpus=config['gpus'], inference=inference, 70 | use_dataset_tensors=use_dataset_tensors, 71 | epochs=int(args.epochs)) 72 | else: 73 | model.run_benchmark(gpus=config['gpus'], inference=inference, 74 | use_dataset_tensors=use_dataset_tensors) 75 | if args.dry_run: 76 | print("Model :total_time", model.test_name, model.total_time) 77 | -------------------------------------------------------------------------------- /benchmark/scripts/run_mxnet_backend.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | # Keras MXNet Backend 4 | # Credit: 5 | # Script modified from TensoFlow Benchmark repo: 6 | # https://github.com/tensorflow/benchmarks/blob/keras-benchmarks/scripts/keras_benchmarks/run_tf_backend.sh 7 | 8 | ###################################### 9 | # 10 | # USAGE: 11 | # $ bash run_mxnet_backend.sh $mode $model $inference_mode $epochs 12 | # 13 | # SUPPORTED VALUES: 14 | # mode: cpu_config, gpu_config, 4_gpu_config, 8_gpu_config 15 | # models: resnet50, lstm_synthetic, lstm_nietzsche, lstm_wikitext2 16 | # inference_mode: True, False 17 | # epochs: Int. Ex: 20 18 | # EXAMPLE: 19 | # $ bash run_mxnet_backend.sh cpu_config lstm_synthetic False 5 20 | # 21 | ##################################### 22 | 23 | python -c "from keras import backend" 24 | KERAS_BACKEND=mxnet 25 | sed -i -e 's/"backend":[[:space:]]*"[^"]*/"backend":\ "'$KERAS_BACKEND'/g' ~/.keras/keras.json; 26 | echo -e "Running tests with the following config:\n$(cat ~/.keras/keras.json)" 27 | 28 | # Use "cpu_config", "gpu_config", "4_gpu_config", and "8_gpu_config" as command line arguments to load the right 29 | # config file. 30 | 31 | # Supported models='resnet50 resnet50_tf_keras lstm_synthetic lstm_nietzsche lstm_wikitext2' 32 | 33 | dir=`pwd` 34 | 35 | python $dir/run_benchmark.py --pwd=$dir --mode="$1" --model_name="$2" --dry_run=True --inference="$3" --epochs="$4" -------------------------------------------------------------------------------- /benchmark/scripts/run_tf_backend.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | # Keras MXNet Backend 4 | # Credit: 5 | # Script modified from TensoFlow Benchmark repo: 6 | # https://github.com/tensorflow/benchmarks/blob/keras-benchmarks/scripts/keras_benchmarks/run_tf_backend.sh 7 | 8 | ###################################### 9 | # 10 | # USAGE: 11 | # $ bash run_tf_backend.sh $mode $model $inference_mode $epochs 12 | # 13 | # SUPPORTED VALUES: 14 | # mode: cpu_config, gpu_config, 4_gpu_config, 8_gpu_config 15 | # models: resnet50, lstm_synthetic, lstm_nietzsche, lstm_wikitext2 16 | # inference_mode: True, False 17 | # epochs: Int. Ex: 20 18 | # EXAMPLE: 19 | # $ bash run_tf_backend.sh cpu_config lstm_synthetic False 5 20 | # 21 | ##################################### 22 | 23 | python -c "from keras import backend" 24 | KERAS_BACKEND=tensorflow 25 | sed -i -e 's/"backend":[[:space:]]*"[^"]*/"backend":\ "'$KERAS_BACKEND'/g' ~/.keras/keras.json; 26 | echo -e "Running tests with the following config:\n$(cat ~/.keras/keras.json)" 27 | 28 | # Use "cpu_config", "gpu_config", "4_gpu_config", and "8_gpu_config" as command line arguments to load the right 29 | # config file. 30 | 31 | # Supported models='resnet50 resnet50_tf_keras lstm_synthetic lstm_nietzsche lstm_wikitext2' 32 | 33 | dir=`pwd` 34 | 35 | python $dir/run_benchmark.py --pwd=$dir --mode="$1" --model_name="$2" --dry_run=True --inference="$3" --epochs="$4" -------------------------------------------------------------------------------- /benchmark/scripts/run_tf_keras_backend.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | # Keras Tensorflow Backend using tf.keras 4 | # Credit: 5 | # Script modified from TensoFlow Benchmark repo: 6 | # https://github.com/tensorflow/benchmarks/blob/keras-benchmarks/scripts/keras_benchmarks/run_tf_backend.sh 7 | python -c "from keras import backend" 8 | KERAS_BACKEND=tensorflow 9 | sed -i -e 's/"backend":[[:space:]]*"[^"]*/"backend":\ "'$KERAS_BACKEND'/g' ~/.keras/keras.json; 10 | echo -e "Running tests with the following config:\n$(cat ~/.keras/keras.json)" 11 | 12 | # Use "cpu_config", "gpu_config", "4_gpu_config", and "8_gpu_config" as command line arguments to load the right 13 | # config file. 14 | #models='resnet50 resnet50_tf_keras lstm_synthetic lstm_nietzsche lstm_wikitext2' 15 | models='resnet50_tf_keras' 16 | dir=`pwd` 17 | for name in $models 18 | do 19 | python $dir/run_benchmark.py --pwd=$dir --mode="$1" --model_name="$name" --dry_run=True --inference="$2" --epochs="$3" 20 | done 21 | #!/usr/bin/env bash 22 | -------------------------------------------------------------------------------- /benchmark/sparse/linear_regression/inference_benchmark.md: -------------------------------------------------------------------------------- 1 | # Linear Regression Benchmark Results 2 | 3 | ## Summary 4 | ``` 5 | Results below show the performance comparison of linear regression with MXNet vs Keras-Tensorflow using sparse tensors 6 | ``` 7 | 8 | ### Results 9 | ### Inference Benchmark 10 | #### Input samples are sparse feature vectors with 10,000 dimensions 11 | ### Configuration 12 | | Dataset | Synthetic(Randomly generated) | 13 | | :--------------- | :----------------------------------------------------------- | 14 | | Keras | v2.2.4 | 15 | | TensorFlow | v1.11.0 | 16 | | MXNet-mkl | v1.3.0 17 | 18 | #### CPU 19 | ##### Speed 20 | | Instance Type | GPUs | Batch Size | Keras-MXNet (Samples/Sec) | Keras-TensorFlow (Samples/Sec) | 21 | |-----|-----|-----|-----|-----| 22 | | C5.8XLarge | 0 | 64 | 71K | 77K 23 | | C5.8XLarge | 0 | 128 | 111K | 143K 24 | | C5.8XLarge | 0 | 256 | 166K | 200K 25 | | C5.8XLarge | 0 | 512 | 250K | 250K 26 | | C5.8XLarge | 0 | 1024 | 333K | 333K 27 | 28 | #### Memory Consumed 29 | | Instance Type | GPUs | Batch Size | Keras-MXNet (MB) | Keras-TensorFlow (MB) | 30 | |-----|-----|-----|-----|-----| 31 | | C5.8XLarge | 0 | 64 | 1630.8 | 1573.8 | 32 | | C5.8XLarge | 0 | 128 | 1574.7 | 1561.2 | 33 | | C5.8XLarge | 0 | 256 | 1477.8 | 1501.4 | 34 | | C5.8XLarge | 0 | 512 | 1407.0| 1472.5 | 35 | | C5.8XLarge | 0 | 1024 | 1336.3 | 1466.8 | 36 | 37 | #### GPU 38 | ### Configuration 39 | ##### Input samples are sparse feature vectors with 10,000 dimensions 40 | | Dataset | Synthetic(Randomly generated) | 41 | | :--------------- | :----------------------------------------------------------- | 42 | | Keras | v2.2.4 | 43 | | TensorFlow-GPU | v1.11.0 | 44 | | MXNet-cu90mkl | v1.3.0 | 45 | 46 | ##### Single GPU 47 | ##### Speed 48 | | Instance Type | GPUs | Batch Size | Keras-MXNet (Samples/Sec) | Keras-TensorFlow (Samples/Sec) | 49 | |-----|-----|-----|-----|-----| 50 | | P3.8XLarge | 1 | 64 | 33K | 50K 51 | | P3.8XLarge | 1 | 128 | 43K | 83K 52 | | P3.8XLarge | 1 | 256 | 83K | 143K 53 | | P3.8XLarge | 1 | 512 | 125K | 200K 54 | | P3.8XLarge | 1 | 1024 |250K | 250K 55 | 56 | ##### GPU Utilization 57 | | Instance Type | GPUs | Batch Size | Keras-MXNet (GPU Utilization %) | Keras-TensorFlow (GPU Utilization %) | 58 | |-----|-----|-----|-----|-----| 59 | | P3.8XLarge | 1 | 64 | 9 | 7 60 | | P3.8XLarge | 1 | 128 | 8 | 7 61 | | P3.8XLarge | 1 | 256 | 8 | 7 62 | | P3.8XLarge | 1 | 512 | 8 | 7 63 | | P3.8XLarge | 1 | 1024 | 7 | 8 64 | 65 | ##### GPU Memory Consumed 66 | | Instance Type | GPUs | Batch Size | Keras-MXNet (GPU Memory (MB)) | Keras-TensorFlow (GPU Memory (MB)) | 67 | |-----|-----|-----|-----|-----| 68 | | P3.8XLarge | 1 | 64 | 966.7 | 16135.5 69 | | P3.8XLarge | 1 | 128 | 970.9 | 16135.5 70 | | P3.8XLarge | 1 | 256 | 973.1 | 16135.5 71 | | P3.8XLarge | 1 | 512 | 994.1 | 16135.5 72 | | P3.8XLarge | 1 | 1024 | 987.7 | 16135.5 73 | 74 | ### Note 75 | Run the file as `python run_sparse_benchmark.py`, by default the benchmark runs for `training` with `10 epochs` and batch size of `512` 76 | Inference results have been rounded to the nearest 1000th value and in the above numbers, `K` represents `1000`, so `1K=1000` 77 | 78 | ### References 79 | MXNet supports sparse data in 2 NDArray formats - CSRNDArray and RowSparseNDArray which are defined in `mxnet.ndarray.sparse` package 80 | For further details on MXNet Sparse NDArray API check [documentation related to MXNet Sparse](https://mxnet.incubator.apache.org/api/python/ndarray/sparse.html) 81 | 82 | Keras Input layer supports sparse data by setting a boolean placeholder value - check document for [Keras Input layer](https://keras.io/layers/core/#input) 83 | -------------------------------------------------------------------------------- /benchmark/sparse/linear_regression/keras_sparse_model.py: -------------------------------------------------------------------------------- 1 | """ 2 | Linear Regression model with sparse synthetic data for Keras 3 | """ 4 | from __future__ import print_function 5 | 6 | import time 7 | 8 | from keras import Model 9 | from keras.layers import Dense, Input 10 | from keras.optimizers import SGD 11 | from keras import backend as K 12 | from keras.utils import multi_gpu_model 13 | 14 | 15 | def _validate_backend(): 16 | if K.backend() != 'mxnet' and K.backend() != 'tensorflow': 17 | raise NotImplementedError('This benchmark script only supports MXNet and TensorFlow backend') 18 | 19 | 20 | def run_benchmark(train_data, train_label, eval_data, eval_label, batch_size, epochs, num_gpu, mode): 21 | _validate_backend() 22 | 23 | inputs = Input(batch_shape=(None, train_data.shape[1]), dtype='float32', sparse=True) 24 | 25 | if K.backend() == 'mxnet': 26 | predictions = Dense(units=1, activation='linear', kernel_initializer='normal', sparse_weight=True)(inputs) 27 | else: 28 | predictions = Dense(units=1, activation='linear', kernel_initializer='normal')(inputs) 29 | 30 | model = Model(inputs=inputs, outputs=predictions) 31 | model.summary() 32 | 33 | sgd = SGD(lr=0.1, momentum=0.9) 34 | 35 | if num_gpu > 1: 36 | model = multi_gpu_model(model, gpus=num_gpu) 37 | 38 | model.compile(loss='mse', 39 | optimizer=sgd, 40 | metrics=['accuracy']) 41 | 42 | if mode == 'training': 43 | start = time.time() 44 | 45 | model.fit(train_data, 46 | train_label, 47 | epochs=epochs, 48 | batch_size=batch_size, 49 | verbose=1) 50 | 51 | if mode == 'inference': 52 | start = time.time() 53 | model.predict(train_data, batch_size) 54 | 55 | print("Keras Benchmark Results") 56 | print("Dataset: Synthetic Sparse Data") 57 | print("Backend: ", K.backend().capitalize()) 58 | print("Mode: ", mode) 59 | print("Batch Size: ", batch_size) 60 | if num_gpu >= 1: 61 | print("Benchmark run on {0} GPU".format(num_gpu)) 62 | else: 63 | print("Benchmark run on CPU") 64 | print("Total Time: ", time.time() - start) 65 | 66 | mse = model.evaluate(eval_data, eval_label, verbose=0, batch_size=batch_size) 67 | 68 | print("Achieved {0:.6f} validation MSE".format(mse[0])) 69 | print(model.evaluate(eval_data, eval_label, verbose=1, batch_size=batch_size)) 70 | -------------------------------------------------------------------------------- /benchmark/sparse/linear_regression/run_sparse_benchmark.py: -------------------------------------------------------------------------------- 1 | """ 2 | Prepare data for running benchmark on sparse linear regression model 3 | """ 4 | from __future__ import print_function 5 | 6 | import argparse 7 | 8 | import keras_sparse_model 9 | import mxnet as mx 10 | from scipy import sparse 11 | 12 | from keras import backend as K 13 | from keras.utils.data_utils import prepare_sliced_sparse_data 14 | 15 | 16 | def invoke_benchmark(batch_size, epochs, num_gpu, mode): 17 | feature_dimension = 10000 18 | train_data = mx.test_utils.rand_ndarray((100000, feature_dimension), 'csr', 0.01) 19 | target_weight = mx.nd.arange(1, feature_dimension + 1).reshape((feature_dimension, 1)) 20 | train_label = mx.nd.dot(train_data, target_weight) 21 | eval_data = train_data 22 | eval_label = mx.nd.dot(eval_data, target_weight) 23 | 24 | train_data = prepare_sliced_sparse_data(train_data, batch_size) 25 | train_label = prepare_sliced_sparse_data(train_label, batch_size) 26 | eval_data = prepare_sliced_sparse_data(eval_data, batch_size) 27 | eval_label = prepare_sliced_sparse_data(eval_label, batch_size) 28 | 29 | print("Running Keras benchmark script on sparse data") 30 | keras_sparse_model.run_benchmark(train_data=sparse.csr_matrix(train_data.asnumpy()), 31 | train_label=train_label.asnumpy(), 32 | eval_data=sparse.csr_matrix(eval_data.asnumpy()), 33 | eval_label=eval_label.asnumpy(), 34 | batch_size=batch_size, 35 | epochs=epochs, 36 | num_gpu=num_gpu, 37 | mode=mode) 38 | 39 | 40 | if __name__ == '__main__': 41 | parser = argparse.ArgumentParser() 42 | parser.add_argument("--batch", default=512, 43 | help="Batch of data to be processed for training") 44 | parser.add_argument("--epochs", default=10, 45 | help="Number of epochs to train the model on. Set epochs>=10 for the best results") 46 | parser.add_argument("--gpus", default=0, 47 | help="Benchmark scripts run by default on CPU. Set gpus>=1 for running model on single or " 48 | "multi-GPU") 49 | parser.add_argument("--mode", default='training', 50 | help="Benchmark scripts run by default for training the model. Set mode=inference for running " 51 | "benchmark on inference") 52 | args = parser.parse_args() 53 | 54 | invoke_benchmark(int(args.batch), int(args.epochs), int(args.gpus), str(args.mode)) 55 | -------------------------------------------------------------------------------- /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 | cntk-gpu && \ 46 | conda install \ 47 | bcolz \ 48 | h5py \ 49 | matplotlib \ 50 | mkl \ 51 | nose \ 52 | notebook \ 53 | Pillow \ 54 | pandas \ 55 | pydot \ 56 | pygpu \ 57 | pyyaml \ 58 | scikit-learn \ 59 | six \ 60 | theano && \ 61 | git clone git://github.com/keras-team/keras.git /src && pip install -e /src[tests] && \ 62 | pip install git+git://github.com/keras-team/keras.git && \ 63 | conda clean -yt 64 | 65 | ADD theanorc /home/keras/.theanorc 66 | 67 | ENV PYTHONPATH='/src/:$PYTHONPATH' 68 | 69 | WORKDIR /src 70 | 71 | EXPOSE 8888 72 | 73 | CMD jupyter notebook --port=8888 --ip=0.0.0.0 74 | 75 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /docker/theanorc: -------------------------------------------------------------------------------- 1 | [global] 2 | floatX = float32 3 | optimizer=None 4 | device = cuda 5 | 6 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /docs/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/awslabs/keras-apache-mxnet/5497ebd50a45ccc446b8944ebbe11fb7721a5533/docs/__init__.py -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /docs/mxnet_backend/README.md: -------------------------------------------------------------------------------- 1 | # Keras with Apache MXNet Documentation 2 | 3 | 1. [Installation Guide](installation.md) 4 | 2. [Multi GPU Model Training](multi_gpu_training.md) 5 | 3. [Using RNN with MXNet backend](using_rnn_with_mxnet_backend.md) 6 | 4. [Save MXNet model from Keras-MXNet](save_mxnet_model.md) 7 | 5. [Using Sparse Data with Keras-MXNet](using_sparse_data_with_mxnet_backend.md) 8 | 6. [Performance Tuning](performance_guide.md) 9 | 7. [Benchmark Results](../../benchmark/README.md) 10 | -------------------------------------------------------------------------------- /docs/mxnet_backend/using_rnn_with_mxnet_backend.md: -------------------------------------------------------------------------------- 1 | # Using Recurrent Neural Network (RNN) 2 | 3 | ## Table of Contents 4 | 5 | 1. [Overview](#overview) 6 | 2. [Variable length inputs are not supported for unrolling](#variable-length-inputs-are-not-supported-for-unrolling) 7 | 3. [Using Unrolling in RNN](#using-unrolling-in-rnn) 8 | 4. [Slower CPU training performance](#slower-cpu-training-performance) 9 | 10 | ## Overview 11 | 12 | In this document, we describe the limitations of using RNNs with MXNet backend and available workarounds for the same. 13 | 14 | ## Variable length inputs are not supported for unrolling 15 | 16 | MXNet backend does not support variable length inputs when you unrolling RNN cells in the recurrent layers. To overcome this limitation, you can 17 | pad the input sequences to prepare fixed length inputs. The MXNet backend requires both the `input_shape` and 18 | `unroll=True` parameters while adding the SimpleRNN/LSTM/GRU layer. 19 | 20 | 21 | ### Transform variable length to fixed length inputs 22 | 23 | You should pad the variable length input sequences to make it a fixed length. You can use Keras API - `keras.preprocessing.sequence.pad_sequences` for padding the input. 24 | 25 | Below is an example use case: 26 | 27 | ```python 28 | 29 | # Convert variable length input to fixed length by padding. 30 | # Usually, you choose maxlen to be maximum length of the variable input sequence. 31 | # This converts all input to be of length maxlen. 32 | new_x_train = keras.preprocessing.sequence.pad_sequences(old_x_train, maxlen=100) 33 | 34 | # Build the Model 35 | print('Build model...') 36 | model = Sequential() 37 | # len(chars) => Feature Size 38 | model.add(LSTM(128, input_shape=(maxlen, len(chars)), unroll=True)) 39 | model.add(Dense(len(chars))) 40 | model.add(Activation('softmax')) 41 | 42 | optimizer = RMSprop(lr=0.01) 43 | model.compile(loss='categorical_crossentropy', optimizer=optimizer) 44 | 45 | # Train the Model 46 | model.fit(new_x_train, y_train, batch_size=128, epochs=60) 47 | 48 | ``` 49 | 50 | ``` 51 | NOTE: 52 | Padding the input has performance implication due to wasted computation on paddings. You should not pad to 53 | arbitrary large maxlen. It is always optimal to choose maxlen for padding to be equal to the max length of the 54 | input sequences. 55 | ``` 56 | ## Using unrolling in RNN 57 | 58 | In Keras RNN layers, by default unroll is set to False, and it requires control flow operators(e.g. foreach, while_loop). 59 | We recently added support for this feature so by default RNN layers are not unrolled, same as other backends. 60 | 61 | Unrolling RNN Cells will have better performance but consumes more memory. It's only suitable for short sequences. For more details, refer to 62 | [Keras RNN API](https://keras.io/layers/recurrent/) 63 | 64 | ## Slower CPU training performance 65 | 66 | Performance of training a model with RNN layers on a CPU with MXNet backend is not optimal. This is a known issue and actively being worked on. Please expect this issue to be resolved in further releases of keras-mxnet. See [benchmark results](../../benchmark/README.md) for more detailed analysis. 67 | 68 | ``` 69 | NOTE: 70 | There is no performance degradation on GPUs. 71 | ``` 72 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /docs/templates/index.md: -------------------------------------------------------------------------------- 1 | # Keras: The Python Deep Learning library 2 | 3 | 4 | 5 | {{autogenerated}} -------------------------------------------------------------------------------- /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/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 | 15 | class MyLayer(Layer): 16 | 17 | def __init__(self, output_dim, **kwargs): 18 | self.output_dim = output_dim 19 | super(MyLayer, self).__init__(**kwargs) 20 | 21 | def build(self, input_shape): 22 | # Create a trainable weight variable for this layer. 23 | self.kernel = self.add_weight(name='kernel', 24 | shape=(input_shape[1], self.output_dim), 25 | initializer='uniform', 26 | trainable=True) 27 | super(MyLayer, self).build(input_shape) # Be sure to call this at the end 28 | 29 | def call(self, x): 30 | return K.dot(x, self.kernel) 31 | 32 | def compute_output_shape(self, input_shape): 33 | return (input_shape[0], self.output_dim) 34 | ``` 35 | 36 | It is also possible to define Keras layers which have multiple input tensors and multiple ouput tensors. To do this, you should assume that the inputs and outputs of the methods `build(input_shape)`, `call(x)` and `compute_output_shape(input_shape)` are lists. Here is an example, similar to the one above: 37 | 38 | ```python 39 | from keras import backend as K 40 | from keras.engine.topology import Layer 41 | 42 | class MyLayer(Layer): 43 | 44 | def __init__(self, output_dim, **kwargs): 45 | self.output_dim = output_dim 46 | super(MyLayer, self).__init__(**kwargs) 47 | 48 | def build(self, input_shape): 49 | assert isinstance(input_shape, list) 50 | # Create a trainable weight variable for this layer. 51 | self.kernel = self.add_weight(name='kernel', 52 | shape=(input_shape[0][1], self.output_dim), 53 | initializer='uniform', 54 | trainable=True) 55 | super(MyLayer, self).build(input_shape) # Be sure to call this at the end 56 | 57 | def call(self, x): 58 | assert isinstance(x, list) 59 | a, b = x 60 | return [K.dot(a, self.kernel) + b, K.mean(b, axis=-1)] 61 | 62 | def compute_output_shape(self, input_shape): 63 | assert isinstance(input_shape, list) 64 | shape_a, shape_b = input_shape 65 | return [(shape_a[0], self.output_dim), shape_b[:-1]] 66 | ``` 67 | 68 | The existing Keras layers provide examples of how to implement almost anything. Never hesitate to read the source code! 69 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | 25 | ## Methods 26 | 27 | {{autogenerated}} 28 | -------------------------------------------------------------------------------- /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 | ---- 6 | 7 | ## Sequential model methods 8 | 9 | {{autogenerated}} -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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(l1=0.01, l2=0.01) 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. 47 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | 27 | ## Training history visualization 28 | 29 | The `fit()` method on a Keras `Model` returns a `History` object. The `History.history` attribute is a dictionary recording training loss values and metrics values at successive epochs, as well as validation loss values and validation metrics values (if applicable). Here is a simple example using `matplotlib` to generate loss & accuracy plots for training & validation: 30 | 31 | ```python 32 | import matplotlib.pyplot as plt 33 | 34 | history = model.fit(x, y, validation_split=0.25, epochs=50, batch_size=16, verbose=1) 35 | 36 | # Plot training & validation accuracy values 37 | plt.plot(history.history['acc']) 38 | plt.plot(history.history['val_acc']) 39 | plt.title('Model accuracy') 40 | plt.ylabel('Accuracy') 41 | plt.xlabel('Epoch') 42 | plt.legend(['Train', 'Test'], loc='upper left') 43 | plt.show() 44 | 45 | # Plot training & validation loss values 46 | plt.plot(history.history['loss']) 47 | plt.plot(history.history['val_loss']) 48 | plt.title('Model loss') 49 | plt.ylabel('Loss') 50 | plt.xlabel('Epoch') 51 | plt.legend(['Train', 'Test'], loc='upper left') 52 | plt.show() 53 | ``` 54 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /examples/cifar10_mobilenet.py: -------------------------------------------------------------------------------- 1 | '''Train a MobileNet on the CIFAR10 small images dataset. 2 | Credit: scripts modified from examples/cifar10_cnn.py 3 | 4 | ''' 5 | 6 | from __future__ import print_function 7 | import keras 8 | from keras.datasets import cifar10 9 | from keras.preprocessing.image import ImageDataGenerator 10 | from keras.applications import MobileNet 11 | import os 12 | 13 | batch_size = 32 14 | num_classes = 10 15 | epochs = 100 16 | data_augmentation = True 17 | num_predictions = 20 18 | save_dir = os.path.join(os.getcwd(), 'saved_models') 19 | model_name = 'keras_cifar10_trained_model.h5' 20 | 21 | # The data, split between train and test sets: 22 | (x_train, y_train), (x_test, y_test) = cifar10.load_data() 23 | print('x_train shape:', x_train.shape) 24 | print(x_train.shape[0], 'train samples') 25 | print(x_test.shape[0], 'test samples') 26 | 27 | # Convert class vectors to binary class matrices. 28 | y_train = keras.utils.to_categorical(y_train, num_classes) 29 | y_test = keras.utils.to_categorical(y_test, num_classes) 30 | 31 | # MobileNet only supports channels last format 32 | model = MobileNet(input_shape=(32, 32, 3), weights=None, classes=num_classes) 33 | 34 | # initiate RMSprop optimizer 35 | opt = keras.optimizers.rmsprop(lr=0.0001, decay=1e-6) 36 | 37 | # Let's train the model using RMSprop 38 | model.compile(loss='categorical_crossentropy', 39 | optimizer=opt, 40 | metrics=['accuracy']) 41 | 42 | x_train = x_train.astype('float32') 43 | x_test = x_test.astype('float32') 44 | x_train /= 255 45 | x_test /= 255 46 | 47 | if not data_augmentation: 48 | print('Not using data augmentation.') 49 | model.fit(x_train, y_train, 50 | batch_size=batch_size, 51 | epochs=epochs, 52 | validation_data=(x_test, y_test), 53 | shuffle=True) 54 | else: 55 | print('Using real-time data augmentation.') 56 | # This will do preprocessing and realtime data augmentation: 57 | datagen = ImageDataGenerator( 58 | featurewise_center=False, # set input mean to 0 over the dataset 59 | samplewise_center=False, # set each sample mean to 0 60 | featurewise_std_normalization=False, # divide inputs by std of the dataset 61 | samplewise_std_normalization=False, # divide each input by its std 62 | zca_whitening=False, # apply ZCA whitening 63 | rotation_range=0, # randomly rotate images in the range (degrees, 0 to 180) 64 | width_shift_range=0.1, # randomly shift images horizontally (fraction of total width) 65 | height_shift_range=0.1, # randomly shift images vertically (fraction of total height) 66 | horizontal_flip=True, # randomly flip images 67 | vertical_flip=False) # randomly flip images 68 | 69 | # Compute quantities required for feature-wise normalization 70 | # (std, mean, and principal components if ZCA whitening is applied). 71 | datagen.fit(x_train) 72 | 73 | # Fit the model on the batches generated by datagen.flow(). 74 | model.fit_generator(datagen.flow(x_train, y_train, 75 | batch_size=batch_size), 76 | epochs=epochs, 77 | validation_data=(x_test, y_test), 78 | workers=4) 79 | 80 | # Save model and weights 81 | if not os.path.isdir(save_dir): 82 | os.makedirs(save_dir) 83 | model_path = os.path.join(save_dir, model_name) 84 | model.save(model_path) 85 | print('Saved trained model at %s ' % model_path) 86 | 87 | # Score trained model. 88 | scores = model.evaluate(x_test, y_test, verbose=1) 89 | print('Test loss:', scores[0]) 90 | print('Test accuracy:', scores[1]) 91 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | from keras import backend as K 23 | 24 | max_features = 20000 25 | # cut texts after this number of words (among top max_features most common words) 26 | maxlen = 80 27 | batch_size = 32 28 | 29 | print('Loading data...') 30 | (x_train, y_train), (x_test, y_test) = imdb.load_data(num_words=max_features) 31 | print(len(x_train), 'train sequences') 32 | print(len(x_test), 'test sequences') 33 | 34 | print('Pad sequences (samples x time)') 35 | x_train = sequence.pad_sequences(x_train, maxlen=maxlen) 36 | x_test = sequence.pad_sequences(x_test, maxlen=maxlen) 37 | print('x_train shape:', x_train.shape) 38 | print('x_test shape:', x_test.shape) 39 | 40 | print('Build model...') 41 | model = Sequential() 42 | 43 | model.add(Embedding(max_features, 128)) 44 | model.add(LSTM(128, dropout=0.2, recurrent_dropout=0.2)) 45 | model.add(Dense(1, activation='sigmoid')) 46 | 47 | # try using different optimizers and different optimizer configs 48 | model.compile(loss='binary_crossentropy', 49 | optimizer='adam', 50 | metrics=['accuracy']) 51 | 52 | print('Train...') 53 | model.fit(x_train, y_train, 54 | batch_size=batch_size, 55 | epochs=15, 56 | validation_data=(x_test, y_test)) 57 | score, acc = model.evaluate(x_test, y_test, 58 | batch_size=batch_size) 59 | print('Test score:', score) 60 | print('Test accuracy:', acc) 61 | -------------------------------------------------------------------------------- /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 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( 26 | 'nietzsche.txt', 27 | origin='https://s3.amazonaws.com/text-datasets/nietzsche.txt') 28 | with io.open(path, encoding='utf-8') as f: 29 | text = f.read().lower() 30 | print('corpus length:', len(text)) 31 | 32 | chars = sorted(list(set(text))) 33 | print('total chars:', len(chars)) 34 | char_indices = dict((c, i) for i, c in enumerate(chars)) 35 | indices_char = dict((i, c) for i, c in enumerate(chars)) 36 | 37 | # cut the text in semi-redundant sequences of maxlen characters 38 | maxlen = 40 39 | step = 3 40 | sentences = [] 41 | next_chars = [] 42 | for i in range(0, len(text) - maxlen, step): 43 | sentences.append(text[i: i + maxlen]) 44 | next_chars.append(text[i + maxlen]) 45 | print('nb sequences:', len(sentences)) 46 | 47 | print('Vectorization...') 48 | x = np.zeros((len(sentences), maxlen, len(chars)), dtype=np.bool) 49 | y = np.zeros((len(sentences), len(chars)), dtype=np.bool) 50 | for i, sentence in enumerate(sentences): 51 | for t, char in enumerate(sentence): 52 | x[i, t, char_indices[char]] = 1 53 | y[i, char_indices[next_chars[i]]] = 1 54 | 55 | 56 | # build the model: a single LSTM 57 | print('Build model...') 58 | model = Sequential() 59 | model.add(LSTM(128, input_shape=(maxlen, len(chars)))) 60 | model.add(Dense(len(chars), activation='softmax')) 61 | 62 | optimizer = RMSprop(lr=0.01) 63 | model.compile(loss='categorical_crossentropy', optimizer=optimizer) 64 | 65 | 66 | def sample(preds, temperature=1.0): 67 | # helper function to sample an index from a probability array 68 | preds = np.asarray(preds).astype('float64') 69 | preds = np.log(preds) / temperature 70 | exp_preds = np.exp(preds) 71 | preds = exp_preds / np.sum(exp_preds) 72 | probas = np.random.multinomial(1, preds, 1) 73 | return np.argmax(probas) 74 | 75 | 76 | def on_epoch_end(epoch, _): 77 | # Function invoked at end of each epoch. Prints generated text. 78 | print() 79 | print('----- Generating text after Epoch: %d' % epoch) 80 | 81 | start_index = random.randint(0, len(text) - maxlen - 1) 82 | for diversity in [0.2, 0.5, 1.0, 1.2]: 83 | print('----- diversity:', diversity) 84 | 85 | generated = '' 86 | sentence = text[start_index: start_index + maxlen] 87 | generated += sentence 88 | print('----- Generating with seed: "' + sentence + '"') 89 | sys.stdout.write(generated) 90 | 91 | for i in range(400): 92 | x_pred = np.zeros((1, maxlen, len(chars))) 93 | for t, char in enumerate(sentence): 94 | x_pred[0, t, char_indices[char]] = 1. 95 | 96 | preds = model.predict(x_pred, verbose=0)[0] 97 | next_index = sample(preds, diversity) 98 | next_char = indices_char[next_index] 99 | 100 | generated += next_char 101 | sentence = sentence[1:] + next_char 102 | 103 | sys.stdout.write(next_char) 104 | sys.stdout.flush() 105 | print() 106 | 107 | print_callback = LambdaCallback(on_epoch_end=on_epoch_end) 108 | 109 | model.fit(x, y, 110 | batch_size=128, 111 | epochs=60, 112 | callbacks=[print_callback]) 113 | -------------------------------------------------------------------------------- /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_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_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] 16 | (https://arxiv.org/abs/1506.01057) 17 | Encodes paragraphs and documents with HRNN. 18 | Results have shown that HRNN outperforms standard 19 | RNNs and may play some role in more sophisticated generation tasks like 20 | summarization or question answering. 21 | - [Hierarchical recurrent neural network for skeleton based action recognition] 22 | (http://ieeexplore.ieee.org/stamp/stamp.jsp?tp=&arnumber=7298714) 23 | Achieved state-of-the-art results on 24 | skeleton based action recognition with 3 levels 25 | of bidirectional HRNN combined with fully connected layers. 26 | 27 | In the below MNIST example the first LSTM layer first encodes every 28 | column of pixels of shape (28, 1) to a column vector of shape (128,). 29 | The second LSTM layer encodes then these 28 column vectors of shape (28, 128) 30 | to a image vector representing the whole image. 31 | A final Dense layer is added for prediction. 32 | 33 | After 5 epochs: train acc: 0.9858, val acc: 0.9864 34 | """ 35 | from __future__ import print_function 36 | 37 | import keras 38 | from keras.datasets import mnist 39 | from keras.models import Model 40 | from keras.layers import Input, Dense, TimeDistributed 41 | from keras.layers import LSTM 42 | 43 | # Training parameters. 44 | batch_size = 32 45 | num_classes = 10 46 | epochs = 5 47 | 48 | # Embedding dimensions. 49 | row_hidden = 128 50 | col_hidden = 128 51 | 52 | # The data, split between train and test sets. 53 | (x_train, y_train), (x_test, y_test) = mnist.load_data() 54 | 55 | # Reshapes data to 4D for Hierarchical RNN. 56 | x_train = x_train.reshape(x_train.shape[0], 28, 28, 1) 57 | x_test = x_test.reshape(x_test.shape[0], 28, 28, 1) 58 | x_train = x_train.astype('float32') 59 | x_test = x_test.astype('float32') 60 | x_train /= 255 61 | x_test /= 255 62 | print('x_train shape:', x_train.shape) 63 | print(x_train.shape[0], 'train samples') 64 | print(x_test.shape[0], 'test samples') 65 | 66 | # Converts class vectors to binary class matrices. 67 | y_train = keras.utils.to_categorical(y_train, num_classes) 68 | y_test = keras.utils.to_categorical(y_test, num_classes) 69 | 70 | row, col, pixel = x_train.shape[1:] 71 | 72 | # 4D input. 73 | x = Input(shape=(row, col, pixel)) 74 | 75 | # Encodes a row of pixels using TimeDistributed Wrapper. 76 | encoded_rows = TimeDistributed(LSTM(row_hidden))(x) 77 | 78 | # Encodes columns of encoded rows. 79 | encoded_columns = LSTM(col_hidden)(encoded_rows) 80 | 81 | # Final predictions and model. 82 | prediction = Dense(num_classes, activation='softmax')(encoded_columns) 83 | model = Model(x, prediction) 84 | model.compile(loss='categorical_crossentropy', 85 | optimizer='rmsprop', 86 | metrics=['accuracy']) 87 | 88 | # Training. 89 | model.fit(x_train, y_train, 90 | batch_size=batch_size, 91 | epochs=epochs, 92 | verbose=1, 93 | validation_data=(x_test, y_test)) 94 | 95 | # Evaluation. 96 | scores = model.evaluate(x_test, y_test, verbose=0) 97 | print('Test loss:', scores[0]) 98 | print('Test accuracy:', scores[1]) 99 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /examples/tensorboard_embeddings_mnist.py: -------------------------------------------------------------------------------- 1 | '''Trains a simple convnet on the MNIST dataset and embeds test data. 2 | 3 | The test data is embedded using the weights of the final dense layer, just 4 | before the classification head. This embedding can then be visualized using 5 | TensorBoard's Embedding Projector. 6 | ''' 7 | 8 | from __future__ import print_function 9 | 10 | from os import makedirs 11 | from os.path import exists, join 12 | 13 | import keras 14 | from keras.callbacks import TensorBoard 15 | from keras.datasets import mnist 16 | from keras.models import Sequential 17 | from keras.layers import Dense, Dropout, Flatten 18 | from keras.layers import Conv2D, MaxPooling2D 19 | from keras import backend as K 20 | 21 | import numpy as np 22 | 23 | batch_size = 128 24 | num_classes = 10 25 | epochs = 12 26 | log_dir = './logs' 27 | 28 | if not exists(log_dir): 29 | makedirs(log_dir) 30 | 31 | # input image dimensions 32 | img_rows, img_cols = 28, 28 33 | 34 | # the data, split between train and test sets 35 | (x_train, y_train), (x_test, y_test) = mnist.load_data() 36 | 37 | if K.image_data_format() == 'channels_first': 38 | x_train = x_train.reshape(x_train.shape[0], 1, img_rows, img_cols) 39 | x_test = x_test.reshape(x_test.shape[0], 1, img_rows, img_cols) 40 | input_shape = (1, img_rows, img_cols) 41 | else: 42 | x_train = x_train.reshape(x_train.shape[0], img_rows, img_cols, 1) 43 | x_test = x_test.reshape(x_test.shape[0], img_rows, img_cols, 1) 44 | input_shape = (img_rows, img_cols, 1) 45 | 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 | # save class labels to disk to color data points in TensorBoard accordingly 55 | with open(join(log_dir, 'metadata.tsv'), 'w') as f: 56 | np.savetxt(f, y_test) 57 | 58 | # convert class vectors to binary class matrices 59 | y_train = keras.utils.to_categorical(y_train, num_classes) 60 | y_test = keras.utils.to_categorical(y_test, num_classes) 61 | 62 | tensorboard = TensorBoard(batch_size=batch_size, 63 | embeddings_freq=1, 64 | embeddings_layer_names=['features'], 65 | embeddings_metadata='metadata.tsv', 66 | embeddings_data=x_test) 67 | 68 | model = Sequential() 69 | model.add(Conv2D(32, kernel_size=(3, 3), 70 | activation='relu', 71 | input_shape=input_shape)) 72 | model.add(Conv2D(64, (3, 3), activation='relu')) 73 | model.add(MaxPooling2D(pool_size=(2, 2))) 74 | model.add(Dropout(0.25)) 75 | model.add(Flatten()) 76 | model.add(Dense(128, activation='relu', name='features')) 77 | model.add(Dropout(0.5)) 78 | model.add(Dense(num_classes, activation='softmax')) 79 | 80 | model.compile(loss=keras.losses.categorical_crossentropy, 81 | optimizer=keras.optimizers.Adadelta(), 82 | metrics=['accuracy']) 83 | 84 | model.fit(x_train, y_train, 85 | batch_size=batch_size, 86 | callbacks=[tensorboard], 87 | epochs=epochs, 88 | verbose=1, 89 | validation_data=(x_test, y_test)) 90 | score = model.evaluate(x_test, y_test, verbose=0) 91 | print('Test loss:', score[0]) 92 | print('Test accuracy:', score[1]) 93 | 94 | # You can now launch tensorboard with `tensorboard --logdir=./logs` on your 95 | # command line and then go to http://localhost:6006/#projector to view the 96 | # embeddings 97 | -------------------------------------------------------------------------------- /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 | import warnings 27 | warnings.simplefilter('always', DeprecationWarning) 28 | warnings.warn("MXNet support in Keras is going to be discontinued and v2.2.4.3 is the last " 29 | "release as multi-backend Keras has been discontinued . It is recommended to " 30 | "consider switching to MXNet Gluon. More information can be found here: " 31 | "https://github.com/awslabs/keras-apache-mxnet", DeprecationWarning) 32 | 33 | __version__ = '2.2.4.3' 34 | -------------------------------------------------------------------------------- /keras/applications/__init__.py: -------------------------------------------------------------------------------- 1 | from __future__ import absolute_import 2 | from __future__ import division 3 | from __future__ import print_function 4 | 5 | from .. import backend 6 | from .. import layers 7 | from .. import models 8 | from .. import utils 9 | 10 | import keras_applications 11 | 12 | if not hasattr(keras_applications, 'get_submodules_from_kwargs'): 13 | keras_applications.set_keras_submodules( 14 | backend=backend, 15 | layers=layers, 16 | models=models, 17 | utils=utils) 18 | 19 | 20 | def keras_modules_injection(base_fun): 21 | 22 | def wrapper(*args, **kwargs): 23 | if hasattr(keras_applications, 'get_submodules_from_kwargs'): 24 | kwargs['backend'] = backend 25 | kwargs['layers'] = layers 26 | kwargs['models'] = models 27 | kwargs['utils'] = utils 28 | return base_fun(*args, **kwargs) 29 | 30 | return wrapper 31 | 32 | 33 | from .vgg16 import VGG16 34 | from .vgg19 import VGG19 35 | from .resnet50 import ResNet50 36 | from .inception_v3 import InceptionV3 37 | from .inception_resnet_v2 import InceptionResNetV2 38 | from .xception import Xception 39 | from .mobilenet import MobileNet 40 | from .mobilenet_v2 import MobileNetV2 41 | from .densenet import DenseNet121, DenseNet169, DenseNet201 42 | from .nasnet import NASNetMobile, NASNetLarge 43 | -------------------------------------------------------------------------------- /keras/applications/densenet.py: -------------------------------------------------------------------------------- 1 | from __future__ import absolute_import 2 | from __future__ import division 3 | from __future__ import print_function 4 | 5 | from keras_applications import densenet 6 | from . import keras_modules_injection 7 | 8 | 9 | @keras_modules_injection 10 | def DenseNet121(*args, **kwargs): 11 | return densenet.DenseNet121(*args, **kwargs) 12 | 13 | 14 | @keras_modules_injection 15 | def DenseNet169(*args, **kwargs): 16 | return densenet.DenseNet169(*args, **kwargs) 17 | 18 | 19 | @keras_modules_injection 20 | def DenseNet201(*args, **kwargs): 21 | return densenet.DenseNet201(*args, **kwargs) 22 | 23 | 24 | @keras_modules_injection 25 | def decode_predictions(*args, **kwargs): 26 | return densenet.decode_predictions(*args, **kwargs) 27 | 28 | 29 | @keras_modules_injection 30 | def preprocess_input(*args, **kwargs): 31 | return densenet.preprocess_input(*args, **kwargs) 32 | -------------------------------------------------------------------------------- /keras/applications/imagenet_utils.py: -------------------------------------------------------------------------------- 1 | """Utilities for ImageNet data preprocessing & prediction decoding. 2 | """ 3 | from __future__ import absolute_import 4 | from __future__ import division 5 | from __future__ import print_function 6 | 7 | from keras_applications import imagenet_utils 8 | from . import keras_modules_injection 9 | 10 | 11 | @keras_modules_injection 12 | def decode_predictions(*args, **kwargs): 13 | return imagenet_utils.decode_predictions( 14 | *args, **kwargs) 15 | 16 | 17 | @keras_modules_injection 18 | def preprocess_input(*args, **kwargs): 19 | return imagenet_utils.preprocess_input(*args, **kwargs) 20 | -------------------------------------------------------------------------------- /keras/applications/inception_resnet_v2.py: -------------------------------------------------------------------------------- 1 | from __future__ import absolute_import 2 | from __future__ import division 3 | from __future__ import print_function 4 | 5 | from keras_applications import inception_resnet_v2 6 | from . import keras_modules_injection 7 | 8 | 9 | @keras_modules_injection 10 | def InceptionResNetV2(*args, **kwargs): 11 | return inception_resnet_v2.InceptionResNetV2(*args, **kwargs) 12 | 13 | 14 | @keras_modules_injection 15 | def decode_predictions(*args, **kwargs): 16 | return inception_resnet_v2.decode_predictions(*args, **kwargs) 17 | 18 | 19 | @keras_modules_injection 20 | def preprocess_input(*args, **kwargs): 21 | return inception_resnet_v2.preprocess_input(*args, **kwargs) 22 | -------------------------------------------------------------------------------- /keras/applications/inception_v3.py: -------------------------------------------------------------------------------- 1 | from __future__ import absolute_import 2 | from __future__ import division 3 | from __future__ import print_function 4 | 5 | from keras_applications import inception_v3 6 | from . import keras_modules_injection 7 | 8 | 9 | @keras_modules_injection 10 | def InceptionV3(*args, **kwargs): 11 | return inception_v3.InceptionV3(*args, **kwargs) 12 | 13 | 14 | @keras_modules_injection 15 | def decode_predictions(*args, **kwargs): 16 | return inception_v3.decode_predictions(*args, **kwargs) 17 | 18 | 19 | @keras_modules_injection 20 | def preprocess_input(*args, **kwargs): 21 | return inception_v3.preprocess_input(*args, **kwargs) 22 | -------------------------------------------------------------------------------- /keras/applications/mobilenet.py: -------------------------------------------------------------------------------- 1 | from __future__ import absolute_import 2 | from __future__ import division 3 | from __future__ import print_function 4 | 5 | from keras_applications import mobilenet 6 | from . import keras_modules_injection 7 | 8 | 9 | @keras_modules_injection 10 | def MobileNet(*args, **kwargs): 11 | return mobilenet.MobileNet(*args, **kwargs) 12 | 13 | 14 | @keras_modules_injection 15 | def decode_predictions(*args, **kwargs): 16 | return mobilenet.decode_predictions(*args, **kwargs) 17 | 18 | 19 | @keras_modules_injection 20 | def preprocess_input(*args, **kwargs): 21 | return mobilenet.preprocess_input(*args, **kwargs) 22 | -------------------------------------------------------------------------------- /keras/applications/mobilenet_v2.py: -------------------------------------------------------------------------------- 1 | from __future__ import absolute_import 2 | from __future__ import division 3 | from __future__ import print_function 4 | 5 | from keras_applications import mobilenet_v2 6 | from . import keras_modules_injection 7 | 8 | 9 | @keras_modules_injection 10 | def MobileNetV2(*args, **kwargs): 11 | return mobilenet_v2.MobileNetV2(*args, **kwargs) 12 | 13 | 14 | @keras_modules_injection 15 | def decode_predictions(*args, **kwargs): 16 | return mobilenet_v2.decode_predictions(*args, **kwargs) 17 | 18 | 19 | @keras_modules_injection 20 | def preprocess_input(*args, **kwargs): 21 | return mobilenet_v2.preprocess_input(*args, **kwargs) 22 | -------------------------------------------------------------------------------- /keras/applications/mobilenetv2.py: -------------------------------------------------------------------------------- 1 | # Only for backwards compatibility. 2 | from .mobilenet_v2 import * 3 | -------------------------------------------------------------------------------- /keras/applications/nasnet.py: -------------------------------------------------------------------------------- 1 | from __future__ import absolute_import 2 | from __future__ import division 3 | from __future__ import print_function 4 | 5 | from keras_applications import nasnet 6 | from . import keras_modules_injection 7 | 8 | 9 | @keras_modules_injection 10 | def NASNetMobile(*args, **kwargs): 11 | return nasnet.NASNetMobile(*args, **kwargs) 12 | 13 | 14 | @keras_modules_injection 15 | def NASNetLarge(*args, **kwargs): 16 | return nasnet.NASNetLarge(*args, **kwargs) 17 | 18 | 19 | @keras_modules_injection 20 | def decode_predictions(*args, **kwargs): 21 | return nasnet.decode_predictions(*args, **kwargs) 22 | 23 | 24 | @keras_modules_injection 25 | def preprocess_input(*args, **kwargs): 26 | return nasnet.preprocess_input(*args, **kwargs) 27 | -------------------------------------------------------------------------------- /keras/applications/resnet50.py: -------------------------------------------------------------------------------- 1 | from __future__ import absolute_import 2 | from __future__ import division 3 | from __future__ import print_function 4 | 5 | from keras_applications import resnet50 6 | from . import keras_modules_injection 7 | 8 | 9 | @keras_modules_injection 10 | def ResNet50(*args, **kwargs): 11 | return resnet50.ResNet50(*args, **kwargs) 12 | 13 | 14 | @keras_modules_injection 15 | def decode_predictions(*args, **kwargs): 16 | return resnet50.decode_predictions(*args, **kwargs) 17 | 18 | 19 | @keras_modules_injection 20 | def preprocess_input(*args, **kwargs): 21 | return resnet50.preprocess_input(*args, **kwargs) 22 | -------------------------------------------------------------------------------- /keras/applications/vgg16.py: -------------------------------------------------------------------------------- 1 | from __future__ import absolute_import 2 | from __future__ import division 3 | from __future__ import print_function 4 | 5 | from keras_applications import vgg16 6 | from . import keras_modules_injection 7 | 8 | 9 | @keras_modules_injection 10 | def VGG16(*args, **kwargs): 11 | return vgg16.VGG16(*args, **kwargs) 12 | 13 | 14 | @keras_modules_injection 15 | def decode_predictions(*args, **kwargs): 16 | return vgg16.decode_predictions(*args, **kwargs) 17 | 18 | 19 | @keras_modules_injection 20 | def preprocess_input(*args, **kwargs): 21 | return vgg16.preprocess_input(*args, **kwargs) 22 | -------------------------------------------------------------------------------- /keras/applications/vgg19.py: -------------------------------------------------------------------------------- 1 | from __future__ import absolute_import 2 | from __future__ import division 3 | from __future__ import print_function 4 | 5 | from keras_applications import vgg19 6 | from . import keras_modules_injection 7 | 8 | 9 | @keras_modules_injection 10 | def VGG19(*args, **kwargs): 11 | return vgg19.VGG19(*args, **kwargs) 12 | 13 | 14 | @keras_modules_injection 15 | def decode_predictions(*args, **kwargs): 16 | return vgg19.decode_predictions(*args, **kwargs) 17 | 18 | 19 | @keras_modules_injection 20 | def preprocess_input(*args, **kwargs): 21 | return vgg19.preprocess_input(*args, **kwargs) 22 | -------------------------------------------------------------------------------- /keras/applications/xception.py: -------------------------------------------------------------------------------- 1 | from __future__ import absolute_import 2 | from __future__ import division 3 | from __future__ import print_function 4 | 5 | from keras_applications import xception 6 | from . import keras_modules_injection 7 | 8 | 9 | @keras_modules_injection 10 | def Xception(*args, **kwargs): 11 | return xception.Xception(*args, **kwargs) 12 | 13 | 14 | @keras_modules_injection 15 | def decode_predictions(*args, **kwargs): 16 | return xception.decode_predictions(*args, **kwargs) 17 | 18 | 19 | @keras_modules_injection 20 | def preprocess_input(*args, **kwargs): 21 | return xception.preprocess_input(*args, **kwargs) 22 | -------------------------------------------------------------------------------- /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/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( 26 | path, 27 | origin='https://s3.amazonaws.com/keras-datasets/boston_housing.npz', 28 | file_hash='f553886a1f8d56431e820c5b82552d9d95cfcb96d1e678153f8839538947dff5') 29 | with np.load(path, allow_pickle=True) as f: 30 | x = f['x'] 31 | y = f['y'] 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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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/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 | -------------------------------------------------------------------------------- /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/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 | -------------------------------------------------------------------------------- /keras/legacy/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/awslabs/keras-apache-mxnet/5497ebd50a45ccc446b8944ebbe11fb7721a5533/keras/legacy/__init__.py -------------------------------------------------------------------------------- /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 multi_hot_sparse_categorical_crossentropy 19 | from .losses import binary_crossentropy 20 | from .losses import kullback_leibler_divergence 21 | from .losses import poisson 22 | from .losses import cosine_proximity 23 | from .utils.generic_utils import deserialize_keras_object 24 | from .utils.generic_utils import serialize_keras_object 25 | 26 | 27 | def binary_accuracy(y_true, y_pred): 28 | return K.mean(K.equal(y_true, K.round(y_pred)), axis=-1) 29 | 30 | 31 | def categorical_accuracy(y_true, y_pred): 32 | return K.cast(K.equal(K.argmax(y_true, axis=-1), 33 | K.argmax(y_pred, axis=-1)), 34 | K.floatx()) 35 | 36 | 37 | def sparse_categorical_accuracy(y_true, y_pred): 38 | # flatten y_true in case it's in shape (num_samples, 1) instead of (num_samples,) 39 | return K.cast(K.equal(K.flatten(y_true), 40 | K.cast(K.argmax(y_pred, axis=-1), K.floatx())), 41 | K.floatx()) 42 | 43 | 44 | def multi_hot_sparse_categorical_accuracy(y_true, y_pred): 45 | return K.cast(K.equal(K.max(y_true, axis=-1), 46 | K.cast(K.argmax(y_pred, axis=-1), K.floatx())), 47 | K.floatx()) 48 | 49 | 50 | def top_k_categorical_accuracy(y_true, y_pred, k=5): 51 | return K.mean(K.in_top_k(y_pred, K.argmax(y_true, axis=-1), k), axis=-1) 52 | 53 | 54 | def sparse_top_k_categorical_accuracy(y_true, y_pred, k=5): 55 | # If the shape of y_true is (num_samples, 1), flatten to (num_samples,) 56 | return K.mean(K.in_top_k(y_pred, K.cast(K.flatten(y_true), 'int32'), k), 57 | axis=-1) 58 | 59 | 60 | # Aliases 61 | 62 | mse = MSE = mean_squared_error 63 | mae = MAE = mean_absolute_error 64 | mape = MAPE = mean_absolute_percentage_error 65 | msle = MSLE = mean_squared_logarithmic_error 66 | cosine = cosine_proximity 67 | 68 | 69 | def serialize(metric): 70 | return serialize_keras_object(metric) 71 | 72 | 73 | def deserialize(config, custom_objects=None): 74 | return deserialize_keras_object(config, 75 | module_objects=globals(), 76 | custom_objects=custom_objects, 77 | printable_module_name='metric function') 78 | 79 | 80 | def get(identifier): 81 | if isinstance(identifier, dict): 82 | config = {'class_name': str(identifier), 'config': {}} 83 | return deserialize(config) 84 | elif isinstance(identifier, six.string_types): 85 | return deserialize(str(identifier)) 86 | elif callable(identifier): 87 | return identifier 88 | else: 89 | raise ValueError('Could not interpret ' 90 | 'metric function identifier:', identifier) 91 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /keras/preprocessing/__init__.py: -------------------------------------------------------------------------------- 1 | from __future__ import absolute_import 2 | from __future__ import division 3 | from __future__ import print_function 4 | 5 | from .. import backend 6 | from .. import utils 7 | 8 | import keras_preprocessing 9 | 10 | keras_preprocessing.set_keras_submodules(backend=backend, utils=utils) 11 | 12 | from . import image 13 | from . import sequence 14 | from . import text 15 | -------------------------------------------------------------------------------- /keras/preprocessing/sequence.py: -------------------------------------------------------------------------------- 1 | """Utilities for preprocessing sequence data. 2 | """ 3 | from __future__ import absolute_import 4 | from __future__ import division 5 | from __future__ import print_function 6 | 7 | from keras_preprocessing import sequence 8 | from .. import utils 9 | 10 | pad_sequences = sequence.pad_sequences 11 | make_sampling_table = sequence.make_sampling_table 12 | skipgrams = sequence.skipgrams 13 | _remove_long_seq = sequence._remove_long_seq # TODO: make it public? 14 | 15 | 16 | class TimeseriesGenerator(sequence.TimeseriesGenerator, utils.Sequence): 17 | """Utility class for generating batches of temporal data. 18 | 19 | This class takes in a sequence of data-points gathered at 20 | equal intervals, along with time series parameters such as 21 | stride, length of history, etc., to produce batches for 22 | training/validation. 23 | 24 | # Arguments 25 | data: Indexable generator (such as list or Numpy array) 26 | containing consecutive data points (timesteps). 27 | The data should be at 2D, and axis 0 is expected 28 | to be the time dimension. 29 | targets: Targets corresponding to timesteps in `data`. 30 | It should have same length as `data`. 31 | length: Length of the output sequences (in number of timesteps). 32 | sampling_rate: Period between successive individual timesteps 33 | within sequences. For rate `r`, timesteps 34 | `data[i]`, `data[i-r]`, ... `data[i - length]` 35 | are used for create a sample sequence. 36 | stride: Period between successive output sequences. 37 | For stride `s`, consecutive output samples would 38 | be centered around `data[i]`, `data[i+s]`, `data[i+2*s]`, etc. 39 | start_index: Data points earlier than `start_index` will not be used 40 | in the output sequences. This is useful to reserve part of the 41 | data for test or validation. 42 | end_index: Data points later than `end_index` will not be used 43 | in the output sequences. This is useful to reserve part of the 44 | data for test or validation. 45 | shuffle: Whether to shuffle output samples, 46 | or instead draw them in chronological order. 47 | reverse: Boolean: if `true`, timesteps in each output sample will be 48 | in reverse chronological order. 49 | batch_size: Number of timeseries samples in each batch 50 | (except maybe the last one). 51 | 52 | # Returns 53 | A [Sequence](/utils/#sequence) instance. 54 | 55 | # Examples 56 | 57 | ```python 58 | from keras.preprocessing.sequence import TimeseriesGenerator 59 | import numpy as np 60 | 61 | data = np.array([[i] for i in range(50)]) 62 | targets = np.array([[i] for i in range(50)]) 63 | 64 | data_gen = TimeseriesGenerator(data, targets, 65 | length=10, sampling_rate=2, 66 | batch_size=2) 67 | assert len(data_gen) == 20 68 | 69 | batch_0 = data_gen[0] 70 | x, y = batch_0 71 | assert np.array_equal(x, 72 | np.array([[[0], [2], [4], [6], [8]], 73 | [[1], [3], [5], [7], [9]]])) 74 | assert np.array_equal(y, 75 | np.array([[10], [11]])) 76 | ``` 77 | """ 78 | pass 79 | -------------------------------------------------------------------------------- /keras/preprocessing/text.py: -------------------------------------------------------------------------------- 1 | """Utilities for text input preprocessing. 2 | """ 3 | from __future__ import absolute_import 4 | from __future__ import division 5 | from __future__ import print_function 6 | 7 | from keras_preprocessing import text 8 | 9 | text_to_word_sequence = text.text_to_word_sequence 10 | one_hot = text.one_hot 11 | hashing_trick = text.hashing_trick 12 | Tokenizer = text.Tokenizer 13 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 .io_utils import h5dict 11 | from .data_utils import get_file 12 | from .data_utils import Sequence 13 | from .data_utils import GeneratorEnqueuer 14 | from .data_utils import OrderedEnqueuer 15 | from .generic_utils import CustomObjectScope 16 | from .generic_utils import custom_object_scope 17 | from .generic_utils import get_custom_objects 18 | from .generic_utils import serialize_keras_object 19 | from .generic_utils import deserialize_keras_object 20 | from .generic_utils import Progbar 21 | from .layer_utils import convert_all_kernels_in_model 22 | from .layer_utils import get_source_inputs 23 | from .layer_utils import print_summary 24 | from .vis_utils import plot_model 25 | from .np_utils import to_categorical 26 | from .np_utils import normalize 27 | from .np_utils import to_channels_first 28 | from .multi_gpu_utils import multi_gpu_model 29 | -------------------------------------------------------------------------------- /keras/wrappers/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/awslabs/keras-apache-mxnet/5497ebd50a45ccc446b8944ebbe11fb7721a5533/keras/wrappers/__init__.py -------------------------------------------------------------------------------- /keras_mxnet_ci/nightly-buildspec-python2.yml: -------------------------------------------------------------------------------- 1 | version: 0.2 2 | 3 | phases: 4 | install: 5 | commands: 6 | echo "Checking out master branch"; 7 | git fetch; 8 | git checkout master; 9 | echo "Installing MXNet"; 10 | apt-get update; 11 | pip install --upgrade pip; 12 | pip install mxnet==1.6.0; 13 | echo "Installing Tensorflow"; 14 | pip install tensorflow==1.15.0; 15 | echo "Installing Theano"; 16 | pip install theano; 17 | pip install pillow; 18 | sudo apt-get -y install graphviz; 19 | pip install graphviz; 20 | pip install pydot; 21 | pip install nose; 22 | pip install PyYAML==3.13; 23 | pip install pytest>=4.6; 24 | pip install pytest-xdist==1.22.2; 25 | echo "Installing Keras from source"; 26 | pip install -e .[visualize,tests]; 27 | 28 | build: 29 | commands: 30 | echo "Running PEP tests"; 31 | py.test --pep8 -m pep8 -n0; 32 | echo "Running Keras Unit Tests and Integration Tests for all the backends"; 33 | python -m pytest tests/; -------------------------------------------------------------------------------- /keras_mxnet_ci/nightly-buildspec-python3.yml: -------------------------------------------------------------------------------- 1 | version: 0.2 2 | 3 | phases: 4 | install: 5 | runtime-versions: 6 | python: 3.7 7 | commands: 8 | echo "Checking out master branch"; 9 | git fetch; 10 | git checkout master; 11 | echo "Installing MXNet"; 12 | apt-get update; 13 | apt-get install python3.7-dev; 14 | python3.7 -m pip install pip; 15 | python3.7 -m pip install mxnet==1.6.0; 16 | echo "Installing Tensorflow"; 17 | python3.7 -m pip install tensorflow==1.15.0; 18 | echo "Installing Theano"; 19 | python3.7 -m pip install theano; 20 | python3.7 -m pip install pillow; 21 | apt-get -y install graphviz; 22 | python3.7 -m pip install graphviz; 23 | python3.7 -m pip install pydot; 24 | python3.7 -m pip install nose; 25 | python3.7 -m pip install --ignore-installed PyYAML==3.13; 26 | python3.7 -m pip install pytest>=4.6; 27 | python3.7 -m pip install pytest-xdist==1.22.2; 28 | echo "Installing Keras from source"; 29 | python3.7 -m pip install -e .[visualize,tests]; 30 | 31 | build: 32 | commands: 33 | echo "Running PEP tests"; 34 | py.test --pep8 -m pep8 -n0; 35 | echo "Running Keras Unit Tests and Integration Tests for all the backends"; 36 | python3.7 -m pytest tests/; -------------------------------------------------------------------------------- /keras_mxnet_ci/pr-buildspec-python2.yml: -------------------------------------------------------------------------------- 1 | version: 0.2 2 | 3 | phases: 4 | install: 5 | commands: 6 | echo $CODEBUILD_SOURCE_VERSION; 7 | PRID=$(echo $CODEBUILD_SOURCE_VERSION | sed "s/pr/pull/g"); 8 | echo "Checking out $PRID"; 9 | git fetch origin $PRID/head:pr_test; 10 | git checkout pr_test; 11 | echo "Installing MXNet"; 12 | apt-get update; 13 | pip install --upgrade pip; 14 | pip install mxnet==1.6.0; 15 | echo "Installing Tensorflow"; 16 | pip install tensorflow==1.15.0; 17 | echo "Installing Theano"; 18 | pip install theano; 19 | pip install pillow; 20 | sudo apt-get -y install graphviz; 21 | pip install graphviz; 22 | pip install pydot; 23 | pip install nose; 24 | pip3 install PyYAML==3.13; 25 | pip3 install pytest>=4.6; 26 | pip3 install pytest-xdist==1.22.2; 27 | echo "Installing Keras from source"; 28 | pip install -e .[visualize,tests]; 29 | 30 | build: 31 | commands: 32 | echo "Running PEP tests"; 33 | py.test --pep8 -m pep8 -n0; 34 | echo "Running Keras Unit Tests and Integration Tests for all the backends"; 35 | python -m pytest tests/; -------------------------------------------------------------------------------- /keras_mxnet_ci/pr-buildspec-python3.yml: -------------------------------------------------------------------------------- 1 | version: 0.2 2 | 3 | phases: 4 | install: 5 | runtime-versions: 6 | python: 3.7 7 | commands: 8 | echo $CODEBUILD_SOURCE_VERSION; 9 | PRID=$(echo $CODEBUILD_SOURCE_VERSION | sed "s/pr/pull/g"); 10 | echo "Checking out $PRID"; 11 | git fetch origin $PRID/head:pr_test; 12 | git checkout pr_test; 13 | echo "Installing MXNet"; 14 | apt-get update; 15 | apt-get install python3.7-dev; 16 | python3.7 -m pip install pip; 17 | python3.7 -m pip install mxnet==1.6.0; 18 | echo "Installing Tensorflow"; 19 | python3.7 -m pip install tensorflow==1.15.0; 20 | echo "Installing Theano"; 21 | python3.7 -m pip install theano; 22 | python3.7 -m pip install pillow; 23 | apt-get -y install graphviz; 24 | python3.7 -m pip install graphviz; 25 | python3.7 -m pip install pydot; 26 | python3.7 -m pip install nose; 27 | python3.7 -m pip install --ignore-installed PyYAML==3.13; 28 | python3.7 -m pip install pytest>=4.6; 29 | python3.7 -m pip install pytest-xdist==1.22.2; 30 | echo "Installing Keras from source"; 31 | python3.7 -m pip install -e .[visualize,tests]; 32 | 33 | build: 34 | commands: 35 | echo "Running PEP tests"; 36 | py.test --pep8 -m pep8 -n0; 37 | echo "Running Keras Unit Tests and Integration Tests for all the backends"; 38 | python3.7 -m pytest tests/; -------------------------------------------------------------------------------- /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 | # Running all tests should take less than 20 minutes. 11 | # Otherwise, something went wrong. 12 | timeout = 1200 13 | 14 | # PEP-8 The following are ignored: 15 | # E501 line too long (82 > 79 characters) 16 | # E402 module level import not at top of file - temporary measure to continue adding ros python packaged in sys.path 17 | # E731 do not assign a lambda expression, use a def 18 | # W503 line break occurred before a binary operator 19 | 20 | pep8ignore=* E402 \ 21 | * E731 \ 22 | * W503 \ 23 | keras/backend/cntk_backend.py E501 \ 24 | keras/backend/common.py E501 \ 25 | keras/callbacks.py E501 \ 26 | keras/layers/embeddings.py E501 \ 27 | keras/backend/tensorflow_backend.py E501 \ 28 | keras/backend/theano_backend.py E501 \ 29 | tests/keras/backend/backend_test.py E501 30 | 31 | # Enable line length testing with maximum line length of 120 32 | pep8maxlinelength = 120 33 | -------------------------------------------------------------------------------- /setup.cfg: -------------------------------------------------------------------------------- 1 | [metadata] 2 | description-file = README.md -------------------------------------------------------------------------------- /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. Keras-MXNet is capable of running on top of 7 | high performance, scalable Apache MXNet deep learning engine. 8 | 9 | Use Keras-MXNet 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, one GPU and multi-GPU. 16 | 17 | Read the Keras documentation at: https://keras.io/ 18 | 19 | Read the Keras-MXNet documentation at: 20 | https://github.com/awslabs/keras-apache-mxnet/tree/master/docs/mxnet_backend 21 | 22 | For a detailed overview of what makes Keras special, see: 23 | https://keras.io/why-use-keras/ 24 | Keras is compatible with Python 2.7-3.6 25 | and is distributed under the MIT liense. 26 | ''' 27 | 28 | setup(name='keras-mxnet', 29 | version='2.2.4.3', 30 | description='Deep Learning for humans. Keras with highly scalable,\ 31 | high performance Apache MXNet backend support.', 32 | long_description=long_description, 33 | author='Amazon Web Services', 34 | url='https://github.com/awslabs/keras-apache-mxnet', 35 | license='MIT', 36 | install_requires=['numpy>=1.9.1', 37 | 'scipy>=0.14', 38 | 'six>=1.9.0', 39 | 'pyyaml', 40 | 'h5py', 41 | 'keras_applications>=1.0.6', 42 | 'keras_preprocessing>=1.0.5'], 43 | extras_require={ 44 | 'visualize': ['pydot>=1.2.4'], 45 | 'tests': ['pytest', 46 | 'pytest-pep8', 47 | 'pytest-xdist', 48 | 'pytest-cov', 49 | 'pytest-timeout', 50 | 'pandas', 51 | 'requests'], 52 | }, 53 | classifiers=[ 54 | 'Development Status :: 5 - Production/Stable', 55 | 'Intended Audience :: Developers', 56 | 'Intended Audience :: Education', 57 | 'Intended Audience :: Science/Research', 58 | 'License :: OSI Approved :: MIT License', 59 | 'Programming Language :: Python :: 2', 60 | 'Programming Language :: Python :: 2.7', 61 | 'Programming Language :: Python :: 3', 62 | 'Programming Language :: Python :: 3.6', 63 | 'Topic :: Software Development :: Libraries', 64 | 'Topic :: Software Development :: Libraries :: Python Modules' 65 | ], 66 | packages=find_packages()) 67 | -------------------------------------------------------------------------------- /tests/conftest.py: -------------------------------------------------------------------------------- 1 | import pytest 2 | from keras import backend as K 3 | 4 | 5 | @pytest.fixture(autouse=True) 6 | def clear_session_after_test(): 7 | """Test wrapper to clean up after TensorFlow and CNTK tests. 8 | 9 | This wrapper runs for all the tests in the keras test suite. 10 | """ 11 | yield 12 | if K.backend() == 'tensorflow' or K.backend() == 'cntk': 13 | K.clear_session() 14 | -------------------------------------------------------------------------------- /tests/integration_tests/applications_test.py: -------------------------------------------------------------------------------- 1 | import pytest 2 | import random 3 | import os 4 | from multiprocessing import Process, Queue 5 | from keras import applications 6 | from keras import backend as K 7 | 8 | 9 | pytestmark = pytest.mark.skipif( 10 | K.backend() == 'mxnet' or 11 | (os.environ.get('CORE_CHANGED', 'True') == 'False' and 12 | os.environ.get('APP_CHANGED', 'True') == 'False'), 13 | reason='Runs only when the relevant files have been modified.') 14 | 15 | 16 | MODEL_LIST = [ 17 | (applications.ResNet50, 2048), 18 | (applications.VGG16, 512), 19 | (applications.VGG19, 512), 20 | (applications.Xception, 2048), 21 | (applications.InceptionV3, 2048), 22 | (applications.InceptionResNetV2, 1536), 23 | (applications.MobileNet, 1024), 24 | (applications.MobileNetV2, 1280), 25 | (applications.DenseNet121, 1024), 26 | (applications.DenseNet169, 1664), 27 | (applications.DenseNet201, 1920), 28 | # Note that NASNetLarge is too heavy to test on Travis. 29 | (applications.NASNetMobile, 1056) 30 | ] 31 | 32 | 33 | def _get_output_shape(model_fn): 34 | if K.backend() == 'cntk': 35 | # Create model in a subprocess so that 36 | # the memory consumed by InceptionResNetV2 will be 37 | # released back to the system after this test 38 | # (to deal with OOM error on CNTK backend). 39 | # TODO: remove the use of multiprocessing from these tests 40 | # once a memory clearing mechanism 41 | # is implemented in the CNTK backend. 42 | def target(queue): 43 | model = model_fn() 44 | queue.put(model.output_shape) 45 | queue = Queue() 46 | p = Process(target=target, args=(queue,)) 47 | p.start() 48 | p.join() 49 | # The error in a subprocess won't propagate 50 | # to the main process, so we check if the model 51 | # is successfully created by checking if the output shape 52 | # has been put into the queue 53 | assert not queue.empty(), 'Model creation failed.' 54 | return queue.get_nowait() 55 | else: 56 | model = model_fn() 57 | return model.output_shape 58 | 59 | 60 | def _test_application_basic(app, last_dim=1000): 61 | output_shape = _get_output_shape(lambda: app(weights=None)) 62 | assert output_shape == (None, last_dim) 63 | 64 | 65 | @pytest.mark.skipif((K.backend() == 'mxnet'), 66 | reason='MXNet backend requires input shape for convolution') 67 | def _test_application_notop(app, last_dim): 68 | output_shape = _get_output_shape( 69 | lambda: app(weights=None, include_top=False)) 70 | assert output_shape == (None, None, None, last_dim) 71 | 72 | 73 | def test_mobilenet_v2_legacy_import(): 74 | from keras.applications import mobilenetv2 75 | assert hasattr(mobilenetv2, 'MobileNetV2') 76 | from keras.applications import mobilenet_v2 77 | assert hasattr(mobilenet_v2, 'MobileNetV2') 78 | 79 | 80 | def test_applications(): 81 | for _ in range(3): 82 | app, last_dim = random.choice(MODEL_LIST) 83 | _test_application_basic(app) 84 | _test_application_notop(app, last_dim) 85 | 86 | 87 | if __name__ == '__main__': 88 | pytest.main([__file__]) 89 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /tests/integration_tests/test_eia_integration.py: -------------------------------------------------------------------------------- 1 | import pytest 2 | import keras 3 | import numpy as np 4 | from keras import backend as K 5 | from keras.applications.vgg16 import VGG16 6 | from keras.applications.vgg16 import preprocess_input, decode_predictions 7 | 8 | 9 | def has_eia(): 10 | if K.backend() != 'mxnet': 11 | return False 12 | 13 | import mxnet as mx 14 | try: 15 | # try to create eia context 16 | mx.eia() 17 | except: 18 | return False 19 | 20 | return True 21 | 22 | 23 | @pytest.mark.skipif(K.backend() != 'mxnet' or not has_eia(), 24 | reason='Inference with AWS EIA is currently supported ' 25 | 'with MXNet backend only. We need to have EIA ' 26 | 'to run Keras predictions on EIA tests.') 27 | def test_prediction_with_eia(): 28 | import mxnet as mx 29 | 30 | # 1. Download and save ImageNet Pre-Trained VGG-16 31 | model = VGG16(weights='imagenet', input_shape=(224, 224, 3)) 32 | model.save("imagenet_vgg16.h5") 33 | 34 | # 2. Load the Model in EIA Context 35 | with K.Context("eia"): 36 | model = keras.models.load_model("imagenet_vgg16.h5") 37 | 38 | # Verify Model is loaded in EIA context 39 | assert model._context 40 | assert model._context[0] == mx.eia() 41 | 42 | # 3. Prepare inputs for prediction 43 | dummy_image1 = np.random.randint(low=0, high=255, size=(224, 224, 3)) 44 | dummy_image1 = np.expand_dims(dummy_image1, axis=0) 45 | dummy_image1 = preprocess_input(dummy_image1) 46 | preds = model.predict(dummy_image1) 47 | assert len(decode_predictions(preds, top=3)[0]) == 3 48 | 49 | # 4. Test batch prediction 50 | dummy_image2 = np.random.randint(low=0, high=255, size=(224, 224, 3)) 51 | dummy_image2 = np.expand_dims(dummy_image2, axis=0) 52 | dummy_image2 = preprocess_input(dummy_image2) 53 | 54 | batch_input = np.concatenate((dummy_image1, dummy_image2), axis=0) 55 | batch_preds = model.predict_on_batch(batch_input) 56 | assert len(batch_preds) == 2 57 | for pred in decode_predictions(batch_preds, top=3): 58 | assert len(pred[0]) == 3 59 | 60 | 61 | if __name__ == '__main__': 62 | pytest.main([__file__]) 63 | -------------------------------------------------------------------------------- /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 6 | from keras.models import Sequential 7 | from keras import layers 8 | from keras.utils.np_utils import to_categorical 9 | from keras import backend as K 10 | 11 | pytestmark = pytest.mark.skipif(K.backend() == 'mxnet', 12 | reason='MXNet backend does not support Pooling2d with SAME mode yet.') 13 | 14 | 15 | def test_image_classification(): 16 | np.random.seed(1337) 17 | input_shape = (16, 16, 3) 18 | (x_train, y_train), (x_test, y_test) = get_test_data(num_train=500, 19 | num_test=200, 20 | input_shape=input_shape, 21 | classification=True, 22 | num_classes=4) 23 | y_train = to_categorical(y_train) 24 | y_test = to_categorical(y_test) 25 | 26 | model = Sequential([ 27 | layers.Conv2D(filters=8, kernel_size=3, 28 | activation='relu', 29 | input_shape=input_shape), 30 | layers.MaxPooling2D(pool_size=2), 31 | layers.Conv2D(filters=4, kernel_size=(3, 3), 32 | activation='relu', padding='same'), 33 | layers.GlobalAveragePooling2D(), 34 | layers.Dense(y_test.shape[-1], activation='softmax') 35 | ]) 36 | model.compile(loss='categorical_crossentropy', 37 | optimizer='rmsprop', 38 | metrics=['accuracy']) 39 | model.summary() 40 | history = model.fit(x_train, y_train, epochs=10, batch_size=16, 41 | validation_data=(x_test, y_test), 42 | verbose=0) 43 | assert history.history['val_acc'][-1] > 0.75 44 | config = model.get_config() 45 | model = Sequential.from_config(config) 46 | 47 | 48 | if __name__ == '__main__': 49 | pytest.main([__file__]) 50 | -------------------------------------------------------------------------------- /tests/integration_tests/test_tensorflow_integration.py: -------------------------------------------------------------------------------- 1 | from __future__ import print_function 2 | 3 | import os 4 | import tempfile 5 | import pytest 6 | import keras 7 | from keras import layers 8 | from keras.utils.test_utils import get_test_data 9 | 10 | 11 | @pytest.mark.skipif(keras.backend.backend() != 'tensorflow', 12 | reason='Requires TF backend') 13 | def test_tf_optimizer(): 14 | import tensorflow as tf 15 | 16 | num_hidden = 10 17 | output_dim = 2 18 | input_dim = 10 19 | target = 0.8 20 | optimizer = tf.train.AdadeltaOptimizer( 21 | learning_rate=1., rho=0.95, epsilon=1e-08) 22 | 23 | (x_train, y_train), (x_test, y_test) = get_test_data( 24 | num_train=1000, num_test=200, 25 | input_shape=(input_dim,), 26 | classification=True, num_classes=output_dim) 27 | 28 | model = keras.Sequential() 29 | model.add(layers.Dense(num_hidden, 30 | activation='relu', 31 | input_shape=(input_dim,))) 32 | model.add(layers.Dense(output_dim, activation='softmax')) 33 | 34 | model.compile(loss='sparse_categorical_crossentropy', 35 | optimizer=optimizer, 36 | metrics=['accuracy']) 37 | history = model.fit(x_train, y_train, epochs=8, batch_size=16, 38 | validation_data=(x_test, y_test), verbose=2) 39 | assert history.history['val_acc'][-1] >= target 40 | 41 | # Test saving. 42 | _, fname = tempfile.mkstemp('.h5') 43 | model.save(fname) 44 | model = keras.models.load_model(fname) 45 | assert len(model.weights) == 4 46 | os.remove(fname) 47 | 48 | 49 | if __name__ == '__main__': 50 | pytest.main([__file__]) 51 | -------------------------------------------------------------------------------- /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 5 | from keras.models import Sequential 6 | from keras import layers 7 | import keras 8 | from keras.utils.np_utils import to_categorical 9 | import keras.backend as K 10 | 11 | num_classes = 2 12 | 13 | 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 | @pytest.mark.skipif(K.backend() == 'mxnet', reason='MXNet backend does not support Sparse yet.') 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 | def test_vector_regression(): 70 | ''' 71 | Perform float data prediction (regression) using 2 layer MLP 72 | with tanh and sigmoid activations. 73 | ''' 74 | (x_train, y_train), (x_test, y_test) = get_test_data(num_train=500, 75 | num_test=200, 76 | input_shape=(20,), 77 | output_shape=(num_classes,), 78 | classification=False) 79 | 80 | model = Sequential([ 81 | layers.Dense(16, input_shape=(x_train.shape[-1],), activation='tanh'), 82 | layers.Dense(num_classes) 83 | ]) 84 | 85 | model.compile(loss='hinge', optimizer='adagrad') 86 | history = model.fit(x_train, y_train, epochs=20, batch_size=16, 87 | validation_data=(x_test, y_test), verbose=0) 88 | assert (history.history['val_loss'][-1] < 0.9) 89 | 90 | 91 | if __name__ == '__main__': 92 | pytest.main([__file__]) 93 | -------------------------------------------------------------------------------- /tests/keras/backend/mxnet_context_test.py: -------------------------------------------------------------------------------- 1 | import mxnet as mx 2 | import pytest 3 | 4 | import keras 5 | from keras import backend as K 6 | from keras.datasets import mnist 7 | from keras.layers import Dense, Dropout 8 | from keras.models import Sequential 9 | from keras.optimizers import RMSprop 10 | 11 | pytestmark = pytest.mark.skipif(K.backend() != 'mxnet', 12 | reason='Testing MXNet context supports only for MXNet backend') 13 | 14 | 15 | class TestMXNetContext(object): 16 | batch_size = 128 17 | num_classes = 10 18 | epochs = 2 19 | gpus = mx.test_utils.list_gpus() 20 | if len(gpus) > 0: 21 | context = 'gpu(%d)' % gpus[-1] 22 | else: 23 | context = 'cpu' 24 | 25 | def _get_data(self): 26 | (x_train, y_train), (x_test, y_test) = mnist.load_data() 27 | 28 | x_train = x_train.reshape(60000, 784)[:500] 29 | x_train = x_train.astype('float32') 30 | x_train /= 255 31 | y_train = keras.utils.to_categorical(y_train[:500], self.num_classes) 32 | return x_train, y_train 33 | 34 | def _get_model(self): 35 | model = Sequential() 36 | model.add(Dense(512, activation='relu', input_shape=(784,))) 37 | model.add(Dropout(0.2)) 38 | model.add(Dense(512, activation='relu')) 39 | model.add(Dropout(0.2)) 40 | model.add(Dense(self.num_classes, activation='softmax')) 41 | return model 42 | 43 | def test_context_model(self): 44 | model = Sequential(context=self.context) 45 | model.add(Dense(512, activation='relu', input_shape=(784,))) 46 | model.add(Dropout(0.2)) 47 | model.add(Dense(512, activation='relu')) 48 | model.add(Dropout(0.2)) 49 | model.add(Dense(self.num_classes, activation='softmax')) 50 | model.compile(loss='categorical_crossentropy', 51 | optimizer=RMSprop(), 52 | metrics=['accuracy']) 53 | x_train, y_train = self._get_data() 54 | model.fit(x_train, y_train, 55 | batch_size=self.batch_size, 56 | epochs=self.epochs) 57 | if len(self.gpus) > 0: 58 | assert model._context == [mx.gpu(self.gpus[-1])] 59 | else: 60 | assert model._context == [mx.cpu()] 61 | 62 | def test_context_compile(self): 63 | model = self._get_model() 64 | model.compile(loss='categorical_crossentropy', 65 | optimizer=RMSprop(), 66 | metrics=['accuracy'], 67 | context=self.context) 68 | x_train, y_train = self._get_data() 69 | model.fit(x_train, y_train, 70 | batch_size=self.batch_size, 71 | epochs=self.epochs) 72 | if len(self.gpus) > 0: 73 | assert model._context == [mx.gpu(self.gpus[-1])] 74 | else: 75 | assert model._context == [mx.cpu()] 76 | 77 | def test_set_mxnet_context(self): 78 | model = self._get_model() 79 | model.set_mxnet_context(self.context) 80 | model.compile(loss='categorical_crossentropy', 81 | optimizer=RMSprop(), 82 | metrics=['accuracy']) 83 | 84 | x_train, y_train = self._get_data() 85 | model.fit(x_train, y_train, 86 | batch_size=self.batch_size, 87 | epochs=self.epochs) 88 | if len(self.gpus) > 0: 89 | assert model._context == [mx.gpu(self.gpus[-1])] 90 | else: 91 | assert model._context == [mx.cpu()] 92 | 93 | if __name__ == '__main__': 94 | pytest.main([__file__]) 95 | -------------------------------------------------------------------------------- /tests/keras/backend/mxnet_operator_test.py: -------------------------------------------------------------------------------- 1 | import pytest 2 | 3 | import numpy as np 4 | import tempfile 5 | from keras import backend as K 6 | from keras import Model 7 | from keras.layers import Input, Dense, Activation, BatchNormalization 8 | 9 | pytestmark = pytest.mark.skipif(K.backend() != 'mxnet', 10 | reason='Testing MXNet context supports only for MXNet backend') 11 | 12 | 13 | class TestMXNetOperator(object): 14 | 15 | def test_batchnorm_freeze(self): 16 | data = np.array([ 17 | [i, i ** 2, i + 2] for i in range(10) 18 | ]) 19 | label = np.array([ 20 | [i % 2] for i in range(10) 21 | ]) 22 | x = Input(shape=(3,), name='x') 23 | f = Dense(10, name='h1')(x) 24 | f = BatchNormalization(name='bn1')(f) 25 | f = Activation('relu', name='r1')(f) 26 | y = Dense(1, name='y')(f) 27 | 28 | model = Model(inputs=[x], outputs=[y]) 29 | model.compile(loss='binary_crossentropy', optimizer='sgd') 30 | model.fit(data, label, batch_size=5, epochs=10, verbose=1) 31 | _, fname = tempfile.mkstemp('.h5') 32 | model.save_weights(fname) 33 | 34 | # reconstruct model with trainable=False except last layer 35 | x = Input(shape=(3,), name='x') 36 | f = Dense(10, name='h1', trainable=False)(x) 37 | f = BatchNormalization(name='bn1', trainable=False)(f) 38 | f = Activation('relu', name='r1', trainable=False)(f) 39 | y = Dense(1, name='y')(f) 40 | 41 | loaded = Model(inputs=[x], outputs=[y]) 42 | loaded.load_weights(fname) 43 | loaded.compile(loss='binary_crossentropy', optimizer='sgd') 44 | 45 | loaded.fit(data, label, batch_size=5, epochs=10, verbose=1) 46 | for l1, l2 in zip(model.layers, loaded.layers): 47 | l1_weights = l1.get_weights() 48 | l2_weights = l2.get_weights() 49 | check = True 50 | for idx in range(len(l1_weights)): 51 | check &= np.allclose(l1_weights[idx], l2_weights[idx]) 52 | # only the last layers have different weights 53 | if l1.name != 'y': 54 | assert check is True 55 | 56 | 57 | if __name__ == '__main__': 58 | pytest.main([__file__]) 59 | -------------------------------------------------------------------------------- /tests/keras/backend/mxnet_tf_model_test.py: -------------------------------------------------------------------------------- 1 | import pytest 2 | 3 | import tempfile 4 | from keras import backend as K 5 | from keras.layers import Dense, BatchNormalization 6 | from keras.models import load_model, Sequential 7 | from keras.backend import tensorflow_backend as KTF 8 | import warnings 9 | 10 | pytestmark = pytest.mark.skipif(K.backend() != "mxnet", 11 | reason="Testing MXNet context supports only for MXNet backend") 12 | 13 | 14 | class TestMXNetTfModel(object): 15 | def test_batchnorm_layer_reload(self): 16 | # Save a tf backend keras h5 model 17 | tf_model = KTF.tf.keras.models.Sequential([ 18 | KTF.tf.keras.layers.Dense(10, kernel_initializer="zeros"), 19 | KTF.tf.keras.layers.BatchNormalization(), 20 | ]) 21 | tf_model.build(input_shape=(1, 10)) 22 | _, fname = tempfile.mkstemp(".h5") 23 | tf_model.save(fname) 24 | 25 | # Load from MXNet backend keras 26 | try: 27 | mx_model = load_model(fname, compile=False) 28 | except TypeError: 29 | warnings.warn("Could not reload from tensorflow backend saved model.") 30 | assert False 31 | 32 | # Retest with mxnet backend keras save + load 33 | mx_model_2 = Sequential([ 34 | Dense(10, kernel_initializer="zeros"), 35 | BatchNormalization(), 36 | ]) 37 | mx_model_2.build(input_shape=(1, 10)) 38 | _, fname = tempfile.mkstemp(".h5") 39 | mx_model_2.save(fname) 40 | 41 | try: 42 | mx_model_3 = load_model(fname, compile=False) 43 | except TypeError: 44 | warnings.warn("Could not reload from MXNet backend saved model.") 45 | assert False 46 | 47 | 48 | if __name__ == "__main__": 49 | pytest.main([__file__]) 50 | -------------------------------------------------------------------------------- /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 | 8 | 9 | def get_test_values(): 10 | return [0.1, 0.5, 3, 8, 1e-7] 11 | 12 | 13 | def get_example_array(): 14 | np.random.seed(3537) 15 | example_array = np.random.random((100, 100)) * 100. - 50. 16 | example_array[0, 0] = 0. # 0 could possibly cause trouble 17 | return example_array 18 | 19 | 20 | def test_serialization(): 21 | all_activations = ['max_norm', 'non_neg', 22 | 'unit_norm', 'min_max_norm'] 23 | for name in all_activations: 24 | fn = constraints.get(name) 25 | ref_fn = getattr(constraints, name)() 26 | assert fn.__class__ == ref_fn.__class__ 27 | config = constraints.serialize(fn) 28 | fn = constraints.deserialize(config) 29 | assert fn.__class__ == ref_fn.__class__ 30 | 31 | 32 | def test_max_norm(): 33 | array = get_example_array() 34 | for m in get_test_values(): 35 | norm_instance = constraints.max_norm(m) 36 | normed = norm_instance(K.variable(array)) 37 | assert(np.all(K.eval(normed) < m)) 38 | 39 | # a more explicit example 40 | norm_instance = constraints.max_norm(2.0) 41 | x = np.array([[0, 0, 0], [1.0, 0, 0], [3, 0, 0], [3, 3, 3]]).T 42 | x_normed_target = np.array([[0, 0, 0], [1.0, 0, 0], 43 | [2.0, 0, 0], 44 | [2. / np.sqrt(3), 45 | 2. / np.sqrt(3), 46 | 2. / np.sqrt(3)]]).T 47 | x_normed_actual = K.eval(norm_instance(K.variable(x))) 48 | assert_allclose(x_normed_actual, x_normed_target, rtol=1e-05) 49 | 50 | 51 | def test_non_neg(): 52 | non_neg_instance = constraints.non_neg() 53 | normed = non_neg_instance(K.variable(get_example_array())) 54 | assert(np.all(np.min(K.eval(normed), axis=1) == 0.)) 55 | 56 | 57 | def test_unit_norm(): 58 | unit_norm_instance = constraints.unit_norm() 59 | normalized = unit_norm_instance(K.variable(get_example_array())) 60 | norm_of_normalized = np.sqrt(np.sum(K.eval(normalized) ** 2, axis=0)) 61 | # In the unit norm constraint, it should be equal to 1. 62 | difference = norm_of_normalized - 1. 63 | largest_difference = np.max(np.abs(difference)) 64 | assert(np.abs(largest_difference) < 10e-5) 65 | 66 | 67 | def test_min_max_norm(): 68 | array = get_example_array() 69 | for m in get_test_values(): 70 | norm_instance = constraints.min_max_norm(min_value=m, max_value=m * 2) 71 | normed = norm_instance(K.variable(array)) 72 | value = K.eval(normed) 73 | l2 = np.sqrt(np.sum(np.square(value), axis=0)) 74 | assert not l2[l2 < m] 75 | assert not l2[l2 > m * 2 + 1e-5] 76 | 77 | 78 | if __name__ == '__main__': 79 | pytest.main([__file__]) 80 | -------------------------------------------------------------------------------- /tests/keras/layers/advanced_activations_test.py: -------------------------------------------------------------------------------- 1 | import pytest 2 | from keras.utils.test_utils import layer_test 3 | from keras import layers 4 | from keras import backend as K 5 | 6 | 7 | def test_leaky_relu(): 8 | for alpha in [0., .5, -1.]: 9 | layer_test(layers.LeakyReLU, kwargs={'alpha': alpha}, 10 | input_shape=(2, 3, 4)) 11 | 12 | 13 | def test_prelu(): 14 | layer_test(layers.PReLU, kwargs={}, 15 | input_shape=(2, 3, 4)) 16 | 17 | 18 | def test_prelu_share(): 19 | layer_test(layers.PReLU, kwargs={'shared_axes': 1}, 20 | input_shape=(2, 3, 4)) 21 | 22 | 23 | def test_elu(): 24 | for alpha in [0., .5, -1.]: 25 | layer_test(layers.ELU, kwargs={'alpha': alpha}, 26 | input_shape=(2, 3, 4)) 27 | 28 | 29 | def test_thresholded_relu(): 30 | layer_test(layers.ThresholdedReLU, kwargs={'theta': 0.5}, 31 | input_shape=(2, 3, 4)) 32 | 33 | 34 | def test_softmax(): 35 | for axis in [1, -1]: 36 | layer_test(layers.Softmax, kwargs={'axis': axis}, 37 | input_shape=(2, 3, 4)) 38 | 39 | 40 | def test_relu(): 41 | layer_test(layers.ReLU, 42 | kwargs={'max_value': 10, 43 | 'negative_slope': 0.2, 44 | 'threshold': 3.0}, 45 | input_shape=(2, 3, 4)) 46 | layer_test(layers.ReLU, 47 | kwargs={'max_value': 6}, 48 | input_shape=(2, 3, 4)) 49 | layer_test(layers.ReLU, 50 | kwargs={'negative_slope': 0.2}, 51 | input_shape=(2, 3, 4)) 52 | 53 | # max_value of ReLU layer cannot be negative value 54 | with pytest.raises(ValueError): 55 | layer_test(layers.ReLU, kwargs={'max_value': -2.0}, 56 | input_shape=(2, 3, 4)) 57 | 58 | # negative_slope of ReLU layer cannot be negative value 59 | with pytest.raises(ValueError): 60 | layer_test(layers.ReLU, kwargs={'negative_slope': -2.0}, 61 | input_shape=(2, 3, 4)) 62 | 63 | 64 | @pytest.mark.skipif((K.backend() != 'tensorflow'), 65 | reason='TF-specific implementation.') 66 | def test_relu_tf_ops(): 67 | inputs = layers.Input((3,)) 68 | # Test that `relu` op gets used. 69 | outputs = layers.ReLU()(inputs) 70 | assert outputs.op.name.lower().endswith('/relu') 71 | # Test that `leakyrelu` op gets used. 72 | outputs = layers.ReLU(negative_slope=0.2)(inputs) 73 | assert outputs.op.name.lower().endswith('/leakyrelu') 74 | # Test that `relu6` op gets used. 75 | outputs = layers.ReLU(max_value=6)(inputs) 76 | assert outputs.op.name.lower().endswith('/relu6') 77 | 78 | 79 | if __name__ == '__main__': 80 | pytest.main([__file__]) 81 | -------------------------------------------------------------------------------- /tests/keras/layers/embeddings_test.py: -------------------------------------------------------------------------------- 1 | import pytest 2 | from keras.utils.test_utils import layer_test 3 | from keras.layers.embeddings import Embedding 4 | from keras.models import Sequential 5 | import keras.backend as K 6 | 7 | 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, 26 | 'input_length': (None, 5)}, 27 | input_shape=(3, 2, 5), 28 | input_dtype='int32', 29 | expected_output_dtype=K.floatx()) 30 | layer_test(Embedding, 31 | kwargs={'output_dim': 4, 'input_dim': 10, 'mask_zero': True, 'input_length': (None, 5), 32 | 'sparse_grad': True}, 33 | input_shape=(3, 2, 5), 34 | input_dtype='int32', 35 | expected_output_dtype=K.floatx() 36 | ) 37 | 38 | 39 | def test_embedding_invalid(): 40 | 41 | # len(input_length) should be equal to len(input_shape) - 1 42 | with pytest.raises(ValueError): 43 | model = Sequential([Embedding( 44 | input_dim=10, 45 | output_dim=4, 46 | input_length=2, 47 | input_shape=(3, 4, 5))]) 48 | 49 | # input_length should be equal to input_shape[1:] 50 | with pytest.raises(ValueError): 51 | model = Sequential([Embedding( 52 | input_dim=10, 53 | output_dim=4, 54 | input_length=2, 55 | input_shape=(3, 5))]) 56 | 57 | 58 | if __name__ == '__main__': 59 | pytest.main([__file__]) 60 | -------------------------------------------------------------------------------- /tests/keras/layers/local_test.py: -------------------------------------------------------------------------------- 1 | import pytest 2 | 3 | from keras.utils.test_utils import layer_test 4 | from keras.layers import local 5 | from keras import backend as K 6 | 7 | 8 | pytestmark = pytest.mark.skipif(K.backend() == 'mxnet', 9 | reason='MXNet backend does not support local_conv1d/2d yet.') 10 | 11 | 12 | def test_locallyconnected_1d(): 13 | num_samples = 2 14 | num_steps = 8 15 | input_dim = 5 16 | filter_length = 3 17 | filters = 4 18 | padding = 'valid' 19 | strides = 1 20 | 21 | layer_test(local.LocallyConnected1D, 22 | kwargs={'filters': filters, 23 | 'kernel_size': filter_length, 24 | 'padding': padding, 25 | 'kernel_regularizer': 'l2', 26 | 'bias_regularizer': 'l2', 27 | 'activity_regularizer': 'l2', 28 | 'strides': strides}, 29 | input_shape=(num_samples, num_steps, input_dim)) 30 | 31 | 32 | def test_locallyconnected_2d(): 33 | num_samples = 5 34 | filters = 3 35 | stack_size = 4 36 | num_row = 6 37 | num_col = 8 38 | padding = 'valid' 39 | 40 | for strides in [(1, 1), (2, 2)]: 41 | layer_test(local.LocallyConnected2D, 42 | kwargs={'filters': filters, 43 | 'kernel_size': 3, 44 | 'padding': padding, 45 | 'kernel_regularizer': 'l2', 46 | 'bias_regularizer': 'l2', 47 | 'activity_regularizer': 'l2', 48 | 'strides': strides, 49 | 'data_format': 'channels_last'}, 50 | input_shape=(num_samples, num_row, num_col, stack_size)) 51 | 52 | layer_test(local.LocallyConnected2D, 53 | kwargs={'filters': filters, 54 | 'kernel_size': (3, 3), 55 | 'padding': padding, 56 | 'kernel_regularizer': 'l2', 57 | 'bias_regularizer': 'l2', 58 | 'activity_regularizer': 'l2', 59 | 'strides': strides, 60 | 'data_format': 'channels_first'}, 61 | input_shape=(num_samples, stack_size, num_row, num_col)) 62 | 63 | 64 | if __name__ == '__main__': 65 | pytest.main([__file__]) 66 | -------------------------------------------------------------------------------- /tests/keras/layers/noise_test.py: -------------------------------------------------------------------------------- 1 | import pytest 2 | from keras.utils.test_utils import layer_test 3 | from keras.layers import noise 4 | from keras import backend as K 5 | 6 | 7 | @pytest.mark.skipif((K.backend() == 'cntk' or K.backend() == 'mxnet'), 8 | reason='cntk/mxnet does not support it yet') 9 | def test_GaussianNoise(): 10 | layer_test(noise.GaussianNoise, 11 | kwargs={'stddev': 1.}, 12 | input_shape=(3, 2, 3)) 13 | 14 | 15 | @pytest.mark.skipif((K.backend() == 'cntk' or K.backend() == 'mxnet'), 16 | reason='cntk/mxnet does not support it yet') 17 | def test_GaussianDropout(): 18 | layer_test(noise.GaussianDropout, 19 | kwargs={'rate': 0.5}, 20 | input_shape=(3, 2, 3)) 21 | 22 | 23 | @pytest.mark.skipif((K.backend() == 'cntk' or K.backend() == 'mxnet'), 24 | reason='cntk/mxnet does not support it yet') 25 | def test_AlphaDropout(): 26 | layer_test(noise.AlphaDropout, 27 | kwargs={'rate': 0.1}, 28 | input_shape=(3, 2, 3)) 29 | 30 | 31 | if __name__ == '__main__': 32 | pytest.main([__file__]) 33 | -------------------------------------------------------------------------------- /tests/keras/legacy/layers_test.py: -------------------------------------------------------------------------------- 1 | import pytest 2 | 3 | from keras.utils.test_utils import layer_test 4 | from keras.legacy import layers as legacy_layers 5 | from keras import regularizers 6 | from keras import constraints 7 | 8 | 9 | def test_highway(): 10 | layer_test(legacy_layers.Highway, 11 | kwargs={}, 12 | input_shape=(3, 2)) 13 | 14 | layer_test(legacy_layers.Highway, 15 | kwargs={'W_regularizer': regularizers.l2(0.01), 16 | 'b_regularizer': regularizers.l1(0.01), 17 | 'activity_regularizer': regularizers.l2(0.01), 18 | 'W_constraint': constraints.MaxNorm(1), 19 | 'b_constraint': constraints.MaxNorm(1)}, 20 | input_shape=(3, 2)) 21 | 22 | 23 | def test_maxout_dense(): 24 | layer_test(legacy_layers.MaxoutDense, 25 | kwargs={'output_dim': 3}, 26 | input_shape=(3, 2)) 27 | 28 | layer_test(legacy_layers.MaxoutDense, 29 | kwargs={'output_dim': 3, 30 | 'W_regularizer': regularizers.l2(0.01), 31 | 'b_regularizer': regularizers.l1(0.01), 32 | 'activity_regularizer': regularizers.l2(0.01), 33 | 'W_constraint': constraints.MaxNorm(1), 34 | 'b_constraint': constraints.MaxNorm(1)}, 35 | input_shape=(3, 2)) 36 | 37 | 38 | if __name__ == '__main__': 39 | pytest.main([__file__]) 40 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /tests/keras/utils/conv_utils_test.py: -------------------------------------------------------------------------------- 1 | import pytest 2 | import numpy as np 3 | from keras.utils import conv_utils 4 | from keras import backend as K 5 | 6 | 7 | def test_normalize_tuple(): 8 | assert conv_utils.normalize_tuple(5, 2, 'kernel_size') == (5, 5) 9 | assert conv_utils.normalize_tuple([7, 9], 2, 'kernel_size') == (7, 9) 10 | 11 | with pytest.raises(ValueError): 12 | conv_utils.normalize_tuple(None, 2, 'kernel_size') 13 | with pytest.raises(ValueError): 14 | conv_utils.normalize_tuple([2, 3, 4], 2, 'kernel_size') 15 | with pytest.raises(ValueError): 16 | conv_utils.normalize_tuple(['str', 'impossible'], 2, 'kernel_size') 17 | 18 | 19 | def test_invalid_data_format(): 20 | with pytest.raises(ValueError): 21 | K.normalize_data_format('channels_middle') 22 | 23 | 24 | def test_invalid_padding(): 25 | with pytest.raises(ValueError): 26 | conv_utils.normalize_padding('diagonal') 27 | 28 | 29 | def test_invalid_convert_kernel(): 30 | with pytest.raises(ValueError): 31 | conv_utils.convert_kernel(np.zeros((10, 20))) 32 | 33 | 34 | def test_conv_output_length(): 35 | assert conv_utils.conv_output_length(None, 7, 'same', 1) is None 36 | assert conv_utils.conv_output_length(224, 7, 'same', 1) == 224 37 | assert conv_utils.conv_output_length(224, 7, 'same', 2) == 112 38 | assert conv_utils.conv_output_length(32, 5, 'valid', 1) == 28 39 | assert conv_utils.conv_output_length(32, 5, 'valid', 2) == 14 40 | assert conv_utils.conv_output_length(32, 5, 'causal', 1) == 32 41 | assert conv_utils.conv_output_length(32, 5, 'causal', 2) == 16 42 | assert conv_utils.conv_output_length(32, 5, 'full', 1) == 36 43 | assert conv_utils.conv_output_length(32, 5, 'full', 2) == 18 44 | 45 | with pytest.raises(AssertionError): 46 | conv_utils.conv_output_length(32, 5, 'diagonal', 2) 47 | 48 | 49 | def test_conv_input_length(): 50 | assert conv_utils.conv_input_length(None, 7, 'same', 1) is None 51 | assert conv_utils.conv_input_length(112, 7, 'same', 1) == 112 52 | assert conv_utils.conv_input_length(112, 7, 'same', 2) == 223 53 | assert conv_utils.conv_input_length(28, 5, 'valid', 1) == 32 54 | assert conv_utils.conv_input_length(14, 5, 'valid', 2) == 31 55 | assert conv_utils.conv_input_length(36, 5, 'full', 1) == 32 56 | assert conv_utils.conv_input_length(18, 5, 'full', 2) == 31 57 | 58 | with pytest.raises(AssertionError): 59 | conv_utils.conv_output_length(18, 5, 'diagonal', 2) 60 | 61 | 62 | def test_deconv_length(): 63 | assert conv_utils.deconv_length(None, 1, 7, 'same', None) is None 64 | assert conv_utils.deconv_length(224, 1, 7, 'same', None) == 224 65 | assert conv_utils.deconv_length(224, 2, 7, 'same', None) == 448 66 | assert conv_utils.deconv_length(32, 1, 5, 'valid', None) == 36 67 | assert conv_utils.deconv_length(32, 2, 5, 'valid', None) == 67 68 | assert conv_utils.deconv_length(32, 1, 5, 'full', None) == 28 69 | assert conv_utils.deconv_length(32, 2, 5, 'full', None) == 59 70 | assert conv_utils.deconv_length(224, 1, 7, 'same', 0) == 224 71 | assert conv_utils.deconv_length(224, 2, 7, 'same', 0) == 447 72 | assert conv_utils.deconv_length(224, 2, 7, 'same', 1) == 448 73 | assert conv_utils.deconv_length(32, 1, 5, 'valid', 0) == 36 74 | assert conv_utils.deconv_length(32, 2, 5, 'valid', 0) == 67 75 | assert conv_utils.deconv_length(32, 2, 5, 'valid', 1) == 68 76 | assert conv_utils.deconv_length(6, 1, 3, 'full', 0) == 4 77 | assert conv_utils.deconv_length(6, 2, 3, 'full', 1) == 10 78 | assert conv_utils.deconv_length(6, 2, 3, 'full', 2) == 11 79 | 80 | 81 | if __name__ == '__main__': 82 | pytest.main([__file__]) 83 | -------------------------------------------------------------------------------- /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 | 11 | 12 | @pytest.mark.skipif(K.backend() == 'mxnet', reason='Test assumes kernel in channels_last format always. MXNet backend ' 13 | 'has performance optimization that changes kernel from ' 14 | '"channels_first" to "channels_last" based on image_data_format') 15 | def test_convert_weights(): 16 | def get_model(shape, data_format): 17 | model = Sequential() 18 | model.add(Conv2D(filters=2, 19 | kernel_size=(4, 3), 20 | input_shape=shape, 21 | data_format=data_format)) 22 | model.add(Flatten()) 23 | model.add(Dense(5)) 24 | return model 25 | 26 | for data_format in ['channels_first', 'channels_last']: 27 | if data_format == 'channels_first': 28 | shape = (3, 5, 5) 29 | target_shape = (5, 5, 3) 30 | prev_shape = (2, 3, 2) 31 | flip = lambda x: np.flip(np.flip(x, axis=2), axis=3) 32 | transpose = lambda x: np.transpose(x, (0, 2, 3, 1)) 33 | target_data_format = 'channels_last' 34 | elif data_format == 'channels_last': 35 | shape = (5, 5, 3) 36 | target_shape = (3, 5, 5) 37 | prev_shape = (2, 2, 3) 38 | flip = lambda x: np.flip(np.flip(x, axis=1), axis=2) 39 | transpose = lambda x: np.transpose(x, (0, 3, 1, 2)) 40 | target_data_format = 'channels_first' 41 | 42 | model1 = get_model(shape, data_format) 43 | model2 = get_model(target_shape, target_data_format) 44 | conv = K.function([model1.input], [model1.layers[0].output]) 45 | 46 | x = np.random.random((1,) + shape) 47 | 48 | # Test equivalence of convert_all_kernels_in_model 49 | convout1 = conv([x])[0] 50 | layer_utils.convert_all_kernels_in_model(model1) 51 | convout2 = flip(conv([flip(x)])[0]) 52 | 53 | assert_allclose(convout1, convout2, atol=1e-5) 54 | 55 | # Test equivalence of convert_dense_weights_data_format 56 | out1 = model1.predict(x) 57 | layer_utils.convert_dense_weights_data_format( 58 | model1.layers[2], prev_shape, target_data_format) 59 | for (src, dst) in zip(model1.layers, model2.layers): 60 | dst.set_weights(src.get_weights()) 61 | out2 = model2.predict(transpose(x)) 62 | 63 | assert_allclose(out1, out2, atol=1e-5) 64 | 65 | 66 | if __name__ == '__main__': 67 | pytest.main([__file__]) 68 | -------------------------------------------------------------------------------- /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 | from keras.utils import to_channels_first 7 | 8 | 9 | def test_to_categorical(): 10 | num_classes = 5 11 | shapes = [(1,), (3,), (4, 3), (5, 4, 3), (3, 1), (3, 2, 1)] 12 | expected_shapes = [(1, num_classes), 13 | (3, num_classes), 14 | (4, 3, num_classes), 15 | (5, 4, 3, num_classes), 16 | (3, num_classes), 17 | (3, 2, num_classes)] 18 | labels = [np.random.randint(0, num_classes, shape) for shape in shapes] 19 | one_hots = [to_categorical(label, num_classes) for label in labels] 20 | for label, one_hot, expected_shape in zip(labels, 21 | one_hots, 22 | expected_shapes): 23 | # Check shape 24 | assert one_hot.shape == expected_shape 25 | # Make sure there are only 0s and 1s 26 | assert np.array_equal(one_hot, one_hot.astype(bool)) 27 | # Make sure there is exactly one 1 in a row 28 | assert np.all(one_hot.sum(axis=-1) == 1) 29 | # Get original labels back from one hots 30 | assert np.all(np.argmax(one_hot, -1).reshape(label.shape) == label) 31 | 32 | 33 | def test_to_channels_first(): 34 | shapes = [ 35 | (10, 32, 32, 3), 36 | (10, 24, 128, 128, 1) 37 | ] 38 | 39 | expected_shapes = [ 40 | (10, 3, 32, 32), 41 | (10, 1, 24, 128, 128) 42 | ] 43 | 44 | inputs = [np.random.randint(0, 255, shape) for shape in shapes] 45 | 46 | # Test for single numpy array 47 | for inp, exp_shape in zip(inputs, expected_shapes): 48 | inp = to_channels_first(inp) 49 | assert inp.shape == exp_shape 50 | 51 | # Test for list of numpy arrays 52 | inputs = to_channels_first(inputs) 53 | for inp, exp_shape in zip(inputs, expected_shapes): 54 | assert inp.shape == exp_shape 55 | 56 | shapes = [ 57 | (10, 32, 32, 64, 64, 3), 58 | (10, 28, 28), 59 | (10, 28), 60 | (10) 61 | ] 62 | 63 | # Negative use case 64 | # Test for single numpy array 65 | inputs = [np.random.randint(0, 255, shape) for shape in shapes] 66 | for inp in inputs: 67 | with pytest.raises(ValueError) as excinfo: 68 | to_channels_first(inp) 69 | assert str(excinfo.typename) == 'ValueError' 70 | 71 | # Test for list of numpy arrays 72 | with pytest.raises(ValueError) as excinfo: 73 | to_channels_first(inputs) 74 | assert str(excinfo.typename) == 'ValueError' 75 | 76 | 77 | if __name__ == '__main__': 78 | pytest.main([__file__]) 79 | -------------------------------------------------------------------------------- /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(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 | -------------------------------------------------------------------------------- /tests/test_dynamic_trainability.py: -------------------------------------------------------------------------------- 1 | from __future__ import absolute_import 2 | from __future__ import print_function 3 | import pytest 4 | 5 | from keras.models import Model, Sequential 6 | from keras.layers import Dense, Input 7 | 8 | 9 | def test_layer_trainability_switch(): 10 | # with constructor argument, in Sequential 11 | model = Sequential() 12 | model.add(Dense(2, trainable=False, input_dim=1)) 13 | assert model.trainable_weights == [] 14 | 15 | # by setting the `trainable` argument, in Sequential 16 | model = Sequential() 17 | layer = Dense(2, input_dim=1) 18 | model.add(layer) 19 | assert model.trainable_weights == layer.trainable_weights 20 | layer.trainable = False 21 | assert model.trainable_weights == [] 22 | 23 | # with constructor argument, in Model 24 | x = Input(shape=(1,)) 25 | y = Dense(2, trainable=False)(x) 26 | model = Model(x, y) 27 | assert model.trainable_weights == [] 28 | 29 | # by setting the `trainable` argument, in Model 30 | x = Input(shape=(1,)) 31 | layer = Dense(2) 32 | y = layer(x) 33 | model = Model(x, y) 34 | assert model.trainable_weights == layer.trainable_weights 35 | layer.trainable = False 36 | assert model.trainable_weights == [] 37 | 38 | 39 | def test_model_trainability_switch(): 40 | # a non-trainable model has no trainable weights 41 | x = Input(shape=(1,)) 42 | y = Dense(2)(x) 43 | model = Model(x, y) 44 | model.trainable = False 45 | assert model.trainable_weights == [] 46 | 47 | # same for Sequential 48 | model = Sequential() 49 | model.add(Dense(2, input_dim=1)) 50 | model.trainable = False 51 | assert model.trainable_weights == [] 52 | 53 | 54 | def test_nested_model_trainability(): 55 | # a Sequential inside a Model 56 | inner_model = Sequential() 57 | inner_model.add(Dense(2, input_dim=1)) 58 | 59 | x = Input(shape=(1,)) 60 | y = inner_model(x) 61 | outer_model = Model(x, y) 62 | assert outer_model.trainable_weights == inner_model.trainable_weights 63 | inner_model.trainable = False 64 | assert outer_model.trainable_weights == [] 65 | inner_model.trainable = True 66 | inner_model.layers[-1].trainable = False 67 | assert outer_model.trainable_weights == [] 68 | 69 | # a Sequential inside a Sequential 70 | inner_model = Sequential() 71 | inner_model.add(Dense(2, input_dim=1)) 72 | outer_model = Sequential() 73 | outer_model.add(inner_model) 74 | assert outer_model.trainable_weights == inner_model.trainable_weights 75 | inner_model.trainable = False 76 | assert outer_model.trainable_weights == [] 77 | inner_model.trainable = True 78 | inner_model.layers[-1].trainable = False 79 | assert outer_model.trainable_weights == [] 80 | 81 | # a Model inside a Model 82 | x = Input(shape=(1,)) 83 | y = Dense(2)(x) 84 | inner_model = Model(x, y) 85 | x = Input(shape=(1,)) 86 | y = inner_model(x) 87 | outer_model = Model(x, y) 88 | assert outer_model.trainable_weights == inner_model.trainable_weights 89 | inner_model.trainable = False 90 | assert outer_model.trainable_weights == [] 91 | inner_model.trainable = True 92 | inner_model.layers[-1].trainable = False 93 | assert outer_model.trainable_weights == [] 94 | 95 | # a Model inside a Sequential 96 | x = Input(shape=(1,)) 97 | y = Dense(2)(x) 98 | inner_model = Model(x, y) 99 | outer_model = Sequential() 100 | outer_model.add(inner_model) 101 | assert outer_model.trainable_weights == inner_model.trainable_weights 102 | inner_model.trainable = False 103 | assert outer_model.trainable_weights == [] 104 | inner_model.trainable = True 105 | inner_model.layers[-1].trainable = False 106 | assert outer_model.trainable_weights == [] 107 | 108 | 109 | if __name__ == '__main__': 110 | pytest.main([__file__]) 111 | -------------------------------------------------------------------------------- /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 import losses 8 | from keras import backend as K 9 | 10 | 11 | def test_masking(): 12 | np.random.seed(1337) 13 | x = np.array([[[1], [1]], 14 | [[0], [0]]]) 15 | model = Sequential() 16 | model.add(Masking(mask_value=0, input_shape=(2, 1))) 17 | model.add(TimeDistributed(Dense(1, kernel_initializer='one'))) 18 | model.compile(loss='mse', optimizer='sgd') 19 | y = np.array([[[1], [1]], 20 | [[1], [1]]]) 21 | loss = model.train_on_batch(x, y) 22 | assert loss == 0 23 | 24 | 25 | def test_loss_masking(): 26 | weighted_loss = weighted_masked_objective(losses.get('mae')) 27 | shape = (3, 4, 2) 28 | x = np.arange(24).reshape(shape) 29 | y = 2 * x 30 | 31 | # Normally the trailing 1 is added by standardize_weights 32 | weights = np.ones((3,)) 33 | mask = np.ones((3, 4)) 34 | mask[1, 0] = 0 35 | 36 | out = K.eval(weighted_loss(K.variable(x), 37 | K.variable(y), 38 | K.variable(weights), 39 | K.variable(mask))) 40 | 41 | 42 | if __name__ == '__main__': 43 | pytest.main([__file__]) 44 | --------------------------------------------------------------------------------