Comparar commits

..

226 Commits

Autor SHA1 Mensagem Data
Francois Chollet afbd5d34a3 merge 2017-05-24 14:57:32 -07:00
Francois Chollet 07e0fbc963 Style fixes. 2017-05-24 14:46:15 -07:00
nzw 7ef13165b7 Improve documents (#6727)
* Improve documents

* Fix style
2017-05-23 17:13:09 -07:00
Andrew Hundt d939f14843 stale bot specifies 30 days when it posts (#6735)
Got notified about some stale threads, but realized I forgot to put the number of days in the post's string.
2017-05-23 14:13:32 -07:00
Taehoon Lee ce0f97dbe3 Fix ImageNet weight loading for ResNet50 with channels_first (#6658)
Fix ImageNet weight loading for InceptionV3
2017-05-23 14:12:07 -07:00
cocuh 7e870a97ec Fix edge cases of custom object deserialization 2017-05-23 11:30:45 -07:00
Daniel Høyer Iversen a2c3fa2b96 Small clean ups (#6724)
* Remove unused variables

* progbar
2017-05-23 11:22:34 -07:00
nameless-Chatoyant 7f09d45efb Fix typo in docstring
Corrected a small annotation in Input()
2017-05-23 11:11:29 -07:00
meberstein 85fe6427a5 Added hinge loss for categorical classification (#6687)
* Fix bug in EarlyStopping to reset stopped_epoch in on_train_begin to allow it to be re-used

* Added hinge loss for categorical classification
2017-05-23 10:49:23 -07:00
Andrew Hundt b205ba1270 Automatically close stale issues (#6701)
@fchollet Merge this pull request plus follow https://github.com/integration/probot-stale to automatically mark the many 3 month old issues as stale, then close them after an additional 30 days.

I chose 30 additional days for closing because sometimes people go on vacation for a few weeks, this way they'll have time after being notified.
2017-05-23 10:34:31 -07:00
Francois Chollet 99e4e481c5 merge master 2017-05-22 15:30:24 -07:00
Arun Lobo e74a37438b Add Chrome timeline support in Tensorflow (#6693)
Fixes #6606
2017-05-22 13:55:03 -07:00
Francois Chollet 2061f41987 style fixes 2017-05-22 13:25:04 -07:00
Rusty c8d35caa7f Updated filters in text processing documentation (#6696) 2017-05-22 13:10:27 -07:00
Andrew Hundt cf57d28452 get_file() progbar fix (#6670)
* Fix get_file download progress bar

* Added a comment to clarify the purpose of the "enclosed" dictionary

* pep8

* Fix get_file download progress bar, including no Content-Length header.

* Progbar accepts target None in addition to -1.

* #6670 Remove Progbar implementation details from docstring
Only None should be supported on the Progbar target parameter,
target values of -1 are an unsupported implementation detail
that may be removed in the future.
2017-05-22 12:04:33 -07:00
Indy M c1a1c33ef9 updated fit on texts description (#6699)
fit_on_texts returns a list of words not integer indices as stated. I've corrected this.
2017-05-21 10:52:03 -07:00
Taehoon Lee bac16379a2 Fix typos (#6702) 2017-05-21 10:51:19 -07:00
Matt Gardner b5490b20d2 Fix depth calculation for shared layers (#6668)
* Fixed depth calculation for shared layers

* Added a failing test

* Update the node's depth too
2017-05-19 16:54:03 -07:00
Jun Kim 3061fcce60 Fix a typo in expand_dims() (#6671)
In comment: expended -> expanded
2017-05-18 16:12:48 -07:00
Daniel Høyer Iversen 7a3190de3b Typo in documentation (#6672) 2017-05-18 16:12:37 -07:00
Stefano ed9e8d2ff0 Update image.py (#6618)
Fixes https://github.com/fchollet/keras/issues/6612
2017-05-16 13:40:01 -07:00
Stefano 13303663ff Change save_format from jpg top png, because jpg is a loss format. (#6638)
* Change save_format from jpg top png, because jpg is a loss format.

* Change default format to png, because jpeg is a loss format.
2017-05-16 07:57:50 -07:00
Fariz Rahman 0d27d903c2 Bug fix in convolutional recurrent state setting
* Bug fix: convolutional recurrent (again)

* pep8

* Update convolutional_recurrent.py

* pep8
2017-05-14 10:42:44 -07:00
Kevin Mader 6220e35ccd adding io_utils test for hdf5matrix (#6610)
* adding io_utils test for hdf5matrix

* incorporating pep8 and feedback from @fchollet
2017-05-14 10:40:28 -07:00
Francois Chollet bc9dbc5de0 Style fix in error message 2017-05-12 11:08:46 -07:00
Kyle Dorman d67cf89759 Better error message for invalid functional api inputs (#6589) (#6593)
* Better error message for invalid funcational api inputs (#6589)

* raise ValueError if `inputs` is not a Keras tensor

* Move  to respective backends

* raise error if is_keras_tensor is called on a non-tensor object

* Fix failing tests

* responding to comments

* Update docstring comments to better explain expected behavior
2017-05-12 11:04:27 -07:00
Gökçen Eraslan a2dde60a2f TensorBoard: Embed only given layers (#6565)
* TensorBoard: Embed only given layers.

* TensorBoard: Fix pep8
2017-05-11 14:34:33 -07:00
rejunity e177397427 Small fixes for Neural_Doodle example (#6577)
* Fixed type conversion in neural_doodle example. Shape returns number of channels as int32 however further calculations require it to be float

* Updated neural doodle example to follow Keras2 API. Renamed ‘border_mode’ argument to ‘padding’.

* Fixed apostrophe for consistency.
2017-05-11 14:33:39 -07:00
meberstein 5f4f234f9b In EarlyStopping, reset stopped_epoch in on_train_begin to allow it to be re-used (#6591) 2017-05-11 14:32:49 -07:00
Daniel Høyer Iversen 24db6bfaaf Fix in ZeroPadding3D (#6574)
* Buf fix in ZeroPadding3D

* test_zero_padding_3d
2017-05-11 08:53:15 -07:00
Clara Eng 504bded884 Add callback to terminate training if NaN loss encountered. (Update to #4849) (#6456)
* Added callback TerminateOnNaN.

* Added fixes.
2017-05-10 13:44:57 -07:00
catta202000 08aa6ae555 Added batch histogram computation (#6065)
* Added batch histogram computation

* batch_size_histogram renamed to batch_size, default set to 32, added spaces around operators

* PEP8 fix

* Added batch_size in tests/keras/test_callbacks.py::test_TensorBoard_convnet

* PEP8 fix

* Batch size reduced in tests, targets and sample_weights sliced
2017-05-10 08:16:55 -07:00
popyy0101 737ae88a02 Use OrderedDict instead of normal dict to produce reliable iteration for dict.items() (#6573) 2017-05-10 08:13:53 -07:00
Fariz Rahman 6642d496e5 Recurrent : InputSpec fixes (#6568)
* Recurrent : InputSpec fixes

* Update convolutional_recurrent.py

* pep8 fix

* Update convolutional_recurrent.py
2017-05-09 23:40:57 -07:00
Fariz Rahman 2766074d19 Bug fix + test : Initializing states for ConvLSTM2D (#6564)
* Bug fix

* Update convolutional_recurrent_test.py

* Update convolutional_recurrent.py
2017-05-09 17:32:37 -07:00
Gökçen Eraslan 672028a5f2 Fix hyperlink misrendering in documentation (#6558)
Two hyperlinks (namely `[here]` and `[details]`) are misrendered in TensorBoard documentation, see https://keras.io/callbacks/#tensorboard. Fix exclude `(` in argument names, because otherwise `[link](http://` is rendered as a function/class argument.
2017-05-09 12:13:24 -07:00
Francois Chollet 1a89b13cb4 Try reverting previously merged PR. 2017-05-08 09:33:42 -07:00
Han Lin 268672df65 Don't rate limit final update (#6536) 2017-05-07 17:34:44 -07:00
iddober bfae0a6191 remove unused import in tests folder (#6534) 2017-05-07 14:46:26 -07:00
Moussa Taifi a2a0f66276 Add exception handling when attempting to write keras config file (#6453)
* add exception handling when attempting to write keras config file to disk to match tf.contrib.keras implementation

* Add reliance on exceptions rather than testing write access to the target directory.
2017-05-06 20:02:08 -07:00
Kosuke Kusano ea8e2edf17 fix tuple error message (#6530) 2017-05-06 19:18:29 -07:00
Murat Ambarkutuk d223cc0ff7 Add an option to create dot model in different directions (#5472)
* Add an option to create dot model in different directions

This commit adds an optional argument to functions plot() and model_to_dot() specifying the direction of the dot object

* Rename visualize_util.py to vis_utils.py and and model plot direction

* Format the code in the PEP8 style guide

* Add docstring for plot_model method, format code according to PEP8

pycodestyle and pydocstyle raises no info, warning, or error with this pr.

* Docstring style

* Docstring fixes.
2017-05-05 13:35:09 -07:00
Grégory Châtel 8ac1b1fdc9 Add a directory iterator option to allow to work easily with autoencoders (issue #4260) (#6510)
* identical class_mode code.

* New directory iterator testing function

* class_mode keyword changed to input + clearer doc.
2017-05-05 09:07:31 -07:00
Dr. Kashif Rasul 23833417cf fixed typo (#6523) 2017-05-05 09:05:15 -07:00
Eric Xihui Lin 61c9cdc53c Use axis instead of reduction_indices in logsumexp 2017-05-04 19:47:41 -07:00
Francois Chollet 1c7e63e42c Update docs autogen script. 2017-05-04 17:02:32 -07:00
Francois Chollet 6582043276 Update CONTRIBUTING.md 2017-05-04 17:02:17 -07:00
Parag S. Chandakkar 85221ccd13 Changed l2_normalization in theano_backend.py (#6513) 2017-05-04 15:18:26 -07:00
Gökçen Eraslan cf550db5a5 Visualize grad distributions in TensorBoard (#6313)
* Visualize weight grad distributions in TensorBoard

* TensorBoard: Add learning_phase if needed and fix fit_generator target dimensions.

* TensorBoard: Fix pep8

* TensorBoard: Add a flag to make grad visualization optional.

* TensorBoard: Test grad visualizations as well.

* TensorBoard: Documentation and further pep8 changes.

* TensorBoard: Add dropout layer to test K.learning_phase()

* Add learning_phase check in fit() to fit_generator().

* Tensorboard: Add test for comparing cbk.validation_data for fit() and fit_generator()

* Tensorboard: Fix cbk.val_data test.

* Tensorboard: Enable grad vis in tb convnet test.

* Tensorboard: No linebreak for more readability
2017-05-04 15:17:10 -07:00
SimonMarkWarren 75519651bb Fix error in test_saving_without_compilation (#6504)
* Fix error in test_saving_without_compilation

* Update test_model_saving.py
2017-05-04 13:52:15 -07:00
Stephan Heijl b93d3b23f5 Moved start/end (#6502) 2017-05-04 13:07:26 -07:00
Abhai Kollara Dilip dc3d164c6b Tokenizer docs patch (#6506)
* Tokenizer docs patch

* Minor change
2017-05-04 13:06:26 -07:00
Taehoon Lee 47dddaa7fd Fix valid condition for TF <-> TH conversion of Conv1D (#6497) 2017-05-04 11:49:37 -07:00
Gökçen Eraslan fdd822c03e Tensorboard: Check weight dimensions better in write_images (#6505)
* Tensorboard: Add a convnet test for tensorboard

* Tensorboard: Check weight dimensions better in write_images and make the code more explicit

* Tensorboard: 2 epochs is enough for tb convnet test

* Tensorboard: Fix pep8
2017-05-04 11:48:13 -07:00
Frédéric Bastien a736c2632b Add function names to help profiling/printing of function. (#6463)
* Add function names to help profiling/printing of function.

* Docstring and pass to Theano the new parameter
2017-05-02 08:43:17 -07:00
Daniel Julius Lasiman 1a707ea11e Handle properly values with str type in CSVLogger #6459 (#6460) 2017-05-01 16:56:00 -07:00
Shaofan Lai c430b6c492 Add skip_compile option to keras.models.load_model() (#6436)
* add skip_compile option to keras.models.load_model()

* update document

* change name from skip_compile to compile
2017-04-30 12:20:55 -07:00
Francois Chollet c627fa5bbd Prepare new PyPI release. 2017-04-29 16:18:54 -07:00
Francois Chollet affaa77078 Merge branch 'master' of github.com:fchollet/keras 2017-04-29 15:54:48 -07:00
Ben f1df88737c Fix CSV formatting for Windows with Python 2 (#6311)
* Fix CSV formatting for windows with python 2

* Fix pep8 whitespace

* Fix quote style
2017-04-29 15:50:24 -07:00
Michael R. Kirchner 0ddc3360b7 Update docs and add contributing page (#6432)
* Update docs and add contributing page

* Add space for pep8
2017-04-29 15:37:05 -07:00
Ilya Ivanov eaca5da3e2 Fix indent size of docstring's line (#6440)
Remove excess preceding 4 spaces in Model.fit(..) docstring line
corresponding to verbose parameter.
2017-04-29 11:14:12 -07:00
Francois Chollet 70da22c31f Merge branch 'master' of github.com:fchollet/keras 2017-04-28 12:00:12 -07:00
jcuypers c158410168 Update image.md docs
* Update image.md

Enhancements for _flow_from_directory.  Classes and class_mode None

* Update image.md

Reworked it based on the comments

* Update image.md

* Update image.md

* Update image.md

typos

* Fix docstring
2017-04-28 11:19:01 -07:00
Santiago Castro 0c237ebea2 Fix missing quote mark in Cropping2D docstring (#6428) 2017-04-28 09:54:04 -07:00
Tim O'Shea 8967d16d00 add huber loss function (for robust regression) (#6410)
* add huber loss function (for robust regression)

* rename huber to logcosh (PR comments were correct), fix PEP8 whitespace checks

* logcosh loss: change from lambda to fn def'n, add text coverage
2017-04-27 20:39:05 -07:00
Francois Chollet 964023bec7 Merge branch 'master' of github.com:fchollet/keras 2017-04-27 14:54:53 -07:00
Francois Chollet 16aa56bb1d Small docstring precision 2017-04-27 14:54:48 -07:00
Mako bdf05c48ef Fix typo
* Add a symbol to avoid indent

* Fix typo
2017-04-26 20:44:11 -07:00
Frédéric Branchaud-Charron 653cfd2076 Add test for documentation (#6324)
* Add test for documentation

* Changes according to review

* Changes according to review

* Fix documentation and add Travis task

* Style fixes.

* Fix line length

* PEP8
2017-04-26 11:29:53 -07:00
Andrew Poliakov bcbfcc000c Fix oov_char=None case in IMDB/Reuters datasets (#6397)
Closes: #3688
2017-04-25 19:48:42 -07:00
Nigel 54a417f616 Added support for new pydot versions to fix find_graphviz error (#6398)
* Added support for the new pydot API to fix find_graphviz error

* Simplified pydot installation checking

* Workaround for pydot generic Exception raising

* Removed hacky workaround for pyplot Exception, included comment
2017-04-25 19:20:01 -07:00
Yorwba 5e51d02a94 Use linear time algorithm for topological sorting. (#6347) 2017-04-25 11:30:20 -07:00
nzw d3b9b9d5bb Style Fix in image.md (#6396) 2017-04-25 11:06:19 -07:00
Daniel Høyer Iversen 4f9e7bf93c Bug fix in recurrent layer (#6393)
* Bug fix in recurrent layer

* Add test to recurent layer
2017-04-25 11:05:53 -07:00
Francois Chollet d491dafb80 Merge branch 'master' of github.com:fchollet/keras 2017-04-24 20:21:49 -07:00
Joshua Chin 365f621b24 Fix Specifying Initial States of RNN Layers (#5795)
* fix specify state

* Added documentation for `reset_states`

* Remove unneeded check

* Update Documentation

* pep8

* Fix when initial_states is a tensor

* modify tests for non-list initial states.

* use initial_state instead of initial_states

* pep8

* change get_initial_states to get_initial_state in ConvLSTM2D

* Check for Keras Tensors in Recurrent

* check if initial_state is passed to call

* pep8

* Move state_spec definition to __init__

* Fix reset states

* fix masking when specifying state

* added masking test for RNNs with specified state

* pep8

* remove unnecessary blank line
2017-04-24 20:20:04 -07:00
Francois Chollet 7481b5d060 Update deep dream config. 2017-04-24 19:03:39 -07:00
Francois Chollet 9295efb216 Simplify the deep dream example 2017-04-24 18:23:09 -07:00
Francois Chollet 0d4fb04c7f Style fix in image preprocessing 2017-04-24 18:22:38 -07:00
Francois Chollet 791cba094c Cast kernels as np arrays before TF <-> TH conversion 2017-04-24 18:22:22 -07:00
Francois Chollet 2bb9014c91 Fix a padding bug with Theano average pooling gradients 2017-04-24 18:21:54 -07:00
Francois Chollet 5be73f1ab3 Simplify implementation of BN layer. 2017-04-24 11:47:11 -07:00
Francois Chollet b8134f529c Add “et al” to Keras bibtex entry. 2017-04-24 10:45:31 -07:00
Philipp Gross 7d52af64c0 Added logsumexp to backend. (#6346) 2017-04-22 11:49:33 -07:00
Piasy 70ffba0766 fix stateful RNNs FAQ link (#6336) 2017-04-20 08:30:02 -07:00
nzw e7f3317de6 Style fixes (#6335) 2017-04-20 08:29:45 -07:00
Francois Chollet 47350dc607 Switch to a more reasonable way of initializing LSTM bias 2017-04-19 14:28:49 -07:00
Francois Chollet d498a98465 Make Input importable from root 2017-04-19 14:27:37 -07:00
Francois Chollet 0976afb46d Update add_weight docstring 2017-04-19 14:27:07 -07:00
/c/ympfh 7088ebd294 Fix: doc (#6316) 2017-04-19 09:46:37 -07:00
Andrei Costinescu f71831790f Update check for sequential models (#6305)
* Update layer_utils.py

Model is not sequential if there is a "merge" layer somewhere in the graph. So if a layer has multiple input layers ("inbound_layers"), the whole model is no longer sequential...

* Explanation of changed condition

Added a comment to explain the check for sequentiality in a model:
A model is not sequential if it has multiple nodes or if a layer has multiple inbound_layers
2017-04-18 13:43:31 -07:00
Francois Chollet 83001d195c merge 2017-04-18 11:34:56 -07:00
Francois Chollet 8830c53135 Refactor add_weight to align it with get_variable 2017-04-18 11:34:24 -07:00
HaleyWu d89afdfd82 Update the value of steps_per_epoch of fit_generator to be divided by batch_size (#6301)
* Update the value of 'steps_per_epoch'

* Update the docstring of fit_generator to steps_per_epoch * batch_size

* Update the value of 'steps_per_epoch'

* Update the docstring of fit_generator: when 'steps_per_epoch' batches have been seen
2017-04-18 11:17:26 -07:00
Icyblade Dai 562860ca42 add warnings when advanced activations are passed into Activation (#6280)
* add warnings when advanced activations were passed into Activation

* fix import issue

* warning message beautify

* adopt user-friendly message
2017-04-18 11:01:41 -07:00
Sergey Kojoian fc4874f82c Updated the HDF5Matrix class to support inferred slice indeces such as data[:10] or data[19120:]. (#6299) 2017-04-17 14:24:11 -07:00
nzw 73a620b6e8 Update calback page (#6289) 2017-04-17 14:20:42 -07:00
Andrei Costinescu e0697c3768 Corrected a comment in function "print_layer_summary_with_connections" && Fixed issue #6286 (#6284)
* Corrected a comment in function "print_layer_summary_with_connections"

Changed line 82 from "# node is node part of the current network" to "# node is not part of the current network"

* Fixed issue #6286

Fixed the issue where the summary of non-sequential models would not display content of "Connected to" column
2017-04-17 14:20:23 -07:00
nzw 73bf06fb02 Style fixes (#6271)
* Fix link in FAQ

* Fix link in FAQ

* Style fix

* Rename objectives to losses
2017-04-16 13:08:44 -07:00
Vladimir Alekseichenko 53bee20647 Explicit import of ifelse in Theano backend 2017-04-16 13:08:04 -07:00
Francois Chollet 18ed60b9f2 Fix PEP8 issue. 2017-04-15 17:40:25 -07:00
Russ09 707534e46e Allows preprocess_weights_for_loading() to consider layers wrapped in TimeDistributed or Bidirectional (#5836)
* Allows preprocess_weights_for_loading() to consider layers wrapped in TimeDistributed or Bidirectional.

* fixed whitespace PEP8 issue

* Allows preprocess_weights_for_loading() to consider layers wrapped in TimeDistributed or Bidirectional.

* Allows preprocess_weights_for_loading() to consider layers wrapped in TimeDistributed or Bidirectional.

* Refactored preprocess_weights_for_loading() to allow for loading to TimeDistributed and Bidirectional. PEP8 Fixes.

* PEP8 Fixes

* Recursive implementation of preprocess_weights_for_loading to accomodate Bidirectional and TimeDistributed wrappers.

* Recursive implementation of preprocess_weights_for_loading to accomodate Bidirectional and TimeDistributed wrappers.

* deindentation and doc-string formatting. method argument formating.
2017-04-15 16:11:51 -07:00
Vasilis Vryniotis cd6bbe7290 Adding backwards compatibility for old models by concerting input_dtype to dtype on InputLayers. (#6248) 2017-04-15 16:11:17 -07:00
nzw f6cc059104 Update datasets docs (#6266)
* Update docs

* Style fix
2017-04-15 16:10:31 -07:00
Francois Chollet 6572934f9a Merge branch 'master' of github.com:fchollet/keras 2017-04-14 18:08:35 -07:00
Francois Chollet 2a67506728 Fix GRU bias initializer selection 2017-04-14 18:08:22 -07:00
nzw 4507057e11 Update docs (#6249)
* Fix file path

* Update docs for keras v2
2017-04-14 13:15:30 -07:00
Francois Chollet eee1d90ef2 Merge branch 'master' of github.com:fchollet/keras 2017-04-14 12:31:38 -07:00
Francois Chollet 9d0efc081e Update Travis config 2017-04-14 12:31:21 -07:00
Yorwba 2c284017d4 Fix Model.fit_generator for multiple outputs with same name. (#6239) 2017-04-13 13:00:49 -07:00
alexantoinefortin 90758c3f4e typo in model_from_config error flag (#6238) 2017-04-12 22:11:06 -07:00
John B Nelson dcacdd3747 Update fit_generator docstr for new API (#6230) 2017-04-12 22:10:51 -07:00
Mohanson 5bd3976e79 Spelling errors (#6232) 2017-04-12 22:10:15 -07:00
Francois Chollet 9eb7ecd3e5 Merge branch 'master' of github.com:fchollet/keras 2017-04-11 13:56:43 -07:00
Francois Chollet 05589a7c27 Merge branch 'Spotlight0xff-origin/vae_add_loss' 2017-04-11 13:43:37 -07:00
Francois Chollet 4aa41625bf Switch variational examples to new API. 2017-04-11 13:43:04 -07:00
Francois Chollet b2f0dd4cb2 Improve error messages in data validation checks. 2017-04-11 13:42:18 -07:00
Francois Chollet 17ef113ed7 Add identity op, avoid having input tensors in layer outputs (metadata loss). 2017-04-11 13:41:54 -07:00
Francois Chollet c029fa2f62 Merge branch 'origin/vae_add_loss' of https://github.com/Spotlight0xff/keras into Spotlight0xff-origin/vae_add_loss 2017-04-11 12:57:28 -07:00
Nigel Ng 52b1377fe6 Update mnist_siamese_graph example (#6223)
Take max of squared distance and K.epsilon() because some data points will throw `nan` for euclidean distance.
2017-04-11 12:09:44 -07:00
Francois Chollet 5598fcd33e Merge branch 'master' of github.com:fchollet/keras 2017-04-11 11:33:52 -07:00
Francois Chollet b558a7e97c Add RNN unit test 2017-04-11 11:32:41 -07:00
Francois Chollet 172397ebf4 Simplify param counting in model summary. 2017-04-11 11:32:11 -07:00
Francois Chollet 9adb43e44b Improve style of some comments. 2017-04-11 11:31:42 -07:00
Fariz Rahman ac6fde801c Bug fix: K.batch_dot(); tf backend (#6219)
* Update tensorflow_backend.py

* Update tensorflow_backend.py

* add unit tests
2017-04-10 15:56:00 -07:00
Francois Chollet 457b0c1d3e Merge branch 'master' into tf-keras 2017-04-07 14:15:00 -07:00
Francois Chollet bf5735b577 Merge branch 'master' into tf-keras 2017-04-07 14:03:45 -07:00
Francois Chollet 1f82b19349 add changes to inception_v3 2017-04-07 13:46:10 -07:00
Francois Chollet b8b2fc4e6c merge. 2017-04-07 13:43:32 -07:00
Francois Chollet b5411f10a1 Merge branch 'master' into tf-keras 2017-04-05 11:57:42 -07:00
Francois Chollet fce18b245c merge 2017-04-05 10:46:20 -07:00
Francois Chollet e872da85e4 exception json decoding error 2017-04-04 11:46:23 -07:00
Francois Chollet a2a2e49457 merge 2017-04-03 17:18:56 -07:00
Francois Chollet 032abdb666 Merge branch 'tf-keras' of github.com:fchollet/keras into tf-keras 2017-04-03 15:29:06 -07:00
Francois Chollet 8100ac79c1 Make config file saving conditional on permissions. 2017-04-03 15:28:51 -07:00
Francois Chollet 16db6db6ae style fix 2017-04-02 13:23:13 -07:00
Francois Chollet 4026f89bd1 merge master. 2017-04-02 13:22:06 -07:00
Francois Chollet 5436b4fb00 Merge branch 'master' into tf-keras 2017-04-02 12:56:20 -07:00
Francois Chollet 6c199c41dd Style fix. 2017-04-02 12:54:50 -07:00
Francois Chollet 1a7e51cfc8 Backend fixes. 2017-04-02 12:50:18 -07:00
Francois Chollet df14349c2a Merge branch 'tf-keras' of github.com:fchollet/keras into tf-keras 2017-04-02 12:46:57 -07:00
Francois Chollet 855e8dccde backport py3 fix in backend 2017-04-02 12:46:37 -07:00
Francois Chollet 850d92516c Merge branch 'master' into tf-keras 2017-04-02 12:45:16 -07:00
Francois Chollet 96909acd1e Merge branch 'tf-keras' of github.com:fchollet/keras into tf-keras 2017-04-02 11:49:55 -07:00
Francois Chollet 9479666083 Adapt merge tests. 2017-04-02 11:49:41 -07:00
Francois Chollet c0e972f3b4 Merge branch 'master' into tf-keras 2017-04-02 11:42:28 -07:00
Francois Chollet a517bc69fb rm legacy interface tests 2017-04-02 11:39:32 -07:00
Francois Chollet 57d7fce61d Remove backend tests 2017-04-02 11:29:43 -07:00
Francois Chollet 4a5a3dd685 merge master. 2017-04-02 11:21:03 -07:00
Francois Chollet 819f3e2ba5 style fix 2017-03-15 15:55:09 -07:00
Francois Chollet 6ef5bb2ddc style fixes 2017-03-15 12:47:08 -07:00
Francois Chollet 01081d4899 Merge branch 'tf-keras' of https://github.com/fchollet/keras into tf-keras 2017-03-15 12:46:22 -07:00
Francois Chollet f37c7d4fd9 rm objectives 2017-03-15 12:46:00 -07:00
Francois Chollet 4a2ff8d019 Remove common.py 2017-03-15 12:45:23 -07:00
Francois Chollet 59b1e2a25c Merge branch 'master' into tf-keras
# Conflicts:
#	keras/backend/__init__.py
#	keras/backend/common.py
#	keras/backend/theano_backend.py
2017-03-15 12:44:24 -07:00
Spotlight0xff e848463347 using .add_loss in custom layer for VAE example 2017-03-15 13:21:45 +01:00
Francois Chollet 240eda535d Merge branch 'tf-keras' of https://github.com/fchollet/keras into tf-keras 2017-03-14 09:11:09 -07:00
Francois Chollet 9d62df3f21 Merge branch 'master' into tf-keras 2017-03-14 09:10:19 -07:00
Francois Chollet 776a15aad9 Merge branch 'tf-keras' of https://github.com/fchollet/keras into tf-keras 2017-03-13 17:44:26 -07:00
Francois Chollet 4b79df99b9 Make Keras dir creation safer in multithreaded envs. 2017-03-13 17:44:15 -07:00
Francois Chollet 3acd5b2e86 Merge branch 'tf-keras' of https://github.com/fchollet/keras into tf-keras 2017-03-13 14:16:37 -07:00
Francois Chollet 5952ea52aa Merge branch 'keras-2' into tf-keras 2017-03-13 14:16:06 -07:00
Francois Chollet 50eb3bfa1b Merge branch 'tf-keras' of https://github.com/fchollet/keras into tf-keras 2017-03-13 12:18:53 -07:00
Francois Chollet b6b5343af3 Fixes. 2017-03-13 12:18:43 -07:00
Francois Chollet ff7209cc16 Merge branch 'tf-keras' of https://github.com/fchollet/keras into tf-keras 2017-03-13 12:08:14 -07:00
Francois Chollet 4736730e22 Add docstring. 2017-03-13 12:07:52 -07:00
Francois Chollet 38cdc03cc4 Merge branch 'tf-keras' of https://github.com/fchollet/keras into tf-keras 2017-03-13 12:02:07 -07:00
Francois Chollet c00a73a65e Merge 2017-03-13 12:01:54 -07:00
Francois Chollet 6a835faf79 Merge branch 'tf-keras' of https://github.com/fchollet/keras into tf-keras 2017-03-13 11:59:16 -07:00
Francois Chollet 3f9379ec3e Add mandatory imports 2017-03-13 11:59:12 -07:00
Francois Chollet c11bbd807c merge. 2017-03-13 11:58:27 -07:00
Francois Chollet ba3ea75307 Merge branch 'tf-keras' of github.com:fchollet/keras into tf-keras 2017-03-10 20:40:27 -08:00
Francois Chollet 4b975c113c merge. 2017-03-10 20:40:14 -08:00
Francois Chollet 50ee2f9602 merge. 2017-03-10 10:22:03 -08:00
Francois Chollet 565d1d5116 Fix TF imports 2017-03-10 10:21:02 -08:00
Francois Chollet 4794363fae merge 2017-03-10 10:19:13 -08:00
Francois Chollet c2cc739938 Merge branch 'tf-keras' of https://github.com/fchollet/keras into tf-keras 2017-03-10 09:51:37 -08:00
Francois Chollet 6790c0d247 add mandatory imports 2017-03-10 09:51:30 -08:00
Francois Chollet a92026d719 Remove internal docs links from docstrings. 2017-03-10 09:47:23 -08:00
Francois Chollet d14df154e5 merge 2017-03-10 09:39:28 -08:00
Francois Chollet 1b6e14e944 Use int_shape where appropriate. 2017-03-06 19:02:10 -08:00
Francois Chollet 27329b8400 merge 2017-03-06 18:47:56 -08:00
Francois Chollet 28f3eaedd1 Merge. 2017-03-05 17:44:00 -08:00
Francois Chollet 2abdcdfb8e Update. 2017-03-05 17:42:59 -08:00
Francois Chollet 4b7122ef9c Add module level imports 2017-03-05 16:11:10 -08:00
Francois Chollet 9840b5ad24 fix backend imports. 2017-03-05 14:02:11 -08:00
Francois Chollet 4adb619518 merge 2017-03-05 13:57:49 -08:00
Francois Chollet cff822c9df Merge branch 'keras-2' into tf-keras 2017-03-04 17:59:19 -08:00
Francois Chollet f9ede2ba1b Merge branch 'keras-2' into tf-keras 2017-03-03 11:52:09 -08:00
Francois Chollet 71b5c2d05b Add comment. 2017-03-03 11:51:11 -08:00
Francois Chollet d69432cd0a Add short aliases for global pooling layers. 2017-03-03 10:44:15 -08:00
Francois Chollet 8a99aaf604 Merge. 2017-03-01 18:23:31 -08:00
Francois Chollet aabe81e82b Merge. 2017-03-01 18:22:44 -08:00
Francois Chollet 94545cea9b Linter fixes. 2017-03-01 18:21:28 -08:00
Francois Chollet c25a319463 Fix circular imports issue. 2017-02-28 15:27:26 -08:00
Francois Chollet 1847036cfd Reallow causal padding. 2017-02-28 15:27:17 -08:00
Francois Chollet 3865b589e1 merge. 2017-02-28 14:46:38 -08:00
Francois Chollet 2832c740aa Reallow causal padding. 2017-02-26 16:45:27 -08:00
Francois Chollet 9ee8425072 Linter fixes. 2017-02-26 16:32:01 -08:00
Francois Chollet 1db4438ac9 merge. 2017-02-26 15:21:58 -08:00
Francois Chollet 1bd4fac1a7 merge 2017-02-26 15:02:04 -08:00
Francois Chollet a58fb1d917 merge. 2017-02-26 14:34:43 -08:00
Francois Chollet 5ae17cd983 Linter fixes. 2017-02-26 14:31:41 -08:00
Francois Chollet 9b0ff98ead merge. 2017-02-26 13:53:12 -08:00
Francois Chollet 27e943eda2 Linter fixes. 2017-02-26 13:51:27 -08:00
Francois Chollet 71b74d6b89 Merge branch 'tf-keras' of https://github.com/fchollet/keras into tf-keras 2017-02-26 13:22:38 -08:00
Francois Chollet afd9d8087d Linter fixes. 2017-02-26 13:22:30 -08:00
Francois Chollet dc66ca402a Merge branch 'keras-2' into tf-keras 2017-02-26 13:20:52 -08:00
Francois Chollet 7b231e2819 Merge branch 'tf-keras' of https://github.com/fchollet/keras into tf-keras 2017-02-26 12:47:57 -08:00
Francois Chollet 369bfcb1bf lint fixes 2017-02-26 12:47:53 -08:00
Francois Chollet 2ca7908f59 merge 2017-02-26 12:46:35 -08:00
Francois Chollet d684124d89 Fixes. 2017-02-26 12:44:02 -08:00
Francois Chollet d2a609e459 Add names in optimizer variables. 2017-02-26 11:17:57 -08:00
Francois Chollet bd4d40e514 Fix typo. 2017-02-25 15:13:53 -08:00
Francois Chollet 5abbd05245 Docstring fixes. 2017-02-25 14:24:15 -08:00
Francois Chollet 265464141e Style fixes. 2017-02-25 14:21:07 -08:00
Francois Chollet 6feb1d9e27 Update setup.py. 2017-02-25 13:55:49 -08:00
Francois Chollet 7936401a87 Remove backend tests. 2017-02-25 13:54:18 -08:00
Francois Chollet 672fe90dd9 Atomize TF imports in backend. 2017-02-25 13:50:38 -08:00
Francois Chollet d5a384ed61 Atomize TF imports (except for backend). 2017-02-25 13:50:28 -08:00
Francois Chollet 22b943e935 Merge branch 'keras-2' into tf-keras 2017-02-25 11:29:28 -08:00
Francois Chollet 94268267c4 Make compute_output_shape private. 2017-02-25 11:06:08 -08:00
Francois Chollet 350da1a3c3 Merge keras-2. 2017-02-25 10:54:48 -08:00
Francois Chollet 3fc74cfc0b Second pass over TF conversion (nearly done). 2017-02-24 16:45:35 -08:00
Francois Chollet c6e6acdebf First pass over TF conversion. 2017-02-24 13:35:00 -08:00
106 arquivos alterados com 3759 adições e 6497 exclusões
+19
Ver Arquivo
@@ -0,0 +1,19 @@
# Configuration for probot-stale - https://github.com/probot/stale
# Number of days of inactivity before an Issue or Pull Request becomes stale
daysUntilStale: 90
# Number of days of inactivity before a stale Issue or Pull Request is closed
daysUntilClose: 30
# Issues or Pull Requests with these labels will never be considered stale
exemptLabels:
- bug
- Announcement
- help wanted
- To investigate
# Label to use when marking as stale
staleLabel: stale
# Comment to post when marking as stale. Set to `false` to disable
markComment: >
This issue has been automatically marked as stale because it has not had
recent activity. It will be closed after 30 days if no further activity
occurs, but feel free to re-open a closed issue if needed.
+7 -3
Ver Arquivo
@@ -7,6 +7,8 @@ matrix:
env: KERAS_BACKEND=tensorflow TEST_MODE=PEP8
- python: 2.7
env: KERAS_BACKEND=tensorflow TEST_MODE=INTEGRATION_TESTS
- python: 3.5
env: KERAS_BACKEND=tensorflow TEST_MODE=DOC
- python: 2.7
env: KERAS_BACKEND=tensorflow
- python: 3.5
@@ -34,7 +36,7 @@ install:
- conda create -q -n test-environment python=$TRAVIS_PYTHON_VERSION numpy scipy matplotlib pandas pytest h5py
- source activate test-environment
- pip install git+git://github.com/Theano/Theano.git
- pip install theano
# install PIL for preprocessing tests
- if [[ "$TRAVIS_PYTHON_VERSION" == "2.7" ]]; then
@@ -45,7 +47,7 @@ install:
- pip install -e .[tests]
# install TensorFlow
# install TensorFlow (CPU version).
- pip install tensorflow
# command to run tests
@@ -61,6 +63,8 @@ script:
PYTHONPATH=$PWD:$PYTHONPATH py.test tests/integration_tests;
elif [[ "$TEST_MODE" == "PEP8" ]]; then
PYTHONPATH=$PWD:$PYTHONPATH py.test --pep8 -m pep8 -n0;
elif [[ "$TEST_MODE" == "DOC" ]]; then
PYTHONPATH=$PWD:$PYTHONPATH py.test tests/test_documentation.py;
else
PYTHONPATH=$PWD:$PYTHONPATH py.test tests/ --ignore=tests/integration_tests --cov=keras tests/ --cov-fail-under 78 --cov-report term-missing;
PYTHONPATH=$PWD:$PYTHONPATH py.test tests/ --ignore=tests/integration_tests --ignore=tests/test_documentation.py --cov=keras tests/ --cov-fail-under 78 --cov-report term-missing;
fi
+6 -5
Ver Arquivo
@@ -49,7 +49,7 @@ Here's a quick guide to submitting your improvements:
2. Write the code. This is the hard part!
3. Make sure any new function or class you introduce has proper docstrings. Make sure any code you touch still has up-to-date docstrings and documentation.
3. Make sure any new function or class you introduce has proper docstrings. Make sure any code you touch still has up-to-date docstrings and documentation. **Docstring style should be respected.** In particular, they should be formatted in MarkDown, and there should be sections for `Arguments`, `Returns`, `Raises` (if applicable). Look at other docstrings in the codebase for examples.
4. Write tests. Your code should have full unit test coverage. If you want to see your PR merged promptly, this is crucial.
@@ -57,19 +57,20 @@ Here's a quick guide to submitting your improvements:
- You will need to install the test requirements as well: `pip install -e .[tests]`.
6. Make sure all tests are passing:
- with the Theano backend, on Python 2.7 and Python 3.5
- with the TensorFlow backend, on Python 2.7
- with the Theano backend, on Python 2.7 and Python 3.5. Make sure you have the development version of Theano.
- with the TensorFlow backend, on Python 2.7 and Python 3.5. Make sure you have the development version of TensorFlow.
7. We use PEP8 syntax conventions, but we aren't dogmatic when it comes to line length. Make sure your lines stay reasonably sized, though. To make your life easier, we recommend running a PEP8 linter:
- Install PEP8 packages: `pip install pep8 pytest-pep8 autopep8`
- Run a standalone PEP8 check: `py.test --pep8 -m pep8`
- You can automatically fix some PEP8 error by running: `autopep8 -i --select <errors> <FILENAME>` for example: `autopep8 -i --select E128 tests/keras/backend/test_backends.py`
8. When committing, use appropriate, descriptive commit messages. Make sure that your branch history is not a string of "bug fix", "fix", "oops", etc. When submitting your PR, squash your commits into a single commit with an appropriate commit message, to make sure the project history stays clean and readable. See ['rebase and squash'](http://rebaseandsqua.sh/) for technical help on how to squash your commits.
8. When committing, use appropriate, descriptive commit messages.
9. Update the documentation. If introducing new functionality, make sure you include code snippets demonstrating the usage of your new feature.
10. Submit your PR. If your changes have been approved in a previous discussion, and if you have complete (and passing) unit tests, your PR is likely to be merged promptly. Otherwise, well...
10. Submit your PR. If your changes have been approved in a previous discussion, and if you have complete (and passing) unit tests as well as proper docstrings/documentation, your PR is likely to be merged promptly. Otherwise, well...
## Adding new examples
+24 -17
Ver Arquivo
@@ -8,9 +8,7 @@ Index
- Getting started
Getting started with the sequential model
Getting started with the functional api
Examples
FAQ
Installation guide
- Models
About Keras models
@@ -26,18 +24,23 @@ Index
explain common layer functions: get_weights, set_weights, get_config
explain input_shape
explain usage on non-Keras tensors
Core layers
Convolutional
Recurrent
Embeddings
Normalization
Advanced activations
Noise
Core Layers
Convolutional Layers
Pooling Layers
Locally-connected Layers
Recurrent Layers
Embedding Layers
Merge Layers
Advanced Activations Layers
Normalization Layers
Noise Layers
Layer Wrappers
Writing your own Keras layers
- Preprocessing
Image preprocessing
Text preprocessing
Sequence preprocessing
Sequence Preprocessing
Text Preprocessing
Image Preprocessing
Losses
Metrics
@@ -45,12 +48,15 @@ Optimizers
Activations
Callbacks
Datasets
Applications
Backend
Initializations
Initializers
Regularizers
Constraints
Visualization
Scikit-learn API
Utils
Contributing
'''
from __future__ import print_function
@@ -114,14 +120,13 @@ PAGES = [
models.Sequential.fit,
models.Sequential.evaluate,
models.Sequential.predict,
models.Sequential.predict_classes,
models.Sequential.predict_proba,
models.Sequential.train_on_batch,
models.Sequential.test_on_batch,
models.Sequential.predict_on_batch,
models.Sequential.fit_generator,
models.Sequential.evaluate_generator,
models.Sequential.predict_generator,
models.Sequential.get_layer,
],
},
{
@@ -382,7 +387,7 @@ def process_class_docstring(docstring):
r'\n __\1__\n\n',
docstring)
docstring = re.sub(r' ([^\s\\]+):(.*)\n',
docstring = re.sub(r' ([^\s\\\(]+):(.*)\n',
r' - __\1__:\2\n',
docstring)
@@ -400,7 +405,7 @@ def process_function_docstring(docstring):
r'\n __\1__\n\n',
docstring)
docstring = re.sub(r' ([^\s\\]+):(.*)\n',
docstring = re.sub(r' ([^\s\\\(]+):(.*)\n',
r' - __\1__:\2\n',
docstring)
@@ -509,3 +514,5 @@ for page_data in PAGES:
if not os.path.exists(subdir):
os.makedirs(subdir)
open(path, 'w').write(mkdown)
shutil.copyfile('../CONTRIBUTING.md', 'sources/contributing.md')
+1
Ver Arquivo
@@ -51,3 +51,4 @@ pages:
- Visualization: visualization.md
- Scikit-learn API: scikit-learn-api.md
- Utils: utils.md
- Contributing: contributing.md
+4 -4
Ver Arquivo
@@ -15,7 +15,7 @@ Weights are downloaded automatically when instantiating a model. They are stored
- [ResNet50](#resnet50)
- [InceptionV3](#inceptionv3)
All of these architectures (except Xception) are compatible with both TensorFlow and Theano, and upon instantiation the models will be built according to the image data format set in your Keras configuration file at `~/.keras/keras.json`. For instance, if you have set `image_data_format=tf`, then any model loaded from this repository will get built according to the TensorFlow data format convention, "Width-Height-Depth".
All of these architectures (except Xception) are compatible with both TensorFlow and Theano, and upon instantiation the models will be built according to the image data format set in your Keras configuration file at `~/.keras/keras.json`. For instance, if you have set `image_data_format=channels_last`, then any model loaded from this repository will get built according to the TensorFlow data format convention, "Width-Height-Depth".
The Xception model is only available for TensorFlow, due to its reliance on `SeparableConvolution` layers.
@@ -253,7 +253,7 @@ The default input size for this model is 224x224.
- input_shape: optional shape tuple, only to be specified
if `include_top` is False (otherwise the input shape
has to be `(224, 224, 3)` (with `channels_last` data format)
or `(3, 224, 244)` (with `channels_first` data format).
or `(3, 224, 224)` (with `channels_first` data format).
It should have exactly 3 inputs channels,
and width and height should be no smaller than 48.
E.g. `(200, 200, 3)` would be one valid value.
@@ -309,7 +309,7 @@ The default input size for this model is 224x224.
- input_shape: optional shape tuple, only to be specified
if `include_top` is False (otherwise the input shape
has to be `(224, 224, 3)` (with `channels_last` data format)
or `(3, 224, 244)` (with `channels_first` data format).
or `(3, 224, 224)` (with `channels_first` data format).
It should have exactly 3 inputs channels,
and width and height should be no smaller than 48.
E.g. `(200, 200, 3)` would be one valid value.
@@ -367,7 +367,7 @@ The default input size for this model is 224x224.
- input_shape: optional shape tuple, only to be specified
if `include_top` is False (otherwise the input shape
has to be `(224, 224, 3)` (with `channels_last` data format)
or `(3, 224, 244)` (with `channels_first` data format).
or `(3, 224, 224)` (with `channels_first` data format).
It should have exactly 3 inputs channels,
and width and height should be no smaller than 197.
E.g. `(200, 200, 3)` would be one valid value.
+6 -8
Ver Arquivo
@@ -36,14 +36,14 @@ class LossHistory(keras.callbacks.Callback):
self.losses.append(logs.get('loss'))
model = Sequential()
model.add(Dense(10, input_dim=784, init='uniform'))
model.add(Dense(10, input_dim=784, kernel_initializer='uniform'))
model.add(Activation('softmax'))
model.compile(loss='categorical_crossentropy', optimizer='rmsprop')
history = LossHistory()
model.fit(X_train, Y_train, batch_size=128, epochs=20, verbose=0, callbacks=[history])
model.fit(x_train, y_train, batch_size=128, epochs=20, verbose=0, callbacks=[history])
print history.losses
print(history.losses)
# outputs
'''
[0.66047596406559383, 0.3547245744908703, ..., 0.25953155204159617, 0.25901699725311789]
@@ -58,15 +58,13 @@ print history.losses
from keras.callbacks import ModelCheckpoint
model = Sequential()
model.add(Dense(10, input_dim=784, init='uniform'))
model.add(Dense(10, input_dim=784, kernel_initializer='uniform'))
model.add(Activation('softmax'))
model.compile(loss='categorical_crossentropy', optimizer='rmsprop')
'''
saves the model weights after each epoch if the validation loss decreased
'''
checkpointer = ModelCheckpoint(filepath="/tmp/weights.hdf5", verbose=1, save_best_only=True)
model.fit(X_train, Y_train, batch_size=128, epochs=20, verbose=0, validation_data=(X_test, Y_test), callbacks=[checkpointer])
checkpointer = ModelCheckpoint(filepath='/tmp/weights.hdf5', verbose=1, save_best_only=True)
model.fit(x_train, y_train, batch_size=128, epochs=20, verbose=0, validation_data=(X_test, Y_test), callbacks=[checkpointer])
```
+8 -8
Ver Arquivo
@@ -55,7 +55,7 @@ As a convention, "0" does not stand for a specific word, but instead is used to
```python
from keras.datasets import imdb
(x_train, y_train), (x_test, y_test) = imdb.load_data(path="imdb_full.pkl",
(x_train, y_train), (x_test, y_test) = imdb.load_data(path="imdb.npz",
num_words=None,
skip_top=0,
maxlen=None,
@@ -72,13 +72,13 @@ from keras.datasets import imdb
- __Arguments:__
- __path__: if you do not have the data locally (at `'~/.keras/datasets/' + path`), it will be downloaded to this location.
- __num_words__: integer or None. Top most frequent words to consider. Any less frequent word will appear as 0 in the sequence data.
- __skip_top__: integer. Top most frequent words to ignore (they will appear as 0s in the sequence data).
- __num_words__: integer or None. Top most frequent words to consider. Any less frequent word will appear as `oov_char` value in the sequence data.
- __skip_top__: integer. Top most frequent words to ignore (they will appear as `oov_char` value in the sequence data).
- __maxlen__: int. Maximum sequence length. Any longer sequence will be truncated.
- __seed__: int. Seed for reproducible data shuffling.
- __start_char__: char. The start of a sequence will be marked with this character.
- __start_char__: int. The start of a sequence will be marked with this character.
Set to 1 because 0 is usually the padding character.
- __oov_char__: char. words that were cut out because of the `num_words`
- __oov_char__: int. words that were cut out because of the `num_words`
or `skip_top` limit will be replaced with this character.
- __index_from__: int. Index actual words with this index and higher.
@@ -94,7 +94,7 @@ Dataset of 11,228 newswires from Reuters, labeled over 46 topics. As with the IM
```python
from keras.datasets import reuters
(x_train, y_train), (x_test, y_test) = reuters.load_data(path="reuters.pkl",
(x_train, y_train), (x_test, y_test) = reuters.load_data(path="reuters.npz",
num_words=None,
skip_top=0,
maxlen=None,
@@ -107,12 +107,12 @@ from keras.datasets import reuters
The specifications are the same as that of the IMDB dataset, with the addition of:
- __test_split__: float. Fraction of the dataset to be used as test data.
- __test_split__: float. Fraction of the dataset to be used as test data.
This dataset also makes available the word index used for encoding the sequences:
```python
word_index = reuters.get_word_index(path="reuters_word_index.pkl")
word_index = reuters.get_word_index(path="reuters_word_index.json")
```
- __Returns:__ A dictionary where key are words (str) and values are indexes (integer). eg. `word_index["giraffe"]` might return `1234`.
+19 -19
Ver Arquivo
@@ -27,7 +27,7 @@ Please cite Keras in your publications if it helps your research. Here is an exa
```
@misc{chollet2015keras,
title={Keras},
author={Chollet, Fran\c{c}ois},
author={Chollet, Fran\c{c}ois and others},
year={2015},
publisher={GitHub},
howpublished={\url{https://github.com/fchollet/keras}},
@@ -153,16 +153,16 @@ For example:
"""
Assume original model looks like this:
model = Sequential()
model.add(Dense(2, input_dim=3, name="dense_1"))
model.add(Dense(3, name="dense_2"))
model.add(Dense(2, input_dim=3, name='dense_1'))
model.add(Dense(3, name='dense_2'))
...
model.save_weights(fname)
"""
# new model
model = Sequential()
model.add(Dense(2, input_dim=3, name="dense_1")) # will be loaded
model.add(Dense(10, name="new_dense")) # will not be loaded
model.add(Dense(2, input_dim=3, name='dense_1')) # will be loaded
model.add(Dense(10, name='new_dense')) # will not be loaded
# load weights from first model; will only affect the first layer, dense_1.
model.load_weights(fname, by_name=True)
@@ -201,7 +201,7 @@ from keras import backend as K
# with a Sequential model
get_3rd_layer_output = K.function([model.layers[0].input],
[model.layers[3].output])
layer_output = get_3rd_layer_output([X])[0]
layer_output = get_3rd_layer_output([x])[0]
```
Similarly, you could build a Theano and TensorFlow function directly.
@@ -214,17 +214,17 @@ get_3rd_layer_output = K.function([model.layers[0].input, K.learning_phase()],
[model.layers[3].output])
# output in test mode = 0
layer_output = get_3rd_layer_output([X, 0])[0]
layer_output = get_3rd_layer_output([x, 0])[0]
# output in train mode = 1
layer_output = get_3rd_layer_output([X, 1])[0]
layer_output = get_3rd_layer_output([x, 1])[0]
```
---
### How can I use Keras with datasets that don't fit in memory?
You can do batch training using `model.train_on_batch(X, y)` and `model.test_on_batch(X, y)`. See the [models documentation](/models/sequential).
You can do batch training using `model.train_on_batch(x, y)` and `model.test_on_batch(x, y)`. See the [models documentation](/models/sequential).
Alternatively, you can write a generator that yields batches of training data and use the method `model.fit_generator(data_generator, steps_per_epoch, epochs)`.
@@ -239,7 +239,7 @@ You can use an `EarlyStopping` callback:
```python
from keras.callbacks import EarlyStopping
early_stopping = EarlyStopping(monitor='val_loss', patience=2)
model.fit(X, y, validation_split=0.2, callbacks=[early_stopping])
model.fit(x, y, validation_split=0.2, callbacks=[early_stopping])
```
Find out more in the [callbacks documentation](/callbacks).
@@ -268,7 +268,7 @@ Validation data is never shuffled.
The `model.fit` method returns an `History` callback, which has a `history` attribute containing the lists of successive losses and other metrics.
```python
hist = model.fit(X, y, validation_split=0.2)
hist = model.fit(x, y, validation_split=0.2)
print(hist.history)
```
@@ -315,7 +315,7 @@ Making a RNN stateful means that the states for the samples of each batch will b
When using stateful RNNs, it is therefore assumed that:
- all batches have the same number of samples
- If `X1` and `X2` are successive batches of samples, then `X2[i]` is the follow-up sequence to `X1[i]`, for every `i`.
- If `x1` and `x2` are successive batches of samples, then `x2[i]` is the follow-up sequence to `x1[i]`, for every `i`.
To use statefulness in RNNs, you need to:
@@ -332,7 +332,7 @@ Example:
```python
X # this is our input data, of shape (32, 21, 16)
x # this is our input data, of shape (32, 21, 16)
# we will feed it to our model in sequences of length 10
model = Sequential()
@@ -342,10 +342,10 @@ model.add(Dense(16, activation='softmax'))
model.compile(optimizer='rmsprop', loss='categorical_crossentropy')
# we train the network to predict the 11th timestep given the first 10:
model.train_on_batch(X[:, :10, :], np.reshape(X[:, 10, :], (32, 16)))
model.train_on_batch(x[:, :10, :], np.reshape(x[:, 10, :], (32, 16)))
# the state of the network has changed. We can feed the follow-up sequences:
model.train_on_batch(X[:, 10:20, :], np.reshape(X[:, 20, :], (32, 16)))
model.train_on_batch(x[:, 10:20, :], np.reshape(x[:, 20, :], (32, 16)))
# let's reset the states of the LSTM layer:
model.reset_states()
@@ -411,15 +411,15 @@ The VGG16 model is also the basis for several Keras example scripts:
### How can I use HDF5 inputs with Keras?
You can use the `HDF5Matrix` class from `keras.utils.io_utils`. See [the HDF5Matrix documentation](/io_utils/#HDF5Matrix) for details.
You can use the `HDF5Matrix` class from `keras.utils.io_utils`. See [the HDF5Matrix documentation](/utils/#hdf5matrix) for details.
You can also directly use a HDF5 dataset:
```python
import h5py
with h5py.File('input/file.hdf5', 'r') as f:
X_data = f['X_data']
model.predict(X_data)
x_data = f['x_data']
model.predict(x_data)
```
---
@@ -451,6 +451,6 @@ It contains the following fields:
- The image data format to be used as default by image processing layers and utilities (either `channels_last` or `channels_first`).
- The `epsilon` numerical fuzz factor to be used to prevent division by zero in some operations.
- The default float data type.
- The default backend. See the (backend documentation)[/backend].
- The default backend. See the [backend documentation](/backend).
Likewise, cached dataset files, such as those downloaded with [`get_file()`](/utils/#get_file), are stored by default in `$HOME/.keras/datasets/`.
+1 -1
Ver Arquivo
@@ -354,7 +354,7 @@ A stateful recurrent model is one for which the internal states (memories) obtai
of samples are reused as initial states for the samples of the next batch. This allows to process longer sequences
while keeping computational complexity manageable.
[You can read more about stateful RNNs in the FAQ.](/faq/#how-can-i-use-stateful-rnns)
[You can read more about stateful RNNs in the FAQ.](/getting-started/faq/#how-can-i-use-stateful-rnns)
```python
from keras.models import Sequential
+1 -1
Ver Arquivo
@@ -39,5 +39,5 @@ from keras import backend as K
def my_init(shape, dtype=None):
return K.random_normal(shape, dtype=dtype)
model.add(Dense(64, init=my_init))
model.add(Dense(64, kernel_initializer=my_init))
```
+21 -19
Ver Arquivo
@@ -7,6 +7,7 @@ keras.preprocessing.image.ImageDataGenerator(featurewise_center=False,
featurewise_std_normalization=False,
samplewise_std_normalization=False,
zca_whitening=False,
zca_epsilon=1e-6,
rotation_range=0.,
width_shift_range=0.,
height_shift_range=0.,
@@ -29,6 +30,7 @@ Generate batches of tensor image data with real-time data augmentation. The data
- __samplewise_center__: Boolean. Set each sample mean to 0.
- __featurewise_std_normalization__: Boolean. Divide inputs by std of the dataset, feature-wise.
- __samplewise_std_normalization__: Boolean. Divide each input by its std.
- __zca_epsilon__: epsilon for ZCA whitening. Default is 1e-6.
- __zca_whitening__: Boolean. Apply ZCA whitening.
- __rotation_range__: Int. Degree range for random rotations.
- __width_shift_range__: Float (fraction of total width). Range for random horizontal shifts.
@@ -56,19 +58,19 @@ Generate batches of tensor image data with real-time data augmentation. The data
If you never set it, then it will be "channels_last".
- __Methods__:
- __fit(X)__: Compute the internal data stats related to the data-dependent transformations, based on an array of sample data.
- __fit(x)__: Compute the internal data stats related to the data-dependent transformations, based on an array of sample data.
Only required if featurewise_center or featurewise_std_normalization or zca_whitening.
- __Arguments__:
- __X__: sample data. Should have rank 4.
- __x__: sample data. Should have rank 4.
In case of grayscale data,
the channels axis should have value 1, and in case
of RGB data, it should have value 3.
- __augment__: Boolean (default: False). Whether to fit on randomly augmented samples.
- __rounds__: int (default: 1). If augment, how many augmentation passes over the data to use.
- __seed__: int (default: None). Random seed.
- __flow(X, y)__: Takes numpy data & label arrays, and generates batches of augmented/normalized data. Yields batches indefinitely, in an infinite loop.
- __flow(x, y)__: Takes numpy data & label arrays, and generates batches of augmented/normalized data. Yields batches indefinitely, in an infinite loop.
- __Arguments__:
- __X__: data. Should have rank 4.
- __x__: data. Should have rank 4.
In case of grayscale data,
the channels axis should have value 1, and in case
of RGB data, it should have value 3.
@@ -78,7 +80,7 @@ Generate batches of tensor image data with real-time data augmentation. The data
- __seed__: int (default: None).
- __save_to_dir__: None or str (default: None). This allows you to optimally specify a directory to which to save the augmented pictures being generated (useful for visualizing what you are doing).
- __save_prefix__: str (default: `''`). Prefix to use for filenames of saved pictures (only relevant if `save_to_dir` is set).
- __save_format__: one of "png", "jpeg" (only relevant if `save_to_dir` is set). Default: "jpeg".
- __save_format__: one of "png", "jpeg" (only relevant if `save_to_dir` is set). Default: "png".
- __yields__: Tuples of `(x, y)` where `x` is a numpy array of image data and `y` is a numpy array of corresponding labels.
The generator loops indefinitely.
- __flow_from_directory(directory)__: Takes the path to a directory, and generates batches of augmented/normalized data. Yields batches indefinitely, in an infinite loop.
@@ -88,25 +90,25 @@ Generate batches of tensor image data with real-time data augmentation. The data
See [this script](https://gist.github.com/fchollet/0830affa1f7f19fd47b06d4cf89ed44d) for more details.
- __target_size__: tuple of integers, default: `(256, 256)`. The dimensions to which all images found will be resized.
- __color_mode__: one of "grayscale", "rbg". Default: "rgb". Whether the images will be converted to have 1 or 3 color channels.
- __classes__: optional list of class subdirectories (e.g. `['dogs', 'cats']`). Default: None. If not provided, the list of classes will be automatically inferred (and the order of the classes, which will map to the label indices, will be alphanumeric).
- __class_mode__: one of "categorical", "binary", "sparse" or None. Default: "categorical". Determines the type of label arrays that are returned: "categorical" will be 2D one-hot encoded labels, "binary" will be 1D binary labels, "sparse" will be 1D integer labels. If None, no labels are returned (the generator will only yield batches of image data, which is useful to use `model.predict_generator()`, `model.evaluate_generator()`, etc.).
- __classes__: optional list of class subdirectories (e.g. `['dogs', 'cats']`). Default: None. If not provided, the list of classes will be automatically inferred from the subdirectory names/structure under `directory`, where each subdirectory will be treated as a different class (and the order of the classes, which will map to the label indices, will be alphanumeric). The dictionary containing the mapping from class names to class indices can be obtained via the attribute `class_indices`.
- __class_mode__: one of "categorical", "binary", "sparse" or None. Default: "categorical". Determines the type of label arrays that are returned: "categorical" will be 2D one-hot encoded labels, "binary" will be 1D binary labels, "sparse" will be 1D integer labels. If None, no labels are returned (the generator will only yield batches of image data, which is useful to use `model.predict_generator()`, `model.evaluate_generator()`, etc.). Please note that in case of class_mode None, the data still needs to reside in a subdirectory of `directory` for it to work correctly.
- __batch_size__: size of the batches of data (default: 32).
- __shuffle__: whether to shuffle the data (default: True)
- __seed__: optional random seed for shuffling and transformations.
- __save_to_dir__: None or str (default: None). This allows you to optimally specify a directory to which to save the augmented pictures being generated (useful for visualizing what you are doing).
- __save_prefix__: str. Prefix to use for filenames of saved pictures (only relevant if `save_to_dir` is set).
- __save_format__: one of "png", "jpeg" (only relevant if `save_to_dir` is set). Default: "jpeg".
- __save_format__: one of "png", "jpeg" (only relevant if `save_to_dir` is set). Default: "png".
- __follow_links__: whether to follow symlinks inside class subdirectories (default: False).
- __Examples__:
Example of using `.flow(X, y)`:
Example of using `.flow(x, y)`:
```python
(X_train, y_train), (X_test, y_test) = cifar10.load_data()
Y_train = np_utils.to_categorical(y_train, num_classes)
Y_test = np_utils.to_categorical(y_test, num_classes)
(x_train, y_train), (x_test, y_test) = cifar10.load_data()
y_train = np_utils.to_categorical(y_train, num_classes)
y_test = np_utils.to_categorical(y_test, num_classes)
datagen = ImageDataGenerator(
featurewise_center=True,
@@ -118,20 +120,20 @@ datagen = ImageDataGenerator(
# compute quantities required for featurewise normalization
# (std, mean, and principal components if ZCA whitening is applied)
datagen.fit(X_train)
datagen.fit(x_train)
# fits the model on batches with real-time data augmentation:
model.fit_generator(datagen.flow(X_train, Y_train, batch_size=32),
steps_per_epoch=len(X_train), epochs=epochs)
model.fit_generator(datagen.flow(x_train, y_train, batch_size=32),
steps_per_epoch=len(x_train) / 32, epochs=epochs)
# here's a more "manual" example
for e in range(epochs):
print 'Epoch', e
print('Epoch', e)
batches = 0
for X_batch, Y_batch in datagen.flow(X_train, Y_train, batch_size=32):
loss = model.train(X_batch, Y_batch)
for x_batch, y_batch in datagen.flow(x_train, y_train, batch_size=32):
model.fit(x_batch, y_batch)
batches += 1
if batches >= len(X_train) / 32:
if batches >= len(x_train) / 32:
# we need to break the loop by hand because
# the generator loops indefinitely
break
+6 -5
Ver Arquivo
@@ -3,7 +3,7 @@
```python
keras.preprocessing.text.text_to_word_sequence(text,
filters=base_filter(), lower=True, split=" ")
filters='!"#$%&()*+,-./:;<=>?@[\\]^_`{|}~\t\n', lower=True, split=" ")
```
Split a sentence into a list of words.
@@ -12,7 +12,7 @@ Split a sentence into a list of words.
- __Arguments__:
- __text__: str.
- __filters__: list (or concatenation) of characters to filter out, such as punctuation. Default: base_filter(), includes basic punctuation, tabs, and newlines.
- __filters__: list (or concatenation) of characters to filter out, such as punctuation. Default: '!"#$%&()*+,-./:;<=>?@[\\]^_`{|}~\t\n' , includes basic punctuation, tabs, and newlines.
- __lower__: boolean. Whether to set the text to lowercase.
- __split__: str. Separator for word splitting.
@@ -20,7 +20,7 @@ Split a sentence into a list of words.
```python
keras.preprocessing.text.one_hot(text, n,
filters=base_filter(), lower=True, split=" ")
filters='!"#$%&()*+,-./:;<=>?@[\\]^_`{|}~\t\n', lower=True, split=" ")
```
One-hot encode a text into a list of word indexes in a vocabulary of size n.
@@ -33,14 +33,15 @@ One-hot encode a text into a list of word indexes in a vocabulary of size n.
## Tokenizer
```python
keras.preprocessing.text.Tokenizer(num_words=None, filters=base_filter(),
lower=True, split=" ")
keras.preprocessing.text.Tokenizer(num_words=None, filters='!"#$%&()*+,-./:;<=>?@[\\]^_`{|}~\t\n',
lower=True, split=" ", char_level=False)
```
Class for vectorizing texts, or/and turning texts into sequences (=list of word indexes, where the word of rank i in the dataset (starting at 1) has index i).
- __Arguments__: Same as `text_to_word_sequence` above.
- __num_words__: None or int. Maximum number of words to work with (if set, tokenization will be restricted to the top num_words most common words in the dataset).
- __char_level__: if True, every character will be treated as a token.
- __Methods__:
+6 -6
Ver Arquivo
@@ -78,7 +78,7 @@ INVERT = True
# Maximum length of input is 'int + int' (e.g., '345+678'). Maximum length of
# int is DIGITS.
MAxLEN = DIGITS + 1 + DIGITS
MAXLEN = DIGITS + 1 + DIGITS
# All the numbers, plus sign and space for padding.
chars = '0123456789+ '
@@ -98,9 +98,9 @@ while len(questions) < TRAINING_SIZE:
if key in seen:
continue
seen.add(key)
# Pad the data with spaces such that it is always MAxLEN.
# Pad the data with spaces such that it is always MAXLEN.
q = '{}+{}'.format(a, b)
query = q + ' ' * (MAxLEN - len(q))
query = q + ' ' * (MAXLEN - len(q))
ans = str(a + b)
# Answers can be of maximum size DIGITS + 1.
ans += ' ' * (DIGITS + 1 - len(ans))
@@ -113,10 +113,10 @@ while len(questions) < TRAINING_SIZE:
print('Total addition questions:', len(questions))
print('Vectorization...')
x = np.zeros((len(questions), MAxLEN, len(chars)), dtype=np.bool)
x = np.zeros((len(questions), MAXLEN, len(chars)), dtype=np.bool)
y = np.zeros((len(questions), DIGITS + 1, len(chars)), dtype=np.bool)
for i, sentence in enumerate(questions):
x[i] = ctable.encode(sentence, MAxLEN)
x[i] = ctable.encode(sentence, MAXLEN)
for i, sentence in enumerate(expected):
y[i] = ctable.encode(sentence, DIGITS + 1)
@@ -151,7 +151,7 @@ model = Sequential()
# "Encode" the input sequence using an RNN, producing an output of HIDDEN_SIZE.
# Note: In a situation where your input sequences have a variable length,
# use input_shape=(None, num_feature).
model.add(RNN(HIDDEN_SIZE, input_shape=(MAxLEN, len(chars))))
model.add(RNN(HIDDEN_SIZE, input_shape=(MAXLEN, len(chars))))
# As the decoder RNN's input, repeatedly provide with the last hidden state of
# RNN for each time step. Repeat 'DIGITS + 1' times as that's the maximum
# length of output, e.g., when DIGITS=3, max output is 999+999=1998.
+121 -145
Ver Arquivo
@@ -8,24 +8,16 @@ e.g.:
```
python deep_dream.py img/mypic.jpg results/dream
```
It is preferable to run this script on GPU, for speed.
If running on CPU, prefer the TensorFlow backend (much faster).
Example results: http://i.imgur.com/FX6ROg9.jpg
'''
from __future__ import print_function
from keras.preprocessing.image import load_img, img_to_array
import numpy as np
from scipy.misc import imsave
from scipy.optimize import fmin_l_bfgs_b
import time
import scipy
import argparse
from keras.applications import vgg16
from keras.applications import inception_v3
from keras import backend as K
from keras.layers import Input
parser = argparse.ArgumentParser(description='Deep Dreams with Keras.')
parser.add_argument('base_image_path', metavar='base', type=str,
@@ -37,183 +29,167 @@ args = parser.parse_args()
base_image_path = args.base_image_path
result_prefix = args.result_prefix
# dimensions of the generated picture.
img_height = 600
img_width = 600
# some settings we found interesting
saved_settings = {
'bad_trip': {'features': {'block4_conv1': 0.05,
'block4_conv2': 0.01,
'block4_conv3': 0.01},
'continuity': 0.1,
'dream_l2': 0.8,
'jitter': 5},
'dreamy': {'features': {'block5_conv1': 0.05,
'block5_conv2': 0.02},
'continuity': 0.1,
'dream_l2': 0.02,
'jitter': 0},
# These are the names of the layers
# for which we try to maximize activation,
# as well as their weight in the final loss
# we try to maximize.
# You can tweak these setting to obtain new visual effects.
settings = {
'features': {
'mixed2': 0.2,
'mixed3': 0.5,
'mixed4': 2.,
'mixed5': 1.5,
},
}
# the settings we will use in this experiment
settings = saved_settings['dreamy']
def preprocess_image(image_path):
# util function to open, resize and format pictures
# into appropriate tensors
img = load_img(image_path, target_size=(img_height, img_width))
# Util function to open, resize and format pictures
# into appropriate tensors.
img = load_img(image_path)
img = img_to_array(img)
img = np.expand_dims(img, axis=0)
img = vgg16.preprocess_input(img)
img = inception_v3.preprocess_input(img)
return img
def deprocess_image(x):
# util function to convert a tensor into a valid image
# Util function to convert a tensor into a valid image.
if K.image_data_format() == 'channels_first':
x = x.reshape((3, img_height, img_width))
x = x.reshape((3, x.shape[2], x.shape[3]))
x = x.transpose((1, 2, 0))
else:
x = x.reshape((img_height, img_width, 3))
# Remove zero-center by mean pixel
x[:, :, 0] += 103.939
x[:, :, 1] += 116.779
x[:, :, 2] += 123.68
# 'BGR'->'RGB'
x = x[:, :, ::-1]
x = x.reshape((x.shape[1], x.shape[2], 3))
x /= 2.
x += 0.5
x *= 255.
x = np.clip(x, 0, 255).astype('uint8')
return x
if K.image_data_format() == 'channels_first':
img_size = (3, img_height, img_width)
else:
img_size = (img_height, img_width, 3)
# this will contain our generated image
dream = Input(batch_shape=(1,) + img_size)
K.set_learning_phase(0)
# build the VGG16 network with our placeholder
# the model will be loaded with pre-trained ImageNet weights
model = vgg16.VGG16(input_tensor=dream,
weights='imagenet', include_top=False)
# Build the InceptionV3 network with our placeholder.
# The model will be loaded with pre-trained ImageNet weights.
model = inception_v3.InceptionV3(weights='imagenet',
include_top=False)
dream = model.input
print('Model loaded.')
# get the symbolic outputs of each "key" layer (we gave them unique names).
# Get the symbolic outputs of each "key" layer (we gave them unique names).
layer_dict = dict([(layer.name, layer) for layer in model.layers])
def continuity_loss(x):
# continuity loss util function
assert K.ndim(x) == 4
if K.image_data_format() == 'channels_first':
a = K.square(x[:, :, :img_height - 1, :img_width - 1] -
x[:, :, 1:, :img_width - 1])
b = K.square(x[:, :, :img_height - 1, :img_width - 1] -
x[:, :, :img_height - 1, 1:])
else:
a = K.square(x[:, :img_height - 1, :img_width - 1, :] -
x[:, 1:, :img_width - 1, :])
b = K.square(x[:, :img_height - 1, :img_width - 1, :] -
x[:, :img_height - 1, 1:, :])
return K.sum(K.pow(a + b, 1.25))
# define the loss
# Define the loss.
loss = K.variable(0.)
for layer_name in settings['features']:
# add the L2 norm of the features of a layer to the loss
# Add the L2 norm of the features of a layer to the loss.
assert layer_name in layer_dict.keys(), 'Layer ' + layer_name + ' not found in model.'
coeff = settings['features'][layer_name]
x = layer_dict[layer_name].output
shape = layer_dict[layer_name].output_shape
# we avoid border artifacts by only involving non-border pixels in the loss
# We avoid border artifacts by only involving non-border pixels in the loss.
scaling = K.prod(K.cast(K.shape(x), 'float32'))
if K.image_data_format() == 'channels_first':
loss -= coeff * K.sum(K.square(x[:, :, 2: shape[2] - 2, 2: shape[3] - 2])) / np.prod(shape[1:])
loss += coeff * K.sum(K.square(x[:, :, 2: -2, 2: -2])) / scaling
else:
loss -= coeff * K.sum(K.square(x[:, 2: shape[1] - 2, 2: shape[2] - 2, :])) / np.prod(shape[1:])
loss += coeff * K.sum(K.square(x[:, 2: -2, 2: -2, :])) / scaling
# add continuity loss (gives image local coherence, can result in an artful blur)
loss += settings['continuity'] * continuity_loss(dream) / np.prod(img_size)
# add image L2 norm to loss (prevents pixels from taking very high values, makes image darker)
loss += settings['dream_l2'] * K.sum(K.square(dream)) / np.prod(img_size)
# Compute the gradients of the dream wrt the loss.
grads = K.gradients(loss, dream)[0]
# Normalize gradients.
grads /= K.maximum(K.mean(K.abs(grads)), 1e-7)
# feel free to further modify the loss as you see fit, to achieve new effects...
# compute the gradients of the dream wrt the loss
grads = K.gradients(loss, dream)
outputs = [loss]
if isinstance(grads, (list, tuple)):
outputs += grads
else:
outputs.append(grads)
f_outputs = K.function([dream], outputs)
# Set up function to retrieve the value
# of the loss and gradients given an input image.
outputs = [loss, grads]
fetch_loss_and_grads = K.function([dream], outputs)
def eval_loss_and_grads(x):
x = x.reshape((1,) + img_size)
outs = f_outputs([x])
outs = fetch_loss_and_grads([x])
loss_value = outs[0]
if len(outs[1:]) == 1:
grad_values = outs[1].flatten().astype('float64')
else:
grad_values = np.array(outs[1:]).flatten().astype('float64')
grad_values = outs[1]
return loss_value, grad_values
class Evaluator(object):
"""Loss and gradients evaluator.
def resize_img(img, size):
img = np.copy(img)
if K.image_data_format() == 'channels_first':
factors = (1, 1,
float(size[0]) / img.shape[2],
float(size[1]) / img.shape[3])
else:
factors = (1,
float(size[0]) / img.shape[1],
float(size[1]) / img.shape[2],
1)
return scipy.ndimage.zoom(img, factors, order=1)
This Evaluator class makes it possible
to compute loss and gradients in one pass
while retrieving them via two separate functions,
"loss" and "grads". This is done because scipy.optimize
requires separate functions for loss and gradients,
but computing them separately would be inefficient.
"""
def __init__(self):
self.loss_value = None
self.grad_values = None
def loss(self, x):
assert self.loss_value is None
def gradient_ascent(x, iterations, step, max_loss=None):
for i in range(iterations):
loss_value, grad_values = eval_loss_and_grads(x)
self.loss_value = loss_value
self.grad_values = grad_values
return self.loss_value
if max_loss is not None and loss_value > max_loss:
break
print('..Loss value at', i, ':', loss_value)
x += step * grad_values
return x
def grads(self, x):
assert self.loss_value is not None
grad_values = np.copy(self.grad_values)
self.loss_value = None
self.grad_values = None
return grad_values
evaluator = Evaluator()
def save_img(img, fname):
pil_img = deprocess_image(np.copy(img))
scipy.misc.imsave(fname, pil_img)
# Run scipy-based optimization (L-BFGS) over the pixels of the generated image
# so as to minimize the loss
x = preprocess_image(base_image_path)
for i in range(5):
print('Start of iteration', i)
start_time = time.time()
# Add a random jitter to the initial image.
# This will be reverted at decoding time
random_jitter = (settings['jitter'] * 2) * (np.random.random(img_size) - 0.5)
x += random_jitter
"""Process:
# Run L-BFGS for 7 steps
x, min_val, info = fmin_l_bfgs_b(evaluator.loss, x.flatten(),
fprime=evaluator.grads, maxfun=7)
print('Current loss value:', min_val)
# Decode the dream and save it
x = x.reshape(img_size)
x -= random_jitter
img = deprocess_image(np.copy(x))
fname = result_prefix + '_at_iteration_%d.png' % i
imsave(fname, img)
end_time = time.time()
print('Image saved as', fname)
print('Iteration %d completed in %ds' % (i, end_time - start_time))
- Load the original image.
- Define a number of processing scales (i.e. image shapes),
from smallest to largest.
- Resize the original image to the smallest scale.
- For every scale, starting with the smallest (i.e. current one):
- Run gradient ascent
- Upscale image to the next scale
- Reinject the detail that was lost at upscaling time
- Stop when we are back to the original size.
To obtain the detail lost during upscaling, we simply
take the original image, shrink it down, upscale it,
and compare the result to the (resized) original image.
"""
# Playing with these hyperparameters will also allow you to achieve new effects
step = 0.01 # Gradient ascent step size
num_octave = 3 # Number of scales at which to run gradient ascent
octave_scale = 1.4 # Size ratio between scales
iterations = 20 # Number of ascent steps per scale
max_loss = 10.
img = preprocess_image(base_image_path)
if K.image_data_format() == 'channels_first':
original_shape = img.shape[2:]
else:
original_shape = img.shape[1:3]
successive_shapes = [original_shape]
for i in range(1, num_octave):
shape = tuple([int(dim / (octave_scale ** i)) for dim in original_shape])
successive_shapes.append(shape)
successive_shapes = successive_shapes[::-1]
original_img = np.copy(img)
shrunk_original_img = resize_img(img, successive_shapes[0])
for shape in successive_shapes:
print('Processing image shape', shape)
img = resize_img(img, shape)
img = gradient_ascent(img,
iterations=iterations,
step=step,
max_loss=max_loss)
upscaled_shrunk_original_img = resize_img(shrunk_original_img, shape)
same_size_original = resize_img(original_img, shape)
lost_detail = same_size_original - upscaled_shrunk_original_img
img += lost_detail
shrunk_original_img = resize_img(original_img, shape)
save_img(img, fname=result_prefix + '.png')
+1 -1
Ver Arquivo
@@ -24,7 +24,7 @@ from keras import backend as K
def euclidean_distance(vects):
x, y = vects
return K.sqrt(K.sum(K.square(x - y), axis=1, keepdims=True))
return K.sqrt(K.maximum(K.sum(K.square(x - y), axis=1, keepdims=True), K.epsilon()))
def eucl_dist_output_shape(shapes):
+3 -2
Ver Arquivo
@@ -196,8 +196,8 @@ x = mask_input
for layer in image_model.layers[1:]:
name = 'mask_%s' % layer.name
if 'conv' in layer.name:
x = AveragePooling2D((3, 3), strides=(
1, 1), name=name, border_mode='same')(x)
x = AveragePooling2D((3, 3), padding='same', strides=(
1, 1), name=name)(x)
elif 'pool' in layer.name:
x = AveragePooling2D((2, 2), name=name)(x)
mask_model = Model(mask_input, x)
@@ -238,6 +238,7 @@ def region_style_loss(style_image, target_image, style_mask, target_mask):
masked_target = K.permute_dimensions(
target_image, (2, 0, 1)) * target_mask
num_channels = K.shape(style_image)[-1]
num_channels = K.cast(num_channels, dtype='float32')
s = gram_matrix(masked_style) / K.mean(style_mask) / num_channels
c = gram_matrix(masked_target) / K.mean(target_mask) / num_channels
return K.mean(K.square(s - c))
+25 -8
Ver Arquivo
@@ -6,7 +6,7 @@ import numpy as np
import matplotlib.pyplot as plt
from scipy.stats import norm
from keras.layers import Input, Dense, Lambda
from keras.layers import Input, Dense, Lambda, Layer
from keras.models import Model
from keras import backend as K
from keras import metrics
@@ -19,6 +19,7 @@ intermediate_dim = 256
epochs = 50
epsilon_std = 1.0
x = Input(batch_shape=(batch_size, original_dim))
h = Dense(intermediate_dim, activation='relu')(x)
z_mean = Dense(latent_dim)(h)
@@ -41,13 +42,29 @@ h_decoded = decoder_h(z)
x_decoded_mean = decoder_mean(h_decoded)
def vae_loss(x, x_decoded_mean):
xent_loss = original_dim * metrics.binary_crossentropy(x, x_decoded_mean)
kl_loss = - 0.5 * K.sum(1 + z_log_var - K.square(z_mean) - K.exp(z_log_var), axis=-1)
return xent_loss + kl_loss
# Custom loss layer
class CustomVariationalLayer(Layer):
def __init__(self, **kwargs):
self.is_placeholder = True
super(CustomVariationalLayer, self).__init__(**kwargs)
def vae_loss(self, x, x_decoded_mean):
xent_loss = original_dim * metrics.binary_crossentropy(x, x_decoded_mean)
kl_loss = - 0.5 * K.sum(1 + z_log_var - K.square(z_mean) - K.exp(z_log_var), axis=-1)
return K.mean(xent_loss + kl_loss)
def call(self, inputs):
x = inputs[0]
x_decoded_mean = inputs[1]
loss = self.vae_loss(x, x_decoded_mean)
self.add_loss(loss, inputs=inputs)
# We won't actually use the output.
return x
y = CustomVariationalLayer()([x, x_decoded_mean])
vae = Model(x, y)
vae.compile(optimizer='rmsprop', loss=None)
vae = Model(x, x_decoded_mean)
vae.compile(optimizer='rmsprop', loss=vae_loss)
# train the VAE on MNIST digits
(x_train, y_train), (x_test, y_test) = mnist.load_data()
@@ -57,7 +74,7 @@ x_test = x_test.astype('float32') / 255.
x_train = x_train.reshape((len(x_train), np.prod(x_train.shape[1:])))
x_test = x_test.reshape((len(x_test), np.prod(x_test.shape[1:])))
vae.fit(x_train, x_train,
vae.fit(x_train,
shuffle=True,
epochs=epochs,
batch_size=batch_size,
+26 -12
Ver Arquivo
@@ -7,7 +7,7 @@ import numpy as np
import matplotlib.pyplot as plt
from scipy.stats import norm
from keras.layers import Input, Dense, Lambda, Flatten, Reshape
from keras.layers import Input, Dense, Lambda, Flatten, Reshape, Layer
from keras.layers import Conv2D, Conv2DTranspose
from keras.models import Model
from keras import backend as K
@@ -106,17 +106,31 @@ x_decoded_relu = decoder_deconv_3_upsamp(deconv_2_decoded)
x_decoded_mean_squash = decoder_mean_squash(x_decoded_relu)
def vae_loss(x, x_decoded_mean):
# NOTE: binary_crossentropy expects a batch_size by dim
# for x and x_decoded_mean, so we MUST flatten these!
x = K.flatten(x)
x_decoded_mean = K.flatten(x_decoded_mean)
xent_loss = img_rows * img_cols * metrics.binary_crossentropy(x, x_decoded_mean)
kl_loss = - 0.5 * K.mean(1 + z_log_var - K.square(z_mean) - K.exp(z_log_var), axis=-1)
return xent_loss + kl_loss
# Custom loss layer
class CustomVariationalLayer(Layer):
def __init__(self, **kwargs):
self.is_placeholder = True
super(CustomVariationalLayer, self).__init__(**kwargs)
vae = Model(x, x_decoded_mean_squash)
vae.compile(optimizer='rmsprop', loss=vae_loss)
def vae_loss(self, x, x_decoded_mean_squash):
x = K.flatten(x)
x_decoded_mean_squash = K.flatten(x_decoded_mean_squash)
xent_loss = img_rows * img_cols * metrics.binary_crossentropy(x, x_decoded_mean_squash)
kl_loss = - 0.5 * K.mean(1 + z_log_var - K.square(z_mean) - K.exp(z_log_var), axis=-1)
return K.mean(xent_loss + kl_loss)
def call(self, inputs):
x = inputs[0]
x_decoded_mean_squash = inputs[1]
loss = self.vae_loss(x, x_decoded_mean_squash)
self.add_loss(loss, inputs=inputs)
# We don't use this output.
return x
y = CustomVariationalLayer()([x, x_decoded_mean_squash])
vae = Model(x, y)
vae.compile(optimizer='rmsprop', loss=None)
vae.summary()
# train the VAE on MNIST digits
@@ -129,7 +143,7 @@ x_test = x_test.reshape((x_test.shape[0],) + original_img_size)
print('x_train.shape:', x_train.shape)
vae.fit(x_train, x_train,
vae.fit(x_train,
shuffle=True,
epochs=epochs,
batch_size=batch_size,
+15 -8
Ver Arquivo
@@ -1,21 +1,28 @@
"""The Keras API.
"""
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
from . import activations
from . import applications
from . import backend
from . import datasets
from . import engine
from . import layers
from . import preprocessing
from . import utils
from . import wrappers
from . import callbacks
from . import constraints
from . import datasets
from . import engine
from . import initializers
from . import layers
from . import losses
from . import metrics
from . import models
from . import losses
from . import optimizers
from . import preprocessing
from . import regularizers
from . import utils
from . import wrappers
__version__ = '2.0.3'
# Importable from root because it's technically not a layer
from .layers import Input
__version__ = '2.0.4-tf'
+15 -1
Ver Arquivo
@@ -1,7 +1,14 @@
"""Keras built-in activation functions.
"""
from __future__ import absolute_import
import six
from __future__ import division
from __future__ import print_function
import warnings
from . import backend as K
import six
from .utils.generic_utils import deserialize_keras_object
from .engine import Layer
def softmax(x, axis=-1):
@@ -78,6 +85,13 @@ def get(identifier):
identifier = str(identifier)
return deserialize(identifier)
elif callable(identifier):
if isinstance(identifier, Layer):
warnings.warn((
'Do not pass a layer instance (such as {identifier}) as the '
'activation argument of another layer. Instead, advanced '
'activation layers should be used just like any other '
'layer in a model.'
).format(identifier=identifier.__class__.__name__))
return identifier
else:
raise ValueError('Could not interpret '
+8 -2
Ver Arquivo
@@ -1,5 +1,11 @@
"""Keras Applications: models with automatic loading of pre-trained weights.
"""
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
from .inception_v3 import InceptionV3
from .resnet50 import ResNet50
from .vgg16 import VGG16
from .vgg19 import VGG19
from .resnet50 import ResNet50
from .inception_v3 import InceptionV3
from .xception import Xception
+7 -1
Ver Arquivo
@@ -1,7 +1,13 @@
"""Utilities used by models pre-trained on ImageNet.
"""
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
import json
from ..utils.data_utils import get_file
from .. import backend as K
from ..utils.data_utils import get_file
CLASS_INDEX = None
CLASS_INDEX_PATH = 'https://s3.amazonaws.com/deep-learning-models/image-models/imagenet_class_index.json'
+13 -17
Ver Arquivo
@@ -10,28 +10,29 @@ and that the input preprocessing function is also different (same as Xception).
- [Rethinking the Inception Architecture for Computer Vision](http://arxiv.org/abs/1512.00567)
"""
from __future__ import print_function
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
import warnings
from ..models import Model
from .. import backend as K
from .. import layers
from ..engine.topology import get_source_inputs
from .imagenet_utils import _obtain_input_shape
from .imagenet_utils import decode_predictions # pylint: disable=unused-import
from ..layers import AveragePooling2D
from ..layers import Activation
from ..layers import Dense
from ..layers import Input
from ..layers import BatchNormalization
from ..layers import Conv2D
from ..layers import MaxPooling2D
from ..layers import AveragePooling2D
from ..layers import Dense
from ..layers import GlobalAveragePooling2D
from ..layers import GlobalMaxPooling2D
from ..engine.topology import get_source_inputs
from ..utils.layer_utils import convert_all_kernels_in_model
from ..layers import Input
from ..layers import MaxPooling2D
from ..models import Model
from ..utils.data_utils import get_file
from .. import backend as K
from .imagenet_utils import decode_predictions
from .imagenet_utils import _obtain_input_shape
from ..utils.layer_utils import convert_all_kernels_in_model
WEIGHTS_PATH = 'https://github.com/fchollet/deep-learning-models/releases/download/v0.5/inception_v3_weights_tf_dim_ordering_tf_kernels.h5'
@@ -157,10 +158,7 @@ def InceptionV3(include_top=True,
if input_tensor is None:
img_input = Input(shape=input_shape)
else:
if not K.is_keras_tensor(input_tensor):
img_input = Input(tensor=input_tensor, shape=input_shape)
else:
img_input = input_tensor
img_input = Input(tensor=input_tensor, shape=input_shape)
if K.image_data_format() == 'channels_first':
channel_axis = 1
@@ -384,8 +382,6 @@ def InceptionV3(include_top=True,
cache_subdir='models',
md5_hash='bcbd6486424b2319ff4ef7d526e38f63')
model.load_weights(weights_path)
if K.backend() == 'theano':
convert_all_kernels_in_model(model)
return model
+27 -29
Ver Arquivo
@@ -7,31 +7,32 @@
Adapted from code contributed by BigMoyan.
"""
from __future__ import print_function
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
import warnings
from ..layers import Input
from .. import backend as K
from .. import layers
from ..layers import Dense
from ..engine.topology import get_source_inputs
from .imagenet_utils import _obtain_input_shape
from .imagenet_utils import decode_predictions # pylint: disable=unused-import
from .imagenet_utils import preprocess_input # pylint: disable=unused-import
from ..layers import Activation
from ..layers import Flatten
from ..layers import Conv2D
from ..layers import MaxPooling2D
from ..layers import ZeroPadding2D
from ..layers import AveragePooling2D
from ..layers import BatchNormalization
from ..layers import Conv2D
from ..layers import Dense
from ..layers import Flatten
from ..layers import GlobalAveragePooling2D
from ..layers import GlobalMaxPooling2D
from ..layers import BatchNormalization
from ..layers import Input
from ..layers import MaxPooling2D
from ..layers import ZeroPadding2D
from ..models import Model
from .. import backend as K
from ..engine.topology import get_source_inputs
from ..utils import layer_utils
from ..utils.data_utils import get_file
from .imagenet_utils import decode_predictions
from .imagenet_utils import preprocess_input
from .imagenet_utils import _obtain_input_shape
WEIGHTS_PATH = 'https://github.com/fchollet/deep-learning-models/releases/download/v0.2/resnet50_weights_tf_dim_ordering_tf_kernels.h5'
@@ -77,7 +78,7 @@ def identity_block(input_tensor, kernel_size, filters, stage, block):
def conv_block(input_tensor, kernel_size, filters, stage, block, strides=(2, 2)):
"""conv_block is the block that has a conv layer at shortcut
"""conv_block is the block that has a conv layer at shortcut.
# Arguments
input_tensor: input tensor
@@ -85,6 +86,7 @@ def conv_block(input_tensor, kernel_size, filters, stage, block, strides=(2, 2))
filters: list of integers, the filterss of 3 conv layer at main path
stage: integer, current stage label, used for generating layer names
block: 'a','b'..., current block label, used for generating layer names
strides: Tuple of integers.
# Returns
Output tensor for the block.
@@ -194,10 +196,8 @@ def ResNet50(include_top=True, weights='imagenet',
if input_tensor is None:
img_input = Input(shape=input_shape)
else:
if not K.is_keras_tensor(input_tensor):
img_input = Input(tensor=input_tensor, shape=input_shape)
else:
img_input = input_tensor
img_input = Input(tensor=input_tensor, shape=input_shape)
if K.image_data_format() == 'channels_last':
bn_axis = 3
else:
@@ -264,21 +264,19 @@ def ResNet50(include_top=True, weights='imagenet',
model.load_weights(weights_path)
if K.backend() == 'theano':
layer_utils.convert_all_kernels_in_model(model)
if K.image_data_format() == 'channels_first':
if include_top:
maxpool = model.get_layer(name='avg_pool')
shape = maxpool.output_shape[1:]
dense = model.get_layer(name='fc1000')
layer_utils.convert_dense_weights_data_format(dense, shape, 'channels_first')
if K.backend() == 'tensorflow':
warnings.warn('You are using the TensorFlow backend, yet you '
'are using the Theano '
'image data format convention '
'(`image_data_format="channels_first"`). '
'For best performance, set '
'`image_data_format="channels_last"` in '
'your Keras config '
'at ~/.keras/keras.json.')
if K.image_data_format() == 'channels_first' and K.backend() == 'tensorflow':
warnings.warn('You are using the TensorFlow backend, yet you '
'are using the Theano '
'image data format convention '
'(`image_data_format="channels_first"`). '
'For best performance, set '
'`image_data_format="channels_last"` in '
'your Keras config '
'at ~/.keras/keras.json.')
return model
+15 -16
Ver Arquivo
@@ -6,26 +6,27 @@
- [Very Deep Convolutional Networks for Large-Scale Image Recognition](https://arxiv.org/abs/1409.1556)
"""
from __future__ import print_function
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
import warnings
from ..models import Model
from ..layers import Flatten
from ..layers import Dense
from ..layers import Input
from .. import backend as K
from ..engine.topology import get_source_inputs
from .imagenet_utils import _obtain_input_shape
from .imagenet_utils import decode_predictions # pylint: disable=unused-import
from .imagenet_utils import preprocess_input # pylint: disable=unused-import
from ..layers import Conv2D
from ..layers import MaxPooling2D
from ..layers import Dense
from ..layers import Flatten
from ..layers import GlobalAveragePooling2D
from ..layers import GlobalMaxPooling2D
from ..engine.topology import get_source_inputs
from ..layers import Input
from ..layers import MaxPooling2D
from ..models import Model
from ..utils import layer_utils
from ..utils.data_utils import get_file
from .. import backend as K
from .imagenet_utils import decode_predictions
from .imagenet_utils import preprocess_input
from .imagenet_utils import _obtain_input_shape
WEIGHTS_PATH = 'https://github.com/fchollet/deep-learning-models/releases/download/v0.1/vgg16_weights_tf_dim_ordering_tf_kernels.h5'
@@ -59,7 +60,7 @@ def VGG16(include_top=True, weights='imagenet',
input_shape: optional shape tuple, only to be specified
if `include_top` is False (otherwise the input shape
has to be `(224, 224, 3)` (with `channels_last` data format)
or `(3, 224, 244)` (with `channels_first` data format).
or `(3, 224, 224)` (with `channels_first` data format).
It should have exactly 3 inputs channels,
and width and height should be no smaller than 48.
E.g. `(200, 200, 3)` would be one valid value.
@@ -103,10 +104,8 @@ def VGG16(include_top=True, weights='imagenet',
if input_tensor is None:
img_input = Input(shape=input_shape)
else:
if not K.is_keras_tensor(input_tensor):
img_input = Input(tensor=input_tensor, shape=input_shape)
else:
img_input = input_tensor
img_input = Input(tensor=input_tensor, shape=input_shape)
# Block 1
x = Conv2D(64, (3, 3), activation='relu', padding='same', name='block1_conv1')(img_input)
x = Conv2D(64, (3, 3), activation='relu', padding='same', name='block1_conv2')(x)
+15 -16
Ver Arquivo
@@ -6,26 +6,27 @@
- [Very Deep Convolutional Networks for Large-Scale Image Recognition](https://arxiv.org/abs/1409.1556)
"""
from __future__ import print_function
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
import warnings
from ..models import Model
from ..layers import Flatten
from ..layers import Dense
from ..layers import Input
from .. import backend as K
from ..engine.topology import get_source_inputs
from .imagenet_utils import _obtain_input_shape
from .imagenet_utils import decode_predictions # pylint: disable=unused-import
from .imagenet_utils import preprocess_input # pylint: disable=unused-import
from ..layers import Conv2D
from ..layers import MaxPooling2D
from ..layers import Dense
from ..layers import Flatten
from ..layers import GlobalAveragePooling2D
from ..layers import GlobalMaxPooling2D
from ..engine.topology import get_source_inputs
from ..layers import Input
from ..layers import MaxPooling2D
from ..models import Model
from ..utils import layer_utils
from ..utils.data_utils import get_file
from .. import backend as K
from .imagenet_utils import decode_predictions
from .imagenet_utils import preprocess_input
from .imagenet_utils import _obtain_input_shape
WEIGHTS_PATH = 'https://github.com/fchollet/deep-learning-models/releases/download/v0.1/vgg19_weights_tf_dim_ordering_tf_kernels.h5'
@@ -59,7 +60,7 @@ def VGG19(include_top=True, weights='imagenet',
input_shape: optional shape tuple, only to be specified
if `include_top` is False (otherwise the input shape
has to be `(224, 224, 3)` (with `channels_last` data format)
or `(3, 224, 244)` (with `channels_first` data format).
or `(3, 224, 224)` (with `channels_first` data format).
It should have exactly 3 inputs channels,
and width and height should be no smaller than 48.
E.g. `(200, 200, 3)` would be one valid value.
@@ -103,10 +104,8 @@ def VGG19(include_top=True, weights='imagenet',
if input_tensor is None:
img_input = Input(shape=input_shape)
else:
if not K.is_keras_tensor(input_tensor):
img_input = Input(tensor=input_tensor, shape=input_shape)
else:
img_input = input_tensor
img_input = Input(tensor=input_tensor, shape=input_shape)
# Block 1
x = Conv2D(64, (3, 3), activation='relu', padding='same', name='block1_conv1')(img_input)
x = Conv2D(64, (3, 3), activation='relu', padding='same', name='block1_conv2')(x)
+13 -15
Ver Arquivo
@@ -17,27 +17,28 @@ due to its reliance on `SeparableConvolution` layers.
- [Xception: Deep Learning with Depthwise Separable Convolutions](https://arxiv.org/abs/1610.02357)
"""
from __future__ import print_function
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
import warnings
from ..models import Model
from .. import backend as K
from .. import layers
from ..layers import Dense
from ..layers import Input
from ..layers import BatchNormalization
from ..engine.topology import get_source_inputs
from .imagenet_utils import _obtain_input_shape
from .imagenet_utils import decode_predictions # pylint: disable=unused-import
from ..layers import Activation
from ..layers import BatchNormalization
from ..layers import Conv2D
from ..layers import SeparableConv2D
from ..layers import MaxPooling2D
from ..layers import Dense
from ..layers import GlobalAveragePooling2D
from ..layers import GlobalMaxPooling2D
from ..engine.topology import get_source_inputs
from ..layers import Input
from ..layers import MaxPooling2D
from ..layers import SeparableConv2D
from ..models import Model
from ..utils.data_utils import get_file
from .. import backend as K
from .imagenet_utils import decode_predictions
from .imagenet_utils import _obtain_input_shape
TF_WEIGHTS_PATH = 'https://github.com/fchollet/deep-learning-models/releases/download/v0.4/xception_weights_tf_dim_ordering_tf_kernels.h5'
@@ -133,10 +134,7 @@ def Xception(include_top=True, weights='imagenet',
if input_tensor is None:
img_input = Input(shape=input_shape)
else:
if not K.is_keras_tensor(input_tensor):
img_input = Input(tensor=input_tensor, shape=input_shape)
else:
img_input = input_tensor
img_input = Input(tensor=input_tensor, shape=input_shape)
x = Conv2D(32, (3, 3), strides=(2, 2), use_bias=False, name='block1_conv1')(img_input)
x = BatchNormalization(name='block1_conv1_bn')(x)
Diferenças do arquivo suprimidas por serem muito extensas Carregar Diff
-91
Ver Arquivo
@@ -1,91 +0,0 @@
from __future__ import absolute_import
from __future__ import print_function
import os
import json
import sys
from .common import epsilon
from .common import floatx
from .common import set_epsilon
from .common import set_floatx
from .common import cast_to_floatx
from .common import image_data_format
from .common import set_image_data_format
from .common import is_keras_tensor
# Obtain Keras base dir path: either ~/.keras or /tmp.
_keras_base_dir = os.path.expanduser('~')
if not os.access(_keras_base_dir, os.W_OK):
_keras_base_dir = '/tmp'
_keras_dir = os.path.join(_keras_base_dir, '.keras')
# Default backend: TensorFlow.
_BACKEND = 'tensorflow'
# Attempt to read Keras config file.
_config_path = os.path.expanduser(os.path.join(_keras_dir, 'keras.json'))
if os.path.exists(_config_path):
try:
_config = json.load(open(_config_path))
except ValueError:
_config = {}
_floatx = _config.get('floatx', floatx())
assert _floatx in {'float16', 'float32', 'float64'}
_epsilon = _config.get('epsilon', epsilon())
assert isinstance(_epsilon, float)
_backend = _config.get('backend', _BACKEND)
assert _backend in {'theano', 'tensorflow'}
_image_data_format = _config.get('image_data_format',
image_data_format())
assert _image_data_format in {'channels_last', 'channels_first'}
set_floatx(_floatx)
set_epsilon(_epsilon)
set_image_data_format(_image_data_format)
_BACKEND = _backend
# Save config file, if possible.
if os.access(_keras_base_dir, os.W_OK):
if not os.path.exists(_keras_dir):
try:
os.makedirs(_keras_dir)
except OSError:
pass
if not os.path.exists(_config_path):
_config = {'floatx': floatx(),
'epsilon': epsilon(),
'backend': _BACKEND,
'image_data_format': image_data_format()}
with open(_config_path, 'w') as f:
f.write(json.dumps(_config, indent=4))
# Set backend based on KERAS_BACKEND flag, if applicable.
if 'KERAS_BACKEND' in os.environ:
_backend = os.environ['KERAS_BACKEND']
assert _backend in {'theano', 'tensorflow'}
_BACKEND = _backend
# Import backend functions.
if _BACKEND == 'theano':
sys.stderr.write('Using Theano backend.\n')
from .theano_backend import *
elif _BACKEND == 'tensorflow':
sys.stderr.write('Using TensorFlow backend.\n')
from .tensorflow_backend import *
else:
raise ValueError('Unknown backend: ' + str(_BACKEND))
def backend():
"""Publicly accessible method
for determining the current backend.
# Returns
String, the name of the backend Keras is currently using.
# Example
```python
>>> keras.backend.backend()
'tensorflow'
```
"""
return _BACKEND
-212
Ver Arquivo
@@ -1,212 +0,0 @@
import numpy as np
# the type of float to use throughout the session.
_FLOATX = 'float32'
_EPSILON = 10e-8
_IMAGE_DATA_FORMAT = 'channels_last'
def epsilon():
"""Returns the value of the fuzz
factor used in numeric expressions.
# Returns
A float.
# Example
```python
>>> keras.backend.epsilon()
1e-08
```
"""
return _EPSILON
def set_epsilon(e):
"""Sets the value of the fuzz
factor used in numeric expressions.
# Arguments
e: float. New value of epsilon.
# Example
```python
>>> from keras import backend as K
>>> K.epsilon()
1e-08
>>> K.set_epsilon(1e-05)
>>> K.epsilon()
1e-05
```
"""
global _EPSILON
_EPSILON = e
def floatx():
"""Returns the default float type, as a string
(e.g. 'float16', 'float32', 'float64').
# Returns
String, the current default float type.
# Example
```python
>>> keras.backend.floatx()
'float32'
```
"""
return _FLOATX
def set_floatx(floatx):
"""Sets the default float type.
# Arguments
String: 'float16', 'float32', or 'float64'.
# Example
```python
>>> from keras import backend as K
>>> K.floatx()
'float32'
>>> K.set_floatx('float16')
>>> K.floatx()
'float16'
```
"""
global _FLOATX
if floatx not in {'float16', 'float32', 'float64'}:
raise ValueError('Unknown floatx type: ' + str(floatx))
_FLOATX = str(floatx)
def cast_to_floatx(x):
"""Cast a Numpy array to the default Keras float type.
# Arguments
x: Numpy array.
# Returns
The same Numpy array, cast to its new type.
# Example
```python
>>> from keras import backend as K
>>> K.floatx()
'float32'
>>> arr = numpy.array([1.0, 2.0], dtype='float64')
>>> arr.dtype
dtype('float64')
>>> new_arr = K.cast_to_floatx(arr)
>>> new_arr
array([ 1., 2.], dtype=float32)
>>> new_arr.dtype
dtype('float32')
```
"""
return np.asarray(x, dtype=_FLOATX)
def image_data_format():
"""Returns the default image data format
convention ('channels_first' or 'channels_last').
# Returns
A string, either `'channels_first'` or `'channels_last'`
# Example
```python
>>> keras.backend.image_data_format()
'channels_first'
```
"""
return _IMAGE_DATA_FORMAT
def set_image_data_format(data_format):
"""Sets the value of the data format convention.
# Arguments
data_format: string. `'channels_first'` or `'channels_last'`.
# Example
```python
>>> from keras import backend as K
>>> K.image_data_format()
'channels_first'
>>> K.set_image_data_format('channels_last')
>>> K.image_data_format()
'channels_last'
```
"""
global _IMAGE_DATA_FORMAT
if data_format not in {'channels_last', 'channels_first'}:
raise ValueError('Unknown data_format:', data_format)
_IMAGE_DATA_FORMAT = str(data_format)
def is_keras_tensor(x):
"""Returns whether `x` is a Keras tensor.
# Arguments
x: a potential tensor.
# Returns
A boolean: whether the argument is a Keras tensor.
# Examples
```python
>>> from keras import backend as K
>>> np_var = numpy.array([1, 2])
>>> K.is_keras_tensor(np_var)
False
>>> keras_var = K.variable(np_var)
>>> K.is_keras_tensor(keras_var) # A variable is not a Tensor.
False
>>> keras_placeholder = K.placeholder(shape=(2, 4, 5))
>>> K.is_keras_tensor(keras_placeholder) # A placeholder is a Tensor.
True
```
"""
if hasattr(x, '_keras_shape'):
return True
else:
return False
# Legacy methods
def set_image_dim_ordering(dim_ordering):
"""Legacy setter for `image_data_format`.
# Arguments
dim_ordering: string. `'tf'` or `'th'`.
# Example
```python
>>> from keras import backend as K
>>> K.image_data_format()
'channels_first'
>>> K.set_image_data_format('channels_last')
>>> K.image_data_format()
'channels_last'
```
"""
global _IMAGE_DATA_FORMAT
if dim_ordering not in {'tf', 'th'}:
raise ValueError('Unknown dim_ordering:', dim_ordering)
if dim_ordering == 'th':
data_format = 'channels_first'
else:
data_format = 'channels_last'
_IMAGE_DATA_FORMAT = data_format
def image_dim_ordering():
"""Legacy getter for `image_data_format`.
"""
if _IMAGE_DATA_FORMAT == 'channels_first':
return 'th'
else:
return 'tf'
Diferenças do arquivo suprimidas por serem muito extensas Carregar Diff
+137 -70
Ver Arquivo
@@ -1,28 +1,33 @@
"""Keras callbacks: utilities called at certain points during model training.
"""
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
import os
from collections import deque
from collections import Iterable
from collections import OrderedDict
import csv
import numpy as np
import time
import six
import json
import os
import time
import warnings
from collections import deque
from collections import OrderedDict
from collections import Iterable
from .utils.generic_utils import Progbar
from . import backend as K
import numpy as np
from tensorflow.python.summary import summary as tf_summary
from tensorflow.contrib.tensorboard.plugins import projector
from tensorflow.python.training import saver as saver_lib
from tensorflow.python.ops import array_ops
from .utils.generic_utils import Progbar
# pylint: disable=g-import-not-at-top
try:
import requests
except ImportError:
requests = None
if K.backend() == 'tensorflow':
import tensorflow as tf
from tensorflow.contrib.tensorboard.plugins import projector
# pylint: enable=g-import-not-at-top
class CallbackList(object):
@@ -226,6 +231,21 @@ class BaseLogger(Callback):
logs[k] = self.totals[k] / self.seen
class TerminateOnNaN(Callback):
"""Callback that terminates training when a NaN loss is encountered."""
def __init__(self):
super(TerminateOnNaN, self).__init__()
def on_batch_end(self, batch, logs=None):
logs = logs or {}
loss = logs.get('loss')
if loss is not None:
if np.isnan(loss) or np.isinf(loss):
print('Batch %d: Invalid loss, terminating training' % (batch))
self.model.stop_training = True
class ProgbarLogger(Callback):
"""Callback that prints metrics to stdout.
@@ -466,7 +486,9 @@ class EarlyStopping(Callback):
self.min_delta *= -1
def on_train_begin(self, logs=None):
self.wait = 0 # Allow instances to be re-used
# Allow instances to be re-used
self.wait = 0
self.stopped_epoch = 0
self.best = np.Inf if self.monitor_op == np.less else -np.Inf
def on_epoch_end(self, epoch, logs=None):
@@ -503,8 +525,7 @@ class RemoteMonitor(Callback):
field: String; JSON field under which the data will be stored.
headers: Dictionary; optional custom HTTP headers.
Defaults to:
`{'Accept': 'application/json',
'Content-Type': 'application/json'}`
`{'Accept': 'application/json', 'Content-Type': 'application/json'}`
"""
def __init__(self,
@@ -563,6 +584,7 @@ class LearningRateScheduler(Callback):
class TensorBoard(Callback):
# pylint: disable=line-too-long
"""Tensorboard basic visualizations.
This callback writes a log for TensorBoard, which allows
@@ -578,19 +600,24 @@ class TensorBoard(Callback):
tensorboard --logdir=/full_path_to_your_logs
```
You can find more information about TensorBoard
[here](https://www.tensorflow.org/versions/master/how_tos/summaries_and_tensorboard/index.html).
[here](https://www.tensorflow.org/get_started/summaries_and_tensorboard).
# Arguments
log_dir: the path of the directory where to save the log
files to be parsed by Tensorboard.
files to be parsed by TensorBoard.
histogram_freq: frequency (in epochs) at which to compute activation
histograms for the layers of the model. If set to 0,
histograms won't be computed.
write_graph: whether to visualize the graph in Tensorboard.
and weight histograms for the layers of the model. If set to 0,
histograms won't be computed. Validation data (or split) must be
specified for histogram visualizations.
write_graph: whether to visualize the graph in TensorBoard.
The log file can become quite large when
write_graph is set to True.
write_grads: whether to visualize gradient histograms in TensorBoard.
`histogram_freq` must be greater than 0.
batch_size: size of batch of inputs to feed to the network
for histograms computation.
write_images: whether to write model weights to visualize as
image in Tensorboard.
image in TensorBoard.
embeddings_freq: frequency (in epochs) at which selected embedding
layers will be saved.
embeddings_layer_names: a list of names of layers to keep eye on. If
@@ -601,59 +628,82 @@ class TensorBoard(Callback):
about metadata files format. In case if the same metadata file is
used for all embedding layers, string can be passed.
"""
# pylint: enable=line-too-long
def __init__(self, log_dir='./logs',
histogram_freq=0,
batch_size=32,
write_graph=True,
write_grads=False,
write_images=False,
embeddings_freq=0,
embeddings_layer_names=None,
embeddings_metadata=None):
super(TensorBoard, self).__init__()
if K.backend() != 'tensorflow':
raise RuntimeError('TensorBoard callback only works '
'with the TensorFlow backend.')
self.log_dir = log_dir
self.histogram_freq = histogram_freq
self.merged = None
self.write_graph = write_graph
self.write_grads = write_grads
self.write_images = write_images
self.embeddings_freq = embeddings_freq
self.embeddings_layer_names = embeddings_layer_names
self.embeddings_metadata = embeddings_metadata or {}
self.batch_size = batch_size
def set_model(self, model):
self.model = model
self.sess = K.get_session()
if self.histogram_freq and self.merged is None:
for layer in self.model.layers:
for weight in layer.weights:
tf.summary.histogram(weight.name, weight)
tf_summary.histogram(weight.name, weight)
if self.write_grads:
grads = model.optimizer.get_gradients(model.total_loss,
weight)
tf_summary.histogram('{}_grad'.format(weight.name), grads)
if self.write_images:
w_img = tf.squeeze(weight)
shape = w_img.get_shape()
if len(shape) > 1 and shape[0] > shape[1]:
w_img = tf.transpose(w_img)
if len(shape) == 1:
w_img = tf.expand_dims(w_img, 0)
w_img = tf.expand_dims(tf.expand_dims(w_img, 0), -1)
tf.summary.image(weight.name, w_img)
w_img = array_ops.squeeze(weight)
shape = K.int_shape(w_img)
if len(shape) == 2: # dense layer kernel case
if shape[0] > shape[1]:
w_img = array_ops.transpose(w_img)
shape = K.int_shape(w_img)
w_img = array_ops.reshape(w_img, [1,
shape[0],
shape[1],
1])
elif len(shape) == 3: # convnet case
if K.image_data_format() == 'channels_last':
# switch to channels_first to display
# every kernel as a separate image
w_img = array_ops.transpose(w_img, perm=[2, 0, 1])
shape = K.int_shape(w_img)
w_img = array_ops.reshape(
w_img, [shape[0], shape[1], shape[2], 1])
elif len(shape) == 1: # bias case
w_img = array_ops.reshape(
w_img, [1, shape[0], 1, 1])
else:
# not possible to handle 3D convnets etc.
continue
shape = K.int_shape(w_img)
assert len(shape) == 4 and shape[-1] in [1, 3, 4]
tf_summary.image(weight.name, w_img)
if hasattr(layer, 'output'):
tf.summary.histogram('{}_out'.format(layer.name),
tf_summary.histogram('{}_out'.format(layer.name),
layer.output)
self.merged = tf.summary.merge_all()
self.merged = tf_summary.merge_all()
if self.write_graph:
self.writer = tf.summary.FileWriter(self.log_dir,
self.writer = tf_summary.FileWriter(self.log_dir,
self.sess.graph)
else:
self.writer = tf.summary.FileWriter(self.log_dir)
self.writer = tf_summary.FileWriter(self.log_dir)
if self.embeddings_freq:
self.saver = tf.train.Saver()
embeddings_layer_names = self.embeddings_layer_names
if not embeddings_layer_names:
@@ -664,6 +714,8 @@ class TensorBoard(Callback):
for layer in self.model.layers
if layer.name in embeddings_layer_names}
self.saver = saver_lib.Saver(list(embeddings.values()))
embeddings_metadata = {}
if not isinstance(self.embeddings_metadata, str):
@@ -673,15 +725,13 @@ class TensorBoard(Callback):
for layer_name in embeddings.keys()}
config = projector.ProjectorConfig()
self.embeddings_logs = []
self.embeddings_ckpt_path = os.path.join(self.log_dir,
'keras_embedding.ckpt')
for layer_name, tensor in embeddings.items():
embedding = config.embeddings.add()
embedding.tensor_name = tensor.name
self.embeddings_logs.append(os.path.join(self.log_dir,
layer_name + '.ckpt'))
if layer_name in embeddings_metadata:
embedding.metadata_path = embeddings_metadata[layer_name]
@@ -692,29 +742,42 @@ class TensorBoard(Callback):
if self.validation_data and self.histogram_freq:
if epoch % self.histogram_freq == 0:
# TODO: implement batched calls to sess.run
# (current call will likely go OOM on GPU)
if self.model.uses_learning_phase:
cut_v_data = len(self.model.inputs)
val_data = self.validation_data[:cut_v_data] + [0]
tensors = self.model.inputs + [K.learning_phase()]
else:
val_data = self.validation_data
tensors = self.model.inputs
feed_dict = dict(zip(tensors, val_data))
result = self.sess.run([self.merged], feed_dict=feed_dict)
summary_str = result[0]
self.writer.add_summary(summary_str, epoch)
if self.embeddings_freq and self.embeddings_logs:
val_data = self.validation_data
tensors = (self.model.inputs +
self.model.targets +
self.model.sample_weights)
if self.model.uses_learning_phase:
tensors += [K.learning_phase()]
assert len(val_data) == len(tensors)
val_size = val_data[0].shape[0]
i = 0
while i < val_size:
step = min(self.batch_size, val_size - i)
batch_val = []
batch_val.append(val_data[0][i:i + step])
batch_val.append(val_data[1][i:i + step])
batch_val.append(val_data[2][i:i + step])
if self.model.uses_learning_phase:
batch_val.append(val_data[3])
feed_dict = dict(zip(tensors, batch_val))
result = self.sess.run([self.merged], feed_dict=feed_dict)
summary_str = result[0]
self.writer.add_summary(summary_str, epoch)
i += self.batch_size
if self.embeddings_freq and self.embeddings_ckpt_path:
if epoch % self.embeddings_freq == 0:
for log in self.embeddings_logs:
self.saver.save(self.sess, log, epoch)
self.saver.save(self.sess,
self.embeddings_ckpt_path,
epoch)
for name, value in logs.items():
if name in ['batch', 'size']:
continue
summary = tf.Summary()
summary = tf_summary.Summary()
summary_value = summary.value.add()
summary_value.simple_value = value.item()
summary_value.tag = name
@@ -735,9 +798,9 @@ class ReduceLROnPlateau(Callback):
# Example
```python
reduce_lr = ReduceLROnPlateau(monitor='val_loss', factor=0.2,
patience=5, min_lr=0.001)
model.fit(X_train, Y_train, callbacks=[reduce_lr])
reduce_lr = ReduceLROnPlateau(monitor='val_loss', factor=0.2,
patience=5, min_lr=0.001)
model.fit(X_train, Y_train, callbacks=[reduce_lr])
```
# Arguments
@@ -844,8 +907,8 @@ class CSVLogger(Callback):
# Example
```python
csv_logger = CSVLogger('training.log')
model.fit(X_train, Y_train, callbacks=[csv_logger])
csv_logger = CSVLogger('training.log')
model.fit(X_train, Y_train, callbacks=[csv_logger])
```
# Arguments
@@ -862,23 +925,26 @@ class CSVLogger(Callback):
self.writer = None
self.keys = None
self.append_header = True
self.file_flags = 'b' if six.PY2 and os.name == 'nt' else ''
super(CSVLogger, self).__init__()
def on_train_begin(self, logs=None):
if self.append:
if os.path.exists(self.filename):
with open(self.filename) as f:
with open(self.filename, 'r' + self.file_flags) as f:
self.append_header = not bool(len(f.readline()))
self.csv_file = open(self.filename, 'a')
self.csv_file = open(self.filename, 'a' + self.file_flags)
else:
self.csv_file = open(self.filename, 'w')
self.csv_file = open(self.filename, 'w' + self.file_flags)
def on_epoch_end(self, epoch, logs=None):
logs = logs or {}
def handle_value(k):
is_zero_dim_ndarray = isinstance(k, np.ndarray) and k.ndim == 0
if isinstance(k, Iterable) and not is_zero_dim_ndarray:
if isinstance(k, six.string_types):
return k
elif isinstance(k, Iterable) and not is_zero_dim_ndarray:
return '"[%s]"' % (', '.join(map(str, k)))
else:
return k
@@ -910,6 +976,7 @@ class LambdaCallback(Callback):
This callback is constructed with anonymous functions that will be called
at the appropriate time. Note that the callbacks expects positional
arguments, as:
- `on_epoch_begin` and `on_epoch_end` expect two positional arguments:
`epoch`, `logs`
- `on_batch_begin` and `on_batch_end` expect two positional arguments:
+9 -8
Ver Arquivo
@@ -1,8 +1,13 @@
"""Constraints: functions that impose constraints on weights values.
"""
from __future__ import absolute_import
import six
from __future__ import division
from __future__ import print_function
from . import backend as K
from .utils.generic_utils import serialize_keras_object
import six
from .utils.generic_utils import deserialize_keras_object
from .utils.generic_utils import serialize_keras_object
class Constraint(object):
@@ -142,16 +147,12 @@ class MinMaxNorm(Constraint):
# Aliases.
# pylint: disable=invalid-name
max_norm = MaxNorm
non_neg = NonNeg
unit_norm = UnitNorm
min_max_norm = MinMaxNorm
# Legacy aliases.
maxnorm = max_norm
nonneg = non_neg
unitnorm = unit_norm
# pylint: enable=invalid-name
def serialize(constraint):
+9 -4
Ver Arquivo
@@ -1,8 +1,13 @@
"""Keras datasets: utilities for downloading and pre-processing common datasets.
"""
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
from . import mnist
from . import imdb
from . import reuters
from . import boston_housing
from . import cifar10
from . import cifar100
from . import boston_housing
from . import imdb
from . import mnist
from . import reuters
+7 -1
Ver Arquivo
@@ -1,5 +1,11 @@
from ..utils.data_utils import get_file
"""Boston housing price regression dataset.
"""
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
import numpy as np
from ..utils.data_utils import get_file
def load_data(path='boston_housing.npz', seed=113, test_split=0.2):
+5
Ver Arquivo
@@ -1,5 +1,10 @@
"""Utilities used by the CIFAR10 and CIFAR100 datasets.
"""
# -*- coding: utf-8 -*-
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
import sys
from six.moves import cPickle
+10 -4
Ver Arquivo
@@ -1,10 +1,16 @@
"""CIFAR10 small image classification dataset.
"""
from __future__ import absolute_import
from .cifar import load_batch
from ..utils.data_utils import get_file
from .. import backend as K
import numpy as np
from __future__ import division
from __future__ import print_function
import os
from .. import backend as K
from .cifar import load_batch
import numpy as np
from ..utils.data_utils import get_file
def load_data():
"""Loads CIFAR10 dataset.
+10 -4
Ver Arquivo
@@ -1,10 +1,16 @@
"""CIFAR100 small image classification dataset.
"""
from __future__ import absolute_import
from .cifar import load_batch
from ..utils.data_utils import get_file
from .. import backend as K
import numpy as np
from __future__ import division
from __future__ import print_function
import os
from .. import backend as K
from .cifar import load_batch
import numpy as np
from ..utils.data_utils import get_file
def load_data(label_mode='fine'):
"""Loads CIFAR100 dataset.
+11 -14
Ver Arquivo
@@ -1,14 +1,19 @@
"""IMDB movie review sentiment classification dataset.
"""
from __future__ import absolute_import
from ..utils.data_utils import get_file
from six.moves import zip
import numpy as np
from __future__ import division
from __future__ import print_function
import json
import warnings
import numpy as np
from six.moves import zip
from ..utils.data_utils import get_file
def load_data(path='imdb.npz', num_words=None, skip_top=0,
maxlen=None, seed=113,
start_char=1, oov_char=2, index_from=3, **kwargs):
start_char=1, oov_char=2, index_from=3):
"""Loads the IMDB dataset.
# Arguments
@@ -39,14 +44,6 @@ def load_data(path='imdb.npz', num_words=None, skip_top=0,
Words that were not seen in the training set but are in the test set
have simply been skipped.
"""
# Legacy support
if 'nb_words' in kwargs:
warnings.warn('The `nb_words` argument in `load_data` '
'has been renamed `num_words`.')
num_words = kwargs.pop('nb_words')
if kwargs:
raise TypeError('Unrecognized keyword arguments: ' + str(kwargs))
path = get_file(path,
origin='https://s3.amazonaws.com/text-datasets/imdb.npz')
f = np.load(path)
@@ -100,7 +97,7 @@ def load_data(path='imdb.npz', num_words=None, skip_top=0,
for x in xs:
nx = []
for w in x:
if w >= num_words or w < skip_top:
if skip_top <= w < num_words:
nx.append(w)
new_xs.append(nx)
xs = new_xs
+7 -1
Ver Arquivo
@@ -1,5 +1,11 @@
from ..utils.data_utils import get_file
"""MNIST handwritten digits classification dataset.
"""
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
import numpy as np
from ..utils.data_utils import get_file
def load_data(path='mnist.npz'):
+11 -14
Ver Arquivo
@@ -1,15 +1,20 @@
"""Reuters newswire topic classification dataset.
"""
# -*- coding: utf-8 -*-
from __future__ import absolute_import
from ..utils.data_utils import get_file
from six.moves import zip
import numpy as np
from __future__ import division
from __future__ import print_function
import json
import warnings
import numpy as np
from six.moves import zip
from ..utils.data_utils import get_file
def load_data(path='reuters.npz', num_words=None, skip_top=0,
maxlen=None, test_split=0.2, seed=113,
start_char=1, oov_char=2, index_from=3, **kwargs):
start_char=1, oov_char=2, index_from=3):
"""Loads the Reuters newswire classification dataset.
# Arguments
@@ -37,14 +42,6 @@ def load_data(path='reuters.npz', num_words=None, skip_top=0,
Words that were not seen in the training set but are in the test set
have simply been skipped.
"""
# Legacy support
if 'nb_words' in kwargs:
warnings.warn('The `nb_words` argument in `load_data` '
'has been renamed `num_words`.')
num_words = kwargs.pop('nb_words')
if kwargs:
raise TypeError('Unrecognized keyword arguments: ' + str(kwargs))
path = get_file(path, origin='https://s3.amazonaws.com/text-datasets/reuters.npz')
npzfile = np.load(path)
xs = npzfile['x']
@@ -84,7 +81,7 @@ def load_data(path='reuters.npz', num_words=None, skip_top=0,
for x in xs:
nx = []
for w in x:
if w >= num_words or w < skip_top:
if skip_top <= w < num_words:
nx.append(w)
new_xs.append(nx)
xs = new_xs
+9 -3
Ver Arquivo
@@ -1,8 +1,14 @@
# note: topology.Node is an internal class,
"""The Keras Engine: graph topology and training loop functionality.
"""
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
# Note: topology.Node is an internal class,
# it isn't meant to be used by Keras users.
from .topology import InputSpec
from .topology import get_source_inputs
from .topology import Input
from .topology import InputLayer
from .topology import InputSpec
from .topology import Layer
from .topology import get_source_inputs
from .training import Model
+238 -178
Ver Arquivo
@@ -1,30 +1,38 @@
# -*- coding: utf-8 -*-
from __future__ import print_function
"""Base layer code and base model (Container) code.
"""
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
import numpy as np
import json
import yaml
import warnings
import copy
import inspect
import json
import os
import re
import inspect
from six.moves import zip
import warnings
from .. import backend as K
from .. import initializers
import numpy as np
from six.moves import zip
from tensorflow.python.framework import tensor_shape
from ..utils.io_utils import ask_to_proceed_with_overwrite
from ..utils.layer_utils import print_summary as print_layer_summary
from ..utils import conv_utils
from ..legacy import interfaces
# pylint: disable=g-import-not-at-top
try:
import h5py
except ImportError:
h5py = None
try:
import yaml
except ImportError:
yaml = None
# pylint: enable=g-import-not-at-top
class InputSpec(object):
"""Specifies the ndim, dtype and shape of every input to a layer.
@@ -93,15 +101,11 @@ class Node(object):
output_tensors: list of output tensors.
input_masks: list of input masks (a mask can be a tensor, or None).
output_masks: list of output masks (a mask can be a tensor, or None).
input_shapes: list of input shape tuples.
output_shapes: list of output shape tuples.
arguments: dictionary of keyword arguments that were passed to the
`call` method of the layer at the call that created the node.
`node_indices` and `tensor_indices` are basically fine-grained coordinates
describing the origin of the `input_tensors`, verifying the following:
`input_tensors[i] == inbound_layers[i].inbound_nodes[node_indices[i]].output_tensors[tensor_indices[i]]`
describing the origin of the `input_tensors`.
A node from layer A to layer B is added to:
A.outbound_nodes
@@ -112,7 +116,6 @@ class Node(object):
inbound_layers, node_indices, tensor_indices,
input_tensors, output_tensors,
input_masks, output_masks,
input_shapes, output_shapes,
arguments=None):
# Layer instance (NOT a list).
# this is the layer that takes a list of input tensors
@@ -150,9 +153,9 @@ class Node(object):
# Following 2 properties: input and output shapes.
# List of shape tuples, shapes of input_tensors.
self.input_shapes = input_shapes
self.input_shapes = [K.int_shape(x) for x in input_tensors]
# List of shape tuples, shapes of output_tensors.
self.output_shapes = output_shapes
self.output_shapes = [K.int_shape(x) for x in output_tensors]
# Optional keyword arguments to layer's `call`.
self.arguments = arguments
@@ -221,12 +224,12 @@ class Layer(object):
`self._add_inbound_node(last_layer)`
- Add layer to tensor history
If layer is not built:
- Build from x._keras_shape
- Build from inputs shape
get_weights()
set_weights(weights)
get_config()
count_params()
compute_output_shape(input_shape)
_compute_output_shape(input_shape)
compute_mask(x, mask)
get_input_at(node_index)
get_output_at(node_index)
@@ -360,28 +363,35 @@ class Layer(object):
def non_trainable_weights(self, weights):
self._non_trainable_weights = weights
def add_weight(self, shape, initializer,
name=None,
trainable=True,
def add_weight(self,
name,
shape,
dtype=None,
initializer=None,
regularizer=None,
trainable=True,
constraint=None):
"""Adds a weight variable to the layer.
# Arguments
shape: The shape tuple of the weight.
initializer: An Initializer instance (callable).
name: String, the name for the weight variable.
shape: The shape tuple of the weight.
dtype: The dtype of the weight.
initializer: An Initializer instance (callable).
regularizer: An optional Regularizer instance.
trainable: A boolean, whether the weight should
be trained via backprop or not (assuming
that the layer itself is also trainable).
regularizer: An optional Regularizer instance.
constraint: An optional Constraint instance.
# Returns
The created weight variable.
"""
shape = tuple(tensor_shape.TensorShape(shape).as_list())
initializer = initializers.get(initializer)
weight = K.variable(initializer(shape), dtype=K.floatx(), name=name)
if dtype is None:
dtype = K.floatx()
weight = K.variable(initializer(shape), dtype=dtype, name=name)
if regularizer is not None:
self.add_loss(regularizer(weight))
if constraint is not None:
@@ -406,13 +416,22 @@ class Layer(object):
ValueError: in case of mismatch between
the provided inputs and the expectations of the layer.
"""
inputs = _to_list(inputs)
for x in inputs:
if not isinstance(x, K.tensor_types()):
raise ValueError('Layer ' + self.name + ' was called with '
'an input that isn\'t a symbolic tensor. '
'Received type: ' +
str(type(x)) + '. Full input: ' +
str(inputs) + '. All inputs to the layer '
'should be tensors.')
if not self.input_spec:
return
if not isinstance(self.input_spec, (list, tuple)):
input_spec = _to_list(self.input_spec)
else:
input_spec = self.input_spec
inputs = _to_list(inputs)
if len(inputs) != len(input_spec):
raise ValueError('Layer ' + self.name + ' expects ' +
str(len(input_spec)) + ' inputs, '
@@ -463,6 +482,8 @@ class Layer(object):
x_shape = None
if x_shape is not None:
for axis, value in spec.axes.items():
if hasattr(value, 'value'):
value = value.value
if value is not None and x_shape[int(axis)] not in {value, None}:
raise ValueError('Input ' + str(input_index) +
' is incompatible with layer ' +
@@ -478,6 +499,8 @@ class Layer(object):
x_shape = None
if x_shape is not None:
for spec_dim, dim in zip(spec.shape, x_shape):
if hasattr(spec_dim, 'value'):
spec_dim = spec_dim.value
if spec_dim is not None and dim is not None:
if spec_dim != dim:
raise ValueError(
@@ -487,7 +510,7 @@ class Layer(object):
str(spec.shape) + ', found shape=' +
str(x_shape))
def call(self, inputs, **kwargs):
def call(self, inputs, **kwargs): # pylint: disable=unused-argument
"""This is where the layer's logic lives.
# Arguments
@@ -505,10 +528,7 @@ class Layer(object):
If a Keras tensor is passed:
- We call self._add_inbound_node().
- If necessary, we `build` the layer to match
the _keras_shape of the input(s).
- We update the _keras_shape of every input tensor with
its new shape (obtained via self.compute_output_shape).
This is done as part of _add_inbound_node().
the shape of the input(s).
- We update the _keras_history of the output tensor(s)
with the current layer.
This is done as part of _add_inbound_node().
@@ -536,17 +556,7 @@ class Layer(object):
# Collect input shapes to build layer.
input_shapes = []
for x_elem in _to_list(inputs):
if hasattr(x_elem, '_keras_shape'):
input_shapes.append(x_elem._keras_shape)
elif hasattr(K, 'int_shape'):
input_shapes.append(K.int_shape(x_elem))
else:
raise ValueError('You tried to call layer "' + self.name +
'". This layer has no information'
' about its expected input shape, '
'and thus cannot be built. '
'You can build it manually via: '
'`layer.build(batch_input_shape)`')
input_shapes.append(K.int_shape(x_elem))
if len(input_shapes) == 1:
self.build(input_shapes[0])
else:
@@ -571,21 +581,24 @@ class Layer(object):
# If mask is explicitly passed to __call__,
# we should override the default mask.
kwargs['mask'] = previous_mask
# Handle automatic shape inference (only useful for Theano).
input_shape = _collect_input_shape(inputs)
# Actually call the layer, collecting output(s), mask(s), and shape(s).
output = self.call(inputs, **kwargs)
output_mask = self.compute_mask(inputs, previous_mask)
# Infering the output shape is only relevant for Theano.
if all([s is not None for s in _to_list(input_shape)]):
output_shape = self.compute_output_shape(input_shape)
# If the layer returns tensors from its inputs, unmodified,
# we copy them to avoid loss of tensor metadata.
output_ls = _to_list(output)
inputs_ls = _to_list(inputs)
output_ls_copy = []
for x in output_ls:
if x in inputs_ls:
x = K.identity(x)
output_ls_copy.append(x)
if len(output_ls_copy) == 1:
output = output_ls_copy[0]
else:
if isinstance(input_shape, list):
output_shape = [None for _ in input_shape]
else:
output_shape = None
output = output_ls_copy
# Add an inbound node to the layer, so that it keeps track
# of the call and of all new variables created during the call.
@@ -594,7 +607,6 @@ class Layer(object):
# this does nothing.
self._add_inbound_node(input_tensors=inputs, output_tensors=output,
input_masks=previous_mask, output_masks=output_mask,
input_shapes=input_shape, output_shapes=output_shape,
arguments=user_kwargs)
# Apply activity regularizer if any:
@@ -605,7 +617,7 @@ class Layer(object):
def _add_inbound_node(self, input_tensors, output_tensors,
input_masks, output_masks,
input_shapes, output_shapes, arguments=None):
arguments=None):
"""Internal method to create an inbound node for the layer.
# Arguments
@@ -613,8 +625,6 @@ class Layer(object):
output_tensors: list of output tensors.
input_masks: list of input masks (a mask can be a tensor, or None).
output_masks: list of output masks (a mask can be a tensor, or None).
input_shapes: list of input shape tuples.
output_shapes: list of output shape tuples.
arguments: dictionary of keyword arguments that were passed to the
`call` method of the layer at the call that created the node.
"""
@@ -622,8 +632,6 @@ class Layer(object):
output_tensors = _to_list(output_tensors)
input_masks = _to_list(input_masks)
output_masks = _to_list(output_masks)
input_shapes = _to_list(input_shapes)
output_shapes = _to_list(output_shapes)
# Collect input tensor(s) coordinates.
inbound_layers = []
@@ -650,14 +658,11 @@ class Layer(object):
output_tensors=output_tensors,
input_masks=input_masks,
output_masks=output_masks,
input_shapes=input_shapes,
output_shapes=output_shapes,
arguments=arguments
)
# Update tensor history, _keras_shape and _uses_learning_phase.
# Update tensor history and `_uses_learning_phase`.
for i in range(len(output_tensors)):
output_tensors[i]._keras_shape = output_shapes[i]
uses_lp = any([getattr(x, '_uses_learning_phase', False) for x in input_tensors])
uses_lp = getattr(self, 'uses_learning_phase', False) or uses_lp
output_tensors[i]._uses_learning_phase = getattr(output_tensors[i], '_uses_learning_phase', False) or uses_lp
@@ -665,7 +670,7 @@ class Layer(object):
len(self.inbound_nodes) - 1,
i)
def compute_output_shape(self, input_shape):
def _compute_output_shape(self, input_shape):
"""Computes the output shape of the layer.
Assumes that the layer will be built
@@ -680,13 +685,12 @@ class Layer(object):
# Returns
An input shape tuple.
"""
if hasattr(self, 'get_output_shape_for'):
msg = "Class `{}.{}` defines `get_output_shape_for` but does not override `compute_output_shape`. " + \
"If this is a Keras 1 layer, please implement `compute_output_shape` to support Keras 2."
warnings.warn(msg.format(type(self).__module__, type(self).__name__), stacklevel=2)
return input_shape
if isinstance(input_shape, list):
return [tensor_shape.TensorShape(shape) for shape in input_shape]
else:
return tensor_shape.TensorShape(input_shape)
def compute_mask(self, inputs, mask=None):
def compute_mask(self, inputs, mask=None): # pylint: disable=unused-argument
"""Computes an output mask tensor.
# Arguments
@@ -716,7 +720,7 @@ class Layer(object):
# carry over the input mask
return mask
def build(self, input_shape):
def build(self, input_shape): # pylint: disable=unused-argument
"""Creates the layer weights.
Must be implemented on all layers that have weights.
@@ -988,9 +992,9 @@ class Layer(object):
if len(all_input_shapes) == 1:
input_shapes = self.inbound_nodes[0].input_shapes
if len(input_shapes) == 1:
return input_shapes[0]
return tuple(tensor_shape.TensorShape(input_shapes[0]).as_list())
else:
return input_shapes
return [tuple(tensor_shape.TensorShape(shape).as_list()) for shape in input_shapes]
else:
raise AttributeError('The layer "' + str(self.name) +
' has multiple inbound nodes, '
@@ -1022,9 +1026,9 @@ class Layer(object):
if len(all_output_shapes) == 1:
output_shapes = self.inbound_nodes[0].output_shapes
if len(output_shapes) == 1:
return output_shapes[0]
return tuple(tensor_shape.TensorShape(output_shapes[0]).as_list())
else:
return output_shapes
return [tuple(tensor_shape.TensorShape(shape).as_list()) for shape in output_shapes]
else:
raise AttributeError('The layer "' + str(self.name) +
' has multiple inbound nodes, '
@@ -1049,14 +1053,14 @@ class Layer(object):
(e.g. L2 weight regularization, which only depends
on the layer's weights variables, not on any inputs tensors).
"""
if losses is None or losses == []:
if losses is None or losses == []: # pylint: disable=g-explicit-bool-comparison
return
# Update self.losses
losses = _to_list(losses)
if hasattr(self, '_losses'):
self._losses += losses
# Update self._per_input_updates
if inputs == []:
if inputs == []: # pylint: disable=g-explicit-bool-comparison
inputs = None
if inputs is not None:
inputs_hash = _object_list_uid(inputs)
@@ -1081,14 +1085,14 @@ class Layer(object):
the updates as conditional on these inputs.
If None is passed, the updates are assumed unconditional.
"""
if updates is None or updates == []:
if updates is None or updates == []: # pylint: disable=g-explicit-bool-comparison
return
# Update self.updates
updates = _to_list(updates)
if hasattr(self, '_updates'):
self._updates += updates
# Update self._per_input_updates
if inputs == []:
if inputs == []: # pylint: disable=g-explicit-bool-comparison
inputs = None
if inputs is not None:
inputs_hash = _object_list_uid(inputs)
@@ -1221,7 +1225,7 @@ class Layer(object):
"""
if not self.built:
if self.__class__.__name__ == 'Sequential':
self.build()
self.build() # pylint: disable=no-value-for-parameter
else:
raise RuntimeError('You tried to call `count_params` on ' +
self.name + ', but the layer isn\'t built. '
@@ -1303,7 +1307,6 @@ class InputLayer(Layer):
name=self.name)
else:
self.is_placeholder = False
input_tensor._keras_shape = batch_input_shape
# Create an input node to add to self.outbound_node
# and set output_tensors' _keras_history.
input_tensor._uses_learning_phase = False
@@ -1315,9 +1318,7 @@ class InputLayer(Layer):
input_tensors=[input_tensor],
output_tensors=[input_tensor],
input_masks=[None],
output_masks=[None],
input_shapes=[batch_input_shape],
output_shapes=[batch_input_shape])
output_masks=[None])
def get_config(self):
config = {'batch_input_shape': self.batch_input_shape,
@@ -1327,9 +1328,13 @@ class InputLayer(Layer):
return config
def Input(shape=None, batch_shape=None,
name=None, dtype=K.floatx(), sparse=False,
tensor=None):
def Input( # pylint: disable=invalid-name
shape=None,
batch_shape=None,
name=None,
dtype=K.floatx(),
sparse=False,
tensor=None):
"""`Input()` is used to instantiate a Keras tensor.
A Keras tensor is a tensor object from the underlying backend
@@ -1337,14 +1342,12 @@ def Input(shape=None, batch_shape=None,
attributes that allow us to build a Keras model
just by knowing the inputs and outputs of the model.
For instance, if a, b and c and Keras tensors,
For instance, if a, b and c are Keras tensors,
it becomes possible to do:
`model = Model(input=[a, b], output=c)`
The added Keras attributes are:
._keras_shape: Integer shape tuple propagated
via Keras-side shape inference.
._keras_history: Last layer applied to the tensor.
The added Keras attribute is:
`_keras_history`: Last layer applied to the tensor.
the entire layer graph is retrievable from that layer,
recursively.
@@ -1390,7 +1393,7 @@ def Input(shape=None, batch_shape=None,
name=name, dtype=dtype,
sparse=sparse,
input_tensor=tensor)
# Return tensor including _keras_shape and _keras_history.
# Return tensor including `_keras_history`.
# Note that in this case train_output and test_output are the same pointer.
outputs = input_layer.inbound_nodes[0].output_tensors
if len(outputs) == 1:
@@ -1436,8 +1439,7 @@ class Container(Layer):
from_config
"""
@interfaces.legacy_model_constructor_support
def __init__(self, inputs, outputs, name=None):
def __init__(self, inputs, outputs, name=None): # pylint: disable=super-init-not-called
# Handle `name` argument.
if not name:
prefix = self.__class__.__name__.lower()
@@ -1572,12 +1574,12 @@ class Container(Layer):
if layer.is_placeholder:
self._feed_input_names.append(layer.name)
self._feed_inputs.append(layer.input)
self._feed_input_shapes.append(self.inputs[i]._keras_shape)
self._feed_input_shapes.append(K.int_shape(self.inputs[i]))
for layer in self.output_layers:
self.output_names.append(layer.name)
self.internal_input_shapes = [x._keras_shape for x in self.inputs]
self.internal_output_shapes = [x._keras_shape for x in self.outputs]
self.internal_input_shapes = [K.int_shape(x) for x in self.inputs]
self.internal_output_shapes = [K.int_shape(x) for x in self.outputs]
# Container_nodes: set of nodes included in the graph
# (not all nodes included in the layers
@@ -1586,72 +1588,92 @@ class Container(Layer):
nodes_depths = {} # dict {node: depth value}
layers_depths = {} # dict {layer: depth value}
layer_indices = {} # dict {layer: index in traversal}
nodes_in_decreasing_depth = []
def make_node_marker(node, depth):
return str(id(node)) + '-' + str(depth)
def build_map_of_graph(tensor, seen_nodes=None, depth=0,
def build_map_of_graph(tensor, finished_nodes, nodes_in_progress,
layer=None, node_index=None, tensor_index=None):
"""Builds a map of the graph of layers.
This recursively updates the maps `nodes_depths`,
`layers_depths` and the set `container_nodes`.
Does not try to detect cycles in the graph.
This recursively updates the map `layer_indices`,
the list `nodes_in_decreasing_depth` and the set `container_nodes`.
# Arguments
tensor: Some tensor in a graph.
seen_nodes: Set of node ids ("{layer.name}_ib-{node_index}")
of nodes seen so far. Useful to prevent infinite loops.
depth: Current depth in the graph (0 = last output).
finished_nodes: Set of nodes whose subgraphs have been traversed
completely. Useful to prevent duplicated work.
nodes_in_progress: Set of nodes that are currently active on the
recursion stack. Useful to detect cycles.
layer: Layer from which `tensor` comes from. If not provided,
will be obtained from `tensor._keras_history`.
node_index: Node index from which `tensor` comes from.
tensor_index: Tensor_index from which `tensor` comes from.
# Raises
RuntimeError: if a cycle is detected.
"""
seen_nodes = seen_nodes or set()
if not layer or node_index is None or tensor_index is None:
layer, node_index, tensor_index = tensor._keras_history
node = layer.inbound_nodes[node_index]
# Prevent cycles.
seen_nodes.add(make_node_marker(node, depth))
if node in nodes_in_progress:
raise RuntimeError(
'The tensor ' + str(tensor) + ' at layer "' +
layer.name + '" is part of a cycle.')
# Don't repeat work for shared subgraphs
if node in finished_nodes:
return
node_key = layer.name + '_ib-' + str(node_index)
# Update container_nodes.
container_nodes.add(node_key)
# Update nodes_depths.
node_depth = nodes_depths.get(node)
if node_depth is None:
nodes_depths[node] = depth
else:
nodes_depths[node] = max(depth, node_depth)
# Update layers_depths.
previously_seen_depth = layers_depths.get(layer)
if previously_seen_depth is None:
current_depth = depth
else:
current_depth = max(depth, previously_seen_depth)
layers_depths[layer] = current_depth
# Store the traversal order for layer sorting.
if layer not in layer_indices:
layer_indices[layer] = len(layer_indices)
nodes_in_progress.add(node)
# Propagate to all previous tensors connected to this node.
for i in range(len(node.inbound_layers)):
x = node.input_tensors[i]
layer = node.inbound_layers[i]
node_index = node.node_indices[i]
tensor_index = node.tensor_indices[i]
next_node = layer.inbound_nodes[node_index]
# use node_marker to prevent cycles
node_marker = make_node_marker(next_node, current_depth + 1)
if node_marker not in seen_nodes:
build_map_of_graph(x, seen_nodes, current_depth + 1,
layer, node_index, tensor_index)
build_map_of_graph(x, finished_nodes, nodes_in_progress,
layer, node_index, tensor_index)
finished_nodes.add(node)
nodes_in_progress.remove(node)
nodes_in_decreasing_depth.append(node)
finished_nodes = set()
nodes_in_progress = set()
for x in self.outputs:
seen_nodes = set()
build_map_of_graph(x, seen_nodes, depth=0)
build_map_of_graph(x, finished_nodes, nodes_in_progress)
for node in reversed(nodes_in_decreasing_depth):
# If the depth is not set, the node has no outbound nodes (depth 0).
depth = nodes_depths.setdefault(node, 0)
# Update the depth of the corresponding layer
previous_depth = layers_depths.get(node.outbound_layer, 0)
# If we've seen this layer before at a higher depth, we should use that depth instead
# of the node depth. This is necessary for shared layers that have inputs at different
# depth levels in the graph.
depth = max(depth, previous_depth)
layers_depths[node.outbound_layer] = depth
nodes_depths[node] = depth
# Update the depth of inbound nodes.
for i in range(len(node.inbound_layers)):
inbound_layer = node.inbound_layers[i]
node_index = node.node_indices[i]
inbound_node = inbound_layer.inbound_nodes[node_index]
previous_depth = nodes_depths.get(inbound_node, 0)
nodes_depths[inbound_node] = max(depth + 1, previous_depth)
# Build a dict {depth: list of nodes with this depth}
nodes_by_depth = {}
@@ -1740,9 +1762,7 @@ class Container(Layer):
output_tensors=self.outputs,
# No container-level masking for now.
input_masks=[None for _ in self.inputs],
output_masks=[None for _ in self.outputs],
input_shapes=[x._keras_shape for x in self.inputs],
output_shapes=[x._keras_shape for x in self.outputs])
output_masks=[None for _ in self.outputs])
self.built = True
# The following are implemented as property functions:
@@ -2003,8 +2023,20 @@ class Container(Layer):
_, output_masks, _ = self.run_internal_graph(inputs, masks)
return output_masks
def compute_output_shape(self, input_shape):
input_shapes = _to_list(input_shape)
def _compute_output_shape(self, input_shape):
if isinstance(input_shape, list):
input_shapes = []
for shape in input_shape:
if shape is not None:
input_shapes.append(tuple(tensor_shape.TensorShape(shape).as_list()))
else:
input_shapes.append(None)
else:
if input_shape is not None:
input_shapes = [tuple(tensor_shape.TensorShape(input_shape).as_list())]
else:
input_shapes = [None]
if len(input_shapes) != len(self.input_layers):
raise ValueError('Invalid input_shape argument ' +
str(input_shape) + ': model has ' +
@@ -2013,9 +2045,13 @@ class Container(Layer):
cache_key = ','.join([str(x) for x in input_shapes])
if cache_key in self._output_shape_cache:
output_shapes = self._output_shape_cache[cache_key]
if isinstance(output_shapes, list) and len(output_shapes) == 1:
return output_shapes[0]
return output_shapes
if isinstance(output_shapes, list):
if len(output_shapes) == 1:
return tensor_shape.TensorShape(output_shapes[0])
else:
return [tensor_shape.TensorShape(shape) for shape in output_shapes]
else:
return tensor_shape.TensorShape(output_shapes)
else:
# Bad luck, we have to run the graph manually.
layers_to_output_shapes = {}
@@ -2052,11 +2088,14 @@ class Container(Layer):
input_shapes.append(input_shape)
if len(input_shapes) == 1:
output_shape = layer.compute_output_shape(input_shapes[0])
output_shape = layer._compute_output_shape(input_shapes[0])
else:
output_shape = layer.compute_output_shape(input_shapes)
output_shape = layer._compute_output_shape(input_shapes)
if isinstance(output_shape, list):
output_shapes = [tuple(tensor_shape.TensorShape(shape).as_list()) for shape in output_shape]
else:
output_shapes = [tuple(tensor_shape.TensorShape(output_shape).as_list())]
output_shapes = _to_list(output_shape)
node_index = layer.inbound_nodes.index(node)
for j in range(len(output_shapes)):
shape_key = layer.name + '_%s_%s' % (node_index, j)
@@ -2077,9 +2116,13 @@ class Container(Layer):
output_shapes.append(layers_to_output_shapes[key])
# Store in cache.
self._output_shape_cache[cache_key] = output_shapes
if isinstance(output_shapes, list) and len(output_shapes) == 1:
return output_shapes[0]
return output_shapes
if isinstance(output_shapes, list):
if len(output_shapes) == 1:
return tensor_shape.TensorShape(output_shapes[0])
else:
return [tensor_shape.TensorShape(shape) for shape in output_shapes]
else:
return tensor_shape.TensorShape(output_shapes)
def run_internal_graph(self, inputs, masks=None):
"""Computes output tensors for new inputs.
@@ -2170,17 +2213,13 @@ class Container(Layer):
# (e.g. weight regularizers).
self.add_loss(layer.get_losses_for(None), None)
# Update _keras_shape.
if all([hasattr(x, '_keras_shape') for x in computed_tensors]):
if len(computed_tensors) == 1:
shapes = _to_list(layer.compute_output_shape(computed_tensors[0]._keras_shape))
uses_learning_phase = computed_tensors[0]._uses_learning_phase
else:
shapes = _to_list(layer.compute_output_shape([x._keras_shape for x in computed_tensors]))
uses_learning_phase = any([x._uses_learning_phase for x in computed_tensors])
for x, s in zip(output_tensors, shapes):
x._keras_shape = s
x._uses_learning_phase = getattr(x, '_uses_learning_phase', False) or uses_learning_phase
# Update `_uses_learning_phase`.
if len(computed_tensors) == 1:
uses_learning_phase = getattr(computed_tensors[0], '_uses_learning_phase', False)
else:
uses_learning_phase = any([getattr(x, '_uses_learning_phase', False) for x in computed_tensors])
for x in output_tensors:
x._uses_learning_phase = getattr(x, '_uses_learning_phase', False) or uses_learning_phase
# Update tensor_map.
for x, y, mask in zip(reference_output_tensors, output_tensors, output_masks):
@@ -2192,11 +2231,7 @@ class Container(Layer):
for x in self.outputs:
assert str(id(x)) in tensor_map, 'Could not compute output ' + str(x)
tensor, mask = tensor_map[str(id(x))]
if hasattr(tensor, '_keras_shape') and output_shapes is not None:
shape = tensor._keras_shape
output_shapes.append(shape)
else:
output_shapes = None
output_shapes.append(K.int_shape(x))
output_tensors.append(tensor)
output_masks.append(mask)
@@ -2218,7 +2253,7 @@ class Container(Layer):
self._output_mask_cache[cache_key] = output_masks
if output_shapes is not None:
input_shapes = [x._keras_shape for x in inputs]
input_shapes = [K.int_shape(x) for x in inputs]
cache_key = ','.join([str(x) for x in input_shapes])
if len(output_shapes) == 1:
output_shapes = output_shapes[0]
@@ -2342,7 +2377,7 @@ class Container(Layer):
layer_name = layer_data['name']
# Instantiate layer.
from ..layers import deserialize as deserialize_layer
from ..layers import deserialize as deserialize_layer # pylint: disable=g-import-not-at-top
layer = deserialize_layer(layer_data,
custom_objects=custom_objects)
created_layers[layer_name] = layer
@@ -2430,7 +2465,7 @@ class Container(Layer):
model = load_model('my_model.h5')
```
"""
from ..models import save_model
from ..models import save_model # pylint: disable=g-import-not-at-top
save_model(self, filepath, overwrite, include_optimizer)
def save_weights(self, filepath, overwrite=True):
@@ -2508,7 +2543,7 @@ class Container(Layer):
# Returns
Model config with Keras version information added.
"""
from .. import __version__ as keras_version
from .. import __version__ as keras_version # pylint: disable=g-import-not-at-top
config = self.get_config()
model_config = {
@@ -2562,7 +2597,12 @@ class Container(Layer):
# Returns
A YAML string.
# Raises
ImportError: if yaml module is not found.
"""
if yaml is None:
raise ImportError('Requires yaml module installed.')
return yaml.dump(self._updated_config(), **kwargs)
def summary(self, line_length=None, positions=None):
@@ -2693,17 +2733,14 @@ def _collect_input_shape(input_tensors):
input_tensors = _to_list(input_tensors)
shapes = []
for x in input_tensors:
try:
shapes.append(K.int_shape(x))
except TypeError:
shapes.append(None)
shapes.append(K.int_shape(x))
if len(shapes) == 1:
return shapes[0]
return shapes
def save_weights_to_hdf5_group(f, layers):
from .. import __version__ as keras_version
from .. import __version__ as keras_version # pylint: disable=g-import-not-at-top
f.attrs['layer_names'] = [layer.name.encode('utf8') for layer in layers]
f.attrs['backend'] = K.backend().encode('utf8')
@@ -2747,6 +2784,25 @@ def preprocess_weights_for_loading(layer, weights,
A list of weights values (Numpy arrays).
"""
if original_keras_version == '1':
if layer.__class__.__name__ == 'Bidirectional':
num_weights_per_layer = len(weights) // 2
forward_weights = preprocess_weights_for_loading(layer.forward_layer,
weights[:num_weights_per_layer],
original_keras_version,
original_backend)
backward_weights = preprocess_weights_for_loading(layer.backward_layer,
weights[num_weights_per_layer:],
original_keras_version,
original_backend)
weights = forward_weights + backward_weights
if layer.__class__.__name__ == 'TimeDistributed':
weights = preprocess_weights_for_loading(layer.layer,
weights,
original_keras_version,
original_backend)
if layer.__class__.__name__ == 'Conv1D':
shape = weights[0].shape
# Handle Keras 1.1 format
@@ -2832,16 +2888,20 @@ def preprocess_weights_for_loading(layer, weights,
(2, 3, 1, 0))
weights = [kernel, recurrent_kernel, bias]
if original_backend and K.backend() != original_backend:
conv_layers = ['Conv1D',
'Conv2D',
'Conv3D',
'Conv2DTranspose']
if layer.__class__.__name__ in conv_layers:
conv_layers = ['Conv1D',
'Conv2D',
'Conv3D',
'Conv2DTranspose',
'ConvLSTM2D']
if layer.__class__.__name__ in conv_layers:
if original_backend and K.backend() != original_backend:
weights[0] = conv_utils.convert_kernel(weights[0])
if layer.__class__.__name__ == 'ConvLSTM2D':
weights[0] = conv_utils.convert_kernel(weights[0])
weights[1] = conv_utils.convert_kernel(weights[1])
if layer.__class__.__name__ == 'ConvLSTM2D':
weights[1] = conv_utils.convert_kernel(weights[1])
if K.int_shape(layer.weights[0]) != weights[0].shape:
weights[0] = np.transpose(weights[0], (3, 2, 0, 1))
if layer.__class__.__name__ == 'ConvLSTM2D':
weights[1] = np.transpose(weights[1], (3, 2, 0, 1))
return weights
+103 -92
Ver Arquivo
@@ -1,28 +1,32 @@
"""Keras training and evaluation routines.
"""
# -*- coding: utf-8 -*-
from __future__ import print_function
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
import warnings
import copy
import time
import numpy as np
import multiprocessing
import threading
import six
import time
import warnings
from .. import backend as K
from .. import callbacks as cbks
from .. import losses
from .. import metrics as metrics_module
from .. import optimizers
import numpy as np
import six
from .topology import Container
from ..utils.generic_utils import Progbar
# pylint: disable=g-import-not-at-top
try:
import queue
except ImportError:
import Queue as queue
from .topology import Container
from .. import backend as K
from .. import optimizers
from .. import losses
from .. import metrics as metrics_module
from ..utils.generic_utils import Progbar
from .. import callbacks as cbks
from ..legacy import interfaces
# pylint: enable=g-import-not-at-top
def _standardize_input_data(data, names, shapes=None,
@@ -50,6 +54,8 @@ def _standardize_input_data(data, names, shapes=None,
# Raises
ValueError: in case of improperly formatted user-provided data.
"""
if not names:
return []
if data is None:
return [None for _ in range(len(names))]
if isinstance(data, dict):
@@ -63,7 +69,8 @@ def _standardize_input_data(data, names, shapes=None,
elif isinstance(data, list):
if len(data) != len(names):
if data and hasattr(data[0], 'shape'):
raise ValueError('Error when checking ' + exception_prefix +
raise ValueError('Error when checking model ' +
exception_prefix +
': the list of Numpy arrays '
'that you are passing to your model '
'is not the size the model expected. '
@@ -77,7 +84,8 @@ def _standardize_input_data(data, names, shapes=None,
data = [np.asarray(data)]
else:
raise ValueError(
'Error when checking ' + exception_prefix +
'Error when checking model ' +
exception_prefix +
': you are passing a list as '
'input to your model, '
'but the model expects '
@@ -88,15 +96,17 @@ def _standardize_input_data(data, names, shapes=None,
arrays = data
else:
if not hasattr(data, 'shape'):
raise TypeError('Error when checking ' + exception_prefix +
raise TypeError('Error when checking model ' +
exception_prefix +
': data should be a Numpy array, '
'or list/dict of Numpy arrays. '
'Found: ' + str(data)[:200] + '...')
if len(names) != 1:
if len(names) > 1:
# Case: model expects multiple inputs but only received
# a single Numpy array.
raise ValueError('The model expects ' + str(len(names)) +
' input arrays, but only received one array. '
exception_prefix +
' arrays, but only received one array. '
'Found: array with shape ' + str(data.shape))
arrays = [data]
@@ -149,7 +159,7 @@ def _standardize_sample_or_class_weights(x_weight, output_names, weight_type):
# Raises
ValueError: In case of invalid user-provided argument.
"""
if x_weight is None or len(x_weight) == 0:
if x_weight is None or len(x_weight) == 0: # pylint: disable=g-explicit-length-test
return [None for _ in output_names]
if len(output_names) == 1:
if isinstance(x_weight, list) and len(x_weight) == 1:
@@ -429,7 +439,6 @@ def _weighted_masked_objective(fn):
# score_array has ndim >= 2
score_array = fn(y_true, y_pred)
if mask is not None:
# Cast the mask to floatX to avoid float64 upcasting in theano
mask = K.cast(mask, K.floatx())
# mask should have the same shape as score_array
score_array *= mask
@@ -478,7 +487,6 @@ def _masked_objective(fn):
# score_array has ndim >= 2
score_array = fn(y_true, y_pred)
if mask is not None:
# Cast the mask to floatX to avoid float64 upcasting in theano
mask = K.cast(mask, K.floatx())
# mask should have the same shape as score_array
score_array *= mask
@@ -679,6 +687,8 @@ class Model(Container):
See [losses](/losses).
If the model has multiple outputs, you can use a different loss
on each output by passing a dictionary or a list of losses.
The loss value that will be minimized by the model
will then be the sum of all individual losses.
metrics: list of metrics to be evaluated by the model
during training and testing.
Typically you will use `metrics=['accuracy']`.
@@ -688,6 +698,9 @@ class Model(Container):
loss_weights: Optional list or dictionary specifying scalar
coefficients (Python floats) to weight the loss contributions
of different model outputs.
The loss value that will be minimized by the model
will then be the *weighted sum* of all individual losses,
weighted by the `loss_weights` coefficients.
If a list, it is expected to have a 1:1 mapping
to the model's outputs. If a tensor, it is expected to map
output names (strings) to scalar coefficients.
@@ -697,12 +710,12 @@ class Model(Container):
If the model has multiple outputs, you can use a different
`sample_weight_mode` on each output by passing a
dictionary or a list of modes.
**kwargs: when using the Theano backend, these arguments
are passed into K.function. Ignored for Tensorflow backend.
**kwargs: Additional arguments passed to `tf.Session.run`.
# Raises
ValueError: In case of invalid arguments for
`optimizer`, `loss`, `metrics` or `sample_weight_mode`.
RuntimeError: If the model has no loss to optimize.
"""
loss = loss or {}
self.optimizer = optimizers.get(optimizer)
@@ -972,27 +985,25 @@ class Model(Container):
# Functions for train, test and predict will
# be compiled lazily when required.
# This saves time when the user is not using all functions.
self._function_kwargs = kwargs
self.train_function = None
self.test_function = None
self.predict_function = None
self._function_kwargs = kwargs
# Collected trainable weights and sort them deterministically.
trainable_weights = self.trainable_weights
# Sort weights by name.
if trainable_weights:
if K.backend() == 'theano':
trainable_weights.sort(key=lambda x: x.name if x.name else x.auto_name)
else:
trainable_weights.sort(key=lambda x: x.name)
trainable_weights.sort(key=lambda x: x.name)
self._collected_trainable_weights = trainable_weights
def _make_train_function(self):
if not hasattr(self, 'train_function'):
raise RuntimeError('You must compile your model before using it.')
if self.train_function is None:
inputs = self._feed_inputs + self._feed_targets + self._feed_sample_weights
inputs = (self._feed_inputs +
self._feed_targets +
self._feed_sample_weights)
if self.uses_learning_phase and not isinstance(K.learning_phase(), int):
inputs += [K.learning_phase()]
@@ -1005,13 +1016,16 @@ class Model(Container):
self.train_function = K.function(inputs,
[self.total_loss] + self.metrics_tensors,
updates=updates,
name='train_function',
**self._function_kwargs)
def _make_test_function(self):
if not hasattr(self, 'test_function'):
raise RuntimeError('You must compile your model before using it.')
if self.test_function is None:
inputs = self._feed_inputs + self._feed_targets + self._feed_sample_weights
inputs = (self._feed_inputs +
self._feed_targets +
self._feed_sample_weights)
if self.uses_learning_phase and not isinstance(K.learning_phase(), int):
inputs += [K.learning_phase()]
# Return loss and metrics, no gradient updates.
@@ -1019,11 +1033,13 @@ class Model(Container):
self.test_function = K.function(inputs,
[self.total_loss] + self.metrics_tensors,
updates=self.state_updates,
name='test_function',
**self._function_kwargs)
def _make_predict_function(self):
if not hasattr(self, 'predict_function'):
self.predict_function = None
self._function_kwargs = {}
if self.predict_function is None:
if self.uses_learning_phase and not isinstance(K.learning_phase(), int):
inputs = self._feed_inputs + [K.learning_phase()]
@@ -1031,11 +1047,11 @@ class Model(Container):
inputs = self._feed_inputs
# Gets network outputs. Does not update weights.
# Does update the network states.
kwargs = getattr(self, '_function_kwargs', {})
self.predict_function = K.function(inputs,
self.outputs,
updates=self.state_updates,
**kwargs)
name='predict_function',
**self._function_kwargs)
def _fit_loop(self, f, ins, out_labels=None, batch_size=32,
epochs=100, verbose=1, callbacks=None,
@@ -1126,7 +1142,7 @@ class Model(Container):
batch_ids = index_array[batch_start:batch_end]
try:
if isinstance(ins[-1], float):
# do not slice the training phase flag
# Do not slice the training phase flag.
ins_batch = _slice_arrays(ins[:-1], batch_ids) + [ins[-1]]
else:
ins_batch = _slice_arrays(ins, batch_ids)
@@ -1145,17 +1161,17 @@ class Model(Container):
batch_logs[l] = o
callbacks.on_batch_end(batch_index, batch_logs)
if callback_model.stop_training:
break
if batch_index == len(batches) - 1: # last batch
# validation
if batch_index == len(batches) - 1: # Last batch.
if do_validation:
# replace with self._evaluate
val_outs = self._test_loop(val_f, val_ins,
batch_size=batch_size,
verbose=0)
if not isinstance(val_outs, list):
val_outs = [val_outs]
# same labels assumed
# Same labels assumed.
for l, o in zip(out_labels, val_outs):
epoch_logs['val_' + l] = o
callbacks.on_epoch_end(epoch, epoch_logs)
@@ -1195,7 +1211,7 @@ class Model(Container):
for batch_index, (batch_start, batch_end) in enumerate(batches):
batch_ids = index_array[batch_start:batch_end]
if ins and isinstance(ins[-1], float):
# do not slice the training phase flag
# Do not slice the training phase flag.
ins_batch = _slice_arrays(ins[:-1], batch_ids) + [ins[-1]]
else:
ins_batch = _slice_arrays(ins, batch_ids)
@@ -1249,7 +1265,7 @@ class Model(Container):
for batch_index, (batch_start, batch_end) in enumerate(batches):
batch_ids = index_array[batch_start:batch_end]
if isinstance(ins[-1], float):
# do not slice the training phase flag
# Do not slice the training phase flag.
ins_batch = _slice_arrays(ins[:-1], batch_ids) + [ins[-1]]
else:
ins_batch = _slice_arrays(ins, batch_ids)
@@ -1293,11 +1309,11 @@ class Model(Container):
x = _standardize_input_data(x, self._feed_input_names,
self._feed_input_shapes,
check_batch_axis=False,
exception_prefix='model input')
exception_prefix='input')
y = _standardize_input_data(y, self._feed_output_names,
output_shapes,
check_batch_axis=False,
exception_prefix='model target')
exception_prefix='target')
sample_weights = _standardize_sample_weights(sample_weight,
self._feed_output_names)
class_weights = _standardize_class_weights(class_weight,
@@ -1318,6 +1334,20 @@ class Model(Container):
str(x[0].shape[0]) + ' samples')
return x, y, sample_weights
def _get_deduped_metrics_names(self):
out_labels = self.metrics_names
# Rename duplicated metrics name
# (can happen with an output layer shared among multiple dataflows).
deduped_out_labels = []
for i, label in enumerate(out_labels):
new_label = label
if out_labels.count(label) > 1:
dup_idx = out_labels[:i].count(label)
new_label += '_' + str(dup_idx + 1)
deduped_out_labels.append(new_label)
return deduped_out_labels
def fit(self, x=None,
y=None,
batch_size=32,
@@ -1329,8 +1359,7 @@ class Model(Container):
shuffle=True,
class_weight=None,
sample_weight=None,
initial_epoch=0,
**kwargs):
initial_epoch=0):
"""Trains the model for a fixed number of epochs (iterations on a dataset).
# Arguments
@@ -1347,7 +1376,7 @@ class Model(Container):
batch_size: integer. Number of samples per gradient update.
epochs: integer, the number of times to iterate
over the training data arrays.
verbose: 0, 1, or 2. Verbosity mode.
verbose: 0, 1, or 2. Verbosity mode.
0 = silent, 1 = verbose, 2 = one log line per epoch.
callbacks: list of callbacks to be called during training.
See [callbacks](/callbacks).
@@ -1389,29 +1418,21 @@ class Model(Container):
ValueError: In case of mismatch between the provided input data
and what the model expects.
"""
# Legacy support
if 'nb_epoch' in kwargs:
warnings.warn('The `nb_epoch` argument in `fit` '
'has been renamed `epochs`.', stacklevel=2)
epochs = kwargs.pop('nb_epoch')
if kwargs:
raise TypeError('Unrecognized keyword arguments: ' + str(kwargs))
# validate user data
# Validate user data.
x, y, sample_weights = self._standardize_user_data(
x, y,
sample_weight=sample_weight,
class_weight=class_weight,
check_batch_axis=False,
batch_size=batch_size)
# prepare validation data
# Prepare validation data.
if validation_data:
do_validation = True
if len(validation_data) == 2:
val_x, val_y = validation_data
val_x, val_y = validation_data # pylint: disable=unpacking-non-sequence
val_sample_weight = None
elif len(validation_data) == 3:
val_x, val_y, val_sample_weight = validation_data
val_x, val_y, val_sample_weight = validation_data # pylint: disable=unpacking-non-sequence
else:
raise ValueError('When passing validation_data, '
'it must contain 2 (x_val, y_val) '
@@ -1450,7 +1471,7 @@ class Model(Container):
val_f = None
val_ins = None
# prepare input arrays and training function
# Prepare input arrays and training function.
if self.uses_learning_phase and not isinstance(K.learning_phase(), int):
ins = x + y + sample_weights + [1.]
else:
@@ -1458,26 +1479,15 @@ class Model(Container):
self._make_train_function()
f = self.train_function
# prepare display labels
out_labels = self.metrics_names
# rename duplicated metrics name
# (can happen with an output layer shared among multiple dataflows)
deduped_out_labels = []
for i, label in enumerate(out_labels):
new_label = label
if out_labels.count(label) > 1:
dup_idx = out_labels[:i].count(label)
new_label += '_' + str(dup_idx + 1)
deduped_out_labels.append(new_label)
out_labels = deduped_out_labels
# Prepare display labels.
out_labels = self._get_deduped_metrics_names()
if do_validation:
callback_metrics = copy.copy(out_labels) + ['val_' + n for n in out_labels]
else:
callback_metrics = copy.copy(out_labels)
# delegate logic to _fit_loop
# Delegate logic to `_fit_loop`.
return self._fit_loop(f, ins, out_labels=out_labels,
batch_size=batch_size, epochs=epochs,
verbose=verbose, callbacks=callbacks,
@@ -1512,13 +1522,13 @@ class Model(Container):
and/or metrics). The attribute `model.metrics_names` will give you
the display labels for the scalar outputs.
"""
# validate user data
# Validate user data.
x, y, sample_weights = self._standardize_user_data(
x, y,
sample_weight=sample_weight,
check_batch_axis=False,
batch_size=batch_size)
# prepare inputs, delegate logic to _test_loop
# Prepare inputs, delegate logic to `_test_loop`.
if self.uses_learning_phase and not isinstance(K.learning_phase(), int):
ins = x + y + sample_weights + [0.]
else:
@@ -1549,7 +1559,7 @@ class Model(Container):
or in case a stateful model receives a number of samples
that is not a multiple of the batch size.
"""
# validate user data
# Validate user data.
x = _standardize_input_data(x, self._feed_input_names,
self._feed_input_shapes,
check_batch_axis=False)
@@ -1562,7 +1572,7 @@ class Model(Container):
str(x[0].shape[0]) + ' samples. '
'Batch size: ' + str(batch_size) + '.')
# prepare inputs, delegate logic to _predict_loop
# Prepare inputs, delegate logic to `_predict_loop`.
if self.uses_learning_phase and not isinstance(K.learning_phase(), int):
ins = x + [0.]
else:
@@ -1686,7 +1696,6 @@ class Model(Container):
return outputs[0]
return outputs
@interfaces.legacy_generator_methods_support
def fit_generator(self, generator,
steps_per_epoch,
epochs=1,
@@ -1713,7 +1722,7 @@ class Model(Container):
All arrays should contain the same number of samples.
The generator is expected to loop over its data
indefinitely. An epoch finishes when `steps_per_epoch`
samples have been seen by the model.
batches have been seen by the model.
steps_per_epoch: Total number of steps (batches of samples)
to yield from `generator` before declaring one epoch
finished and starting the next epoch. It should typically
@@ -1785,7 +1794,8 @@ class Model(Container):
'you must specify a value for '
'`validation_steps`.')
out_labels = self.metrics_names
# Prepare display labels.
out_labels = self._get_deduped_metrics_names()
callback_metrics = out_labels + ['val_' + n for n in out_labels]
# prepare callbacks
@@ -1812,10 +1822,10 @@ class Model(Container):
if do_validation and not val_gen:
if len(validation_data) == 2:
val_x, val_y = validation_data
val_x, val_y = validation_data # pylint: disable=unpacking-non-sequence
val_sample_weight = None
elif len(validation_data) == 3:
val_x, val_y, val_sample_weight = validation_data
val_x, val_y, val_sample_weight = validation_data # pylint: disable=unpacking-non-sequence
else:
raise ValueError('validation_data should be a tuple '
'`(val_x, val_y, val_sample_weight)` '
@@ -1823,8 +1833,11 @@ class Model(Container):
str(validation_data))
val_x, val_y, val_sample_weights = self._standardize_user_data(
val_x, val_y, val_sample_weight)
val_data = val_x + val_y + val_sample_weights
if self.uses_learning_phase and not isinstance(K.learning_phase(), int):
val_data += [0.]
for cbk in callbacks:
cbk.validation_data = val_x + [val_y, val_sample_weights]
cbk.validation_data = val_data
enqueuer = None
try:
@@ -1851,10 +1864,10 @@ class Model(Container):
'or `(x, y)`. Found: ' +
str(generator_output))
if len(generator_output) == 2:
x, y = generator_output
x, y = generator_output # pylint: disable=unpacking-non-sequence
sample_weight = None
elif len(generator_output) == 3:
x, y, sample_weight = generator_output
x, y, sample_weight = generator_output # pylint: disable=unpacking-non-sequence
else:
raise ValueError('output of generator should be '
'a tuple `(x, y, sample_weight)` '
@@ -1923,7 +1936,6 @@ class Model(Container):
callbacks.on_train_end()
return self.history
@interfaces.legacy_generator_methods_support
def evaluate_generator(self, generator, steps,
max_q_size=10, workers=1, pickle_safe=False):
"""Evaluates the model on a data generator.
@@ -1931,7 +1943,7 @@ class Model(Container):
The generator should return the same kind of data
as accepted by `test_on_batch`.
Arguments:
# Arguments
generator: Generator yielding tuples (inputs, targets)
or (inputs, targets, sample_weights)
steps: Total number of steps (batches of samples)
@@ -1984,10 +1996,10 @@ class Model(Container):
'or (x, y). Found: ' +
str(generator_output))
if len(generator_output) == 2:
x, y = generator_output
x, y = generator_output # pylint: disable=unpacking-non-sequence
sample_weight = None
elif len(generator_output) == 3:
x, y, sample_weight = generator_output
x, y, sample_weight = generator_output # pylint: disable=unpacking-non-sequence
else:
raise ValueError('output of generator should be a tuple '
'(x, y, sample_weight) '
@@ -2020,7 +2032,6 @@ class Model(Container):
weights=batch_sizes))
return averages
@interfaces.legacy_generator_methods_support
def predict_generator(self, generator, steps,
max_q_size=10, workers=1,
pickle_safe=False, verbose=0):
@@ -2079,9 +2090,9 @@ class Model(Container):
# Compatibility with the generators
# used for training.
if len(generator_output) == 2:
x, _ = generator_output
x, _ = generator_output # pylint: disable=unpacking-non-sequence
elif len(generator_output) == 3:
x, _, _ = generator_output
x, _, _ = generator_output # pylint: disable=unpacking-non-sequence
else:
raise ValueError('output of generator should be '
'a tuple `(x, y, sample_weight)` '
+17 -6
Ver Arquivo
@@ -1,9 +1,17 @@
"""Keras initializer classes (soon to be replaced with core TF initializers).
"""
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
import math
from . import backend as K
import numpy as np
import six
from . import backend as K
from .utils.generic_utils import serialize_keras_object
from tensorflow.python.framework import tensor_shape
from .utils.generic_utils import deserialize_keras_object
from .utils.generic_utils import serialize_keras_object
class Initializer(object):
@@ -199,11 +207,11 @@ class VarianceScaling(Initializer):
else:
scale /= max(1., float(fan_in + fan_out) / 2)
if self.distribution == 'normal':
stddev = np.sqrt(scale)
stddev = math.sqrt(scale)
return K.truncated_normal(shape, 0., stddev,
dtype=dtype, seed=self.seed)
else:
limit = np.sqrt(3. * scale)
limit = math.sqrt(3. * scale)
return K.random_uniform(shape, -limit, limit,
dtype=dtype, seed=self.seed)
@@ -395,6 +403,7 @@ def he_uniform(seed=None):
# Compatibility aliases
# pylint: disable=invalid-name
zero = zeros = Zeros
one = ones = Ones
constant = Constant
@@ -403,6 +412,7 @@ normal = random_normal = RandomNormal
truncated_normal = TruncatedNormal
identity = Identity
orthogonal = Orthogonal
# pylint: enable=invalid-name
# Utility functions
@@ -423,6 +433,7 @@ def _compute_fans(shape, data_format='channels_last'):
# Raises
ValueError: in case of invalid `data_format` argument.
"""
shape = tensor_shape.TensorShape(shape).as_list()
if len(shape) == 2:
fan_in = shape[0]
fan_out = shape[1]
@@ -442,8 +453,8 @@ def _compute_fans(shape, data_format='channels_last'):
raise ValueError('Invalid data_format: ' + data_format)
else:
# No specific assumptions.
fan_in = np.sqrt(np.prod(shape))
fan_out = np.sqrt(np.prod(shape))
fan_in = math.sqrt(np.prod(shape))
fan_out = math.sqrt(np.prod(shape))
return fan_in, fan_out
+17 -37
Ver Arquivo
@@ -1,46 +1,26 @@
"""Keras layers module.
"""
# pylint: disable=wildcard-import
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
from ..utils.generic_utils import deserialize_keras_object
from ..engine import Layer
from .advanced_activations import *
from .convolutional import *
from .convolutional_recurrent import *
from .core import *
from .embeddings import *
from ..engine import Input
from ..engine import InputLayer
from ..engine import InputSpec
from .merge import *
from .core import *
from .convolutional import *
from .pooling import *
from ..engine import Layer
from .local import *
from .recurrent import *
from .normalization import *
from .embeddings import *
from .merge import *
from .noise import *
from .advanced_activations import *
from .normalization import *
from .pooling import *
from .recurrent import *
from .serialization import deserialize
from .serialization import serialize
from .wrappers import *
from .convolutional_recurrent import *
from ..legacy.layers import *
def serialize(layer):
return {'class_name': layer.__class__.__name__,
'config': layer.get_config()}
def deserialize(config, custom_objects=None):
"""Instantiate a layer from a config dictionary.
# Arguments
config: dict of the form {'class_name': str, 'config': dict}
custom_objects: dict mapping class names (or function names)
of custom (non-Keras) objects to class/functions
# Returns
Layer instance (may be Model, Sequential, Layer...)
"""
from .. import models
globs = globals() # All layers.
globs['Model'] = models.Model
globs['Sequential'] = models.Sequential
return deserialize_keras_object(config,
module_objects=globs,
custom_objects=custom_objects,
printable_module_name='layer')
+11 -15
Ver Arquivo
@@ -1,13 +1,17 @@
"""Layers that act as activation functions.
"""
# -*- coding: utf-8 -*-
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
from .. import backend as K
from .. import constraints
from .. import initializers
from .. import regularizers
from .. import constraints
from ..engine import Layer
from ..engine import InputSpec
from .. import backend as K
from ..legacy import interfaces
from ..engine import Layer
from tensorflow.python.framework import tensor_shape
class LeakyReLU(Layer):
@@ -28,8 +32,6 @@ class LeakyReLU(Layer):
# Arguments
alpha: float >= 0. Negative slope coefficient.
# References
- [Rectifier Nonlinearities Improve Neural Network Acoustic Models](https://web.stanford.edu/~awni/papers/relu_hybrid_icml2013_final.pdf)
"""
def __init__(self, alpha=0.3, **kwargs):
@@ -75,11 +77,8 @@ class PReLU(Layer):
so that each filter only has one set of parameters,
set `shared_axes=[1, 2]`.
# References
- [Delving Deep into Rectifiers: Surpassing Human-Level Performance on ImageNet Classification](https://arxiv.org/abs/1502.01852)
"""
@interfaces.legacy_prelu_support
def __init__(self, alpha_initializer='zeros',
alpha_regularizer=None,
alpha_constraint=None,
@@ -98,13 +97,14 @@ class PReLU(Layer):
self.shared_axes = list(shared_axes)
def build(self, input_shape):
param_shape = list(input_shape[1:])
input_shape = tensor_shape.TensorShape(input_shape).as_list()
param_shape = input_shape[1:]
self.param_broadcast = [False] * len(param_shape)
if self.shared_axes is not None:
for i in self.shared_axes:
param_shape[i - 1] = 1
self.param_broadcast[i - 1] = True
self.alpha = self.add_weight(param_shape,
self.alpha = self.add_weight(shape=param_shape,
name='alpha',
initializer=self.alpha_initializer,
regularizer=self.alpha_regularizer,
@@ -156,8 +156,6 @@ class ELU(Layer):
# Arguments
alpha: scale for the negative factor.
# References
- [Fast and Accurate Deep Network Learning by Exponential Linear Units (ELUs)](https://arxiv.org/abs/1511.07289v1)
"""
def __init__(self, alpha=1.0, **kwargs):
@@ -192,8 +190,6 @@ class ThresholdedReLU(Layer):
# Arguments
theta: float >= 0. Threshold location of activation.
# References
- [Zero-Bias Autoencoders and the Benefits of Co-Adapting Features](http://arxiv.org/abs/1402.3337)
"""
def __init__(self, theta=1.0, **kwargs):
+255 -317
Ver Arquivo
@@ -1,26 +1,30 @@
"""Keras convolution layers and image transformation layers.
"""
# -*- coding: utf-8 -*-
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
from .. import backend as K
from .. import activations
from .. import backend as K
from .. import constraints
from .. import initializers
from .. import regularizers
from .. import constraints
from ..engine import Layer
from ..engine import InputSpec
from ..engine import Layer
from ..utils import conv_utils
from ..legacy import interfaces
# imports for backwards namespace compatibility
# pylint: disable=unused-import
from .pooling import AveragePooling1D
from .pooling import AveragePooling2D
from .pooling import AveragePooling3D
from .pooling import MaxPooling1D
from .pooling import MaxPooling2D
from .pooling import MaxPooling3D
# pylint: enable=unused-import
from ..legacy.layers import AtrousConvolution1D
from ..legacy.layers import AtrousConvolution2D
from tensorflow.python.framework import tensor_shape
class _Conv(Layer):
@@ -57,27 +61,19 @@ class _Conv(Layer):
the dilation rate to use for dilated convolution.
Currently, specifying any `dilation_rate` value != 1 is
incompatible with specifying any `strides` value != 1.
activation: Activation function to use
(see [activations](../activations.md)).
activation: Activation function to use.
If you don't specify anything, no activation is applied
(ie. "linear" activation: `a(x) = x`).
use_bias: Boolean, whether the layer uses a bias vector.
kernel_initializer: Initializer for the `kernel` weights matrix
(see [initializers](../initializers.md)).
bias_initializer: Initializer for the bias vector
(see [initializers](../initializers.md)).
kernel_initializer: Initializer for the `kernel` weights matrix.
bias_initializer: Initializer for the bias vector.
kernel_regularizer: Regularizer function applied to
the `kernel` weights matrix
(see [regularizer](../regularizers.md)).
bias_regularizer: Regularizer function applied to the bias vector
(see [regularizer](../regularizers.md)).
the `kernel` weights matrix.
bias_regularizer: Regularizer function applied to the bias vector.
activity_regularizer: Regularizer function applied to
the output of the layer (its "activation").
(see [regularizer](../regularizers.md)).
kernel_constraint: Constraint function applied to the kernel matrix
(see [constraints](../constraints.md)).
bias_constraint: Constraint function applied to the bias vector
(see [constraints](../constraints.md)).
the output of the layer (its "activation")..
kernel_constraint: Constraint function applied to the kernel matrix.
bias_constraint: Constraint function applied to the bias vector.
"""
def __init__(self, rank,
@@ -117,6 +113,7 @@ class _Conv(Layer):
self.input_spec = InputSpec(ndim=self.rank + 2)
def build(self, input_shape):
input_shape = tensor_shape.TensorShape(input_shape).as_list()
if self.data_format == 'channels_first':
channel_axis = 1
else:
@@ -127,13 +124,13 @@ class _Conv(Layer):
input_dim = input_shape[channel_axis]
kernel_shape = self.kernel_size + (input_dim, self.filters)
self.kernel = self.add_weight(kernel_shape,
self.kernel = self.add_weight(shape=kernel_shape,
initializer=self.kernel_initializer,
name='kernel',
regularizer=self.kernel_regularizer,
constraint=self.kernel_constraint)
if self.use_bias:
self.bias = self.add_weight((self.filters,),
self.bias = self.add_weight(shape=(self.filters,),
initializer=self.bias_initializer,
name='bias',
regularizer=self.bias_regularizer,
@@ -181,7 +178,8 @@ class _Conv(Layer):
return self.activation(outputs)
return outputs
def compute_output_shape(self, input_shape):
def _compute_output_shape(self, input_shape):
input_shape = tensor_shape.TensorShape(input_shape).as_list()
if self.data_format == 'channels_last':
space = input_shape[1:-1]
new_space = []
@@ -193,8 +191,8 @@ class _Conv(Layer):
stride=self.strides[i],
dilation=self.dilation_rate[i])
new_space.append(new_dim)
return (input_shape[0],) + tuple(new_space) + (self.filters,)
if self.data_format == 'channels_first':
return tensor_shape.TensorShape([input_shape[0]] + new_space + [self.filters])
else:
space = input_shape[2:]
new_space = []
for i in range(len(space)):
@@ -205,7 +203,7 @@ class _Conv(Layer):
stride=self.strides[i],
dilation=self.dilation_rate[i])
new_space.append(new_dim)
return (input_shape[0], self.filters) + tuple(new_space)
return tensor_shape.TensorShape([input_shape[0], self.filters] + new_space)
def get_config(self):
config = {
@@ -264,27 +262,19 @@ class Conv1D(_Conv):
the dilation rate to use for dilated convolution.
Currently, specifying any `dilation_rate` value != 1 is
incompatible with specifying any `strides` value != 1.
activation: Activation function to use
(see [activations](../activations.md)).
activation: Activation function to use.
If you don't specify anything, no activation is applied
(ie. "linear" activation: `a(x) = x`).
use_bias: Boolean, whether the layer uses a bias vector.
kernel_initializer: Initializer for the `kernel` weights matrix
(see [initializers](../initializers.md)).
bias_initializer: Initializer for the bias vector
(see [initializers](../initializers.md)).
kernel_initializer: Initializer for the `kernel` weights matrix.
bias_initializer: Initializer for the bias vector.
kernel_regularizer: Regularizer function applied to
the `kernel` weights matrix
(see [regularizer](../regularizers.md)).
bias_regularizer: Regularizer function applied to the bias vector
(see [regularizer](../regularizers.md)).
the `kernel` weights matrix.
bias_regularizer: Regularizer function applied to the bias vector.
activity_regularizer: Regularizer function applied to
the output of the layer (its "activation").
(see [regularizer](../regularizers.md)).
kernel_constraint: Constraint function applied to the kernel matrix
(see [constraints](../constraints.md)).
bias_constraint: Constraint function applied to the bias vector
(see [constraints](../constraints.md)).
the output of the layer (its "activation")..
kernel_constraint: Constraint function applied to the kernel matrix.
bias_constraint: Constraint function applied to the bias vector.
# Input shape
3D tensor with shape: `(batch_size, steps, input_dim)`
@@ -294,7 +284,6 @@ class Conv1D(_Conv):
`steps` value might have changed due to padding or strides.
"""
@interfaces.legacy_conv1d_support
def __init__(self, filters,
kernel_size,
strides=1,
@@ -382,27 +371,19 @@ class Conv2D(_Conv):
all spatial dimensions.
Currently, specifying any `dilation_rate` value != 1 is
incompatible with specifying any stride value != 1.
activation: Activation function to use
(see [activations](../activations.md)).
activation: Activation function to use.
If you don't specify anything, no activation is applied
(ie. "linear" activation: `a(x) = x`).
use_bias: Boolean, whether the layer uses a bias vector.
kernel_initializer: Initializer for the `kernel` weights matrix
(see [initializers](../initializers.md)).
bias_initializer: Initializer for the bias vector
(see [initializers](../initializers.md)).
kernel_initializer: Initializer for the `kernel` weights matrix.
bias_initializer: Initializer for the bias vector.
kernel_regularizer: Regularizer function applied to
the `kernel` weights matrix
(see [regularizer](../regularizers.md)).
bias_regularizer: Regularizer function applied to the bias vector
(see [regularizer](../regularizers.md)).
the `kernel` weights matrix.
bias_regularizer: Regularizer function applied to the bias vector.
activity_regularizer: Regularizer function applied to
the output of the layer (its "activation").
(see [regularizer](../regularizers.md)).
kernel_constraint: Constraint function applied to the kernel matrix
(see [constraints](../constraints.md)).
bias_constraint: Constraint function applied to the bias vector
(see [constraints](../constraints.md)).
the output of the layer (its "activation")..
kernel_constraint: Constraint function applied to the kernel matrix.
bias_constraint: Constraint function applied to the bias vector.
# Input shape
4D tensor with shape:
@@ -418,7 +399,6 @@ class Conv2D(_Conv):
`rows` and `cols` values might have changed due to padding.
"""
@interfaces.legacy_conv2d_support
def __init__(self, filters,
kernel_size,
strides=(1, 1),
@@ -507,27 +487,19 @@ class Conv3D(_Conv):
all spatial dimensions.
Currently, specifying any `dilation_rate` value != 1 is
incompatible with specifying any stride value != 1.
activation: Activation function to use
(see [activations](../activations.md)).
activation: Activation function to use.
If you don't specify anything, no activation is applied
(ie. "linear" activation: `a(x) = x`).
use_bias: Boolean, whether the layer uses a bias vector.
kernel_initializer: Initializer for the `kernel` weights matrix
(see [initializers](../initializers.md)).
bias_initializer: Initializer for the bias vector
(see [initializers](../initializers.md)).
kernel_initializer: Initializer for the `kernel` weights matrix.
bias_initializer: Initializer for the bias vector.
kernel_regularizer: Regularizer function applied to
the `kernel` weights matrix
(see [regularizer](../regularizers.md)).
bias_regularizer: Regularizer function applied to the bias vector
(see [regularizer](../regularizers.md)).
the `kernel` weights matrix.
bias_regularizer: Regularizer function applied to the bias vector.
activity_regularizer: Regularizer function applied to
the output of the layer (its "activation").
(see [regularizer](../regularizers.md)).
kernel_constraint: Constraint function applied to the kernel matrix
(see [constraints](../constraints.md)).
bias_constraint: Constraint function applied to the bias vector
(see [constraints](../constraints.md)).
the output of the layer (its "activation")..
kernel_constraint: Constraint function applied to the kernel matrix.
bias_constraint: Constraint function applied to the bias vector.
# Input shape
5D tensor with shape:
@@ -543,7 +515,6 @@ class Conv3D(_Conv):
`new_conv_dim1`, `new_conv_dim2` and `new_conv_dim3` values might have changed due to padding.
"""
@interfaces.legacy_conv3d_support
def __init__(self, filters,
kernel_size,
strides=(1, 1, 1),
@@ -632,27 +603,19 @@ class Conv2DTranspose(Conv2D):
all spatial dimensions.
Currently, specifying any `dilation_rate` value != 1 is
incompatible with specifying any stride value != 1.
activation: Activation function to use
(see [activations](../activations.md)).
activation: Activation function to use.
If you don't specify anything, no activation is applied
(ie. "linear" activation: `a(x) = x`).
use_bias: Boolean, whether the layer uses a bias vector.
kernel_initializer: Initializer for the `kernel` weights matrix
(see [initializers](../initializers.md)).
bias_initializer: Initializer for the bias vector
(see [initializers](../initializers.md)).
kernel_initializer: Initializer for the `kernel` weights matrix.
bias_initializer: Initializer for the bias vector.
kernel_regularizer: Regularizer function applied to
the `kernel` weights matrix
(see [regularizer](../regularizers.md)).
bias_regularizer: Regularizer function applied to the bias vector
(see [regularizer](../regularizers.md)).
the `kernel` weights matrix.
bias_regularizer: Regularizer function applied to the bias vector.
activity_regularizer: Regularizer function applied to
the output of the layer (its "activation").
(see [regularizer](../regularizers.md)).
kernel_constraint: Constraint function applied to the kernel matrix
(see [constraints](../constraints.md)).
bias_constraint: Constraint function applied to the bias vector
(see [constraints](../constraints.md)).
the output of the layer (its "activation")..
kernel_constraint: Constraint function applied to the kernel matrix.
bias_constraint: Constraint function applied to the bias vector.
# Input shape
4D tensor with shape:
@@ -672,7 +635,6 @@ class Conv2DTranspose(Conv2D):
- [Deconvolutional Networks](http://www.matthewzeiler.com/pubs/cvpr2010/cvpr2010.pdf)
"""
@interfaces.legacy_deconv2d_support
def __init__(self, filters,
kernel_size,
strides=(1, 1),
@@ -707,6 +669,7 @@ class Conv2DTranspose(Conv2D):
self.input_spec = InputSpec(ndim=4)
def build(self, input_shape):
input_shape = tensor_shape.TensorShape(input_shape).as_list()
if len(input_shape) != 4:
raise ValueError('Inputs should have rank ' +
str(4) +
@@ -721,13 +684,13 @@ class Conv2DTranspose(Conv2D):
input_dim = input_shape[channel_axis]
kernel_shape = self.kernel_size + (self.filters, input_dim)
self.kernel = self.add_weight(kernel_shape,
self.kernel = self.add_weight(shape=kernel_shape,
initializer=self.kernel_initializer,
name='kernel',
regularizer=self.kernel_regularizer,
constraint=self.kernel_constraint)
if self.use_bias:
self.bias = self.add_weight((self.filters,),
self.bias = self.add_weight(shape=(self.filters,),
initializer=self.bias_initializer,
name='bias',
regularizer=self.bias_regularizer,
@@ -780,7 +743,8 @@ class Conv2DTranspose(Conv2D):
return self.activation(outputs)
return outputs
def compute_output_shape(self, input_shape):
def _compute_output_shape(self, input_shape):
input_shape = tensor_shape.TensorShape(input_shape).as_list()
output_shape = list(input_shape)
if self.data_format == 'channels_first':
c_axis, h_axis, w_axis = 1, 2, 3
@@ -795,7 +759,7 @@ class Conv2DTranspose(Conv2D):
output_shape[h_axis], stride_h, kernel_h, self.padding)
output_shape[w_axis] = conv_utils.deconv_length(
output_shape[w_axis], stride_w, kernel_w, self.padding)
return tuple(output_shape)
return tensor_shape.TensorShape(output_shape)
def get_config(self):
config = super(Conv2DTranspose, self).get_config()
@@ -845,36 +809,25 @@ class SeparableConv2D(Conv2D):
for each input channel.
The total number of depthwise convolution output
channels will be equal to `filterss_in * depth_multiplier`.
activation: Activation function to use
(see [activations](../activations.md)).
activation: Activation function to use.
If you don't specify anything, no activation is applied
(ie. "linear" activation: `a(x) = x`).
use_bias: Boolean, whether the layer uses a bias vector.
depthwise_initializer: Initializer for the depthwise kernel matrix
(see [initializers](../initializers.md)).
pointwise_initializer: Initializer for the pointwise kernel matrix
(see [initializers](../initializers.md)).
bias_initializer: Initializer for the bias vector
(see [initializers](../initializers.md)).
depthwise_initializer: Initializer for the depthwise kernel matrix.
pointwise_initializer: Initializer for the pointwise kernel matrix.
bias_initializer: Initializer for the bias vector.
depthwise_regularizer: Regularizer function applied to
the depthwise kernel matrix
(see [regularizer](../regularizers.md)).
the depthwise kernel matrix.
pointwise_regularizer: Regularizer function applied to
the depthwise kernel matrix
(see [regularizer](../regularizers.md)).
bias_regularizer: Regularizer function applied to the bias vector
(see [regularizer](../regularizers.md)).
the depthwise kernel matrix.
bias_regularizer: Regularizer function applied to the bias vector.
activity_regularizer: Regularizer function applied to
the output of the layer (its "activation").
(see [regularizer](../regularizers.md)).
the output of the layer (its "activation")..
depthwise_constraint: Constraint function applied to
the depthwise kernel matrix
(see [constraints](../constraints.md)).
the depthwise kernel matrix.
pointwise_constraint: Constraint function applied to
the pointwise kernel matrix
(see [constraints](../constraints.md)).
bias_constraint: Constraint function applied to the bias vector
(see [constraints](../constraints.md)).
the pointwise kernel matrix.
bias_constraint: Constraint function applied to the bias vector.
# Input shape
4D tensor with shape:
@@ -890,7 +843,6 @@ class SeparableConv2D(Conv2D):
`rows` and `cols` values might have changed due to padding.
"""
@interfaces.legacy_separable_conv2d_support
def __init__(self, filters,
kernel_size,
strides=(1, 1),
@@ -931,6 +883,7 @@ class SeparableConv2D(Conv2D):
self.pointwise_constraint = constraints.get(pointwise_constraint)
def build(self, input_shape):
input_shape = tensor_shape.TensorShape(input_shape).as_list()
if len(input_shape) < 4:
raise ValueError('Inputs to `SeparableConv2D` should have rank 4. '
'Received input shape:', str(input_shape))
@@ -952,20 +905,20 @@ class SeparableConv2D(Conv2D):
self.filters)
self.depthwise_kernel = self.add_weight(
depthwise_kernel_shape,
shape=depthwise_kernel_shape,
initializer=self.depthwise_initializer,
name='depthwise_kernel',
regularizer=self.depthwise_regularizer,
constraint=self.depthwise_constraint)
self.pointwise_kernel = self.add_weight(
pointwise_kernel_shape,
shape=pointwise_kernel_shape,
initializer=self.pointwise_initializer,
name='pointwise_kernel',
regularizer=self.pointwise_regularizer,
constraint=self.pointwise_constraint)
if self.use_bias:
self.bias = self.add_weight((self.filters,),
self.bias = self.add_weight(shape=(self.filters,),
initializer=self.bias_initializer,
name='bias',
regularizer=self.bias_regularizer,
@@ -995,11 +948,12 @@ class SeparableConv2D(Conv2D):
return self.activation(outputs)
return outputs
def compute_output_shape(self, input_shape):
def _compute_output_shape(self, input_shape):
input_shape = tensor_shape.TensorShape(input_shape).as_list()
if self.data_format == 'channels_first':
rows = input_shape[2]
cols = input_shape[3]
elif self.data_format == 'channels_last':
else:
rows = input_shape[1]
cols = input_shape[2]
@@ -1010,9 +964,9 @@ class SeparableConv2D(Conv2D):
self.padding,
self.strides[1])
if self.data_format == 'channels_first':
return (input_shape[0], self.filters, rows, cols)
elif self.data_format == 'channels_last':
return (input_shape[0], rows, cols, self.filters)
return tensor_shape.TensorShape([input_shape[0], self.filters, rows, cols])
else:
return tensor_shape.TensorShape([input_shape[0], rows, cols, self.filters])
def get_config(self):
config = super(SeparableConv2D, self).get_config()
@@ -1044,15 +998,15 @@ class UpSampling1D(Layer):
3D tensor with shape: `(batch, upsampled_steps, features)`.
"""
@interfaces.legacy_upsampling1d_support
def __init__(self, size=2, **kwargs):
super(UpSampling1D, self).__init__(**kwargs)
self.size = int(size)
self.input_spec = InputSpec(ndim=3)
def compute_output_shape(self, input_shape):
def _compute_output_shape(self, input_shape):
input_shape = tensor_shape.TensorShape(input_shape).as_list()
size = self.size * input_shape[1] if input_shape[1] is not None else None
return (input_shape[0], size, input_shape[2])
return tensor_shape.TensorShape([input_shape[0], size, input_shape[2]])
def call(self, inputs):
output = K.repeat_elements(inputs, self.size, axis=1)
@@ -1099,28 +1053,28 @@ class UpSampling2D(Layer):
`(batch, channels, upsampled_rows, upsampled_cols)`
"""
@interfaces.legacy_upsampling2d_support
def __init__(self, size=(2, 2), data_format=None, **kwargs):
super(UpSampling2D, self).__init__(**kwargs)
self.data_format = conv_utils.normalize_data_format(data_format)
self.size = conv_utils.normalize_tuple(size, 2, 'size')
self.input_spec = InputSpec(ndim=4)
def compute_output_shape(self, input_shape):
def _compute_output_shape(self, input_shape):
input_shape = tensor_shape.TensorShape(input_shape).as_list()
if self.data_format == 'channels_first':
height = self.size[0] * input_shape[2] if input_shape[2] is not None else None
width = self.size[1] * input_shape[3] if input_shape[3] is not None else None
return (input_shape[0],
input_shape[1],
height,
width)
elif self.data_format == 'channels_last':
return tensor_shape.TensorShape([input_shape[0],
input_shape[1],
height,
width])
else:
height = self.size[0] * input_shape[1] if input_shape[1] is not None else None
width = self.size[1] * input_shape[2] if input_shape[2] is not None else None
return (input_shape[0],
height,
width,
input_shape[3])
return tensor_shape.TensorShape([input_shape[0],
height,
width,
input_shape[3]])
def call(self, inputs):
return K.resize_images(inputs, self.size[0], self.size[1],
@@ -1168,32 +1122,32 @@ class UpSampling3D(Layer):
`(batch, channels, upsampled_dim1, upsampled_dim2, upsampled_dim3)`
"""
@interfaces.legacy_upsampling3d_support
def __init__(self, size=(2, 2, 2), data_format=None, **kwargs):
self.data_format = conv_utils.normalize_data_format(data_format)
self.size = conv_utils.normalize_tuple(size, 3, 'size')
self.input_spec = InputSpec(ndim=5)
super(UpSampling3D, self).__init__(**kwargs)
def compute_output_shape(self, input_shape):
def _compute_output_shape(self, input_shape):
input_shape = tensor_shape.TensorShape(input_shape).as_list()
if self.data_format == 'channels_first':
dim1 = self.size[0] * input_shape[2] if input_shape[2] is not None else None
dim2 = self.size[1] * input_shape[3] if input_shape[3] is not None else None
dim3 = self.size[2] * input_shape[4] if input_shape[4] is not None else None
return (input_shape[0],
input_shape[1],
dim1,
dim2,
dim3)
elif self.data_format == 'channels_last':
return tensor_shape.TensorShape([input_shape[0],
input_shape[1],
dim1,
dim2,
dim3])
else:
dim1 = self.size[0] * input_shape[1] if input_shape[1] is not None else None
dim2 = self.size[1] * input_shape[2] if input_shape[2] is not None else None
dim3 = self.size[2] * input_shape[3] if input_shape[3] is not None else None
return (input_shape[0],
dim1,
dim2,
dim3,
input_shape[4])
return tensor_shape.TensorShape([input_shape[0],
dim1,
dim2,
dim3,
input_shape[4]])
def call(self, inputs):
return K.resize_volumes(inputs,
@@ -1231,14 +1185,14 @@ class ZeroPadding1D(Layer):
self.padding = conv_utils.normalize_tuple(padding, 2, 'padding')
self.input_spec = InputSpec(ndim=3)
def compute_output_shape(self, input_shape):
def _compute_output_shape(self, input_shape):
if input_shape[1] is not None:
length = input_shape[1] + self.padding[0] + self.padding[1]
else:
length = None
return (input_shape[0],
length,
input_shape[2])
return tensor_shape.TensorShape([input_shape[0],
length,
input_shape[2]])
def call(self, inputs):
return K.temporal_padding(inputs, padding=self.padding)
@@ -1292,7 +1246,6 @@ class ZeroPadding2D(Layer):
`(batch, channels, padded_rows, padded_cols)`
"""
@interfaces.legacy_zeropadding2d_support
def __init__(self,
padding=(1, 1),
data_format=None,
@@ -1319,7 +1272,8 @@ class ZeroPadding2D(Layer):
'Found: ' + str(padding))
self.input_spec = InputSpec(ndim=4)
def compute_output_shape(self, input_shape):
def _compute_output_shape(self, input_shape):
input_shape = tensor_shape.TensorShape(input_shape).as_list()
if self.data_format == 'channels_first':
if input_shape[2] is not None:
rows = input_shape[2] + self.padding[0][0] + self.padding[0][1]
@@ -1329,10 +1283,10 @@ class ZeroPadding2D(Layer):
cols = input_shape[3] + self.padding[1][0] + self.padding[1][1]
else:
cols = None
return (input_shape[0],
input_shape[1],
rows,
cols)
return tensor_shape.TensorShape([input_shape[0],
input_shape[1],
rows,
cols])
elif self.data_format == 'channels_last':
if input_shape[1] is not None:
rows = input_shape[1] + self.padding[0][0] + self.padding[0][1]
@@ -1342,10 +1296,10 @@ class ZeroPadding2D(Layer):
cols = input_shape[2] + self.padding[1][0] + self.padding[1][1]
else:
cols = None
return (input_shape[0],
rows,
cols,
input_shape[3])
return tensor_shape.TensorShape([input_shape[0],
rows,
cols,
input_shape[3]])
def call(self, inputs):
return K.spatial_2d_padding(inputs,
@@ -1399,7 +1353,6 @@ class ZeroPadding3D(Layer):
`(batch, depth, first_padded_axis, second_padded_axis, third_axis_to_pad)`
"""
@interfaces.legacy_zeropadding3d_support
def __init__(self, padding=(1, 1, 1), data_format=None, **kwargs):
super(ZeroPadding3D, self).__init__(**kwargs)
self.data_format = conv_utils.normalize_data_format(data_format)
@@ -1427,43 +1380,44 @@ class ZeroPadding3D(Layer):
'Found: ' + str(padding))
self.input_spec = InputSpec(ndim=5)
def compute_output_shape(self, input_shape):
def _compute_output_shape(self, input_shape):
input_shape = tensor_shape.TensorShape(input_shape).as_list()
if self.data_format == 'channels_first':
if input_shape[2] is not None:
dim1 = input_shape[2] + 2 * self.padding[0][0]
dim1 = input_shape[2] + self.padding[0][0] + self.padding[0][1]
else:
dim1 = None
if input_shape[3] is not None:
dim2 = input_shape[3] + 2 * self.padding[1][0]
dim2 = input_shape[3] + self.padding[1][0] + self.padding[1][1]
else:
dim2 = None
if input_shape[4] is not None:
dim3 = input_shape[4] + 2 * self.padding[2][0]
dim3 = input_shape[4] + self.padding[2][0] + self.padding[2][1]
else:
dim3 = None
return (input_shape[0],
input_shape[1],
dim1,
dim2,
dim3)
return tensor_shape.TensorShape([input_shape[0],
input_shape[1],
dim1,
dim2,
dim3])
elif self.data_format == 'channels_last':
if input_shape[1] is not None:
dim1 = input_shape[1] + 2 * self.padding[0][1]
dim1 = input_shape[1] + self.padding[0][0] + self.padding[0][1]
else:
dim1 = None
if input_shape[2] is not None:
dim2 = input_shape[2] + 2 * self.padding[1][1]
dim2 = input_shape[2] + self.padding[1][0] + self.padding[1][1]
else:
dim2 = None
if input_shape[3] is not None:
dim3 = input_shape[3] + 2 * self.padding[2][1]
dim3 = input_shape[3] + self.padding[2][0] + self.padding[2][1]
else:
dim3 = None
return (input_shape[0],
dim1,
dim2,
dim3,
input_shape[4])
return tensor_shape.TensorShape([input_shape[0],
dim1,
dim2,
dim3,
input_shape[4]])
def call(self, inputs):
return K.spatial_3d_padding(inputs,
@@ -1501,14 +1455,15 @@ class Cropping1D(Layer):
self.cropping = conv_utils.normalize_tuple(cropping, 2, 'cropping')
self.input_spec = InputSpec(ndim=3)
def compute_output_shape(self, input_shape):
def _compute_output_shape(self, input_shape):
input_shape = tensor_shape.TensorShape(input_shape).as_list()
if input_shape[1] is not None:
length = input_shape[1] - self.cropping[0] - self.cropping[1]
else:
length = None
return (input_shape[0],
length,
input_shape[2])
return tensor_shape.TensorShape([input_shape[0],
length,
input_shape[2]])
def call(self, inputs):
if self.cropping[1] == 0:
@@ -1571,13 +1526,12 @@ class Cropping2D(Layer):
model.add(Cropping2D(cropping=((2, 2), (4, 4)),
input_shape=(28, 28, 3)))
# now model.output_shape == (None, 24, 20, 3)
model.add(Conv2D(64, (3, 3), padding='same))
model.add(Conv2D(64, (3, 3), padding='same'))
model.add(Cropping2D(cropping=((2, 2), (2, 2))))
# now model.output_shape == (None, 20, 16. 64)
```
"""
@interfaces.legacy_cropping2d_support
def __init__(self, cropping=((0, 0), (0, 0)),
data_format=None, **kwargs):
super(Cropping2D, self).__init__(**kwargs)
@@ -1604,19 +1558,27 @@ class Cropping2D(Layer):
'Found: ' + str(cropping))
self.input_spec = InputSpec(ndim=4)
def compute_output_shape(self, input_shape):
def _compute_output_shape(self, input_shape):
input_shape = tensor_shape.TensorShape(input_shape).as_list()
# pylint: disable=invalid-unary-operand-type
if self.data_format == 'channels_first':
return (input_shape[0],
input_shape[1],
input_shape[2] - self.cropping[0][0] - self.cropping[0][1] if input_shape[2] else None,
input_shape[3] - self.cropping[1][0] - self.cropping[1][1] if input_shape[3] else None)
elif self.data_format == 'channels_last':
return (input_shape[0],
input_shape[1] - self.cropping[0][0] - self.cropping[0][1] if input_shape[1] else None,
input_shape[2] - self.cropping[1][0] - self.cropping[1][1] if input_shape[2] else None,
input_shape[3])
return tensor_shape.TensorShape([
input_shape[0],
input_shape[1],
input_shape[2] - self.cropping[0][0] - self.cropping[0][1] if input_shape[2] else None,
input_shape[3] - self.cropping[1][0] - self.cropping[1][1] if input_shape[3] else None
])
else:
return tensor_shape.TensorShape([
input_shape[0],
input_shape[1] - self.cropping[0][0] - self.cropping[0][1] if input_shape[1] else None,
input_shape[2] - self.cropping[1][0] - self.cropping[1][1] if input_shape[2] else None,
input_shape[3]
])
# pylint: enable=invalid-unary-operand-type
def call(self, inputs):
# pylint: disable=invalid-unary-operand-type
if self.data_format == 'channels_first':
if self.cropping[0][1] == self.cropping[1][1] == 0:
return inputs[:,
@@ -1637,7 +1599,7 @@ class Cropping2D(Layer):
:,
self.cropping[0][0]: -self.cropping[0][1],
self.cropping[1][0]: -self.cropping[1][1]]
elif self.data_format == 'channels_last':
else:
if self.cropping[0][1] == self.cropping[1][1] == 0:
return inputs[:,
self.cropping[0][0]:,
@@ -1657,6 +1619,7 @@ class Cropping2D(Layer):
self.cropping[0][0]: -self.cropping[0][1],
self.cropping[1][0]: -self.cropping[1][1],
:]
# pylint: enable=invalid-unary-operand-type
def get_config(self):
config = {'cropping': self.cropping,
@@ -1705,7 +1668,6 @@ class Cropping3D(Layer):
`(batch, depth, first_cropped_axis, second_cropped_axis, third_cropped_axis)`
"""
@interfaces.legacy_cropping3d_support
def __init__(self, cropping=((1, 1), (1, 1), (1, 1)),
data_format=None, **kwargs):
super(Cropping3D, self).__init__(**kwargs)
@@ -1736,7 +1698,9 @@ class Cropping3D(Layer):
'Found: ' + str(cropping))
self.input_spec = InputSpec(ndim=5)
def compute_output_shape(self, input_shape):
def _compute_output_shape(self, input_shape):
input_shape = tensor_shape.TensorShape(input_shape).as_list()
# pylint: disable=invalid-unary-operand-type
if self.data_format == 'channels_first':
if input_shape[2] is not None:
dim1 = input_shape[2] - self.cropping[0][0] - self.cropping[0][1]
@@ -1750,11 +1714,11 @@ class Cropping3D(Layer):
dim3 = input_shape[4] - self.cropping[2][0] - self.cropping[2][1]
else:
dim3 = None
return (input_shape[0],
input_shape[1],
dim1,
dim2,
dim3)
return tensor_shape.TensorShape([input_shape[0],
input_shape[1],
dim1,
dim2,
dim3])
elif self.data_format == 'channels_last':
if input_shape[1] is not None:
dim1 = input_shape[1] - self.cropping[0][0] - self.cropping[0][1]
@@ -1768,110 +1732,88 @@ class Cropping3D(Layer):
dim3 = input_shape[3] - self.cropping[2][0] - self.cropping[2][1]
else:
dim3 = None
return (input_shape[0],
dim1,
dim2,
dim3,
input_shape[4])
return tensor_shape.TensorShape(
[input_shape[0],
dim1,
dim2,
dim3,
input_shape[4]])
# pylint: enable=invalid-unary-operand-type
def call(self, inputs):
# pylint: disable=invalid-unary-operand-type
if self.data_format == 'channels_first':
if self.cropping[0][1] == self.cropping[1][1] == self.cropping[2][1] == 0:
return inputs[:,
:,
self.cropping[0][0]:,
self.cropping[1][0]:,
self.cropping[2][0]:]
elif self.cropping[0][1] == self.cropping[1][1] == 0:
return inputs[:,
:,
self.cropping[0][0]:,
self.cropping[1][0]:,
self.cropping[2][0]: -self.cropping[2][1]]
elif self.cropping[1][1] == self.cropping[2][1] == 0:
return inputs[:,
:,
self.cropping[0][0]: -self.cropping[0][1],
self.cropping[1][0]:,
self.cropping[2][0]:]
elif self.cropping[0][1] == self.cropping[2][1] == 0:
return inputs[:,
:,
self.cropping[0][0]:,
self.cropping[1][0]: -self.cropping[1][1],
self.cropping[2][0]:]
elif self.cropping[0][1] == 0:
return inputs[:,
:,
self.cropping[0][0]:,
self.cropping[1][0]: -self.cropping[1][1],
self.cropping[2][0]: -self.cropping[2][1]]
elif self.cropping[1][1] == 0:
return inputs[:,
:,
self.cropping[0][0]: -self.cropping[0][1],
self.cropping[1][0]:,
self.cropping[2][0]: -self.cropping[2][1]]
elif self.cropping[2][1] == 0:
return inputs[:,
:,
self.cropping[0][0]: -self.cropping[0][1],
self.cropping[1][0]: -self.cropping[1][1],
self.cropping[2][0]:]
if self.cropping[0][1] == self.cropping[1][1] == self.cropping[2][1] == 0:
return inputs[:,
:,
self.cropping[0][0]: -self.cropping[0][1],
self.cropping[1][0]: -self.cropping[1][1],
self.cropping[2][0]: -self.cropping[2][1]]
elif self.data_format == 'channels_last':
if self.cropping[0][1] == self.cropping[1][1] == self.cropping[2][1] == 0:
return inputs[:,
self.cropping[0][0]:,
self.cropping[1][0]:,
self.cropping[2][0]:,
:]
elif self.cropping[0][1] == self.cropping[1][1] == 0:
return inputs[:,
self.cropping[0][0]:,
self.cropping[1][0]:,
self.cropping[2][0]: -self.cropping[2][1],
:]
elif self.cropping[1][1] == self.cropping[2][1] == 0:
return inputs[:,
self.cropping[0][0]: -self.cropping[0][1],
self.cropping[1][0]:,
self.cropping[2][0]:,
:]
elif self.cropping[0][1] == self.cropping[2][1] == 0:
return inputs[:,
self.cropping[0][0]:,
self.cropping[1][0]:-self.cropping[1][1],
self.cropping[2][0]:,
:]
elif self.cropping[0][1] == 0:
return inputs[:,
self.cropping[0][0]:,
self.cropping[1][0]: -self.cropping[1][1],
self.cropping[2][0]: -self.cropping[2][1],
:]
elif self.cropping[1][1] == 0:
return inputs[:,
self.cropping[0][0]: -self.cropping[0][1],
self.cropping[1][0]:,
self.cropping[2][0]: -self.cropping[2][1],
:]
elif self.cropping[2][1] == 0:
return inputs[:,
self.cropping[0][0]: -self.cropping[0][1],
self.cropping[1][0]: -self.cropping[1][1],
self.cropping[2][0]:,
:]
return inputs[:,
self.cropping[0][0]: -self.cropping[0][1],
self.cropping[1][0]: -self.cropping[1][1],
self.cropping[2][0]: -self.cropping[2][1],
:]
self.cropping[0][0]:,
self.cropping[1][0]:,
self.cropping[2][0]:]
elif self.cropping[0][1] == self.cropping[1][1] == 0:
return inputs[:, :,
self.cropping[0][0]:,
self.cropping[1][0]:,
self.cropping[2][0]:-self.cropping[2][1]]
elif self.cropping[1][1] == self.cropping[2][1] == 0:
return inputs[:, :,
self.cropping[0][0]:-self.cropping[0][1],
self.cropping[1][0]:,
self.cropping[2][0]:]
elif self.cropping[0][1] == self.cropping[2][1] == 0:
return inputs[:, :,
self.cropping[0][0]:,
self.cropping[1][0]:-self.cropping[1][1],
self.cropping[2][0]:]
elif self.cropping[0][1] == 0:
return inputs[:, :,
self.cropping[0][0]:,
self.cropping[1][0]:-self.cropping[1][1],
self.cropping[2][0]:-self.cropping[2][1]]
elif self.cropping[1][1] == 0:
return inputs[:, :,
self.cropping[0][0]:-self.cropping[0][1],
self.cropping[1][0]:,
self.cropping[2][0]:-self.cropping[2][1]]
elif self.cropping[2][1] == 0:
return inputs[:, :,
self.cropping[0][0]:-self.cropping[0][1],
self.cropping[1][0]:-self.cropping[1][1],
self.cropping[2][0]:]
return inputs[:, :,
self.cropping[0][0]:-self.cropping[0][1],
self.cropping[1][0]:-self.cropping[1][1],
self.cropping[2][0]:-self.cropping[2][1]]
else:
if self.cropping[0][1] == self.cropping[1][1] == self.cropping[2][1] == 0:
return inputs[:, self.cropping[0][0]:,
self.cropping[1][0]:,
self.cropping[2][0]:, :]
elif self.cropping[0][1] == self.cropping[1][1] == 0:
return inputs[:, self.cropping[0][0]:, self.cropping[1][0]:,
self.cropping[2][0]:-self.cropping[2][1], :]
elif self.cropping[1][1] == self.cropping[2][1] == 0:
return inputs[:, self.cropping[0][0]:-self.cropping[0][1],
self.cropping[1][0]:, self.cropping[2][0]:, :]
elif self.cropping[0][1] == self.cropping[2][1] == 0:
return inputs[:, self.cropping[0][0]:,
self.cropping[1][0]:-self.cropping[1][1],
self.cropping[2][0]:, :]
elif self.cropping[0][1] == 0:
return inputs[:, self.cropping[0][0]:,
self.cropping[1][0]:-self.cropping[1][1],
self.cropping[2][0]:-self.cropping[2][1], :]
elif self.cropping[1][1] == 0:
return inputs[:, self.cropping[0][0]:-self.cropping[0][1],
self.cropping[1][0]:,
self.cropping[2][0]:-self.cropping[2][1], :]
elif self.cropping[2][1] == 0:
return inputs[:, self.cropping[0][0]:-self.cropping[0][1],
self.cropping[1][0]:-self.cropping[1][1],
self.cropping[2][0]:, :]
return inputs[:, self.cropping[0][0]:-self.cropping[0][1],
self.cropping[1][0]:-self.cropping[1][1],
self.cropping[2][0]:-self.cropping[2][1], :]
# pylint: enable=invalid-unary-operand-type
def get_config(self):
config = {'cropping': self.cropping,
@@ -1888,7 +1830,3 @@ Convolution3D = Conv3D
SeparableConvolution2D = SeparableConv2D
Convolution2DTranspose = Conv2DTranspose
Deconvolution2D = Deconv2D = Conv2DTranspose
# Legacy aliases
AtrousConv1D = AtrousConvolution1D
AtrousConv2D = AtrousConvolution2D
+64 -49
Ver Arquivo
@@ -1,17 +1,20 @@
# -*- coding: utf-8 -*-
"""Convolutional-recurrent layers.
"""
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
from .. import backend as K
from .. import activations
from .. import backend as K
from .. import constraints
from .. import initializers
from .. import regularizers
from .. import constraints
from .recurrent import Recurrent
import numpy as np
from ..engine import InputSpec
import numpy as np
from .recurrent import Recurrent
from tensorflow.python.framework import tensor_shape
from ..utils import conv_utils
from ..legacy import interfaces
class ConvRecurrent2D(Recurrent):
@@ -62,7 +65,7 @@ class ConvRecurrent2D(Recurrent):
# Masking
This layer supports masking for input data with a variable number
of timesteps. To introduce masks to your data,
use an [Embedding](embeddings.md) layer with the `mask_zero` parameter
use an `Embedding` layer with the `mask_zero` parameter
set to `True`.
**Note:** for the time being, masking is only supported with Theano.
@@ -105,9 +108,13 @@ class ConvRecurrent2D(Recurrent):
self.return_sequences = return_sequences
self.go_backwards = go_backwards
self.stateful = stateful
self.input_spec = InputSpec(ndim=5)
self.input_spec = [InputSpec(ndim=5)]
self.state_spec = None
def compute_output_shape(self, input_shape):
def _compute_output_shape(self, input_shape):
if type(input_shape) is list:
input_shape = input_shape[0]
input_shape = tensor_shape.TensorShape(input_shape).as_list()
if self.data_format == 'channels_first':
rows = input_shape[3]
cols = input_shape[4]
@@ -126,16 +133,28 @@ class ConvRecurrent2D(Recurrent):
dilation=self.dilation_rate[1])
if self.return_sequences:
if self.data_format == 'channels_first':
return (input_shape[0], input_shape[1],
self.filters, rows, cols)
return tensor_shape.TensorShape([input_shape[0],
input_shape[1],
self.filters,
rows,
cols])
elif self.data_format == 'channels_last':
return (input_shape[0], input_shape[1],
rows, cols, self.filters)
return tensor_shape.TensorShape([input_shape[0],
input_shape[1],
rows,
cols,
self.filters])
else:
if self.data_format == 'channels_first':
return (input_shape[0], self.filters, rows, cols)
return tensor_shape.TensorShape([input_shape[0],
self.filters,
rows,
cols])
elif self.data_format == 'channels_last':
return (input_shape[0], rows, cols, self.filters)
return tensor_shape.TensorShape([input_shape[0],
rows,
cols,
self.filters])
def get_config(self):
config = {'filters': self.filters,
@@ -181,46 +200,34 @@ class ConvLSTM2D(ConvRecurrent2D):
the dilation rate to use for dilated convolution.
Currently, specifying any `dilation_rate` value != 1 is
incompatible with specifying any `strides` value != 1.
activation: Activation function to use
(see [activations](../activations.md)).
activation: Activation function to use.
If you don't specify anything, no activation is applied
(ie. "linear" activation: `a(x) = x`).
recurrent_activation: Activation function to use
for the recurrent step
(see [activations](../activations.md)).
for the recurrent step.
use_bias: Boolean, whether the layer uses a bias vector.
kernel_initializer: Initializer for the `kernel` weights matrix,
used for the linear transformation of the inputs.
(see [initializers](../initializers.md)).
used for the linear transformation of the inputs..
recurrent_initializer: Initializer for the `recurrent_kernel`
weights matrix,
used for the linear transformation of the recurrent state.
(see [initializers](../initializers.md)).
bias_initializer: Initializer for the bias vector
(see [initializers](../initializers.md)).
used for the linear transformation of the recurrent state..
bias_initializer: Initializer for the bias vector.
unit_forget_bias: Boolean.
If True, add 1 to the bias of the forget gate at initialization.
Use in combination with `bias_initializer="zeros"`.
This is recommended in [Jozefowicz et al.](http://www.jmlr.org/proceedings/papers/v37/jozefowicz15.pdf)
kernel_regularizer: Regularizer function applied to
the `kernel` weights matrix
(see [regularizer](../regularizers.md)).
the `kernel` weights matrix.
recurrent_regularizer: Regularizer function applied to
the `recurrent_kernel` weights matrix
(see [regularizer](../regularizers.md)).
bias_regularizer: Regularizer function applied to the bias vector
(see [regularizer](../regularizers.md)).
the `recurrent_kernel` weights matrix.
bias_regularizer: Regularizer function applied to the bias vector.
activity_regularizer: Regularizer function applied to
the output of the layer (its "activation").
(see [regularizer](../regularizers.md)).
the output of the layer (its "activation")..
kernel_constraint: Constraint function applied to
the `kernel` weights matrix
(see [constraints](../constraints.md)).
the `kernel` weights matrix.
recurrent_constraint: Constraint function applied to
the `recurrent_kernel` weights matrix
(see [constraints](../constraints.md)).
bias_constraint: Constraint function applied to the bias vector
(see [constraints](../constraints.md)).
the `recurrent_kernel` weights matrix.
bias_constraint: Constraint function applied to the bias vector.
return_sequences: Boolean. Whether to return the last output
in the output sequence, or the full sequence.
go_backwards: Boolean (default False).
@@ -271,7 +278,6 @@ class ConvLSTM2D(ConvRecurrent2D):
cells output
"""
@interfaces.legacy_convlstm2d_support
def __init__(self, filters,
kernel_size,
strides=(1, 1),
@@ -328,10 +334,14 @@ class ConvLSTM2D(ConvRecurrent2D):
self.dropout = min(1., max(0., dropout))
self.recurrent_dropout = min(1., max(0., recurrent_dropout))
self.state_spec = [InputSpec(ndim=4), InputSpec(ndim=4)]
def build(self, input_shape):
# TODO: better handling of input spec
self.input_spec = InputSpec(shape=input_shape)
if isinstance(input_shape, list):
input_shape = input_shape[0]
input_shape = tuple(tensor_shape.TensorShape(input_shape).as_list())
batch_size = input_shape[0] if self.stateful else None
self.input_spec[0] = InputSpec(shape=(batch_size, None) + input_shape[2:])
if self.stateful:
self.reset_states()
@@ -347,23 +357,27 @@ class ConvLSTM2D(ConvRecurrent2D):
raise ValueError('The channel dimension of the inputs '
'should be defined. Found `None`.')
input_dim = input_shape[channel_axis]
state_shape = [None] * 4
state_shape[channel_axis] = input_dim
state_shape = tuple(state_shape)
self.state_spec = [InputSpec(shape=state_shape), InputSpec(shape=state_shape)]
kernel_shape = self.kernel_size + (input_dim, self.filters * 4)
self.kernel_shape = kernel_shape
recurrent_kernel_shape = self.kernel_size + (self.filters, self.filters * 4)
self.kernel = self.add_weight(kernel_shape,
self.kernel = self.add_weight(shape=kernel_shape,
initializer=self.kernel_initializer,
name='kernel',
regularizer=self.kernel_regularizer,
constraint=self.kernel_constraint)
self.recurrent_kernel = self.add_weight(
recurrent_kernel_shape,
shape=recurrent_kernel_shape,
initializer=self.recurrent_initializer,
name='recurrent_kernel',
regularizer=self.recurrent_regularizer,
constraint=self.recurrent_constraint)
if self.use_bias:
self.bias = self.add_weight((self.filters * 4,),
self.bias = self.add_weight(shape=(self.filters * 4,),
initializer=self.bias_initializer,
name='bias',
regularizer=self.bias_regularizer,
@@ -396,7 +410,7 @@ class ConvLSTM2D(ConvRecurrent2D):
self.bias_o = None
self.built = True
def get_initial_states(self, inputs):
def get_initial_state(self, inputs):
# (samples, timesteps, rows, cols, filters)
initial_state = K.zeros_like(inputs)
# (samples, rows, cols, filters)
@@ -413,8 +427,9 @@ class ConvLSTM2D(ConvRecurrent2D):
def reset_states(self):
if not self.stateful:
raise RuntimeError('Layer must be stateful.')
input_shape = self.input_spec.shape
output_shape = self.compute_output_shape(input_shape)
input_shape = self.input_spec[0].shape
output_shape = self._compute_output_shape(input_shape)
if not input_shape[0]:
raise ValueError('If a RNN is stateful, a complete '
'input_shape must be provided '
@@ -465,7 +480,7 @@ class ConvLSTM2D(ConvRecurrent2D):
padding=self.padding)
ones += 1.
def dropped_inputs():
def dropped_inputs(): # pylint: disable=function-redefined
return K.dropout(ones, self.recurrent_dropout)
rec_dp_mask = [K.in_train_phase(dropped_inputs,
ones,
+49 -154
Ver Arquivo
@@ -1,25 +1,26 @@
# -*- coding: utf-8 -*-
"""Core Keras layers.
"""
from __future__ import absolute_import
from __future__ import division
import numpy as np
from __future__ import print_function
import copy
import inspect
import types as python_types
import warnings
from .. import backend as K
from .. import activations
from .. import backend as K
from .. import constraints
from .. import initializers
from .. import regularizers
from .. import constraints
from ..engine import InputSpec
from ..engine import Layer
import numpy as np
from tensorflow.python.framework import tensor_shape
from ..utils.generic_utils import deserialize_keras_object
from ..utils.generic_utils import func_dump
from ..utils.generic_utils import func_load
from ..utils.generic_utils import deserialize_keras_object
from ..legacy import interfaces
class Masking(Layer):
@@ -85,11 +86,8 @@ class Dropout(Layer):
you want the dropout mask to be the same for all timesteps,
you can use `noise_shape=(batch_size, 1, features)`.
seed: A Python integer to use as random seed.
# References
- [Dropout: A Simple Way to Prevent Neural Networks from Overfitting](http://www.cs.toronto.edu/~rsalakhu/papers/srivastava14a.pdf)
"""
@interfaces.legacy_dropout_support
def __init__(self, rate, noise_shape=None, seed=None, **kwargs):
super(Dropout, self).__init__(**kwargs)
self.rate = min(1., max(0., rate))
@@ -142,7 +140,6 @@ class SpatialDropout1D(Dropout):
- [Efficient Object Localization Using Convolutional Networks](https://arxiv.org/abs/1411.4280)
"""
@interfaces.legacy_spatialdropout1d_support
def __init__(self, rate, **kwargs):
super(SpatialDropout1D, self).__init__(rate, **kwargs)
self.input_spec = InputSpec(ndim=3)
@@ -187,7 +184,6 @@ class SpatialDropout2D(Dropout):
- [Efficient Object Localization Using Convolutional Networks](https://arxiv.org/abs/1411.4280)
"""
@interfaces.legacy_spatialdropoutNd_support
def __init__(self, rate, data_format=None, **kwargs):
super(SpatialDropout2D, self).__init__(rate, **kwargs)
if data_format is None:
@@ -242,7 +238,6 @@ class SpatialDropout3D(Dropout):
- [Efficient Object Localization Using Convolutional Networks](https://arxiv.org/abs/1411.4280)
"""
@interfaces.legacy_spatialdropoutNd_support
def __init__(self, rate, data_format=None, **kwargs):
super(SpatialDropout3D, self).__init__(rate, **kwargs)
if data_format is None:
@@ -269,7 +264,6 @@ class Activation(Layer):
# Arguments
activation: name of activation function to use
(see: [activations](../activations.md)),
or alternatively, a Theano or TensorFlow operation.
# Input shape
@@ -377,30 +371,24 @@ class Reshape(Layer):
output_shape[unknown] = original // known
elif original != known:
raise ValueError(msg)
return output_shape
return tuple(output_shape)
def compute_output_shape(self, input_shape):
return (input_shape[0],) + self._fix_unknown_dimension(
input_shape[1:], self.target_shape)
def _compute_output_shape(self, input_shape):
input_shape = tensor_shape.TensorShape(input_shape).as_list()
output_shape = [input_shape[0]]
output_shape += self._fix_unknown_dimension(input_shape[1:],
self.target_shape)
return tensor_shape.TensorShape(output_shape)
def call(self, inputs):
# In case the target shape is not fully defined,
# we need access to the shape of x.
# solution:
# 1) rely on x._keras_shape
# 2) fallback: K.int_shape
target_shape = self.target_shape
if -1 in target_shape:
# target shape not fully defined
input_shape = None
try:
input_shape = K.int_shape(inputs)
except TypeError:
pass
if input_shape is not None:
target_shape = self.compute_output_shape(input_shape)[1:]
return K.reshape(inputs, (-1,) + target_shape)
target_shape = self._compute_output_shape(inputs.get_shape())
target_shape = target_shape.as_list()[1:]
return K.reshape(inputs, (-1,) + tuple(target_shape))
def get_config(self):
config = {'target_shape': self.target_shape}
@@ -443,13 +431,13 @@ class Permute(Layer):
self.dims = tuple(dims)
self.input_spec = InputSpec(ndim=len(self.dims) + 1)
def compute_output_shape(self, input_shape):
input_shape = list(input_shape)
def _compute_output_shape(self, input_shape):
input_shape = tensor_shape.TensorShape(input_shape).as_list()
output_shape = copy.copy(input_shape)
for i, dim in enumerate(self.dims):
target_dim = input_shape[dim]
output_shape[i + 1] = target_dim
return tuple(output_shape)
return tensor_shape.TensorShape(output_shape)
def call(self, inputs):
return K.permute_dimensions(inputs, (0,) + self.dims)
@@ -481,7 +469,8 @@ class Flatten(Layer):
super(Flatten, self).__init__(**kwargs)
self.input_spec = InputSpec(min_ndim=3)
def compute_output_shape(self, input_shape):
def _compute_output_shape(self, input_shape):
input_shape = tensor_shape.TensorShape(input_shape).as_list()
if not all(input_shape[1:]):
raise ValueError('The shape of the input to "Flatten" '
'is not fully defined '
@@ -489,10 +478,12 @@ class Flatten(Layer):
'Make sure to pass a complete "input_shape" '
'or "batch_input_shape" argument to the first '
'layer in your model.')
return (input_shape[0], np.prod(input_shape[1:]))
return tensor_shape.TensorShape([input_shape[0], np.prod(input_shape[1:])])
def call(self, inputs):
return K.batch_flatten(inputs)
outputs = K.batch_flatten(inputs)
outputs.set_shape(self._compute_output_shape(inputs.get_shape()))
return outputs
class RepeatVector(Layer):
@@ -525,8 +516,9 @@ class RepeatVector(Layer):
self.n = n
self.input_spec = InputSpec(ndim=2)
def compute_output_shape(self, input_shape):
return (input_shape[0], self.n, input_shape[1])
def _compute_output_shape(self, input_shape):
input_shape = tensor_shape.TensorShape(input_shape).as_list()
return tensor_shape.TensorShape([input_shape[0], self.n, input_shape[1]])
def call(self, inputs):
return K.repeat(inputs, self.n)
@@ -558,30 +550,12 @@ class Lambda(Layer):
neg = K.relu(-x)
return K.concatenate([pos, neg], axis=1)
def antirectifier_output_shape(input_shape):
shape = list(input_shape)
assert len(shape) == 2 # only valid for 2D tensors
shape[-1] *= 2
return tuple(shape)
model.add(Lambda(antirectifier,
output_shape=antirectifier_output_shape))
model.add(Lambda(antirectifier))
```
# Arguments
function: The function to be evaluated.
Takes input tensor as first argument.
output_shape: Expected output shape from function.
Only relevant when using Theano.
Can be a tuple or function.
If a tuple, it only specifies the first dimension onward;
sample dimension is assumed either the same as the input:
`output_shape = (input_shape[0], ) + output_shape`
or, the input is `None` and
the sample dimension is also `None`:
`output_shape = (None, ) + output_shape`
If a function, it specifies the entire shape as a function of the
input shape: `output_shape = f(input_shape)`
arguments: optional dictionary of keyword arguments to be passed
to the function.
@@ -595,9 +569,10 @@ class Lambda(Layer):
(or auto-inferred when using TensorFlow).
"""
@interfaces.legacy_lambda_support
def __init__(self, function, output_shape=None,
mask=None, arguments=None, **kwargs):
def __init__(self, function,
mask=None,
arguments=None,
**kwargs):
super(Lambda, self).__init__(**kwargs)
self.function = function
self.arguments = arguments if arguments else {}
@@ -605,52 +580,6 @@ class Lambda(Layer):
self.supports_masking = True
self.mask = mask
if output_shape is None:
self._output_shape = None
elif isinstance(output_shape, (tuple, list)):
self._output_shape = tuple(output_shape)
else:
if not callable(output_shape):
raise TypeError('In Lambda, `output_shape` '
'must be a list, a tuple, or a function.')
self._output_shape = output_shape
def compute_output_shape(self, input_shape):
if self._output_shape is None:
# With TensorFlow, we can infer the output shape directly:
if K.backend() == 'tensorflow':
if isinstance(input_shape, list):
xs = [K.placeholder(shape=shape) for shape in input_shape]
x = self.call(xs)
else:
x = K.placeholder(shape=input_shape)
x = self.call(x)
if isinstance(x, list):
return [K.int_shape(x_elem) for x_elem in x]
else:
return K.int_shape(x)
# Otherwise, we default to the input shape.
warnings.warn('`output_shape` argument not specified for layer {} '
'and cannot be automatically inferred '
'with the Theano backend. '
'Defaulting to output shape `{}` '
'(same as input shape). '
'If the expected output shape is different, '
'specify it via the `output_shape` argument.'
.format(self.name, input_shape))
return input_shape
elif isinstance(self._output_shape, (tuple, list)):
if isinstance(input_shape, list):
num_samples = input_shape[0][0]
else:
num_samples = input_shape[0] if input_shape else None
return (num_samples,) + tuple(self._output_shape)
else:
shape = self._output_shape(input_shape)
if not isinstance(shape, (list, tuple)):
raise ValueError('output_shape function must return a tuple')
return tuple(shape)
def call(self, inputs, mask=None):
arguments = self.arguments
arg_spec = inspect.getargspec(self.function)
@@ -671,20 +600,8 @@ class Lambda(Layer):
function = self.function.__name__
function_type = 'function'
if isinstance(self._output_shape, python_types.LambdaType):
output_shape = func_dump(self._output_shape)
output_shape_type = 'lambda'
elif callable(self._output_shape):
output_shape = self._output_shape.__name__
output_shape_type = 'function'
else:
output_shape = self._output_shape
output_shape_type = 'raw'
config = {'function': function,
'function_type': function_type,
'output_shape': output_shape,
'output_shape_type': output_shape_type,
'arguments': self.arguments}
base_config = super(Lambda, self).get_config()
return dict(list(base_config.items()) + list(config.items()))
@@ -707,21 +624,7 @@ class Lambda(Layer):
else:
raise TypeError('Unknown function type:', function_type)
output_shape_type = config.pop('output_shape_type')
if output_shape_type == 'function':
# Simple lookup in custom objects
output_shape = deserialize_keras_object(
config['output_shape'],
custom_objects=custom_objects,
printable_module_name='output_shape function in Lambda layer')
elif output_shape_type == 'lambda':
# Unsafe deserialization from bytecode
output_shape = func_load(config['output_shape'], globs=globs)
else:
output_shape = config['output_shape']
config['function'] = function
config['output_shape'] = output_shape
return cls(**config)
@@ -754,28 +657,20 @@ class Dense(Layer):
# Arguments
units: Positive integer, dimensionality of the output space.
activation: Activation function to use
(see [activations](../activations.md)).
activation: Activation function to use.
If you don't specify anything, no activation is applied
(ie. "linear" activation: `a(x) = x`).
use_bias: Boolean, whether the layer uses a bias vector.
kernel_initializer: Initializer for the `kernel` weights matrix
(see [initializers](../initializers.md)).
bias_initializer: Initializer for the bias vector
(see [initializers](../initializers.md)).
kernel_initializer: Initializer for the `kernel` weights matrix.
bias_initializer: Initializer for the bias vector.
kernel_regularizer: Regularizer function applied to
the `kernel` weights matrix
(see [regularizer](../regularizers.md)).
bias_regularizer: Regularizer function applied to the bias vector
(see [regularizer](../regularizers.md)).
the `kernel` weights matrix.
bias_regularizer: Regularizer function applied to the bias vector.
activity_regularizer: Regularizer function applied to
the output of the layer (its "activation").
(see [regularizer](../regularizers.md)).
the output of the layer (its "activation")..
kernel_constraint: Constraint function applied to
the `kernel` weights matrix
(see [constraints](../constraints.md)).
bias_constraint: Constraint function applied to the bias vector
(see [constraints](../constraints.md)).
the `kernel` weights matrix.
bias_constraint: Constraint function applied to the bias vector.
# Input shape
nD tensor with shape: `(batch_size, ..., input_dim)`.
@@ -788,7 +683,6 @@ class Dense(Layer):
the output would have shape `(batch_size, units)`.
"""
@interfaces.legacy_dense_support
def __init__(self, units,
activation=None,
use_bias=True,
@@ -820,13 +714,13 @@ class Dense(Layer):
assert len(input_shape) >= 2
input_dim = input_shape[-1]
self.kernel = self.add_weight((input_dim, self.units),
self.kernel = self.add_weight(shape=(input_dim, self.units),
initializer=self.kernel_initializer,
name='kernel',
regularizer=self.kernel_regularizer,
constraint=self.kernel_constraint)
if self.use_bias:
self.bias = self.add_weight((self.units,),
self.bias = self.add_weight(shape=(self.units,),
initializer=self.bias_initializer,
name='bias',
regularizer=self.bias_regularizer,
@@ -844,12 +738,13 @@ class Dense(Layer):
output = self.activation(output)
return output
def compute_output_shape(self, input_shape):
def _compute_output_shape(self, input_shape):
input_shape = tensor_shape.TensorShape(input_shape).as_list()
assert input_shape and len(input_shape) >= 2
assert input_shape[-1]
output_shape = list(input_shape)
output_shape[-1] = self.units
return tuple(output_shape)
return tensor_shape.TensorShape(output_shape)
def get_config(self):
config = {
+18 -14
Ver Arquivo
@@ -1,11 +1,15 @@
"""Embedding layer.
"""
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
from .. import backend as K
from .. import constraints
from .. import initializers
from .. import regularizers
from .. import constraints
from ..engine import Layer
from ..legacy import interfaces
from tensorflow.python.framework import tensor_shape
class Embedding(Layer):
@@ -34,18 +38,15 @@ class Embedding(Layer):
input_dim: int > 0. Size of the vocabulary,
i.e. maximum integer index + 1.
output_dim: int >= 0. Dimension of the dense embedding.
embeddings_initializer: Initializer for the `embeddings` matrix
(see [initializers](../initializers.md)).
embeddings_initializer: Initializer for the `embeddings` matrix.
embeddings_regularizer: Regularizer function applied to
the `embeddings` matrix
(see [regularizer](../regularizers.md)).
the `embeddings` matrix.
embeddings_constraint: Constraint function applied to
the `embeddings` matrix
(see [constraints](../constraints.md)).
the `embeddings` matrix.
mask_zero: Whether or not the input value 0 is a special "padding"
value that should be masked out.
This is useful when using [recurrent layers](recurrent.md)
which may take variable length input.
This is useful when using recurrent layers,
which may take variable length inputs.
If this is `True` then all subsequent layers
in the model need to support masking or an exception will be raised.
If mask_zero is set to True, as a consequence, index 0 cannot be
@@ -66,7 +67,6 @@ class Embedding(Layer):
- [A Theoretically Grounded Application of Dropout in Recurrent Neural Networks](http://arxiv.org/abs/1512.05287)
"""
@interfaces.legacy_embedding_support
def __init__(self, input_dim, output_dim,
embeddings_initializer='uniform',
embeddings_regularizer=None,
@@ -93,8 +93,9 @@ class Embedding(Layer):
self.input_length = input_length
def build(self, input_shape):
input_shape = tensor_shape.TensorShape(input_shape).as_list()
self.embeddings = self.add_weight(
(self.input_dim, self.output_dim),
shape=(self.input_dim, self.output_dim),
initializer=self.embeddings_initializer,
name='embeddings',
regularizer=self.embeddings_regularizer,
@@ -107,12 +108,15 @@ class Embedding(Layer):
else:
return K.not_equal(inputs, 0)
def compute_output_shape(self, input_shape):
def _compute_output_shape(self, input_shape):
input_shape = tensor_shape.TensorShape(input_shape).as_list()
if not self.input_length:
input_length = input_shape[1]
else:
input_length = self.input_length
return (input_shape[0], input_length, self.output_dim)
return tensor_shape.TensorShape([input_shape[0],
input_length,
self.output_dim])
def call(self, inputs):
if K.dtype(inputs) != 'int32':
+38 -48
Ver Arquivo
@@ -1,15 +1,19 @@
"""Locally-connected layers.
"""
# -*- coding: utf-8 -*-
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
from .. import backend as K
from .. import activations
from .. import backend as K
from .. import constraints
from .. import initializers
from .. import regularizers
from .. import constraints
from ..engine import Layer
from ..engine import InputSpec
from ..engine import Layer
from tensorflow.python.framework import tensor_shape
from ..utils import conv_utils
from ..legacy import interfaces
class LocallyConnected1D(Layer):
@@ -43,27 +47,19 @@ class LocallyConnected1D(Layer):
any `dilation_rate` value != 1.
padding: Currently only supports `"valid"` (case-insensitive).
`"same"` may be supported in the future.
activation: Activation function to use
(see [activations](../activations.md)).
activation: Activation function to use.
If you don't specify anything, no activation is applied
(ie. "linear" activation: `a(x) = x`).
use_bias: Boolean, whether the layer uses a bias vector.
kernel_initializer: Initializer for the `kernel` weights matrix
(see [initializers](../initializers.md)).
bias_initializer: Initializer for the bias vector
(see [initializers](../initializers.md)).
kernel_initializer: Initializer for the `kernel` weights matrix.
bias_initializer: Initializer for the bias vector.
kernel_regularizer: Regularizer function applied to
the `kernel` weights matrix
(see [regularizer](../regularizers.md)).
bias_regularizer: Regularizer function applied to the bias vector
(see [regularizer](../regularizers.md)).
the `kernel` weights matrix.
bias_regularizer: Regularizer function applied to the bias vector.
activity_regularizer: Regularizer function applied to
the output of the layer (its "activation").
(see [regularizer](../regularizers.md)).
kernel_constraint: Constraint function applied to the kernel matrix
(see [constraints](../constraints.md)).
bias_constraint: Constraint function applied to the bias vector
(see [constraints](../constraints.md)).
the output of the layer (its "activation")..
kernel_constraint: Constraint function applied to the kernel matrix.
bias_constraint: Constraint function applied to the bias vector.
# Input shape
3D tensor with shape: `(batch_size, steps, input_dim)`
@@ -73,7 +69,6 @@ class LocallyConnected1D(Layer):
`steps` value might have changed due to padding or strides.
"""
@interfaces.legacy_conv1d_support
def __init__(self, filters,
kernel_size,
strides=1,
@@ -110,6 +105,7 @@ class LocallyConnected1D(Layer):
self.input_spec = InputSpec(ndim=3)
def build(self, input_shape):
input_shape = tensor_shape.TensorShape(input_shape).as_list()
input_dim = input_shape[2]
if input_dim is None:
raise ValueError('Axis 2 of input should be fully-defined. '
@@ -122,14 +118,14 @@ class LocallyConnected1D(Layer):
self.kernel_size[0] * input_dim,
self.filters)
self.kernel = self.add_weight(
self.kernel_shape,
shape=self.kernel_shape,
initializer=self.kernel_initializer,
name='kernel',
regularizer=self.kernel_regularizer,
constraint=self.kernel_constraint)
if self.use_bias:
self.bias = self.add_weight(
(output_length, self.filters),
shape=(output_length, self.filters),
initializer=self.bias_initializer,
name='bias',
regularizer=self.bias_regularizer,
@@ -139,12 +135,13 @@ class LocallyConnected1D(Layer):
self.input_spec = InputSpec(ndim=3, axes={2: input_dim})
self.built = True
def compute_output_shape(self, input_shape):
def _compute_output_shape(self, input_shape):
input_shape = tensor_shape.TensorShape(input_shape).as_list()
length = conv_utils.conv_output_length(input_shape[1],
self.kernel_size[0],
self.padding,
self.strides[0])
return (input_shape[0], length, self.filters)
return tensor_shape.TensorShape([input_shape[0], length, self.filters])
def call(self, inputs):
stride = self.strides[0]
@@ -232,27 +229,19 @@ class LocallyConnected2D(Layer):
It defaults to the `image_data_format` value found in your
Keras config file at `~/.keras/keras.json`.
If you never set it, then it will be "channels_last".
activation: Activation function to use
(see [activations](../activations.md)).
activation: Activation function to use.
If you don't specify anything, no activation is applied
(ie. "linear" activation: `a(x) = x`).
use_bias: Boolean, whether the layer uses a bias vector.
kernel_initializer: Initializer for the `kernel` weights matrix
(see [initializers](../initializers.md)).
bias_initializer: Initializer for the bias vector
(see [initializers](../initializers.md)).
kernel_initializer: Initializer for the `kernel` weights matrix.
bias_initializer: Initializer for the bias vector.
kernel_regularizer: Regularizer function applied to
the `kernel` weights matrix
(see [regularizer](../regularizers.md)).
bias_regularizer: Regularizer function applied to the bias vector
(see [regularizer](../regularizers.md)).
the `kernel` weights matrix.
bias_regularizer: Regularizer function applied to the bias vector.
activity_regularizer: Regularizer function applied to
the output of the layer (its "activation").
(see [regularizer](../regularizers.md)).
kernel_constraint: Constraint function applied to the kernel matrix
(see [constraints](../constraints.md)).
bias_constraint: Constraint function applied to the bias vector
(see [constraints](../constraints.md)).
the output of the layer (its "activation")..
kernel_constraint: Constraint function applied to the kernel matrix.
bias_constraint: Constraint function applied to the bias vector.
# Input shape
4D tensor with shape:
@@ -268,7 +257,6 @@ class LocallyConnected2D(Layer):
`rows` and `cols` values might have changed due to padding.
"""
@interfaces.legacy_conv2d_support
def __init__(self, filters,
kernel_size,
strides=(1, 1),
@@ -305,6 +293,7 @@ class LocallyConnected2D(Layer):
self.input_spec = InputSpec(ndim=4)
def build(self, input_shape):
input_shape = tensor_shape.TensorShape(input_shape).as_list()
if self.data_format == 'channels_last':
input_row, input_col = input_shape[1:-1]
input_filter = input_shape[3]
@@ -316,6 +305,7 @@ class LocallyConnected2D(Layer):
' a LocallyConnected2D layer '
'should be fully-defined, but layer received '
'the inputs shape ' + str(input_shape))
output_row = conv_utils.conv_output_length(input_row, self.kernel_size[0],
self.padding, self.strides[0])
output_col = conv_utils.conv_output_length(input_col, self.kernel_size[1],
@@ -325,13 +315,13 @@ class LocallyConnected2D(Layer):
self.kernel_shape = (output_row * output_col,
self.kernel_size[0] * self.kernel_size[1] * input_filter,
self.filters)
self.kernel = self.add_weight(self.kernel_shape,
self.kernel = self.add_weight(shape=self.kernel_shape,
initializer=self.kernel_initializer,
name='kernel',
regularizer=self.kernel_regularizer,
constraint=self.kernel_constraint)
if self.use_bias:
self.bias = self.add_weight((output_row, output_col, self.filters),
self.bias = self.add_weight(shape=(output_row, output_col, self.filters),
initializer=self.bias_initializer,
name='bias',
regularizer=self.bias_regularizer,
@@ -344,23 +334,23 @@ class LocallyConnected2D(Layer):
self.input_spec = InputSpec(ndim=4, axes={-1: input_filter})
self.built = True
def compute_output_shape(self, input_shape):
def _compute_output_shape(self, input_shape):
input_shape = tensor_shape.TensorShape(input_shape).as_list()
if self.data_format == 'channels_first':
rows = input_shape[2]
cols = input_shape[3]
elif self.data_format == 'channels_last':
rows = input_shape[1]
cols = input_shape[2]
rows = conv_utils.conv_output_length(rows, self.kernel_size[0],
self.padding, self.strides[0])
cols = conv_utils.conv_output_length(cols, self.kernel_size[1],
self.padding, self.strides[1])
if self.data_format == 'channels_first':
return (input_shape[0], self.filters, rows, cols)
return tensor_shape.TensorShape([input_shape[0], self.filters, rows, cols])
elif self.data_format == 'channels_last':
return (input_shape[0], rows, cols, self.filters)
return tensor_shape.TensorShape([input_shape[0], rows, cols, self.filters])
def call(self, inputs):
stride_row, stride_col = self.strides
+20 -12
Ver Arquivo
@@ -1,5 +1,12 @@
from ..engine.topology import Layer
"""Layers can merge several input tensors into a single output tensor.
"""
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
from .. import backend as K
from ..engine.topology import Layer
from tensorflow.python.framework import tensor_shape
class _Merge(Layer):
@@ -38,7 +45,7 @@ class _Merge(Layer):
return None
elif len(shape1) < len(shape2):
return self._compute_elemwise_op_output_shape(shape2, shape1)
elif len(shape2) == 0:
elif not shape2:
return shape1
output_shape = list(shape1[:-len(shape2)])
for i, j in zip(shape1[-len(shape2):], shape2):
@@ -265,7 +272,7 @@ class Concatenate(_Merge):
'on a list of inputs')
if all([shape is None for shape in input_shape]):
return
reduced_inputs_shapes = [list(shape) for shape in input_shape]
reduced_inputs_shapes = [tensor_shape.TensorShape(shape).as_list() for shape in input_shape]
shape_set = set()
for i in range(len(reduced_inputs_shapes)):
del reduced_inputs_shapes[i][self.axis]
@@ -282,18 +289,19 @@ class Concatenate(_Merge):
'on a list of inputs.')
return K.concatenate(inputs, axis=self.axis)
def compute_output_shape(self, input_shape):
def _compute_output_shape(self, input_shape):
if not isinstance(input_shape, list):
raise ValueError('A `Concatenate` layer should be called '
'on a list of inputs.')
input_shapes = input_shape
output_shape = list(input_shapes[0])
output_shape = tensor_shape.TensorShape(input_shapes[0]).as_list()
for shape in input_shapes[1:]:
shape = tensor_shape.TensorShape(shape).as_list()
if output_shape[self.axis] is None or shape[self.axis] is None:
output_shape[self.axis] = None
break
output_shape[self.axis] += shape[self.axis]
return tuple(output_shape)
return tensor_shape.TensorShape(output_shape)
def compute_mask(self, inputs, mask=None):
if mask is None:
@@ -371,8 +379,8 @@ class Dot(_Merge):
if not isinstance(input_shape, list) or len(input_shape) != 2:
raise ValueError('A `Dot` layer should be called '
'on a list of 2 inputs.')
shape1 = input_shape[0]
shape2 = input_shape[1]
shape1 = tensor_shape.TensorShape(input_shape[0]).as_list()
shape2 = tensor_shape.TensorShape(input_shape[1]).as_list()
if shape1 is None or shape2 is None:
return
if isinstance(self.axes, int):
@@ -409,12 +417,12 @@ class Dot(_Merge):
output = K.batch_dot(x1, x2, axes)
return output
def compute_output_shape(self, input_shape):
def _compute_output_shape(self, input_shape):
if not isinstance(input_shape, list) or len(input_shape) != 2:
raise ValueError('A `Dot` layer should be called '
'on a list of 2 inputs.')
shape1 = list(input_shape[0])
shape2 = list(input_shape[1])
shape1 = tensor_shape.TensorShape(input_shape[0]).as_list()
shape2 = tensor_shape.TensorShape(input_shape[1]).as_list()
if isinstance(self.axes, int):
if self.axes < 0:
axes = [self.axes % len(shape1), self.axes % len(shape2)]
@@ -428,7 +436,7 @@ class Dot(_Merge):
output_shape = shape1 + shape2
if len(output_shape) == 1:
output_shape += [1]
return tuple(output_shape)
return tensor_shape.TensorShape(output_shape)
def compute_mask(self, inputs, mask=None):
return None
+5 -4
Ver Arquivo
@@ -1,10 +1,13 @@
# -*- coding: utf-8 -*-
"""Layers for regularization models via the addition of noise.
"""
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
from ..engine import Layer
from .. import backend as K
from ..engine import Layer
import numpy as np
from ..legacy import interfaces
class GaussianNoise(Layer):
@@ -29,7 +32,6 @@ class GaussianNoise(Layer):
Same shape as input.
"""
@interfaces.legacy_gaussiannoise_support
def __init__(self, stddev, **kwargs):
super(GaussianNoise, self).__init__(**kwargs)
self.supports_masking = True
@@ -70,7 +72,6 @@ class GaussianDropout(Layer):
- [Dropout: A Simple Way to Prevent Neural Networks from Overfitting Srivastava, Hinton, et al. 2014](http://www.cs.toronto.edu/~rsalakhu/papers/srivastava14a.pdf)
"""
@interfaces.legacy_gaussiandropout_support
def __init__(self, rate, **kwargs):
super(GaussianDropout, self).__init__(**kwargs)
self.supports_masking = True
+61 -54
Ver Arquivo
@@ -1,12 +1,17 @@
# -*- coding: utf-8 -*-
"""Normalization layers.
"""
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
from ..engine import Layer, InputSpec
from .. import backend as K
from .. import constraints
from .. import initializers
from .. import regularizers
from .. import constraints
from .. import backend as K
from ..legacy import interfaces
from ..engine import InputSpec
from ..engine import Layer
from tensorflow.python.framework import tensor_shape
class BatchNormalization(Layer):
@@ -52,7 +57,6 @@ class BatchNormalization(Layer):
- [Batch Normalization: Accelerating Deep Network Training by Reducing Internal Covariate Shift](https://arxiv.org/abs/1502.03167)
"""
@interfaces.legacy_batchnorm_support
def __init__(self,
axis=-1,
momentum=0.99,
@@ -85,6 +89,7 @@ class BatchNormalization(Layer):
self.gamma_constraint = constraints.get(gamma_constraint)
def build(self, input_shape):
input_shape = tensor_shape.TensorShape(input_shape).as_list()
dim = input_shape[self.axis]
if dim is None:
raise ValueError('Axis ' + str(self.axis) + ' of '
@@ -96,7 +101,7 @@ class BatchNormalization(Layer):
shape = (dim,)
if self.scale:
self.gamma = self.add_weight(shape,
self.gamma = self.add_weight(shape=shape,
name='gamma',
initializer=self.gamma_initializer,
regularizer=self.gamma_regularizer,
@@ -104,7 +109,7 @@ class BatchNormalization(Layer):
else:
self.gamma = None
if self.center:
self.beta = self.add_weight(shape,
self.beta = self.add_weight(shape=shape,
name='beta',
initializer=self.beta_initializer,
regularizer=self.beta_regularizer,
@@ -112,19 +117,19 @@ class BatchNormalization(Layer):
else:
self.beta = None
self.moving_mean = self.add_weight(
shape,
shape=shape,
name='moving_mean',
initializer=self.moving_mean_initializer,
trainable=False)
self.moving_variance = self.add_weight(
shape,
shape=shape,
name='moving_variance',
initializer=self.moving_variance_initializer,
trainable=False)
self.built = True
def call(self, inputs, training=None):
input_shape = K.int_shape(inputs)
input_shape = inputs.get_shape().as_list()
# Prepare broadcasting shape.
ndim = len(input_shape)
reduction_axes = list(range(len(input_shape)))
@@ -135,55 +140,57 @@ class BatchNormalization(Layer):
# Determines whether broadcasting is needed.
needs_broadcasting = (sorted(reduction_axes) != list(range(ndim))[:-1])
normed, mean, variance = K.normalize_batch_in_training(
def normalize_inference():
if needs_broadcasting:
# In this case we must explictly broadcast all parameters.
broadcast_moving_mean = K.reshape(self.moving_mean,
broadcast_shape)
broadcast_moving_variance = K.reshape(self.moving_variance,
broadcast_shape)
if self.center:
broadcast_beta = K.reshape(self.beta, broadcast_shape)
else:
broadcast_beta = None
if self.scale:
broadcast_gamma = K.reshape(self.gamma,
broadcast_shape)
else:
broadcast_gamma = None
return K.batch_normalization(
inputs,
broadcast_moving_mean,
broadcast_moving_variance,
broadcast_beta,
broadcast_gamma,
epsilon=self.epsilon)
else:
return K.batch_normalization(
inputs,
self.moving_mean,
self.moving_variance,
self.beta,
self.gamma,
epsilon=self.epsilon)
# If the learning phase is *static* and set to inference:
if training in {0, False}:
return normalize_inference()
# If the learning is either dynamic, or set to training:
normed_training, mean, variance = K.normalize_batch_in_training(
inputs, self.gamma, self.beta, reduction_axes,
epsilon=self.epsilon)
if training in {0, False}:
return normed
else:
self.add_update([K.moving_average_update(self.moving_mean,
mean,
self.momentum),
K.moving_average_update(self.moving_variance,
variance,
self.momentum)],
inputs)
def normalize_inference():
if needs_broadcasting:
# In this case we must explictly broadcast all parameters.
broadcast_moving_mean = K.reshape(self.moving_mean,
broadcast_shape)
broadcast_moving_variance = K.reshape(self.moving_variance,
broadcast_shape)
if self.center:
broadcast_beta = K.reshape(self.beta, broadcast_shape)
else:
broadcast_beta = None
if self.scale:
broadcast_gamma = K.reshape(self.gamma,
broadcast_shape)
else:
broadcast_gamma = None
return K.batch_normalization(
inputs,
broadcast_moving_mean,
broadcast_moving_variance,
broadcast_beta,
broadcast_gamma,
epsilon=self.epsilon)
else:
return K.batch_normalization(
inputs,
self.moving_mean,
self.moving_variance,
self.beta,
self.gamma,
epsilon=self.epsilon)
self.add_update([K.moving_average_update(self.moving_mean,
mean,
self.momentum),
K.moving_average_update(self.moving_variance,
variance,
self.momentum)],
inputs)
# Pick the normalized form corresponding to the training phase.
return K.in_train_phase(normed,
return K.in_train_phase(normed_training,
normalize_inference,
training=training)
+40 -34
Ver Arquivo
@@ -1,11 +1,15 @@
# -*- coding: utf-8 -*-
"""Pooling layers.
"""
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
from .. import backend as K
from ..engine import Layer
from ..engine import InputSpec
from ..engine import Layer
from tensorflow.python.framework import tensor_shape
from ..utils import conv_utils
from ..legacy import interfaces
class _Pooling1D(Layer):
@@ -22,12 +26,13 @@ class _Pooling1D(Layer):
self.padding = conv_utils.normalize_padding(padding)
self.input_spec = InputSpec(ndim=3)
def compute_output_shape(self, input_shape):
def _compute_output_shape(self, input_shape):
input_shape = tensor_shape.TensorShape(input_shape).as_list()
length = conv_utils.conv_output_length(input_shape[1],
self.pool_size[0],
self.padding,
self.strides[0])
return (input_shape[0], length, input_shape[2])
return tensor_shape.TensorShape([input_shape[0], length, input_shape[2]])
def _pooling_function(self, inputs, pool_size, strides,
padding, data_format):
@@ -67,7 +72,6 @@ class MaxPooling1D(_Pooling1D):
3D tensor with shape: `(batch_size, downsampled_steps, features)`.
"""
@interfaces.legacy_pooling1d_support
def __init__(self, pool_size=2, strides=None,
padding='valid', **kwargs):
super(MaxPooling1D, self).__init__(pool_size, strides,
@@ -97,7 +101,6 @@ class AveragePooling1D(_Pooling1D):
3D tensor with shape: `(batch_size, downsampled_steps, features)`.
"""
@interfaces.legacy_pooling1d_support
def __init__(self, pool_size=2, strides=None,
padding='valid', **kwargs):
super(AveragePooling1D, self).__init__(pool_size, strides,
@@ -126,11 +129,12 @@ class _Pooling2D(Layer):
self.data_format = conv_utils.normalize_data_format(data_format)
self.input_spec = InputSpec(ndim=4)
def compute_output_shape(self, input_shape):
def _compute_output_shape(self, input_shape):
input_shape = tensor_shape.TensorShape(input_shape).as_list()
if self.data_format == 'channels_first':
rows = input_shape[2]
cols = input_shape[3]
elif self.data_format == 'channels_last':
else:
rows = input_shape[1]
cols = input_shape[2]
rows = conv_utils.conv_output_length(rows, self.pool_size[0],
@@ -138,9 +142,9 @@ class _Pooling2D(Layer):
cols = conv_utils.conv_output_length(cols, self.pool_size[1],
self.padding, self.strides[1])
if self.data_format == 'channels_first':
return (input_shape[0], input_shape[1], rows, cols)
elif self.data_format == 'channels_last':
return (input_shape[0], rows, cols, input_shape[3])
return tensor_shape.TensorShape([input_shape[0], input_shape[1], rows, cols])
else:
return tensor_shape.TensorShape([input_shape[0], rows, cols, input_shape[3]])
def _pooling_function(self, inputs, pool_size, strides,
padding, data_format):
@@ -204,7 +208,6 @@ class MaxPooling2D(_Pooling2D):
`(batch_size, channels, pooled_rows, pooled_cols)`
"""
@interfaces.legacy_pooling2d_support
def __init__(self, pool_size=(2, 2), strides=None, padding='valid',
data_format=None, **kwargs):
super(MaxPooling2D, self).__init__(pool_size, strides, padding,
@@ -259,7 +262,6 @@ class AveragePooling2D(_Pooling2D):
`(batch_size, channels, pooled_rows, pooled_cols)`
"""
@interfaces.legacy_pooling2d_support
def __init__(self, pool_size=(2, 2), strides=None, padding='valid',
data_format=None, **kwargs):
super(AveragePooling2D, self).__init__(pool_size, strides, padding,
@@ -287,12 +289,13 @@ class _Pooling3D(Layer):
self.data_format = conv_utils.normalize_data_format(data_format)
self.input_spec = InputSpec(ndim=5)
def compute_output_shape(self, input_shape):
def _compute_output_shape(self, input_shape):
input_shape = tensor_shape.TensorShape(input_shape).as_list()
if self.data_format == 'channels_first':
len_dim1 = input_shape[2]
len_dim2 = input_shape[3]
len_dim3 = input_shape[4]
elif self.data_format == 'channels_last':
else:
len_dim1 = input_shape[1]
len_dim2 = input_shape[2]
len_dim3 = input_shape[3]
@@ -303,13 +306,17 @@ class _Pooling3D(Layer):
len_dim3 = conv_utils.conv_output_length(len_dim3, self.pool_size[2],
self.padding, self.strides[2])
if self.data_format == 'channels_first':
return (input_shape[0],
input_shape[1],
len_dim1, len_dim2, len_dim3)
elif self.data_format == 'channels_last':
return (input_shape[0],
len_dim1, len_dim2, len_dim3,
input_shape[4])
return tensor_shape.TensorShape([input_shape[0],
input_shape[1],
len_dim1,
len_dim2,
len_dim3])
else:
return tensor_shape.TensorShape([input_shape[0],
len_dim1,
len_dim2,
len_dim3,
input_shape[4]])
def _pooling_function(self, inputs, pool_size, strides,
padding, data_format):
@@ -369,7 +376,6 @@ class MaxPooling3D(_Pooling3D):
`(batch_size, channels, pooled_dim1, pooled_dim2, pooled_dim3)`
"""
@interfaces.legacy_pooling3d_support
def __init__(self, pool_size=(2, 2, 2), strides=None, padding='valid',
data_format=None, **kwargs):
super(MaxPooling3D, self).__init__(pool_size, strides, padding,
@@ -419,7 +425,6 @@ class AveragePooling3D(_Pooling3D):
`(batch_size, channels, pooled_dim1, pooled_dim2, pooled_dim3)`
"""
@interfaces.legacy_pooling3d_support
def __init__(self, pool_size=(2, 2, 2), strides=None, padding='valid',
data_format=None, **kwargs):
super(AveragePooling3D, self).__init__(pool_size, strides, padding,
@@ -441,8 +446,9 @@ class _GlobalPooling1D(Layer):
super(_GlobalPooling1D, self).__init__(**kwargs)
self.input_spec = InputSpec(ndim=3)
def compute_output_shape(self, input_shape):
return (input_shape[0], input_shape[2])
def _compute_output_shape(self, input_shape):
input_shape = tensor_shape.TensorShape(input_shape).as_list()
return tensor_shape.TensorShape([input_shape[0], input_shape[2]])
def call(self, inputs):
raise NotImplementedError
@@ -482,17 +488,17 @@ class _GlobalPooling2D(Layer):
"""Abstract class for different global pooling 2D layers.
"""
@interfaces.legacy_global_pooling_support
def __init__(self, data_format=None, **kwargs):
super(_GlobalPooling2D, self).__init__(**kwargs)
self.data_format = conv_utils.normalize_data_format(data_format)
self.input_spec = InputSpec(ndim=4)
def compute_output_shape(self, input_shape):
def _compute_output_shape(self, input_shape):
input_shape = tensor_shape.TensorShape(input_shape).as_list()
if self.data_format == 'channels_last':
return (input_shape[0], input_shape[3])
return tensor_shape.TensorShape([input_shape[0], input_shape[3]])
else:
return (input_shape[0], input_shape[1])
return tensor_shape.TensorShape([input_shape[0], input_shape[1]])
def call(self, inputs):
raise NotImplementedError
@@ -577,17 +583,17 @@ class _GlobalPooling3D(Layer):
"""Abstract class for different global pooling 3D layers.
"""
@interfaces.legacy_global_pooling_support
def __init__(self, data_format=None, **kwargs):
super(_GlobalPooling3D, self).__init__(**kwargs)
self.data_format = conv_utils.normalize_data_format(data_format)
self.input_spec = InputSpec(ndim=5)
def compute_output_shape(self, input_shape):
def _compute_output_shape(self, input_shape):
input_shape = tensor_shape.TensorShape(input_shape).as_list()
if self.data_format == 'channels_last':
return (input_shape[0], input_shape[4])
return tensor_shape.TensorShape([input_shape[0], input_shape[4]])
else:
return (input_shape[0], input_shape[1])
return tensor_shape.TensorShape([input_shape[0], input_shape[1]])
def call(self, inputs):
raise NotImplementedError
+171 -183
Ver Arquivo
@@ -1,16 +1,20 @@
# -*- coding: utf-8 -*-
"""Recurrent layers.
"""
from __future__ import absolute_import
import numpy as np
from __future__ import division
from __future__ import print_function
from .. import backend as K
from .. import activations
from .. import backend as K
from .. import constraints
from .. import initializers
from .. import regularizers
from .. import constraints
from ..engine import Layer
from ..engine import InputSpec
from ..legacy import interfaces
from ..engine import Layer
import numpy as np
from tensorflow.python.framework import tensor_shape
# pylint: disable=access-member-before-definition
def _time_distributed_dense(x, w, b=None, dropout=None,
input_dim=None, output_dim=None,
@@ -146,7 +150,7 @@ class Recurrent(Layer):
# Masking
This layer supports masking for input data with a variable number
of timesteps. To introduce masks to your data,
use an [Embedding](embeddings.md) layer with the `mask_zero` parameter
use an `Embedding` layer with the `mask_zero` parameter
set to `True`.
# Note on using statefulness in RNNs
@@ -170,11 +174,16 @@ class Recurrent(Layer):
To reset the states of your model, call `.reset_states()` on either
a specific layer, or on your entire model.
# Note on specifying initial states in RNNs
You can specify the initial state of RNN layers by calling them with
the keyword argument `initial_state`. The value of `initial_state`
should be a tensor or list of tensors representing the initial state
of the RNN layer.
# Note on specifying the initial state of RNNs
You can specify the initial state of RNN layers symbolically by
calling them with the keyword argument `initial_state`. The value of
`initial_state` should be a tensor or list of tensors representing
the initial state of the RNN layer.
You can specify the initial state of RNN layers numerically by
calling `reset_states` with the keyword argument `states`. The value of
`states` should be a numpy array or list of numpy arrays representing
the initial state of the RNN layer.
"""
def __init__(self, return_sequences=False,
@@ -190,21 +199,24 @@ class Recurrent(Layer):
self.unroll = unroll
self.implementation = implementation
self.supports_masking = True
self.input_spec = InputSpec(ndim=3)
self.input_spec = [InputSpec(ndim=3)]
self.state_spec = None
self.dropout = 0
self.recurrent_dropout = 0
def compute_output_shape(self, input_shape):
def _compute_output_shape(self, input_shape):
if isinstance(input_shape, list):
input_shape = input_shape[0]
input_shape = tensor_shape.TensorShape(input_shape).as_list()
if self.return_sequences:
return (input_shape[0], input_shape[1], self.units)
return tensor_shape.TensorShape([input_shape[0], input_shape[1], self.units])
else:
return (input_shape[0], self.units)
return tensor_shape.TensorShape([input_shape[0], self.units])
def compute_mask(self, inputs, mask):
if self.return_sequences:
if isinstance(mask, list):
return mask[0]
return mask
else:
return None
@@ -215,14 +227,14 @@ class Recurrent(Layer):
def get_constants(self, inputs, training=None):
return []
def get_initial_states(self, inputs):
def get_initial_state(self, inputs):
# build an all-zero tensor of shape (samples, output_dim)
initial_state = K.zeros_like(inputs) # (samples, timesteps, input_dim)
initial_state = K.sum(initial_state, axis=(1, 2)) # (samples,)
initial_state = K.expand_dims(initial_state) # (samples, 1)
initial_state = K.tile(initial_state, [1, self.units]) # (samples, output_dim)
initial_states = [initial_state for _ in range(len(self.states))]
return initial_states
initial_state = [initial_state for _ in range(len(self.states))]
return initial_state
def preprocess_input(self, inputs, training=None):
return inputs
@@ -232,51 +244,63 @@ class Recurrent(Layer):
# and if it a Keras tensor,
# then add it to the inputs and temporarily
# modify the input spec to include the state.
if initial_state is not None:
if hasattr(initial_state, '_keras_history'):
# Compute the full input spec, including state
input_spec = self.input_spec
state_spec = self.state_spec
if not isinstance(state_spec, list):
state_spec = [state_spec]
self.input_spec = [input_spec] + state_spec
if initial_state is None:
return super(Recurrent, self).__call__(inputs, **kwargs)
# Compute the full inputs, including state
if not isinstance(initial_state, (list, tuple)):
initial_state = [initial_state]
inputs = [inputs] + list(initial_state)
if not isinstance(initial_state, (list, tuple)):
initial_state = [initial_state]
# Perform the call
output = super(Recurrent, self).__call__(inputs, **kwargs)
is_keras_tensor = hasattr(initial_state[0], '_keras_history')
for tensor in initial_state:
if hasattr(tensor, '_keras_history') != is_keras_tensor:
raise ValueError('The initial state of an RNN layer cannot be'
' specified with a mix of Keras tensors and'
' non-Keras tensors')
# Restore original input spec
self.input_spec = input_spec
return output
else:
kwargs['initial_state'] = initial_state
return super(Recurrent, self).__call__(inputs, **kwargs)
if is_keras_tensor:
# Compute the full input spec, including state
input_spec = self.input_spec
state_spec = self.state_spec
if not isinstance(input_spec, list):
input_spec = [input_spec]
if not isinstance(state_spec, list):
state_spec = [state_spec]
self.input_spec = input_spec + state_spec
def call(self, inputs, mask=None, initial_state=None, training=None):
# Compute the full inputs, including state
inputs = [inputs] + list(initial_state)
# Perform the call
output = super(Recurrent, self).__call__(inputs, **kwargs)
# Restore original input spec
self.input_spec = input_spec
return output
else:
kwargs['initial_state'] = initial_state
return super(Recurrent, self).__call__(inputs, **kwargs)
def call(self, inputs, mask=None, training=None, initial_state=None):
# input shape: `(samples, time (padded with zeros), input_dim)`
# note that the .build() method of subclasses MUST define
# self.input_spec and self.state_spec with complete input shapes.
if initial_state is not None:
if not isinstance(initial_state, (list, tuple)):
initial_states = [initial_state]
else:
initial_states = list(initial_state)
if isinstance(inputs, list):
initial_states = inputs[1:]
initial_state = inputs[1:]
inputs = inputs[0]
elif initial_state is not None:
pass
elif self.stateful:
initial_states = self.states
initial_state = self.states
else:
initial_states = self.get_initial_states(inputs)
initial_state = self.get_initial_state(inputs)
if len(initial_states) != len(self.states):
if isinstance(mask, list):
mask = mask[0]
if len(initial_state) != len(self.states):
raise ValueError('Layer has ' + str(len(self.states)) +
' states but was passed ' +
str(len(initial_states)) +
str(len(initial_state)) +
' initial states.')
input_shape = K.int_shape(inputs)
if self.unroll and input_shape[1] is None:
@@ -295,12 +319,11 @@ class Recurrent(Layer):
preprocessed_input = self.preprocess_input(inputs, training=None)
last_output, outputs, states = K.rnn(self.step,
preprocessed_input,
initial_states,
initial_state,
go_backwards=self.go_backwards,
mask=mask,
constants=constants,
unroll=self.unroll,
input_length=input_shape[1])
unroll=self.unroll)
if self.stateful:
updates = []
for i in range(len(states)):
@@ -317,13 +340,10 @@ class Recurrent(Layer):
else:
return last_output
def reset_states(self, states_value=None):
def reset_states(self, states=None):
if not self.stateful:
raise AttributeError('Layer must be stateful.')
if not self.input_spec:
raise RuntimeError('Layer has never been called '
'and thus has no states.')
batch_size = self.input_spec.shape[0]
batch_size = self.input_spec[0].shape[0]
if not batch_size:
raise ValueError('If a RNN is stateful, it needs to know '
'its batch size. Specify the batch size '
@@ -335,31 +355,30 @@ class Recurrent(Layer):
'- If using the functional API, specify '
'the time dimension by passing a '
'`batch_shape` argument to your Input layer.')
if states_value is not None:
if not isinstance(states_value, (list, tuple)):
states_value = [states_value]
if len(states_value) != len(self.states):
raise ValueError('The layer has ' + str(len(self.states)) +
' states, but the `states_value` '
'argument passed '
'only has ' + str(len(states_value)) +
' entries')
# initialize state if None
if self.states[0] is None:
self.states = [K.zeros((batch_size, self.units))
for _ in self.states]
if not states_value:
return
for i, state in enumerate(self.states):
if states_value:
value = states_value[i]
elif states is None:
for state in self.states:
K.set_value(state, np.zeros((batch_size, self.units)))
else:
if not isinstance(states, (list, tuple)):
states = [states]
if len(states) != len(self.states):
raise ValueError('Layer ' + self.name + ' expects ' +
str(len(self.states)) + ' states, '
'but it received ' + str(len(states)) +
' state values. Input received: ' +
str(states))
for index, (value, state) in enumerate(zip(states, self.states)):
if value.shape != (batch_size, self.units):
raise ValueError(
'Expected state #' + str(i) +
' to have shape ' + str((batch_size, self.units)) +
' but got array with shape ' + str(value.shape))
else:
value = np.zeros((batch_size, self.units))
K.set_value(state, value)
raise ValueError('State ' + str(index) +
' is incompatible with layer ' +
self.name + ': expected shape=' +
str((batch_size, self.units)) +
', found shape=' + str(value.shape))
K.set_value(state, value)
def get_config(self):
config = {'return_sequences': self.return_sequences,
@@ -376,39 +395,29 @@ class SimpleRNN(Recurrent):
# Arguments
units: Positive integer, dimensionality of the output space.
activation: Activation function to use
(see [activations](../activations.md)).
activation: Activation function to use.
If you don't specify anything, no activation is applied
If you pass None, no activation is applied
(ie. "linear" activation: `a(x) = x`).
use_bias: Boolean, whether the layer uses a bias vector.
kernel_initializer: Initializer for the `kernel` weights matrix,
used for the linear transformation of the inputs.
(see [initializers](../initializers.md)).
used for the linear transformation of the inputs..
recurrent_initializer: Initializer for the `recurrent_kernel`
weights matrix,
used for the linear transformation of the recurrent state.
(see [initializers](../initializers.md)).
bias_initializer: Initializer for the bias vector
(see [initializers](../initializers.md)).
used for the linear transformation of the recurrent state..
bias_initializer: Initializer for the bias vector.
kernel_regularizer: Regularizer function applied to
the `kernel` weights matrix
(see [regularizer](../regularizers.md)).
the `kernel` weights matrix.
recurrent_regularizer: Regularizer function applied to
the `recurrent_kernel` weights matrix
(see [regularizer](../regularizers.md)).
bias_regularizer: Regularizer function applied to the bias vector
(see [regularizer](../regularizers.md)).
the `recurrent_kernel` weights matrix.
bias_regularizer: Regularizer function applied to the bias vector.
activity_regularizer: Regularizer function applied to
the output of the layer (its "activation").
(see [regularizer](../regularizers.md)).
the output of the layer (its "activation")..
kernel_constraint: Constraint function applied to
the `kernel` weights matrix
(see [constraints](../constraints.md)).
the `kernel` weights matrix.
recurrent_constraint: Constraint function applied to
the `recurrent_kernel` weights matrix
(see [constraints](../constraints.md)).
bias_constraint: Constraint function applied to the bias vector
(see [constraints](../constraints.md)).
the `recurrent_kernel` weights matrix.
bias_constraint: Constraint function applied to the bias vector.
dropout: Float between 0 and 1.
Fraction of the units to drop for
the linear transformation of the inputs.
@@ -420,7 +429,6 @@ class SimpleRNN(Recurrent):
- [A Theoretically Grounded Application of Dropout in Recurrent Neural Networks](http://arxiv.org/abs/1512.05287)
"""
@interfaces.legacy_recurrent_support
def __init__(self, units,
activation='tanh',
use_bias=True,
@@ -457,33 +465,34 @@ class SimpleRNN(Recurrent):
self.dropout = min(1., max(0., dropout))
self.recurrent_dropout = min(1., max(0., recurrent_dropout))
self.state_spec = InputSpec(shape=(None, self.units))
def build(self, input_shape):
if isinstance(input_shape, list):
input_shape = input_shape[0]
input_shape = tensor_shape.TensorShape(input_shape).as_list()
batch_size = input_shape[0] if self.stateful else None
self.input_dim = input_shape[2]
self.input_spec = InputSpec(shape=(batch_size, None, self.input_dim))
self.state_spec = InputSpec(shape=(batch_size, self.units))
self.input_spec[0] = InputSpec(shape=(batch_size, None, self.input_dim))
self.states = [None]
if self.stateful:
self.reset_states()
self.kernel = self.add_weight((self.input_dim, self.units),
self.kernel = self.add_weight(shape=(self.input_dim, self.units),
name='kernel',
initializer=self.kernel_initializer,
regularizer=self.kernel_regularizer,
constraint=self.kernel_constraint)
self.recurrent_kernel = self.add_weight(
(self.units, self.units),
shape=(self.units, self.units),
name='recurrent_kernel',
initializer=self.recurrent_initializer,
regularizer=self.recurrent_regularizer,
constraint=self.recurrent_constraint)
if self.use_bias:
self.bias = self.add_weight((self.units,),
self.bias = self.add_weight(shape=(self.units,),
name='bias',
initializer=self.bias_initializer,
regularizer=self.bias_regularizer,
@@ -496,7 +505,7 @@ class SimpleRNN(Recurrent):
if self.implementation > 0:
return inputs
else:
input_shape = K.int_shape(inputs)
input_shape = inputs.get_shape().as_list()
input_dim = input_shape[2]
timesteps = input_shape[1]
return _time_distributed_dense(inputs,
@@ -553,7 +562,7 @@ class SimpleRNN(Recurrent):
ones = K.ones_like(K.reshape(inputs[:, 0, 0], (-1, 1)))
ones = K.tile(ones, (1, self.units))
def dropped_inputs():
def dropped_inputs(): # pylint: disable=function-redefined
return K.dropout(ones, self.recurrent_dropout)
rec_dp_mask = K.in_train_phase(dropped_inputs,
ones,
@@ -588,42 +597,30 @@ class GRU(Recurrent):
# Arguments
units: Positive integer, dimensionality of the output space.
activation: Activation function to use
(see [activations](../activations.md)).
activation: Activation function to use.
If you pass None, no activation is applied
(ie. "linear" activation: `a(x) = x`).
recurrent_activation: Activation function to use
for the recurrent step
(see [activations](../activations.md)).
for the recurrent step.
use_bias: Boolean, whether the layer uses a bias vector.
kernel_initializer: Initializer for the `kernel` weights matrix,
used for the linear transformation of the inputs.
(see [initializers](../initializers.md)).
used for the linear transformation of the inputs..
recurrent_initializer: Initializer for the `recurrent_kernel`
weights matrix,
used for the linear transformation of the recurrent state.
(see [initializers](../initializers.md)).
bias_initializer: Initializer for the bias vector
(see [initializers](../initializers.md)).
used for the linear transformation of the recurrent state..
bias_initializer: Initializer for the bias vector.
kernel_regularizer: Regularizer function applied to
the `kernel` weights matrix
(see [regularizer](../regularizers.md)).
the `kernel` weights matrix.
recurrent_regularizer: Regularizer function applied to
the `recurrent_kernel` weights matrix
(see [regularizer](../regularizers.md)).
bias_regularizer: Regularizer function applied to the bias vector
(see [regularizer](../regularizers.md)).
the `recurrent_kernel` weights matrix.
bias_regularizer: Regularizer function applied to the bias vector.
activity_regularizer: Regularizer function applied to
the output of the layer (its "activation").
(see [regularizer](../regularizers.md)).
the output of the layer (its "activation")..
kernel_constraint: Constraint function applied to
the `kernel` weights matrix
(see [constraints](../constraints.md)).
the `kernel` weights matrix.
recurrent_constraint: Constraint function applied to
the `recurrent_kernel` weights matrix
(see [constraints](../constraints.md)).
bias_constraint: Constraint function applied to the bias vector
(see [constraints](../constraints.md)).
the `recurrent_kernel` weights matrix.
bias_constraint: Constraint function applied to the bias vector.
dropout: Float between 0 and 1.
Fraction of the units to drop for
the linear transformation of the inputs.
@@ -637,7 +634,6 @@ class GRU(Recurrent):
- [A Theoretically Grounded Application of Dropout in Recurrent Neural Networks](http://arxiv.org/abs/1512.05287)
"""
@interfaces.legacy_recurrent_support
def __init__(self, units,
activation='tanh',
recurrent_activation='hard_sigmoid',
@@ -676,36 +672,36 @@ class GRU(Recurrent):
self.dropout = min(1., max(0., dropout))
self.recurrent_dropout = min(1., max(0., recurrent_dropout))
self.state_spec = InputSpec(shape=(None, self.units))
def build(self, input_shape):
if isinstance(input_shape, list):
input_shape = input_shape[0]
input_shape = tensor_shape.TensorShape(input_shape).as_list()
batch_size = input_shape[0] if self.stateful else None
self.input_dim = input_shape[2]
self.input_spec = InputSpec(shape=(batch_size, None, self.input_dim))
self.state_spec = InputSpec(shape=(batch_size, self.units))
self.input_spec[0] = InputSpec(shape=(batch_size, None, self.input_dim))
self.states = [None]
if self.stateful:
self.reset_states()
self.kernel = self.add_weight((self.input_dim, self.units * 3),
self.kernel = self.add_weight(shape=(self.input_dim, self.units * 3),
name='kernel',
initializer=self.kernel_initializer,
regularizer=self.kernel_regularizer,
constraint=self.kernel_constraint)
self.recurrent_kernel = self.add_weight(
(self.units, self.units * 3),
shape=(self.units, self.units * 3),
name='recurrent_kernel',
initializer=self.recurrent_initializer,
regularizer=self.recurrent_regularizer,
constraint=self.recurrent_constraint)
if self.use_bias:
self.bias = self.add_weight((self.units * 3,),
self.bias = self.add_weight(shape=(self.units * 3,),
name='bias',
initializer='zero',
initializer=self.bias_initializer,
regularizer=self.bias_regularizer,
constraint=self.bias_constraint)
else:
@@ -732,7 +728,7 @@ class GRU(Recurrent):
def preprocess_input(self, inputs, training=None):
if self.implementation == 0:
input_shape = K.int_shape(inputs)
input_shape = inputs.get_shape().as_list()
input_dim = input_shape[2]
timesteps = input_shape[1]
@@ -771,7 +767,7 @@ class GRU(Recurrent):
ones = K.ones_like(K.reshape(inputs[:, 0, 0], (-1, 1)))
ones = K.tile(ones, (1, self.units))
def dropped_inputs():
def dropped_inputs(): # pylint: disable=function-redefined
return K.dropout(ones, self.recurrent_dropout)
rec_dp_mask = [K.in_train_phase(dropped_inputs,
ones,
@@ -861,46 +857,34 @@ class LSTM(Recurrent):
# Arguments
units: Positive integer, dimensionality of the output space.
activation: Activation function to use
(see [activations](../activations.md)).
activation: Activation function to use.
If you pass None, no activation is applied
(ie. "linear" activation: `a(x) = x`).
recurrent_activation: Activation function to use
for the recurrent step
(see [activations](../activations.md)).
for the recurrent step.
use_bias: Boolean, whether the layer uses a bias vector.
kernel_initializer: Initializer for the `kernel` weights matrix,
used for the linear transformation of the inputs.
(see [initializers](../initializers.md)).
used for the linear transformation of the inputs..
recurrent_initializer: Initializer for the `recurrent_kernel`
weights matrix,
used for the linear transformation of the recurrent state.
(see [initializers](../initializers.md)).
bias_initializer: Initializer for the bias vector
(see [initializers](../initializers.md)).
used for the linear transformation of the recurrent state..
bias_initializer: Initializer for the bias vector.
unit_forget_bias: Boolean.
If True, add 1 to the bias of the forget gate at initialization.
Setting it to true will also force `bias_initializer="zeros"`.
This is recommended in [Jozefowicz et al.](http://www.jmlr.org/proceedings/papers/v37/jozefowicz15.pdf)
kernel_regularizer: Regularizer function applied to
the `kernel` weights matrix
(see [regularizer](../regularizers.md)).
the `kernel` weights matrix.
recurrent_regularizer: Regularizer function applied to
the `recurrent_kernel` weights matrix
(see [regularizer](../regularizers.md)).
bias_regularizer: Regularizer function applied to the bias vector
(see [regularizer](../regularizers.md)).
the `recurrent_kernel` weights matrix.
bias_regularizer: Regularizer function applied to the bias vector.
activity_regularizer: Regularizer function applied to
the output of the layer (its "activation").
(see [regularizer](../regularizers.md)).
the output of the layer (its "activation")..
kernel_constraint: Constraint function applied to
the `kernel` weights matrix
(see [constraints](../constraints.md)).
the `kernel` weights matrix.
recurrent_constraint: Constraint function applied to
the `recurrent_kernel` weights matrix
(see [constraints](../constraints.md)).
bias_constraint: Constraint function applied to the bias vector
(see [constraints](../constraints.md)).
the `recurrent_kernel` weights matrix.
bias_constraint: Constraint function applied to the bias vector.
dropout: Float between 0 and 1.
Fraction of the units to drop for
the linear transformation of the inputs.
@@ -910,11 +894,10 @@ class LSTM(Recurrent):
# References
- [Long short-term memory](http://deeplearning.cs.cmu.edu/pdfs/Hochreiter97_lstm.pdf) (original 1997 paper)
- [Learning to forget: Continual prediction with LSTM](http://www.mitpressjournals.org/doi/pdf/10.1162/089976600300015015)
- [Supervised sequence labeling with recurrent neural networks](http://www.cs.toronto.edu/~graves/preprint.pdf)
- [A Theoretically Grounded Application of Dropout in Recurrent Neural Networks](http://arxiv.org/abs/1512.05287)
"""
@interfaces.legacy_recurrent_support
def __init__(self, units,
activation='tanh',
recurrent_activation='hard_sigmoid',
@@ -955,43 +938,48 @@ class LSTM(Recurrent):
self.dropout = min(1., max(0., dropout))
self.recurrent_dropout = min(1., max(0., recurrent_dropout))
self.state_spec = [InputSpec(shape=(None, self.units)),
InputSpec(shape=(None, self.units))]
def build(self, input_shape):
if isinstance(input_shape, list):
input_shape = input_shape[0]
input_shape = tensor_shape.TensorShape(input_shape).as_list()
batch_size = input_shape[0] if self.stateful else None
self.input_dim = input_shape[2]
self.input_spec = InputSpec(shape=(batch_size, None, self.input_dim))
self.state_spec = [InputSpec(shape=(batch_size, self.units)),
InputSpec(shape=(batch_size, self.units))]
self.input_spec[0] = InputSpec(shape=(batch_size, None, self.input_dim))
self.states = [None, None]
if self.stateful:
self.reset_states()
self.kernel = self.add_weight((self.input_dim, self.units * 4),
self.kernel = self.add_weight(shape=(self.input_dim, self.units * 4),
name='kernel',
initializer=self.kernel_initializer,
regularizer=self.kernel_regularizer,
constraint=self.kernel_constraint)
self.recurrent_kernel = self.add_weight(
(self.units, self.units * 4),
shape=(self.units, self.units * 4),
name='recurrent_kernel',
initializer=self.recurrent_initializer,
regularizer=self.recurrent_regularizer,
constraint=self.recurrent_constraint)
if self.use_bias:
self.bias = self.add_weight((self.units * 4,),
if self.unit_forget_bias:
def bias_initializer(shape, *args, **kwargs):
return K.concatenate([
self.bias_initializer((self.units,), *args, **kwargs),
initializers.Ones()((self.units,), *args, **kwargs),
self.bias_initializer((self.units * 2,), *args, **kwargs),
])
else:
bias_initializer = self.bias_initializer
self.bias = self.add_weight(shape=(self.units * 4,),
name='bias',
initializer=self.bias_initializer,
initializer=bias_initializer,
regularizer=self.bias_regularizer,
constraint=self.bias_constraint)
if self.unit_forget_bias:
bias_value = np.zeros((self.units * 4,))
bias_value[self.units: self.units * 2] = 1.
K.set_value(self.bias, bias_value)
else:
self.bias = None
@@ -1019,7 +1007,7 @@ class LSTM(Recurrent):
def preprocess_input(self, inputs, training=None):
if self.implementation == 0:
input_shape = K.int_shape(inputs)
input_shape = inputs.get_shape().as_list()
input_dim = input_shape[2]
timesteps = input_shape[1]
@@ -1061,7 +1049,7 @@ class LSTM(Recurrent):
ones = K.ones_like(K.reshape(inputs[:, 0, 0], (-1, 1)))
ones = K.tile(ones, (1, self.units))
def dropped_inputs():
def dropped_inputs(): # pylint: disable=function-redefined
return K.dropout(ones, self.recurrent_dropout)
rec_dp_mask = [K.in_train_phase(dropped_inputs,
ones,
+49
Ver Arquivo
@@ -0,0 +1,49 @@
"""Layer serialization/deserialization functions.
"""
# pylint: disable=wildcard-import
# pylint: disable=unused-import
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
from .advanced_activations import *
from .convolutional import *
from .convolutional_recurrent import *
from .core import *
from .embeddings import *
from ..engine import Input
from ..engine import InputLayer
from .local import *
from .merge import *
from .noise import *
from .normalization import *
from .pooling import *
from .recurrent import *
from ..utils.generic_utils import deserialize_keras_object
from .wrappers import *
def serialize(layer):
return {'class_name': layer.__class__.__name__,
'config': layer.get_config()}
def deserialize(config, custom_objects=None):
"""Instantiates a layer from a config dictionary.
# Arguments
config: dict of the form {'class_name': str, 'config': dict}
custom_objects: dict mapping class names (or function names)
of custom (non-Keras) objects to class/functions
# Returns
Layer instance (may be Model, Sequential, Layer...)
"""
from .. import models # pylint: disable=g-import-not-at-top
globs = globals() # All layers.
globs['Model'] = models.Model
globs['Sequential'] = models.Sequential
return deserialize_keras_object(config,
module_objects=globs,
custom_objects=custom_objects,
printable_module_name='layer')
+27 -15
Ver Arquivo
@@ -1,11 +1,16 @@
# -*- coding: utf-8 -*-
"""Wrapper layers: layers that augment the functionality of another layer.
"""
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
import copy
import inspect
from ..engine import Layer
from ..engine import InputSpec
from .. import backend as K
from tensorflow.python.framework import tensor_shape
class Wrapper(Layer):
@@ -83,8 +88,9 @@ class Wrapper(Layer):
@classmethod
def from_config(cls, config, custom_objects=None):
from . import deserialize as deserialize_layer
layer = deserialize_layer(config.pop('layer'), custom_objects=custom_objects)
from . import deserialize as deserialize_layer # pylint: disable=g-import-not-at-top
layer = deserialize_layer(config.pop('layer'),
custom_objects=custom_objects)
return cls(layer, **config)
@@ -138,19 +144,24 @@ class TimeDistributed(Wrapper):
self.supports_masking = True
def build(self, input_shape):
input_shape = tensor_shape.TensorShape(input_shape).as_list()
assert len(input_shape) >= 3
self.input_spec = InputSpec(shape=input_shape)
child_input_shape = (input_shape[0],) + input_shape[2:]
child_input_shape = [input_shape[0]] + input_shape[2:]
if not self.layer.built:
self.layer.build(child_input_shape)
self.layer.built = True
super(TimeDistributed, self).build()
def compute_output_shape(self, input_shape):
child_input_shape = (input_shape[0],) + input_shape[2:]
child_output_shape = self.layer.compute_output_shape(child_input_shape)
def _compute_output_shape(self, input_shape):
input_shape = tensor_shape.TensorShape(input_shape).as_list()
child_input_shape = tensor_shape.TensorShape(
[input_shape[0]] + input_shape[2:])
child_output_shape = self.layer._compute_output_shape( # pylint: disable=protected-access
child_input_shape).as_list()
timesteps = input_shape[1]
return (child_output_shape[0], timesteps) + child_output_shape[1:]
return tensor_shape.TensorShape(
[child_output_shape[0], timesteps] + child_output_shape[1:])
def call(self, inputs, mask=None):
input_shape = K.int_shape(inputs)
@@ -162,7 +173,6 @@ class TimeDistributed(Wrapper):
_, outputs, _ = K.rnn(step, inputs,
initial_states=[],
input_length=input_shape[1],
unroll=False)
y = outputs
else:
@@ -176,8 +186,8 @@ class TimeDistributed(Wrapper):
inputs = K.reshape(inputs, (-1,) + input_shape[2:])
y = self.layer.call(inputs) # (num_samples * timesteps, ...)
# Shape: (num_samples, timesteps, ...)
output_shape = self.compute_output_shape(input_shape)
y = K.reshape(y, (-1, input_length) + output_shape[2:])
output_shape = self._compute_output_shape(input_shape).as_list() # pylint: disable=protected-access
y = K.reshape(y, [-1, input_length] + output_shape[2:])
# Apply activity regularizer if any:
if (hasattr(self.layer, 'activity_regularizer') and
@@ -242,15 +252,17 @@ class Bidirectional(Wrapper):
self.forward_layer.set_weights(weights[:nw // 2])
self.backward_layer.set_weights(weights[nw // 2:])
def compute_output_shape(self, input_shape):
def _compute_output_shape(self, input_shape):
input_shape = tensor_shape.TensorShape(input_shape).as_list()
if self.merge_mode in ['sum', 'ave', 'mul']:
return self.forward_layer.compute_output_shape(input_shape)
return self.forward_layer._compute_output_shape(input_shape) # pylint: disable=protected-access
elif self.merge_mode == 'concat':
shape = list(self.forward_layer.compute_output_shape(input_shape))
shape = self.forward_layer._compute_output_shape(input_shape).as_list() # pylint: disable=protected-access
shape[-1] *= 2
return tuple(shape)
return tensor_shape.TensorShape(shape)
elif self.merge_mode is None:
return [self.forward_layer.compute_output_shape(input_shape)] * 2
shape = self.forward_layer._compute_output_shape(input_shape) # pylint: disable=protected-access
return [shape, copy.copy(shape)]
def call(self, inputs, training=None, mask=None):
kwargs = {}
Ver Arquivo
+21
Ver Arquivo
@@ -602,3 +602,24 @@ legacy_model_constructor_support = generate_legacy_interface(
allowed_positional_args=None,
conversions=[('input', 'inputs'),
('output', 'outputs')])
legacy_input_support = generate_legacy_interface(
allowed_positional_args=None,
conversions=[('input_dtype', 'dtype')])
def add_weight_args_preprocessing(args, kwargs):
if len(args) > 1:
if isinstance(args[1], (tuple, list)):
kwargs['shape'] = args[1]
args = (args[0],) + args[2:]
if len(args) > 1:
if isinstance(args[1], six.string_types):
kwargs['name'] = args[1]
args = (args[0],) + args[2:]
return args, kwargs, []
legacy_add_weight_support = generate_legacy_interface(
allowed_positional_args=['name', 'shape'],
preprocessor=add_weight_args_preprocessing)
-27
Ver Arquivo
@@ -1,27 +0,0 @@
from .layers import Merge
def needs_legacy_support(model):
return isinstance(model.layers[0], Merge)
def legacy_sequential_layers(model):
layers = []
if model.layers:
if isinstance(model.layers[0], Merge):
merge = model.layers[0]
for layer in merge.layers:
if hasattr(layer, 'layers'):
for sublayer in layer.layers:
if sublayer not in layers:
layers.append(sublayer)
else:
if layer not in layers:
layers.append(layer)
else:
if model.layers[0] not in layers:
layers.append(model.layers[0])
for layer in model.layers[1:]:
if layer not in layers:
layers.append(layer)
return layers
+19 -1
Ver Arquivo
@@ -1,6 +1,11 @@
"""Built-in Keras loss functions.
"""
from __future__ import absolute_import
import six
from __future__ import division
from __future__ import print_function
from . import backend as K
import six
from .utils.generic_utils import deserialize_keras_object
@@ -13,6 +18,7 @@ def mean_absolute_error(y_true, y_pred):
def mean_absolute_percentage_error(y_true, y_pred):
# Equivalent to MAE, but sometimes easier to interpret.
diff = K.abs((y_true - y_pred) / K.clip(K.abs(y_true),
K.epsilon(),
None))
@@ -33,6 +39,18 @@ def hinge(y_true, y_pred):
return K.mean(K.maximum(1. - y_true * y_pred, 0.), axis=-1)
def categorical_hinge(y_true, y_pred):
pos = K.sum(y_true * y_pred, axis=-1)
neg = K.max((1.0 - y_true) * y_pred, axis=-1)
return K.mean(K.maximum(0.0, neg - pos + 1), axis=-1)
def logcosh(y_true, y_pred):
def cosh(x):
return (K.exp(x) + K.exp(-x)) / 2
return K.mean(K.log(cosh(y_pred - y_true)), axis=-1)
def categorical_crossentropy(y_true, y_pred):
return K.categorical_crossentropy(y_pred, y_true)
+17 -9
Ver Arquivo
@@ -1,18 +1,26 @@
"""Built-in Keras metrics functions.
"""
from __future__ import absolute_import
import six
from __future__ import division
from __future__ import print_function
from . import backend as K
from .losses import mean_squared_error
# pylint: disable=unused-import
from .losses import binary_crossentropy
from .losses import categorical_crossentropy
from .losses import cosine_proximity
from .losses import hinge
from .losses import kullback_leibler_divergence
from .losses import mean_absolute_error
from .losses import mean_absolute_percentage_error
from .losses import mean_squared_error
from .losses import mean_squared_logarithmic_error
from .losses import hinge
from .losses import squared_hinge
from .losses import categorical_crossentropy
from .losses import sparse_categorical_crossentropy
from .losses import binary_crossentropy
from .losses import kullback_leibler_divergence
from .losses import logcosh
from .losses import poisson
from .losses import cosine_proximity
from .losses import sparse_categorical_crossentropy
from .losses import squared_hinge
# pylint: disable=unused-import
import six
from .utils.generic_utils import deserialize_keras_object
+54 -175
Ver Arquivo
@@ -1,31 +1,37 @@
"""Home of the Sequential model, and the `save_model`/`load_model` functions.
"""
# -*- coding: utf-8 -*-
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
import warnings
import copy
import json
import os
import yaml
import numpy as np
import warnings
from . import backend as K
from . import optimizers
from . import layers as layer_module
from .utils.io_utils import ask_to_proceed_with_overwrite
from .engine.training import Model
from . import optimizers
from .engine import topology
from .engine.topology import Layer
from .engine.topology import Input
from .legacy import layers as legacy_layers
from .legacy import models as legacy_models
from .legacy import interfaces
from .engine.topology import Layer
from .engine.training import Model
import numpy as np
from .utils.io_utils import ask_to_proceed_with_overwrite
# pylint: disable=g-import-not-at-top
try:
import h5py
except ImportError:
h5py = None
try:
import yaml
except ImportError:
yaml = None
# pylint: enable=g-import-not-at-top
def save_model(model, filepath, overwrite=True, include_optimizer=True):
"""Save a model to a HDF5 file.
@@ -86,7 +92,7 @@ def save_model(model, filepath, overwrite=True, include_optimizer=True):
raise TypeError('Not JSON Serializable:', obj)
from . import __version__ as keras_version
from . import __version__ as keras_version # pylint: disable=g-import-not-at-top
# If file exists and should not be overwritten.
if not overwrite and os.path.isfile(filepath):
@@ -103,10 +109,7 @@ def save_model(model, filepath, overwrite=True, include_optimizer=True):
}, default=get_json_type).encode('utf8')
model_weights_group = f.create_group('model_weights')
if legacy_models.needs_legacy_support(model):
model_layers = legacy_models.legacy_sequential_layers(model)
else:
model_layers = model.layers
model_layers = model.layers
topology.save_weights_to_hdf5_group(model_weights_group, model_layers)
if include_optimizer and hasattr(model, 'optimizer'):
@@ -167,7 +170,7 @@ def save_model(model, filepath, overwrite=True, include_optimizer=True):
f.close()
def load_model(filepath, custom_objects=None):
def load_model(filepath, custom_objects=None, compile=True):
"""Loads a model saved via `save_model`.
# Arguments
@@ -175,12 +178,16 @@ def load_model(filepath, custom_objects=None):
custom_objects: Optional dictionary mapping names
(strings) to custom classes or functions to be
considered during deserialization.
compile: Boolean, whether to compile the model
after loading.
# Returns
A Keras model instance. If an optimizer was found
as part of the saved model, the model is already
compiled. Otherwise, the model is uncompiled and
a warning will be displayed.
a warning will be displayed. When `compile` is set
to False, the compilation is omitted without any
warning.
# Raises
ImportError: if h5py is not available.
@@ -242,6 +249,11 @@ def load_model(filepath, custom_objects=None):
# set weights
topology.load_weights_from_hdf5_group(f['model_weights'], model.layers)
# Early return if compilation is not required.
if not compile:
f.close()
return model
# instantiate optimizer
training_config = f.attrs.get('training_config')
if training_config is None:
@@ -293,9 +305,12 @@ def model_from_config(config, custom_objects=None):
# Returns
A Keras model instance (uncompiled).
# Raises
TypeError if `config` is not a dictionary
"""
if isinstance(config, list):
raise TypeError('`model_fom_config` expects a dictionary, not a list. '
raise TypeError('`model_from_config` expects a dictionary, not a list. '
'Maybe you meant to use '
'`Sequential.from_config(config)`?')
return layer_module.deserialize(config, custom_objects=custom_objects)
@@ -312,7 +327,12 @@ def model_from_yaml(yaml_string, custom_objects=None):
# Returns
A Keras model instance (uncompiled).
# Raises
ImportError: if yaml module is not found.
"""
if yaml is None:
raise ImportError('Requires yaml module installed.')
config = yaml.load(yaml_string)
return layer_module.deserialize(config, custom_objects=custom_objects)
@@ -456,9 +476,7 @@ class Sequential(Model):
output_tensors=self.outputs,
# no model-level masking for now
input_masks=[None for _ in self.inputs],
output_masks=[None],
input_shapes=[x._keras_shape for x in self.inputs],
output_shapes=[self.outputs[0]._keras_shape])
output_masks=[None])
else:
output_tensor = layer(self.outputs[0])
if isinstance(output_tensor, list):
@@ -469,7 +487,7 @@ class Sequential(Model):
self.outputs = [output_tensor]
# update self.inbound_nodes
self.inbound_nodes[0].output_tensors = self.outputs
self.inbound_nodes[0].output_shapes = [self.outputs[0]._keras_shape]
self.inbound_nodes[0].output_shapes = [K.int_shape(self.outputs[0])]
self.layers.append(layer)
self.built = False
@@ -493,7 +511,7 @@ class Sequential(Model):
self.outputs = [self.layers[-1].output]
# update self.inbound_nodes
self.inbound_nodes[0].output_tensors = self.outputs
self.inbound_nodes[0].output_shapes = [self.outputs[0]._keras_shape]
self.inbound_nodes[0].output_shapes = [K.int_shape(self.outputs[0])]
self.built = False
def get_layer(self, name=None, index=None):
@@ -558,36 +576,9 @@ class Sequential(Model):
self.build()
return self.model.uses_learning_phase
@property
def _flattened_layers(self):
layers = []
if self.layers:
# Support for legacy models
if isinstance(self.layers[0], legacy_layers.Merge):
merge = self.layers[0]
for layer in merge.layers:
if hasattr(layer, '_flattened_layers'):
for sublayer in layer._flattened_layers:
if sublayer not in layers:
layers.append(sublayer)
elif hasattr(layer, 'layers'):
for sublayer in layer.layers:
if sublayer not in layers:
layers.append(sublayer)
else:
if layer not in layers:
layers.append(layer)
else:
if self.layers[0] not in layers:
layers.append(self.layers[0])
for layer in self.layers[1:]:
if layer not in layers:
layers.append(layer)
return layers
def _gather_list_attr(self, attr):
all_attrs = []
for layer in self._flattened_layers:
for layer in self.layers:
all_attrs += getattr(layer, attr, [])
return all_attrs
@@ -605,12 +596,10 @@ class Sequential(Model):
def trainable_weights(self):
if not self.trainable:
return []
# Support for legacy behavior
return self._gather_list_attr('trainable_weights')
@property
def non_trainable_weights(self):
# Support for legacy behavior
weights = self._gather_list_attr('non_trainable_weights')
if not self.trainable:
trainable_weights = self._gather_list_attr('trainable_weights')
@@ -664,14 +653,6 @@ class Sequential(Model):
A flat list of Numpy arrays
(one array per model weight).
"""
# Legacy support
if legacy_models.needs_legacy_support(self):
layers = legacy_models.legacy_sequential_layers(self)
weights = []
for layer in layers:
weights.append(layer.get_weights())
return weights
if self.model is None:
self.build()
return self.model.get_weights()
@@ -684,14 +665,6 @@ class Sequential(Model):
of Numpy arrays with shapes and types matching
the output of `model.get_weights()`.
"""
# Legacy support
if legacy_models.needs_legacy_support(self):
layers = legacy_models.legacy_sequential_layers(self)
for layer in layers:
nb_param = len(layer.weights)
layer.set_weights(weights[:nb_param])
weights = weights[nb_param:]
if self.model is None:
self.build()
self.model.set_weights(weights)
@@ -702,12 +675,7 @@ class Sequential(Model):
f = h5py.File(filepath, mode='r')
if 'layer_names' not in f.attrs and 'model_weights' in f:
f = f['model_weights']
# Legacy support
if legacy_models.needs_legacy_support(self):
layers = legacy_models.legacy_sequential_layers(self)
else:
layers = self.layers
layers = self.layers
if by_name:
topology.load_weights_from_hdf5_group_by_name(f, layers)
else:
@@ -723,12 +691,7 @@ class Sequential(Model):
proceed = ask_to_proceed_with_overwrite(filepath)
if not proceed:
return
# Legacy support
if legacy_models.needs_legacy_support(self):
layers = legacy_models.legacy_sequential_layers(self)
else:
layers = self.layers
layers = self.layers
f = h5py.File(filepath, 'w')
topology.save_weights_to_hdf5_group(f, layers)
f.flush()
@@ -744,7 +707,7 @@ class Sequential(Model):
optimizer: str (name of optimizer) or optimizer object.
See [optimizers](/optimizers).
loss: str (name of objective function) or objective function.
See [objectives](/objectives).
See [losses](/losses).
metrics: list of metrics to be evaluated by the model
during training and testing.
Typically you will use `metrics=['accuracy']`.
@@ -753,7 +716,8 @@ class Sequential(Model):
sample weighting (2D weights), set this to "temporal".
"None" defaults to sample-wise weights (1D).
**kwargs: for Theano backend, these are passed into K.function.
Ignored for Tensorflow backend.
When using the Tensorflow backend, these are passed into
`tf.Session.run`.
# Example
```python
@@ -774,15 +738,18 @@ class Sequential(Model):
**kwargs)
self.optimizer = self.model.optimizer
self.loss = self.model.loss
self.total_loss = self.model.total_loss
self.loss_weights = self.model.loss_weights
self.metrics = self.model.metrics
self.metrics_tensors = self.model.metrics_tensors
self.metrics_names = self.model.metrics_names
self.sample_weight_mode = self.model.sample_weight_mode
self.sample_weights = self.model.sample_weights
self.targets = self.model.targets
def fit(self, x, y, batch_size=32, epochs=10, verbose=1, callbacks=None,
validation_split=0., validation_data=None, shuffle=True,
class_weight=None, sample_weight=None, initial_epoch=0, **kwargs):
class_weight=None, sample_weight=None, initial_epoch=0):
"""Trains the model for a fixed number of epochs.
# Arguments
@@ -829,14 +796,6 @@ class Sequential(Model):
# Raises
RuntimeError: if the model was never compiled.
"""
# Legacy support
if 'nb_epoch' in kwargs:
warnings.warn('The `nb_epoch` argument in `fit` '
'has been renamed `epochs`.')
epochs = kwargs.pop('nb_epoch')
if kwargs:
raise TypeError('Unrecognized keyword arguments: ' + str(kwargs))
if self.model is None:
raise RuntimeError('The model needs to be compiled '
'before being used.')
@@ -1007,7 +966,6 @@ class Sequential(Model):
else:
return (proba > 0.5).astype('int32')
@interfaces.legacy_generator_methods_support
def fit_generator(self, generator,
steps_per_epoch,
epochs=1,
@@ -1033,8 +991,8 @@ class Sequential(Model):
- a tuple (inputs, targets, sample_weights).
All arrays should contain the same number of samples.
The generator is expected to loop over its data
indefinitely. An epoch finishes when `samples_per_epoch`
samples have been seen by the model.
indefinitely. An epoch finishes when `steps_per_epoch`
batches have been seen by the model.
steps_per_epoch: Total number of steps (batches of samples)
to yield from `generator` before declaring one epoch
finished and starting the next epoch. It should typically
@@ -1087,7 +1045,7 @@ class Sequential(Model):
f.close()
model.fit_generator(generate_arrays_from_file('/my_file.txt'),
samples_per_epoch=10000, epochs=10)
steps_per_epoch=1000, epochs=10)
```
"""
if self.model is None:
@@ -1106,7 +1064,6 @@ class Sequential(Model):
pickle_safe=pickle_safe,
initial_epoch=initial_epoch)
@interfaces.legacy_generator_methods_support
def evaluate_generator(self, generator, steps,
max_q_size=10, workers=1,
pickle_safe=False):
@@ -1146,7 +1103,6 @@ class Sequential(Model):
workers=workers,
pickle_safe=pickle_safe)
@interfaces.legacy_generator_methods_support
def predict_generator(self, generator, steps,
max_q_size=10, workers=1,
pickle_safe=False, verbose=0):
@@ -1180,9 +1136,6 @@ class Sequential(Model):
verbose=verbose)
def get_config(self):
if isinstance(self.layers[0], legacy_layers.Merge):
return self.legacy_get_config()
config = []
for layer in self.layers:
config.append({'class_name': layer.__class__.__name__,
@@ -1191,82 +1144,8 @@ class Sequential(Model):
@classmethod
def from_config(cls, config, custom_objects=None):
if 'class_name' not in config[0] or config[0]['class_name'] == 'Merge':
return cls.legacy_from_config(config)
model = cls()
for conf in config:
layer = layer_module.deserialize(conf, custom_objects=custom_objects)
model.add(layer)
return model
def legacy_get_config(self):
"""Retrieves the model configuration as a Python list.
# Returns
A list of dicts (each dict is a layer config).
"""
config = []
if isinstance(self.layers[0], legacy_layers.Merge):
assert hasattr(self.layers[0], 'layers')
layers = []
for layer in self.layers[0].layers:
layer_config = {'class_name': layer.__class__.__name__,
'config': layer.get_config()}
layers.append(layer_config)
merge_config = self.layers[0].get_config()
merge_config['layers'] = layers
config.append({'class_name': 'Merge', 'config': merge_config})
else:
config.append({'class_name': self.layers[0].__class__.__name__,
'config': self.layers[0].get_config()})
for layer in self.layers[1:]:
config.append({'class_name': layer.__class__.__name__,
'config': layer.get_config()})
return copy.deepcopy(config)
@classmethod
def legacy_from_config(cls, config, layer_cache=None):
if not layer_cache:
layer_cache = {}
def normalize_legacy_config(conf):
if 'class_name' not in conf:
class_name = conf['name']
name = conf.get('custom_name')
conf['name'] = name
return {'class_name': class_name,
'config': conf}
return conf
# the model we will return
model = cls()
def get_or_create_layer(layer_data):
name = layer_data['config'].get('name')
if name in layer_cache:
return layer_cache[name]
layer = layer_module.deserialize(layer_data)
layer_cache[name] = layer
return layer
first_layer = config[0]
first_layer = normalize_legacy_config(first_layer)
if first_layer['class_name'] == 'Merge':
merge_inputs = []
first_layer_config = first_layer['config']
for merge_input_config in first_layer_config.pop('layers'):
merge_input = layer_module.deserialize(merge_input_config)
merge_inputs.append(merge_input)
first_layer_config['layers'] = merge_inputs
merge = legacy_layers.Merge.from_config(first_layer_config)
model.add(merge)
else:
layer = get_or_create_layer(first_layer)
model.add(layer)
for conf in config[1:]:
conf = normalize_legacy_config(conf)
layer = get_or_create_layer(conf)
model.add(layer)
return model
-6
Ver Arquivo
@@ -1,6 +0,0 @@
"""Legacy objectives module.
Only kept for backwards API compatibility.
"""
from __future__ import absolute_import
from .losses import *
+33 -18
Ver Arquivo
@@ -1,13 +1,15 @@
"""Keras optimizer classes (will eventually be replaced with core optimizers).
"""
from __future__ import absolute_import
import six
from six.moves import zip
from __future__ import division
from __future__ import print_function
from . import backend as K
from .utils.generic_utils import serialize_keras_object
import six
from six.moves import zip
from tensorflow.python.training import optimizer as tf_optimizer_module
from .utils.generic_utils import deserialize_keras_object
if K.backend() == 'tensorflow':
import tensorflow as tf
from .utils.generic_utils import serialize_keras_object
def clip_norm(g, c, n):
@@ -134,7 +136,7 @@ class SGD(Optimizer):
self.updates .append(K.update_add(self.iterations, 1))
# momentum
shapes = [K.get_variable_shape(p) for p in params]
shapes = [K.int_shape(p) for p in params]
moments = [K.zeros(shape) for shape in shapes]
self.weights = [self.iterations] + moments
for p, g, m in zip(params, grads, moments):
@@ -164,6 +166,7 @@ class SGD(Optimizer):
class RMSprop(Optimizer):
# pylint: disable=line-too-long
"""RMSProp optimizer.
It is recommended to leave the parameters of this optimizer
@@ -182,6 +185,7 @@ class RMSprop(Optimizer):
# References
- [rmsprop: Divide the gradient by a running average of its recent magnitude](http://www.cs.toronto.edu/~tijmen/csc321/slides/lecture_slides_lec6.pdf)
"""
# pylint: enable=line-too-long
def __init__(self, lr=0.001, rho=0.9, epsilon=1e-8, decay=0.,
**kwargs):
@@ -195,7 +199,7 @@ class RMSprop(Optimizer):
def get_updates(self, params, constraints, loss):
grads = self.get_gradients(loss, params)
shapes = [K.get_variable_shape(p) for p in params]
shapes = [K.int_shape(p) for p in params]
accumulators = [K.zeros(shape) for shape in shapes]
self.weights = accumulators
self.updates = []
@@ -228,6 +232,7 @@ class RMSprop(Optimizer):
class Adagrad(Optimizer):
# pylint: disable=line-too-long
"""Adagrad optimizer.
It is recommended to leave the parameters of this optimizer
@@ -241,6 +246,7 @@ class Adagrad(Optimizer):
# References
- [Adaptive Subgradient Methods for Online Learning and Stochastic Optimization](http://www.jmlr.org/papers/volume12/duchi11a/duchi11a.pdf)
"""
# pylint: enable=line-too-long
def __init__(self, lr=0.01, epsilon=1e-8, decay=0., **kwargs):
super(Adagrad, self).__init__(**kwargs)
@@ -252,7 +258,7 @@ class Adagrad(Optimizer):
def get_updates(self, params, constraints, loss):
grads = self.get_gradients(loss, params)
shapes = [K.get_variable_shape(p) for p in params]
shapes = [K.int_shape(p) for p in params]
accumulators = [K.zeros(shape) for shape in shapes]
self.weights = accumulators
self.updates = []
@@ -282,6 +288,7 @@ class Adagrad(Optimizer):
class Adadelta(Optimizer):
# pylint: disable=line-too-long
"""Adadelta optimizer.
It is recommended to leave the parameters of this optimizer
@@ -297,6 +304,7 @@ class Adadelta(Optimizer):
# References
- [Adadelta - an adaptive learning rate method](http://arxiv.org/abs/1212.5701)
"""
# pylint: enable=line-too-long
def __init__(self, lr=1.0, rho=0.95, epsilon=1e-8, decay=0.,
**kwargs):
@@ -310,7 +318,7 @@ class Adadelta(Optimizer):
def get_updates(self, params, constraints, loss):
grads = self.get_gradients(loss, params)
shapes = [K.get_variable_shape(p) for p in params]
shapes = [K.int_shape(p) for p in params]
accumulators = [K.zeros(shape) for shape in shapes]
delta_accumulators = [K.zeros(shape) for shape in shapes]
self.weights = accumulators + delta_accumulators
@@ -351,6 +359,7 @@ class Adadelta(Optimizer):
class Adam(Optimizer):
# pylint: disable=line-too-long
"""Adam optimizer.
Default parameters follow those provided in the original paper.
@@ -365,6 +374,7 @@ class Adam(Optimizer):
# References
- [Adam - A Method for Stochastic Optimization](http://arxiv.org/abs/1412.6980v8)
"""
# pylint: enable=line-too-long
def __init__(self, lr=0.001, beta_1=0.9, beta_2=0.999,
epsilon=1e-8, decay=0., **kwargs):
@@ -389,7 +399,7 @@ class Adam(Optimizer):
lr_t = lr * (K.sqrt(1. - K.pow(self.beta_2, t)) /
(1. - K.pow(self.beta_1, t)))
shapes = [K.get_variable_shape(p) for p in params]
shapes = [K.int_shape(p) for p in params]
ms = [K.zeros(shape) for shape in shapes]
vs = [K.zeros(shape) for shape in shapes]
self.weights = [self.iterations] + ms + vs
@@ -421,6 +431,7 @@ class Adam(Optimizer):
class Adamax(Optimizer):
# pylint: disable=line-too-long
"""Adamax optimizer from Adam paper's Section 7.
It is a variant of Adam based on the infinity norm.
@@ -435,6 +446,7 @@ class Adamax(Optimizer):
# References
- [Adam - A Method for Stochastic Optimization](http://arxiv.org/abs/1412.6980v8)
"""
# pylint: enable=line-too-long
def __init__(self, lr=0.002, beta_1=0.9, beta_2=0.999,
epsilon=1e-8, decay=0., **kwargs):
@@ -458,7 +470,7 @@ class Adamax(Optimizer):
t = self.iterations + 1
lr_t = lr / (1. - K.pow(self.beta_1, t))
shapes = [K.get_variable_shape(p) for p in params]
shapes = [K.int_shape(p) for p in params]
# zero init of 1st moment
ms = [K.zeros(shape) for shape in shapes]
# zero init of exponentially weighted infinity norm
@@ -493,6 +505,7 @@ class Adamax(Optimizer):
class Nadam(Optimizer):
# pylint: disable=line-too-long
"""Nesterov Adam optimizer.
Much like Adam is essentially RMSprop with momentum,
@@ -511,6 +524,7 @@ class Nadam(Optimizer):
- [Nadam report](http://cs229.stanford.edu/proj2015/054_report.pdf)
- [On the importance of initialization and momentum in deep learning](http://www.cs.toronto.edu/~fritz/absps/momentum.pdf)
"""
# pylint: enable=line-too-long
def __init__(self, lr=0.002, beta_1=0.9, beta_2=0.999,
epsilon=1e-8, schedule_decay=0.004, **kwargs):
@@ -536,7 +550,7 @@ class Nadam(Optimizer):
m_schedule_next = self.m_schedule * momentum_cache_t * momentum_cache_t_1
self.updates.append((self.m_schedule, m_schedule_new))
shapes = [K.get_variable_shape(p) for p in params]
shapes = [K.int_shape(p) for p in params]
ms = [K.zeros(shape) for shape in shapes]
vs = [K.zeros(shape) for shape in shapes]
@@ -578,7 +592,7 @@ class TFOptimizer(Optimizer):
"""Wrapper class for native TensorFlow optimizers.
"""
def __init__(self, optimizer):
def __init__(self, optimizer): # pylint: disable=super-init-not-called
self.optimizer = optimizer
self.iterations = K.variable(0., name='iterations')
self.updates = []
@@ -608,6 +622,7 @@ class TFOptimizer(Optimizer):
# Aliases.
# pylint: disable=invalid-name
sgd = SGD
rmsprop = RMSprop
adagrad = Adagrad
@@ -615,6 +630,7 @@ adadelta = Adadelta
adam = Adam
adamax = Adamax
nadam = Nadam
# pylint: enable=invalid-name
def serialize(optimizer):
@@ -670,10 +686,9 @@ def get(identifier):
# Raises
ValueError: If `identifier` cannot be interpreted.
"""
if K.backend() == 'tensorflow':
# Wrap TF optimizer instances
if isinstance(identifier, tf.train.Optimizer):
return TFOptimizer(identifier)
# Wrap TF optimizer instances
if isinstance(identifier, tf_optimizer_module.Optimizer):
return TFOptimizer(identifier)
if isinstance(identifier, dict):
return deserialize(identifier)
elif isinstance(identifier, six.string_types):
+9
Ver Arquivo
@@ -0,0 +1,9 @@
"""Data preprocessing module.
"""
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
from . import image
from . import sequence
from . import text
+45 -18
Ver Arquivo
@@ -1,25 +1,33 @@
"""Fairly basic set of tools for real-time data augmentation on image data.
Can easily be extended to include new transformations,
new preprocessing methods, etc...
"""
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
import numpy as np
import re
from scipy import linalg
import scipy.ndimage as ndi
from six.moves import range
import os
import re
import threading
import warnings
from .. import backend as K
import numpy as np
from six.moves import range
# pylint: disable=g-import-not-at-top
try:
from PIL import Image as pil_image
except ImportError:
pil_image = None
try:
from scipy import linalg
import scipy.ndimage as ndi
except ImportError:
linalg = None
ndi = None
# pylint: enable=g-import-not-at-top
def random_rotation(x, rg, row_axis=1, col_axis=2, channel_axis=0,
@@ -250,7 +258,7 @@ def array_to_img(x, data_format=None, scale=True):
if data_format == 'channels_first':
x = x.transpose(1, 2, 0)
if scale:
x = x + max(-np.min(x), 0)
x = x + max(-np.min(x), 0) # pylint: disable=g-no-augmented-assignment
x_max = np.max(x)
if x_max != 0:
x /= x_max
@@ -325,9 +333,9 @@ def load_img(path, grayscale=False, target_size=None):
if img.mode != 'RGB':
img = img.convert('RGB')
if target_size:
wh_tuple = (target_size[1], target_size[0])
if img.size != wh_tuple:
img = img.resize(wh_tuple)
hw_tuple = (target_size[1], target_size[0])
if img.size != hw_tuple:
img = img.resize(hw_tuple)
return img
@@ -346,6 +354,7 @@ class ImageDataGenerator(object):
featurewise_std_normalization: divide inputs by std of the dataset.
samplewise_std_normalization: divide each input by its std.
zca_whitening: apply ZCA whitening.
zca_epsilon: epsilon for ZCA whitening. Default is 1e-6.
rotation_range: degrees (0 to 180).
width_shift_range: fraction of total width.
height_shift_range: fraction of total height.
@@ -382,6 +391,7 @@ class ImageDataGenerator(object):
featurewise_std_normalization=False,
samplewise_std_normalization=False,
zca_whitening=False,
zca_epsilon=1e-6,
rotation_range=0.,
width_shift_range=0.,
height_shift_range=0.,
@@ -402,6 +412,7 @@ class ImageDataGenerator(object):
self.featurewise_std_normalization = featurewise_std_normalization
self.samplewise_std_normalization = samplewise_std_normalization
self.zca_whitening = zca_whitening
self.zca_epsilon = zca_epsilon
self.rotation_range = rotation_range
self.width_shift_range = width_shift_range
self.height_shift_range = height_shift_range
@@ -443,7 +454,7 @@ class ImageDataGenerator(object):
'Received arg: ', zoom_range)
def flow(self, x, y=None, batch_size=32, shuffle=True, seed=None,
save_to_dir=None, save_prefix='', save_format='jpeg'):
save_to_dir=None, save_prefix='', save_format='png'):
return NumpyArrayIterator(
x, y, self,
batch_size=batch_size,
@@ -460,7 +471,7 @@ class ImageDataGenerator(object):
batch_size=32, shuffle=True, seed=None,
save_to_dir=None,
save_prefix='',
save_format='jpeg',
save_format='png',
follow_links=False):
return DirectoryIterator(
directory, self,
@@ -529,7 +540,13 @@ class ImageDataGenerator(object):
# Returns
A randomly transformed version of the input (same shape).
# Raises
ImportError: if Scipy is not available.
"""
if ndi is None:
raise ImportError('Scipy is required for image transformations.')
# x is a single image, so it doesn't have image number at index 0
img_row_axis = self.row_axis - 1
img_col_axis = self.col_axis - 1
@@ -628,6 +645,7 @@ class ImageDataGenerator(object):
# Raises
ValueError: in case of invalid input `x`.
ImportError: if Scipy is not available.
"""
x = np.asarray(x, dtype=K.floatx())
if x.ndim != 4:
@@ -668,10 +686,13 @@ class ImageDataGenerator(object):
x /= (self.std + K.epsilon())
if self.zca_whitening:
if linalg is None:
raise ImportError('Scipy is required for zca_whitening.')
flat_x = np.reshape(x, (x.shape[0], x.shape[1] * x.shape[2] * x.shape[3]))
sigma = np.dot(flat_x.T, flat_x) / flat_x.shape[0]
u, s, _ = linalg.svd(sigma)
self.principal_components = np.dot(np.dot(u, np.diag(1. / np.sqrt(s + 10e-7))), u.T)
self.principal_components = np.dot(np.dot(u, np.diag(1. / np.sqrt(s + self.zca_epsilon))), u.T)
class Iterator(object):
@@ -718,7 +739,7 @@ class Iterator(object):
yield (index_array[current_index: current_index + current_batch_size],
current_index, current_batch_size)
def __iter__(self):
def __iter__(self): # pylint: disable=non-iterator-returned
# Needed if we want to do something like:
# for x, y in data_gen.flow(...):
return self
@@ -752,7 +773,7 @@ class NumpyArrayIterator(Iterator):
def __init__(self, x, y, image_data_generator,
batch_size=32, shuffle=False, seed=None,
data_format=None,
save_to_dir=None, save_prefix='', save_format='jpeg'):
save_to_dir=None, save_prefix='', save_format='png'):
if y is not None and len(x) != len(y):
raise ValueError('X (images tensor) and y (labels) '
'should have the same length. '
@@ -838,6 +859,8 @@ class DirectoryIterator(Iterator):
`"binary"`: binary targets (if there are only two classes),
`"categorical"`: categorical targets,
`"sparse"`: integer targets,
`"input"`: targets are images identical to input images (mainly
used to work with autoencoders),
`None`: no targets get yielded (only input images are yielded).
batch_size: Integer, size of a batch.
shuffle: Boolean, whether to shuffle the data between epochs.
@@ -858,7 +881,7 @@ class DirectoryIterator(Iterator):
classes=None, class_mode='categorical',
batch_size=32, shuffle=True, seed=None,
data_format=None,
save_to_dir=None, save_prefix='', save_format='jpeg',
save_to_dir=None, save_prefix='', save_format='png',
follow_links=False):
if data_format is None:
data_format = K.image_data_format()
@@ -881,10 +904,12 @@ class DirectoryIterator(Iterator):
else:
self.image_shape = (1,) + self.target_size
self.classes = classes
if class_mode not in {'categorical', 'binary', 'sparse', None}:
if class_mode not in {'categorical', 'binary', 'sparse',
'input', None}:
raise ValueError('Invalid class_mode:', class_mode,
'; expected one of "categorical", '
'"binary", "sparse", or None.')
'"binary", "sparse", "input"'
' or None.')
self.class_mode = class_mode
self.save_to_dir = save_to_dir
self.save_prefix = save_prefix
@@ -972,7 +997,9 @@ class DirectoryIterator(Iterator):
format=self.save_format)
img.save(os.path.join(self.save_to_dir, fname))
# build batch of labels
if self.class_mode == 'sparse':
if self.class_mode == 'input':
batch_y = batch_x.copy()
elif self.class_mode == 'sparse':
batch_y = self.classes[index_array]
elif self.class_mode == 'binary':
batch_y = self.classes[index_array].astype(K.floatx())
+9 -4
Ver Arquivo
@@ -1,8 +1,13 @@
# -*- coding: utf-8 -*-
"""Preprocessing utilities for sequence data.
"""
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
import random
import numpy as np
import random
from six.moves import range
@@ -50,16 +55,16 @@ def pad_sequences(sequences, maxlen=None, dtype='int32',
# checking for consistency in the main loop below.
sample_shape = tuple()
for s in sequences:
if len(s) > 0:
if len(s) > 0: # pylint: disable=g-explicit-length-test
sample_shape = np.asarray(s).shape[1:]
break
x = (np.ones((num_samples, maxlen) + sample_shape) * value).astype(dtype)
for idx, s in enumerate(sequences):
if not len(s):
if not len(s): # pylint: disable=g-explicit-length-test
continue # empty list/array was found
if truncating == 'pre':
trunc = s[-maxlen:]
trunc = s[-maxlen:] # pylint: disable=invalid-unary-operand-type
elif truncating == 'post':
trunc = s[:maxlen]
else:
+6 -4
Ver Arquivo
@@ -5,12 +5,14 @@ May benefit from a fast Cython rewrite.
"""
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
import string
import sys
import numpy as np
from six.moves import range
from six.moves import zip
from collections import OrderedDict
import warnings
if sys.version_info < (3,):
@@ -22,7 +24,7 @@ else:
def text_to_word_sequence(text,
filters='!"#$%&()*+,-./:;<=>?@[\\]^_`{|}~\t\n',
lower=True, split=" "):
"""Converts a text to a sequence of word indices.
"""Converts a text to a sequence of words (or tokens).
# Arguments
text: Input text (string).
@@ -31,7 +33,7 @@ def text_to_word_sequence(text,
split: Sentence split marker (string).
# Returns
A list of integer word indices.
A list of words (or tokens).
"""
if lower:
text = text.lower()
@@ -68,7 +70,7 @@ class Tokenizer(object):
tabs and line breaks, minus the `'` character.
lower: boolean. Whether to convert the texts to lowercase.
split: character or string to use for token splitting.
char_level: if True, every character will be treated as a word.
char_level: if True, every character will be treated as a token.
By default, all punctuation is removed, turning the texts into
space-separated sequences of words
@@ -92,7 +94,7 @@ class Tokenizer(object):
if kwargs:
raise TypeError('Unrecognized keyword arguments: ' + str(kwargs))
self.word_counts = {}
self.word_counts = OrderedDict()
self.word_docs = {}
self.filters = filters
self.split = split
+9 -4
Ver Arquivo
@@ -1,8 +1,13 @@
"""Keras built-in regularizers.
"""
from __future__ import absolute_import
import six
from __future__ import division
from __future__ import print_function
from . import backend as K
from .utils.generic_utils import serialize_keras_object
import six
from .utils.generic_utils import deserialize_keras_object
from .utils.generic_utils import serialize_keras_object
class Regularizer(object):
@@ -25,7 +30,7 @@ class L1L2(Regularizer):
l2: Float; L2 regularization factor.
"""
def __init__(self, l1=0., l2=0.):
def __init__(self, l1=0., l2=0.): # pylint: disable=redefined-outer-name
self.l1 = K.cast_to_floatx(l1)
self.l2 = K.cast_to_floatx(l2)
@@ -53,7 +58,7 @@ def l2(l=0.01):
return L1L2(l2=l)
def l1_l2(l1=0.01, l2=0.01):
def l1_l2(l1=0.01, l2=0.01): # pylint: disable=redefined-outer-name
return L1L2(l1=l1, l2=l2)
+6 -1
Ver Arquivo
@@ -1,9 +1,14 @@
"""Keras utilities.
"""
from __future__ import absolute_import
from . import np_utils
from __future__ import division
from __future__ import print_function
from . import conv_utils
from . import data_utils
from . import generic_utils
from . import io_utils
from . import np_utils
# Globally-importable utils.
from .io_utils import HDF5Matrix
+19 -15
Ver Arquivo
@@ -1,6 +1,12 @@
from six.moves import range
import numpy as np
"""Utilities used by convolution layers.
"""
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
from .. import backend as K
import numpy as np
from six.moves import range
def normalize_tuple(value, n, name):
@@ -55,11 +61,9 @@ def normalize_data_format(value):
def normalize_padding(value):
padding = value.lower()
allowed = {'valid', 'same', 'causal'}
if K.backend() == 'theano':
allowed.add('full')
if padding not in allowed:
raise ValueError('The `padding` argument must be one of "valid", "same" (or "causal" for Conv1D). '
if padding not in {'valid', 'same', 'causal'}:
raise ValueError('The `padding` argument must be one of '
'"valid", "same" (or "causal", only for `Conv1D). '
'Received: ' + str(padding))
return padding
@@ -70,7 +74,7 @@ def convert_kernel(kernel):
Also works reciprocally, since the transformation is its own inverse.
# Arguments
kernel: Numpy array (4D or 5D).
kernel: Numpy array (3D, 4D or 5D).
# Returns
The converted kernel.
@@ -78,7 +82,8 @@ def convert_kernel(kernel):
# Raises
ValueError: in case of invalid kernel shape or invalid data_format.
"""
if not 4 <= kernel.ndim <= 5:
kernel = np.asarray(kernel)
if not 3 <= kernel.ndim <= 5:
raise ValueError('Invalid kernel shape:', kernel.shape)
slices = [slice(None, None, -1) for _ in range(kernel.ndim)]
no_flip = (slice(None, None), slice(None, None))
@@ -108,10 +113,10 @@ def conv_output_length(input_length, filter_size,
output_length = input_length
elif padding == 'valid':
output_length = input_length - dilated_filter_size + 1
elif padding == 'causal':
output_length = input_length
elif padding == 'full':
output_length = input_length + dilated_filter_size - 1
elif padding == 'causal':
output_length = input_length
return (output_length + stride - 1) // stride
@@ -142,10 +147,9 @@ def conv_input_length(output_length, filter_size, padding, stride):
def deconv_length(dim_size, stride_size, kernel_size, padding):
if dim_size is None:
return None
dim_size *= stride_size
if padding == 'valid':
dim_size = dim_size * stride_size + max(kernel_size - stride_size, 0)
dim_size += max(kernel_size - stride_size, 0)
elif padding == 'full':
dim_size = dim_size * stride_size - (stride_size + kernel_size - 2)
elif padding == 'same':
dim_size = dim_size * stride_size
dim_size -= (stride_size + kernel_size - 2)
return dim_size
+24 -17
Ver Arquivo
@@ -1,19 +1,19 @@
"""Utilities for file download and caching."""
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
import functools
import hashlib
import tarfile
import zipfile
import os
import sys
import shutil
import hashlib
import six
from six.moves.urllib.request import urlopen
from six.moves.urllib.error import URLError
from six.moves.urllib.error import HTTPError
import sys
from six.moves.urllib.error import HTTPError
from six.moves.urllib.error import URLError
from six.moves.urllib.request import urlopen
from ..utils.generic_utils import Progbar
@@ -36,8 +36,10 @@ if sys.version_info[0] == 2:
data: `data` argument passed to `urlopen`.
"""
def chunk_read(response, chunk_size=8192, reporthook=None):
total_size = response.info().get('Content-Length').strip()
total_size = int(total_size)
content_type = response.info().get('Content-Length')
total_size = -1
if content_type is not None:
total_size = int(content_type.strip())
count = 0
while 1:
chunk = response.read(chunk_size)
@@ -54,7 +56,7 @@ if sys.version_info[0] == 2:
for chunk in chunk_read(response, reporthook=reporthook):
fd.write(chunk)
else:
from six.moves.urllib.request import urlretrieve
from six.moves.urllib.request import urlretrieve # pylint: disable=g-import-not-at-top
def _extract_archive(file_path, path='.', archive_format='auto'):
@@ -186,19 +188,24 @@ def get_file(fname,
if download:
print('Downloading data from', origin)
progbar = None
def dl_progress(count, block_size, total_size, progbar=None):
if progbar is None:
progbar = Progbar(total_size)
class ProgressTracker(object):
# Maintain progbar for the lifetime of download.
# This design was chosen for Python 2.7 compatibility.
progbar = None
def dl_progress(count, block_size, total_size):
if ProgressTracker.progbar is None:
if total_size is -1:
total_size = None
ProgressTracker.progbar = Progbar(total_size)
else:
progbar.update(count * block_size)
ProgressTracker.progbar.update(count * block_size)
error_msg = 'URL fetch failure on {}: {} -- {}'
try:
try:
urlretrieve(origin, fpath,
functools.partial(dl_progress, progbar=progbar))
urlretrieve(origin, fpath, dl_progress)
except URLError as e:
raise Exception(error_msg.format(origin, e.errno, e.reason))
except HTTPError as e:
@@ -207,7 +214,7 @@ def get_file(fname,
if os.path.exists(fpath):
os.remove(fpath)
raise
progbar = None
ProgressTracker.progbar = None
if untar:
if not os.path.exists(untar_fpath):
+40 -32
Ver Arquivo
@@ -1,14 +1,16 @@
"""Python utilities required by Keras."""
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
import inspect
import marshal
import sys
import time
import types as python_types
import numpy as np
import time
import sys
import six
import marshal
import types as python_types
import inspect
_GLOBAL_CUSTOM_OBJECTS = {}
@@ -27,8 +29,8 @@ class CustomObjectScope(object):
Consider a custom object `MyObject`
```python
with CustomObjectScope({"MyObject":MyObject}):
layer = Dense(..., W_regularizer="MyObject")
with CustomObjectScope({'MyObject':MyObject}):
layer = Dense(..., kernel_regularizer='MyObject')
# save, load, etc. will recognize custom object by name
```
"""
@@ -63,8 +65,8 @@ def custom_object_scope(*args):
Consider a custom object `MyObject`
```python
with custom_object_scope({"MyObject":MyObject}):
layer = Dense(..., W_regularizer="MyObject")
with custom_object_scope({'MyObject':MyObject}):
layer = Dense(..., kernel_regularizer='MyObject')
# save, load, etc. will recognize custom object by name
```
@@ -89,7 +91,7 @@ def get_custom_objects():
```python
get_custom_objects().clear()
get_custom_objects()["MyObject"] = MyObject
get_custom_objects()['MyObject'] = MyObject
```
# Returns
@@ -133,17 +135,20 @@ def deserialize_keras_object(identifier, module_objects=None,
': ' + class_name)
if hasattr(cls, 'from_config'):
arg_spec = inspect.getargspec(cls.from_config)
custom_objects = custom_objects or {}
if 'custom_objects' in arg_spec.args:
custom_objects = custom_objects or {}
return cls.from_config(config['config'],
custom_objects=dict(list(_GLOBAL_CUSTOM_OBJECTS.items()) +
list(custom_objects.items())))
return cls.from_config(config['config'])
with CustomObjectScope(custom_objects):
return cls.from_config(config['config'])
else:
# Then `cls` may be a function returning a class.
# in this case by convention `config` holds
# the kwargs of the function.
return cls(**config['config'])
custom_objects = custom_objects or {}
with CustomObjectScope(custom_objects):
return cls(**config['config'])
elif isinstance(identifier, six.string_types):
function_name = identifier
if custom_objects and function_name in custom_objects:
@@ -153,7 +158,7 @@ def deserialize_keras_object(identifier, module_objects=None,
else:
fn = module_objects.get(function_name)
if fn is None:
raise ValueError('Unknown ' + printable_module_name,
raise ValueError('Unknown ' + printable_module_name +
':' + function_name)
return fn
else:
@@ -208,12 +213,14 @@ class Progbar(object):
"""Displays a progress bar.
# Arguments
target: Total number of steps expected.
target: Total number of steps expected, None if unknown.
interval: Minimum visual progress update interval (in seconds).
"""
def __init__(self, target, width=30, verbose=1, interval=0.05):
self.width = width
if target is None:
target = -1
self.target = target
self.sum_values = {}
self.unique_values = []
@@ -253,21 +260,22 @@ class Progbar(object):
sys.stdout.write('\b' * prev_total_width)
sys.stdout.write('\r')
numdigits = int(np.floor(np.log10(self.target))) + 1
barstr = '%%%dd/%%%dd [' % (numdigits, numdigits)
bar = barstr % (current, self.target)
prog = float(current) / self.target
prog_width = int(self.width * prog)
if prog_width > 0:
bar += ('=' * (prog_width - 1))
if current < self.target:
bar += '>'
else:
bar += '='
bar += ('.' * (self.width - prog_width))
bar += ']'
sys.stdout.write(bar)
self.total_width = len(bar)
if self.target is not -1:
numdigits = int(np.floor(np.log10(self.target))) + 1
barstr = '%%%dd/%%%dd [' % (numdigits, numdigits)
bar = barstr % (current, self.target)
prog = float(current) / self.target
prog_width = int(self.width * prog)
if prog_width > 0:
bar += ('=' * (prog_width - 1))
if current < self.target:
bar += '>'
else:
bar += '='
bar += ('.' * (self.width - prog_width))
bar += ']'
sys.stdout.write(bar)
self.total_width = len(bar)
if current:
time_per_unit = (now - self.start) / current
@@ -275,7 +283,7 @@ class Progbar(object):
time_per_unit = 0
eta = time_per_unit * (self.target - current)
info = ''
if current < self.target:
if current < self.target and self.target is not -1:
info += ' - ETA: %ds' % eta
else:
info += ' - %ds' % (now - self.start)
+12 -5
Ver Arquivo
@@ -1,13 +1,15 @@
"""Utilities related to disk I/O."""
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
import numpy as np
import sys
from collections import defaultdict
import sys
import numpy as np
try:
import h5py
import h5py # pylint:disable=g-import-not-at-top
except ImportError:
h5py = None
@@ -63,8 +65,13 @@ class HDF5Matrix(object):
def __getitem__(self, key):
if isinstance(key, slice):
if key.stop + self.start <= self.end:
idx = slice(key.start + self.start, key.stop + self.start)
start, stop = key.start, key.stop
if start is None:
start = 0
if stop is None:
stop = self.data.shape[0]
if stop + self.start <= self.end:
idx = slice(start + self.start, stop + self.start)
else:
raise IndexError
elif isinstance(key, int):
+18 -39
Ver Arquivo
@@ -1,7 +1,11 @@
"""Utilities related to Keras layers.
"""
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
from .conv_utils import convert_kernel
from .. import backend as K
from .conv_utils import convert_kernel
import numpy as np
@@ -19,8 +23,11 @@ def print_summary(model, line_length=None, positions=None):
else:
sequential_like = True
for v in model.nodes_by_depth.values():
if len(v) > 1:
if (len(v) > 1) or (len(v) == 1 and len(v[0].inbound_layers) > 1):
# if the model has multiple nodes or if the nodes have multiple inbound_layers
# the model is no longer sequential
sequential_like = False
break
if sequential_like:
line_length = line_length or 65
@@ -75,12 +82,10 @@ def print_summary(model, line_length=None, positions=None):
except AttributeError:
output_shape = 'multiple'
connections = []
for node_index, node in enumerate(layer.inbound_nodes):
if relevant_nodes:
node_key = layer.name + '_ib-' + str(node_index)
if node_key not in relevant_nodes:
# node is node part of the current network
continue
for node in layer.inbound_nodes:
if relevant_nodes and node not in relevant_nodes:
# node is not part of the current network
continue
for i in range(len(node.inbound_layers)):
inbound_layer = node.inbound_layers[i].name
inbound_node_index = node.node_indices[i]
@@ -111,7 +116,10 @@ def print_summary(model, line_length=None, positions=None):
else:
print('_' * line_length)
trainable_count, non_trainable_count = count_total_params(layers, layer_set=None)
trainable_count = int(
np.sum([K.count_params(p) for p in set(model.trainable_weights)]))
non_trainable_count = int(
np.sum([K.count_params(p) for p in set(model.non_trainable_weights)]))
print('Total params: {:,}'.format(trainable_count + non_trainable_count))
print('Trainable params: {:,}'.format(trainable_count))
@@ -119,35 +127,6 @@ def print_summary(model, line_length=None, positions=None):
print('_' * line_length)
def count_total_params(layers, layer_set=None):
"""Counts the number of parameters in a list of layers.
# Arguments
layers: list of layers.
layer_set: set of layers already seen
(so that we don't count their weights twice).
# Returns
A tuple (count of trainable weights, count of non-trainable weights.)
"""
if layer_set is None:
layer_set = set()
trainable_count = 0
non_trainable_count = 0
for layer in layers:
if layer in layer_set:
continue
layer_set.add(layer)
if hasattr(layer, 'layers'):
t, nt = count_total_params(layer.layers, layer_set)
trainable_count += t
non_trainable_count += nt
else:
trainable_count += np.sum([K.count_params(p) for p in layer.trainable_weights])
non_trainable_count += np.sum([K.count_params(p) for p in layer.non_trainable_weights])
return int(trainable_count), int(non_trainable_count)
def convert_all_kernels_in_model(model):
"""Converts all convolution kernels in a model from Theano to TensorFlow.
@@ -192,7 +171,7 @@ def convert_dense_weights_data_format(dense,
came before the target `Dense` layer.
target_data_format: One of "channels_last", "channels_first".
Set it "channels_last"
if converting a "chnnels_first" model to "channels_last",
if converting a "channels_first" model to "channels_last",
or reciprocally.
"""
assert target_data_format in {'channels_last', 'channels_first'}
+2
Ver Arquivo
@@ -1,5 +1,7 @@
"""Numpy-related utilities."""
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
import numpy as np
+7 -5
Ver Arquivo
@@ -1,4 +1,8 @@
"""Utilities related to Keras unit tests."""
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
import numpy as np
from numpy.testing import assert_allclose
import inspect
@@ -6,7 +10,6 @@ import six
from ..engine import Model, Input
from ..models import Sequential
from ..models import model_from_json
from .. import backend as K
@@ -85,7 +88,7 @@ def layer_test(layer_cls, kwargs={}, input_shape=None, input_dtype=None,
# check shape inference
model = Model(x, y)
expected_output_shape = layer.compute_output_shape(input_shape)
expected_output_shape = tuple(layer._compute_output_shape(input_shape).as_list())
actual_output = model.predict(input_data)
actual_output_shape = actual_output.shape
for expected_dim, actual_dim in zip(expected_output_shape,
@@ -142,7 +145,7 @@ def layer_test(layer_cls, kwargs={}, input_shape=None, input_dtype=None,
def keras_test(func):
"""Function wrapper to clean up after TensorFlow tests.
"""Function wrapper to clean up after tests.
# Arguments
func: test function to clean up after.
@@ -153,7 +156,6 @@ def keras_test(func):
@six.wraps(func)
def wrapper(*args, **kwargs):
output = func(*args, **kwargs)
if K.backend() == 'tensorflow':
K.clear_session()
K.clear_session()
return output
return wrapper
+43 -12
Ver Arquivo
@@ -1,40 +1,57 @@
"""Utilities related to model visualization."""
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
import os
try:
# pydot-ng is a fork of pydot that is better maintained.
import pydot_ng as pydot
import pydot_ng as pydot # pylint: disable=g-import-not-at-top
except ImportError:
# Fall back on pydot if necessary.
try:
import pydot
import pydot # pylint: disable=g-import-not-at-top
except ImportError:
pydot = None
def _check_pydot():
if not (pydot and pydot.find_graphviz()):
try:
# Attempt to create an image of a blank graph
# to check the pydot/graphviz installation.
pydot.Dot.create(pydot.Dot())
except Exception:
# pydot raises a generic Exception here,
# so no specific class can be caught.
raise ImportError('Failed to import pydot. You must install pydot'
' and graphviz for `pydotprint` to work.')
def model_to_dot(model, show_shapes=False, show_layer_names=True):
"""Converts a Keras model to dot format.
def model_to_dot(model,
show_shapes=False,
show_layer_names=True,
rankdir='TB'):
"""Convert a Keras model to dot format.
# Arguments
model: A Keras model instance.
show_shapes: whether to display shape information.
show_layer_names: whether to display layer names.
rankdir: `rankdir` argument passed to PyDot,
a string specifying the format of the plot:
'TB' creates a vertical plot;
'LR' creates a horizontal plot.
# Returns
A `pydot.Dot` instance representing the Keras model.
"""
from ..layers.wrappers import Wrapper
from ..models import Sequential
from ..layers.wrappers import Wrapper # pylint: disable=g-import-not-at-top
from ..models import Sequential # pylint: disable=g-import-not-at-top
_check_pydot()
dot = pydot.Dot()
dot.set('rankdir', 'TB')
dot.set('rankdir', rankdir)
dot.set('concentrate', True)
dot.set_node_defaults(shape='record')
@@ -75,8 +92,9 @@ def model_to_dot(model, show_shapes=False, show_layer_names=True):
[str(ishape) for ishape in layer.input_shapes])
else:
inputlabels = 'multiple'
label = '%s\n|{input:|output:}|{{%s}|{%s}}' % (label, inputlabels, outputlabels)
label = '%s\n|{input:|output:}|{{%s}|{%s}}' % (label,
inputlabels,
outputlabels)
node = pydot.Node(layer_id, label=label)
dot.add_node(node)
@@ -96,8 +114,21 @@ def model_to_dot(model, show_shapes=False, show_layer_names=True):
def plot_model(model,
to_file='model.png',
show_shapes=False,
show_layer_names=True):
dot = model_to_dot(model, show_shapes, show_layer_names)
show_layer_names=True,
rankdir='TB'):
"""Converts a Keras model to dot format and save to a file.
# Arguments
model: A Keras model instance
to_file: File name of the plot image.
show_shapes: whether to display shape information.
show_layer_names: whether to display layer names.
rankdir: `rankdir` argument passed to PyDot,
a string specifying the format of the plot:
'TB' creates a vertical plot;
'LR' creates a horizontal plot.
"""
dot = model_to_dot(model, show_shapes, show_layer_names, rankdir)
_, extension = os.path.splitext(to_file)
if not extension:
extension = 'png'
+7
Ver Arquivo
@@ -0,0 +1,7 @@
"""Keras API wrappers.
"""
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
from . import scikit_learn
+7 -4
Ver Arquivo
@@ -1,13 +1,16 @@
"""API wrapper allowing to use certain Keras models with the Scikit-Learn API.
"""
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
import copy
import inspect
import types
import numpy as np
from ..utils.np_utils import to_categorical
from ..models import Sequential
import numpy as np
from ..utils.np_utils import to_categorical
class BaseWrapper(object):
@@ -86,7 +89,7 @@ class BaseWrapper(object):
raise ValueError(
'{} is not a legal parameter'.format(params_name))
def get_params(self, **params):
def get_params(self, **params): # pylint: disable=unused-argument
"""Gets parameters for this estimator.
# Arguments
+2 -2
Ver Arquivo
@@ -3,12 +3,12 @@ from setuptools import find_packages
setup(name='Keras',
version='2.0.3',
version='2.0.4-tf',
description='Deep Learning for Python',
author='Francois Chollet',
author_email='francois.chollet@gmail.com',
url='https://github.com/fchollet/keras',
download_url='https://github.com/fchollet/keras/tarball/2.0.3',
download_url='https://github.com/fchollet/keras/tarball/2.0.4',
license='MIT',
install_requires=['theano', 'pyyaml', 'six'],
extras_require={
+2 -1
Ver Arquivo
@@ -103,4 +103,5 @@ def test_inceptionv3_pooling():
if __name__ == '__main__':
pytest.main([__file__])
# pytest.main([__file__])
test_vgg16()
+54
Ver Arquivo
@@ -77,12 +77,31 @@ def check_composed_tensor_operations(first_function_name, first_function_args,
class TestBackend(object):
def test_is_keras_tensor(self):
for K in [KTH, KTF]:
np_var = np.array([1, 2])
try:
K.is_keras_tensor(np_var)
assert True is False
except ValueError:
# This is the expected behavior
continue
keras_var = K.variable(np_var)
assert K.is_keras_tensor(keras_var) is True
keras_placeholder = K.placeholder(shape=(2, 4, 5))
assert K.is_keras_tensor(keras_placeholder) is True
def test_linear_operations(self):
check_two_tensor_operation('dot', (4, 2), (2, 4))
check_two_tensor_operation('dot', (4, 2), (5, 2, 3))
check_two_tensor_operation('batch_dot', (4, 2, 3), (4, 5, 3),
axes=(2, 2))
check_two_tensor_operation('batch_dot', (4, 2, 3), (4, 3),
axes=(2, 1))
check_two_tensor_operation('batch_dot', (4, 2), (4, 2, 3),
axes=(1, 1))
check_two_tensor_operation('batch_dot', (32, 20), (32, 20), axes=1)
check_two_tensor_operation('batch_dot', (32, 20), (32, 20), axes=(1, 1))
check_single_tensor_operation('transpose', (4, 2))
@@ -576,6 +595,41 @@ class TestBackend(object):
assert_allclose(tf_last_output, th_last_output, atol=1e-04)
assert_allclose(tf_outputs, th_outputs, atol=1e-04)
@pytest.mark.parametrize('x_np,axis,keepdims', [
(np.array([1.1, 0.8, 0.9]), 0, False),
(np.array([[1.1, 0.8, 0.9]]), 0, False),
(np.array([[1.1, 0.8, 0.9]]), 1, False),
(np.array([[1.1, 0.8, 0.9]]), -1, False),
(np.array([[1.1, 0.8, 0.9]]), 1, True),
(np.array([[1.1], [1.2]]), 0, False),
(np.array([[1.1], [1.2]]), 1, False),
(np.array([[1.1], [1.2]]), -1, False),
(np.array([[1.1], [1.2]]), -1, True),
(np.array([[1.1, 1.2, 1.3], [0.9, 0.7, 1.4]]), None, False),
(np.array([[1.1, 1.2, 1.3], [0.9, 0.7, 1.4]]), 0, False),
(np.array([[1.1, 1.2, 1.3], [0.9, 0.7, 1.4]]), 1, False),
(np.array([[1.1, 1.2, 1.3], [0.9, 0.7, 1.4]]), -1, False),
])
@pytest.mark.parametrize('K', [KTH, KTF], ids=["KTH", "KTF"])
def test_logsumexp(self, x_np, axis, keepdims, K):
'''
Check if K.logsumexp works properly for values close to one.
'''
x = K.variable(x_np)
assert_allclose(K.eval(K.logsumexp(x, axis=axis, keepdims=keepdims)),
np.log(np.sum(np.exp(x_np), axis=axis, keepdims=keepdims)),
rtol=1e-5)
@pytest.mark.parametrize('K', [KTH, KTF], ids=["KTH", "KTF"])
def test_logsumexp_optim(self, K):
'''
Check if optimization works.
'''
x_np = np.array([1e+4, 1e-4])
assert_allclose(K.eval(K.logsumexp(K.variable(x_np), axis=0)),
1e4,
rtol=1e-5)
def test_switch(self):
val = np.random.random()
xth = KTH.variable(val)
+120 -36
Ver Arquivo
@@ -1,6 +1,7 @@
import pytest
import json
import numpy as np
import tensorflow as tf
from keras.layers import Dense, Dropout, InputLayer
from keras import layers
@@ -144,7 +145,7 @@ def test_node_construction():
a = Input(shape=(32,), name='input_a')
b = Input(shape=(32,), name='input_b')
assert a._keras_shape == (None, 32)
assert a.get_shape().as_list() == [None, 32]
a_layer, a_node_index, a_tensor_index = a._keras_history
b_layer, b_node_index, b_tensor_index = b._keras_history
assert len(a_layer.inbound_nodes) == 1
@@ -185,7 +186,7 @@ def test_node_construction():
# test layer properties
test_layer = Dense(16, name='test_layer')
a_test = test_layer(a)
assert K.int_shape(test_layer.kernel) == (32, 16)
assert test_layer.kernel.get_shape().as_list() == [32, 16]
assert test_layer.input == a
assert test_layer.output == a_test
assert test_layer.input_mask is None
@@ -228,7 +229,7 @@ def test_multi_input_layer():
b_2 = dense(b)
merged = layers.concatenate([a_2, b_2], name='merge')
assert merged._keras_shape == (None, 16 * 2)
assert merged.get_shape().as_list() == [None, 16 * 2]
merge_layer, merge_node_index, merge_tensor_index = merged._keras_history
assert merge_node_index == 0
@@ -250,15 +251,14 @@ def test_multi_input_layer():
print('model.input_layers_tensor_indices:', model.input_layers_tensor_indices)
print('model.output_layers', model.output_layers)
print('output_shape:', model.compute_output_shape([(None, 32), (None, 32)]))
assert model.compute_output_shape([(None, 32), (None, 32)]) == [(None, 64), (None, 5)]
print('output_shape:', model._compute_output_shape([(None, 32), (None, 32)]))
output_shapes = model._compute_output_shape([(None, 32), (None, 32)])
assert output_shapes[0].as_list() == [None, 64]
assert output_shapes[1].as_list() == [None, 5]
print('mask:', model.compute_mask([a, b], [None, None]))
assert model.compute_mask([a, b], [None, None]) == [None, None]
print('output_shape:', model.compute_output_shape([(None, 32), (None, 32)]))
assert model.compute_output_shape([(None, 32), (None, 32)]) == [(None, 64), (None, 5)]
# we don't check names of first 2 layers (inputs) because
# ordering of same-level layers is not fixed
print('layers:', [layer.name for layer in model.layers])
@@ -320,10 +320,8 @@ def test_recursion():
f = Input(shape=(32,), name='input_f')
g, h = model([e, f])
# g2, h2 = model([e, f])
assert g._keras_shape == c._keras_shape
assert h._keras_shape == d._keras_shape
assert g.get_shape().as_list() == c.get_shape().as_list()
assert h.get_shape().as_list() == d.get_shape().as_list()
# test separate manipulation of different layer outputs
i = Dense(7, name='dense_4')(h)
@@ -341,8 +339,8 @@ def test_recursion():
print(model.compute_mask([e, f], [None, None]))
assert model.compute_mask([e, f], [None, None]) == [None, None]
print(final_model.compute_output_shape([(10, 32), (10, 32)]))
assert final_model.compute_output_shape([(10, 32), (10, 32)]) == [(10, 7), (10, 64)]
print(final_model._compute_output_shape([(10, 32), (10, 32)]))
assert final_model._compute_output_shape([(10, 32), (10, 32)]) == [(10, 7), (10, 64)]
# run recursive model
fn = K.function(final_model.inputs, final_model.outputs)
@@ -373,10 +371,10 @@ def test_recursion():
p = Input(shape=(32,), name='input_p')
q, r = model([o, p])
assert n._keras_shape == (None, 5)
assert q._keras_shape == (None, 64)
assert n.get_shape().as_list() == [None, 5]
assert q.get_shape().as_list() == [None, 64]
s = layers.concatenate([n, q], name='merge_nq')
assert s._keras_shape == (None, 64 + 5)
assert s.get_shape().as_list() == [None, 64 + 5]
# test with single output as 1-elem list
multi_io_model = Model([j, k, o, p], [s])
@@ -464,32 +462,91 @@ def test_recursion():
####################################################
# test calling layers/models on TF tensors
if K._BACKEND == 'tensorflow':
import tensorflow as tf
j = Input(shape=(32,), name='input_j')
k = Input(shape=(32,), name='input_k')
m, n = model([j, k])
tf_model = Model([j, k], [m, n])
j = Input(shape=(32,), name='input_j')
k = Input(shape=(32,), name='input_k')
m, n = model([j, k])
tf_model = Model([j, k], [m, n])
j_tf = tf.placeholder(dtype=K.floatx())
k_tf = tf.placeholder(dtype=K.floatx())
m_tf, n_tf = tf_model([j_tf, k_tf])
assert m_tf.get_shape().as_list() == [None, 64]
assert n_tf.get_shape().as_list() == [None, 5]
j_tf = tf.placeholder(dtype=K.floatx())
k_tf = tf.placeholder(dtype=K.floatx())
m_tf, n_tf = tf_model([j_tf, k_tf])
assert m_tf.get_shape().as_list() == [None, 64]
assert n_tf.get_shape().as_list() == [None, 5]
# test merge
layers.concatenate([j_tf, k_tf], axis=1)
layers.add([j_tf, k_tf])
# test merge
layers.concatenate([j_tf, k_tf], axis=1)
layers.add([j_tf, k_tf])
# test tensor input
x = tf.placeholder(shape=(None, 2), dtype=K.floatx())
InputLayer(input_tensor=x)
# test tensor input
x = tf.placeholder(shape=(None, 2), dtype=K.floatx())
InputLayer(input_tensor=x)
x = Input(tensor=x)
Dense(2)(x)
x = Input(tensor=x)
Dense(2)(x)
@keras_test
def test_load_layers():
from keras.layers import ConvLSTM2D, TimeDistributed, Bidirectional, Conv2D, Input
from keras.models import Model
from keras.engine.topology import preprocess_weights_for_loading
if K.backend() == 'tensorflow':
inputs = Input(shape=(10, 20, 20, 1))
else:
inputs = Input(shape=(10, 1, 20, 20))
td_conv = TimeDistributed(Conv2D(15, (5, 5)))(inputs)
bi_convlstm2d = Bidirectional(ConvLSTM2D(10, (3, 3)), merge_mode='concat')(td_conv)
model = Model(inputs=inputs, outputs=bi_convlstm2d)
weight_value_tuples = []
# TimeDistributed Conv2D layer
# use 'channels_first' data format to check that the function is being called correctly for Conv2D
# old: (filters, stack_size, kernel_rows, kernel_cols)
# new: (kernel_rows, kernel_cols, stack_size, filters)
weight_tensor_td_conv_old = list()
weight_tensor_td_conv_old.append(np.zeros((15, 1, 5, 5)))
weight_tensor_td_conv_old.append(np.zeros((15,)))
td_conv_layer = model.layers[1]
td_conv_layer.layer.data_format = 'channels_first'
weight_tensor_td_conv_new = preprocess_weights_for_loading(td_conv_layer,
weight_tensor_td_conv_old,
original_keras_version='1')
symbolic_weights = td_conv_layer.weights
assert (len(symbolic_weights) == len(weight_tensor_td_conv_new))
weight_value_tuples += zip(symbolic_weights, weight_tensor_td_conv_new)
# Bidirectional ConvLSTM2D layer
# old ConvLSTM2D took a list of 12 weight tensors, returns a list of 3 concatenated larger tensors.
weight_tensor_bi_convlstm_old = []
for j in range(2): # bidirectional
for i in range(4):
weight_tensor_bi_convlstm_old.append(np.zeros((3, 3, 15, 10))) # kernel
weight_tensor_bi_convlstm_old.append(np.zeros((3, 3, 10, 10))) # recurrent kernel
weight_tensor_bi_convlstm_old.append(np.zeros((10,))) # bias
bi_convlstm_layer = model.layers[2]
weight_tensor_bi_convlstm_new = preprocess_weights_for_loading(bi_convlstm_layer,
weight_tensor_bi_convlstm_old,
original_keras_version='1')
symbolic_weights = bi_convlstm_layer.weights
assert (len(symbolic_weights) == len(weight_tensor_bi_convlstm_new))
weight_value_tuples += zip(symbolic_weights, weight_tensor_bi_convlstm_new)
K.batch_set_value(weight_value_tuples)
assert np.all(K.eval(model.layers[1].weights[0]) == weight_tensor_td_conv_new[0])
assert np.all(K.eval(model.layers[1].weights[1]) == weight_tensor_td_conv_new[1])
assert np.all(K.eval(model.layers[2].weights[0]) == weight_tensor_bi_convlstm_new[0])
assert np.all(K.eval(model.layers[2].weights[1]) == weight_tensor_bi_convlstm_new[1])
assert np.all(K.eval(model.layers[2].weights[2]) == weight_tensor_bi_convlstm_new[2])
assert np.all(K.eval(model.layers[2].weights[3]) == weight_tensor_bi_convlstm_new[3])
assert np.all(K.eval(model.layers[2].weights[4]) == weight_tensor_bi_convlstm_new[4])
assert np.all(K.eval(model.layers[2].weights[5]) == weight_tensor_bi_convlstm_new[5])
def test_recursion_with_bn_and_loss():
model1 = Sequential([
layers.Dense(5, input_dim=5, activity_regularizer='l1'),
@@ -516,5 +573,32 @@ def test_recursion_with_bn_and_loss():
model2.fit(x, y, verbose=0, epochs=1)
def test_shared_layer_depth_is_correct():
# Basic outline here: we have a shared embedding layer, and two inputs that go through
# different depths of computation in the graph before the final output. We need the computed
# depth of the input layers to be the same, because they both pass through the embedding layer
# before anything else happens. That's what we're testing.
from keras.layers import Embedding, Input, Dense, Concatenate
from keras.models import Model
input1 = Input(shape=(10,), name="input1")
input2 = Input(shape=(10,), name="input2")
embedding_layer = Embedding(name="embedding", input_dim=5, output_dim=10)
embedded_input1 = embedding_layer(input1)
embedded_input2 = embedding_layer(input2)
transformed_input2 = Dense(6)(Dense(5)(Dense(3)(embedded_input2)))
final_output = Dense(2)(Concatenate()([embedded_input1, transformed_input2]))
model = Model(inputs=[input1, input2], outputs=final_output)
input1_depth = -1
input2_depth = -1
for depth, layers in model.layers_by_depth.items():
for layer in layers:
if layer.name == 'input1':
input1_depth = depth
if layer.name == 'input2':
input2_depth = depth
assert input1_depth != -1
assert input1_depth == input2_depth
if __name__ == '__main__':
pytest.main([__file__])
@@ -3,8 +3,8 @@ import numpy as np
from numpy.testing import assert_allclose
from keras import backend as K
from keras.models import Sequential
from keras.layers import convolutional_recurrent
from keras.models import Sequential, Model
from keras.layers import convolutional_recurrent, Input
from keras.utils.test_utils import layer_test
from keras import regularizers
@@ -116,5 +116,18 @@ def test_convolutional_recurrent():
'recurrent_dropout': 0.1},
input_shape=inputs.shape)
# check state initialization
layer = convolutional_recurrent.ConvLSTM2D(filters=filters,
kernel_size=(num_row, num_col),
data_format=data_format,
return_sequences=return_sequences)
layer.build(inputs.shape)
x = Input(batch_shape=inputs.shape)
initial_state = layer.get_initial_state(x)
y = layer(x, initial_state=initial_state)
model = Model(x, y)
assert model.predict(inputs).shape == layer._compute_output_shape(inputs.shape)
if __name__ == '__main__':
pytest.main([__file__])
+47 -13
Ver Arquivo
@@ -494,20 +494,54 @@ def test_zero_padding_3d():
stack_size))
# basic test
layer_test(convolutional.ZeroPadding3D,
kwargs={'padding': (2, 2, 2)},
input_shape=inputs.shape)
for data_format in ['channels_first', 'channels_last']:
layer_test(convolutional.ZeroPadding3D,
kwargs={'padding': (2, 2, 2), 'data_format': data_format},
input_shape=inputs.shape)
layer_test(convolutional.ZeroPadding3D,
kwargs={'padding': ((1, 2), (3, 4), (0, 2)), 'data_format': data_format},
input_shape=inputs.shape)
# correctness test
layer = convolutional.ZeroPadding3D(padding=(2, 2, 2))
layer.build(inputs.shape)
output = layer(K.variable(inputs))
np_output = K.eval(output)
for offset in [0, 1, -1, -2]:
assert_allclose(np_output[:, offset, :, :, :], 0.)
assert_allclose(np_output[:, :, offset, :, :], 0.)
assert_allclose(np_output[:, :, :, offset, :], 0.)
assert_allclose(np_output[:, 2:-2, 2:-2, 2:-2, :], 1.)
# correctness test
layer = convolutional.ZeroPadding3D(padding=(2, 2, 2),
data_format=data_format)
layer.build(inputs.shape)
output = layer(K.variable(inputs))
np_output = K.eval(output)
if data_format == 'channels_last':
for offset in [0, 1, -1, -2]:
assert_allclose(np_output[:, offset, :, :, :], 0.)
assert_allclose(np_output[:, :, offset, :, :], 0.)
assert_allclose(np_output[:, :, :, offset, :], 0.)
assert_allclose(np_output[:, 2:-2, 2:-2, 2:-2, :], 1.)
elif data_format == 'channels_first':
for offset in [0, 1, -1, -2]:
assert_allclose(np_output[:, :, offset, :, :], 0.)
assert_allclose(np_output[:, :, :, offset, :], 0.)
assert_allclose(np_output[:, :, :, :, offset], 0.)
assert_allclose(np_output[:, :, 2:-2, 2:-2, 2:-2], 1.)
layer = convolutional.ZeroPadding3D(padding=((1, 2), (3, 4), (0, 2)),
data_format=data_format)
layer.build(inputs.shape)
output = layer(K.variable(inputs))
np_output = K.eval(output)
if data_format == 'channels_last':
for dim1_offset in [0, -1, -2]:
assert_allclose(np_output[:, dim1_offset, :, :, :], 0.)
for dim2_offset in [0, 1, 2, -1, -2, -3, -4]:
assert_allclose(np_output[:, :, dim2_offset, :, :], 0.)
for dim3_offset in [-1, -2]:
assert_allclose(np_output[:, :, :, dim3_offset, :], 0.)
assert_allclose(np_output[:, 1:-2, 3:-4, 0:-2, :], 1.)
elif data_format == 'channels_first':
for dim1_offset in [0, -1, -2]:
assert_allclose(np_output[:, :, dim1_offset, :, :], 0.)
for dim2_offset in [0, 1, 2, -1, -2, -3, -4]:
assert_allclose(np_output[:, :, :, dim2_offset, :], 0.)
for dim3_offset in [-1, -2]:
assert_allclose(np_output[:, :, :, :, dim3_offset], 0.)
assert_allclose(np_output[:, :, 1:-2, 3:-4, 0:-2], 1.)
@keras_test
+1 -14
Ver Arquivo
@@ -106,23 +106,10 @@ def test_lambda():
ld = deserialize_layer({'class_name': 'Lambda', 'config': config})
# test with lambda
ld = layers.Lambda(
lambda x: K.concatenate([K.square(x), x]),
output_shape=lambda s: tuple(list(s)[:-1] + [2 * s[-1]]))
ld = layers.Lambda(lambda x: K.concatenate([K.square(x), x]))
config = ld.get_config()
ld = layers.Lambda.from_config(config)
# test serialization with output_shape function
def f(x):
return K.concatenate([K.square(x), x])
def f_shape(s):
return tuple(list(s)[:-1] + [2 * s[-1]])
ld = layers.Lambda(f, output_shape=f_shape)
config = ld.get_config()
ld = deserialize_layer({'class_name': 'Lambda', 'config': config})
@keras_test
def test_dense():
+10 -12
Ver Arquivo
@@ -4,9 +4,7 @@ from numpy.testing import assert_allclose
from keras import layers
from keras import models
from keras import backend as K
from keras.utils.test_utils import layer_test
from keras.utils.test_utils import keras_test
from keras.layers import merge
@keras_test
@@ -14,8 +12,9 @@ def test_merge_add():
i1 = layers.Input(shape=(4, 5))
i2 = layers.Input(shape=(4, 5))
i3 = layers.Input(shape=(4, 5))
o = layers.add([i1, i2, i3])
assert o._keras_shape == (None, 4, 5)
assert o.get_shape().as_list() == [None, 4, 5]
model = models.Model([i1, i2, i3], o)
add_layer = layers.Add()
@@ -36,7 +35,7 @@ def test_merge_multiply():
i2 = layers.Input(shape=(4, 5))
i3 = layers.Input(shape=(4, 5))
o = layers.multiply([i1, i2, i3])
assert o._keras_shape == (None, 4, 5)
assert o.get_shape().as_list() == [None, 4, 5]
model = models.Model([i1, i2, i3], o)
mul_layer = layers.Multiply()
@@ -56,7 +55,7 @@ def test_merge_average():
i1 = layers.Input(shape=(4, 5))
i2 = layers.Input(shape=(4, 5))
o = layers.average([i1, i2])
assert o._keras_shape == (None, 4, 5)
assert o.get_shape().as_list() == [None, 4, 5]
model = models.Model([i1, i2], o)
avg_layer = layers.Average()
@@ -75,7 +74,7 @@ def test_merge_maximum():
i1 = layers.Input(shape=(4, 5))
i2 = layers.Input(shape=(4, 5))
o = layers.maximum([i1, i2])
assert o._keras_shape == (None, 4, 5)
assert o.get_shape().as_list() == [None, 4, 5]
model = models.Model([i1, i2], o)
max_layer = layers.Maximum()
@@ -94,7 +93,7 @@ def test_merge_concatenate():
i1 = layers.Input(shape=(4, 5))
i2 = layers.Input(shape=(4, 5))
o = layers.concatenate([i1, i2], axis=1)
assert o._keras_shape == (None, 8, 5)
assert o.get_shape().as_list() == [None, 8, 5]
model = models.Model([i1, i2], o)
concat_layer = layers.Concatenate(axis=1)
@@ -127,7 +126,7 @@ def test_merge_dot():
i1 = layers.Input(shape=(4,))
i2 = layers.Input(shape=(4,))
o = layers.dot([i1, i2], axes=1)
assert o._keras_shape == (None, 1)
assert o.get_shape().as_list() == [None, 1]
model = models.Model([i1, i2], o)
dot_layer = layers.Dot(axes=1)
@@ -145,7 +144,7 @@ def test_merge_dot():
# Test with negative tuple of axes.
o = layers.dot([i1, i2], axes=(-1, -1))
assert o._keras_shape == (None, 1)
assert o.get_shape().as_list() == [None, 1]
model = models.Model([i1, i2], o)
out = model.predict([x1, x2])
assert out.shape == (2, 1)
@@ -160,7 +159,7 @@ def test_merge_broadcast():
ops = [layers.add, layers.maximum]
for op in ops:
o = op([i1, i2])
assert o._keras_shape == (None, 4, 5)
assert K.int_shape(o) == (None, 4, 5)
model = models.Model([i1, i2], o)
x1 = np.random.random((2, 4, 5))
@@ -174,7 +173,7 @@ def test_merge_broadcast():
ops = [layers.add, layers.maximum]
for op in ops:
o = op([i1, i2])
assert o._keras_shape == (None, None, None)
assert K.int_shape(o) == (None, None, None)
model = models.Model([i1, i2], o)
x1 = np.random.random((2, 4, 5))
@@ -192,7 +191,6 @@ def test_merge_broadcast():
ops = [layers.add, layers.maximum]
for op in ops:
o = op([i1, i2])
assert o._keras_shape == (None, None, None)
model = models.Model([i1, i2], o)
x1 = np.random.random((2, 4, 5))
+26 -1
Ver Arquivo
@@ -2,7 +2,7 @@ import pytest
import numpy as np
from numpy.testing import assert_allclose
from keras.layers import Dense, Activation, Input
from keras.layers import Input
from keras.utils.test_utils import layer_test, keras_test
from keras.layers import normalization
from keras.models import Sequential, Model
@@ -52,6 +52,31 @@ def test_batchnorm_correctness():
assert_allclose(out.std(), 1.0, atol=1e-1)
@keras_test
def test_batchnorm_training_argument():
bn1 = normalization.BatchNormalization(input_shape=(10,))
x1 = Input(shape=(10,))
y1 = bn1(x1, training=True)
assert bn1.updates
model1 = Model(x1, y1)
np.random.seed(123)
x = np.random.normal(loc=5.0, scale=10.0, size=(20, 10))
output_a = model1.predict(x)
model1.compile(loss='mse', optimizer='rmsprop')
model1.fit(x, x, epochs=1, verbose=0)
output_b = model1.predict(x)
assert np.abs(np.sum(output_a - output_b)) > 0.1
assert_allclose(output_b.mean(), 0.0, atol=1e-1)
assert_allclose(output_b.std(), 1.0, atol=1e-1)
bn2 = normalization.BatchNormalization(input_shape=(10,))
x2 = Input(shape=(10,))
bn2(x2, training=False)
assert not bn2.updates
@keras_test
def test_batchnorm_mode_twice():
# This is a regression test for issue #4881 with the old
+55 -7
Ver Arquivo
@@ -57,6 +57,14 @@ def test_dropout(layer_class):
'dropout': 0.1,
'recurrent_dropout': 0.1},
input_shape=(num_samples, timesteps, embedding_dim))
# Test that dropout is not applied during testing
x = np.random.random((num_samples, timesteps, embedding_dim))
layer = layer_class(units, dropout=0.5, recurrent_dropout=0.5,
input_shape=(timesteps, embedding_dim))
model = Sequential([layer])
y1 = model.predict(x)
y2 = model.predict(x)
assert_allclose(y1, y2)
@rnn_test
@@ -169,29 +177,41 @@ def test_from_config(layer_class):
@rnn_test
def test_specify_initial_state(layer_class):
def test_specify_initial_state_keras_tensor(layer_class):
num_states = 2 if layer_class is recurrent.LSTM else 1
# Test with Keras tensor
inputs = Input((timesteps, embedding_dim))
initial_state = [Input((units,)) for _ in range(num_states)]
layer = layer_class(units)
output = layer(inputs, initial_state=initial_state)
if len(initial_state) == 1:
output = layer(inputs, initial_state=initial_state[0])
else:
output = layer(inputs, initial_state=initial_state)
assert initial_state[0] in layer.inbound_nodes[0].input_tensors
model = Model([inputs] + initial_state, output)
model.compile(loss='categorical_crossentropy', optimizer='adam')
inputs = np.random.random((num_samples, timesteps, embedding_dim))
initial_states = [np.random.random((num_samples, units))
for _ in range(num_states)]
initial_state = [np.random.random((num_samples, units))
for _ in range(num_states)]
targets = np.random.random((num_samples, units))
model.fit([inputs] + initial_states, targets)
model.fit([inputs] + initial_state, targets)
@rnn_test
def test_specify_initial_state_non_keras_tensor(layer_class):
num_states = 2 if layer_class is recurrent.LSTM else 1
# Test with non-Keras tensor
inputs = Input((timesteps, embedding_dim))
initial_state = [K.random_normal_variable((units,), 0, 1) for _ in range(num_states)]
initial_state = [K.random_normal_variable((num_samples, units), 0, 1)
for _ in range(num_states)]
layer = layer_class(units)
output = layer(inputs, initial_state=initial_state)
model = Model([inputs], output)
model = Model(inputs, output)
model.compile(loss='categorical_crossentropy', optimizer='adam')
inputs = np.random.random((num_samples, timesteps, embedding_dim))
@@ -213,10 +233,38 @@ def test_reset_states_with_values(layer_class):
atol=1e-4)
state_shapes = [K.int_shape(state) for state in layer.states]
values = [np.ones(shape) for shape in state_shapes]
if len(values) == 1:
values = values[0]
layer.reset_states(values)
np.testing.assert_allclose(K.eval(layer.states[0]),
np.ones(K.int_shape(layer.states[0])),
atol=1e-4)
# Test fit with invalid data
with pytest.raises(ValueError):
layer.reset_states([1] * (len(layer.states) + 1))
@rnn_test
def test_specify_state_with_masking(layer_class):
''' This test based on a previously failing issue here:
https://github.com/fchollet/keras/issues/1567
'''
num_states = 2 if layer_class is recurrent.LSTM else 1
inputs = Input((timesteps, embedding_dim))
masked_inputs = Masking()(inputs)
initial_state = [Input((units,)) for _ in range(num_states)]
output = layer_class(units)(inputs, initial_state=initial_state)
model = Model([inputs] + initial_state, output)
model.compile(loss='categorical_crossentropy', optimizer='adam')
inputs = np.random.random((num_samples, timesteps, embedding_dim))
initial_state = [np.random.random((num_samples, units))
for _ in range(num_states)]
targets = np.random.random((num_samples, units))
model.fit([inputs] + initial_state, targets)
if __name__ == '__main__':
pytest.main([__file__])
-849
Ver Arquivo
@@ -1,849 +0,0 @@
import pytest
import json
from keras.utils.test_utils import keras_test
import keras
import numpy as np
@keras_test
def test_dense_legacy_interface():
old_layer = keras.layers.Dense(input_dim=3, output_dim=2, name='d')
new_layer = keras.layers.Dense(2, input_shape=(3,), name='d')
assert json.dumps(old_layer.get_config()) == json.dumps(new_layer.get_config())
old_layer = keras.layers.Dense(2, bias=False, init='normal',
W_regularizer='l1',
W_constraint='maxnorm', name='d')
new_layer = keras.layers.Dense(2, use_bias=False,
kernel_initializer='normal',
kernel_regularizer='l1',
kernel_constraint='max_norm', name='d')
assert json.dumps(old_layer.get_config()) == json.dumps(new_layer.get_config())
old_layer = keras.layers.Dense(2, bias=True,
b_regularizer='l1',
b_constraint='maxnorm', name='d')
new_layer = keras.layers.Dense(2, use_bias=True,
bias_regularizer='l1',
bias_constraint='max_norm', name='d')
assert json.dumps(old_layer.get_config()) == json.dumps(new_layer.get_config())
@keras_test
def test_dropout_legacy_interface():
old_layer = keras.layers.Dropout(p=3, name='drop')
new_layer_1 = keras.layers.Dropout(rate=3, name='drop')
new_layer_2 = keras.layers.Dropout(3, name='drop')
assert json.dumps(old_layer.get_config()) == json.dumps(new_layer_1.get_config())
assert json.dumps(old_layer.get_config()) == json.dumps(new_layer_2.get_config())
@keras_test
def test_embedding_legacy_interface():
old_layer = keras.layers.Embedding(4, 2, name='d')
new_layer = keras.layers.Embedding(output_dim=2, input_dim=4, name='d')
assert json.dumps(old_layer.get_config()) == json.dumps(new_layer.get_config())
old_layer = keras.layers.Embedding(input_dim=4, output_dim=2, name='d',
init='normal',
W_regularizer='l1',
W_constraint='maxnorm')
new_layer = keras.layers.Embedding(input_dim=4, output_dim=2, name='d',
embeddings_initializer='normal',
embeddings_regularizer='l1',
embeddings_constraint='max_norm')
assert json.dumps(old_layer.get_config()) == json.dumps(new_layer.get_config())
old_layer = keras.layers.Embedding(1, 1, dropout=0.0, name='d')
new_layer = keras.layers.Embedding(1, 1, name='d')
assert json.dumps(old_layer.get_config()) == json.dumps(new_layer.get_config())
@keras_test
def test_maxpooling1d_legacy_interface():
old_layer = keras.layers.MaxPool1D(pool_length=2,
border_mode='valid',
name='maxpool1d')
new_layer = keras.layers.MaxPool1D(pool_size=2,
padding='valid',
name='maxpool1d')
assert json.dumps(old_layer.get_config()) == json.dumps(new_layer.get_config())
old_layer = keras.layers.MaxPool1D(2, padding='valid', name='maxpool1d')
new_layer = keras.layers.MaxPool1D(pool_size=2,
padding='valid',
name='maxpool1d')
assert json.dumps(old_layer.get_config()) == json.dumps(new_layer.get_config())
@keras_test
def test_avgpooling1d_legacy_interface():
old_layer = keras.layers.AvgPool1D(pool_length=2,
border_mode='valid',
name='d')
new_layer = keras.layers.AvgPool1D(pool_size=2, padding='valid', name='d')
assert json.dumps(old_layer.get_config()) == json.dumps(new_layer.get_config())
old_layer = keras.layers.AvgPool1D(2, padding='valid', name='d')
new_layer = keras.layers.AvgPool1D(pool_size=2, padding='valid', name='d')
assert json.dumps(old_layer.get_config()) == json.dumps(new_layer.get_config())
@keras_test
def test_prelu_legacy_interface():
old_layer = keras.layers.PReLU(init='zero', name='p')
new_layer = keras.layers.PReLU('zero', name='p')
assert json.dumps(old_layer.get_config()) == json.dumps(new_layer.get_config())
@keras_test
def test_gaussiannoise_legacy_interface():
old_layer = keras.layers.GaussianNoise(sigma=0.5, name='gn')
new_layer = keras.layers.GaussianNoise(stddev=0.5, name='gn')
assert json.dumps(old_layer.get_config()) == json.dumps(new_layer.get_config())
@keras_test
def test_lstm_legacy_interface():
old_layer = keras.layers.LSTM(input_shape=[3, 5], output_dim=2, name='d')
new_layer = keras.layers.LSTM(2, input_shape=[3, 5], name='d')
assert json.dumps(old_layer.get_config()) == json.dumps(new_layer.get_config())
old_layer = keras.layers.LSTM(input_shape=[3, 5], output_dim=2, name='d', consume_less='mem')
new_layer = keras.layers.LSTM(2, input_shape=[3, 5], name='d', implementation=1)
assert json.dumps(old_layer.get_config()) == json.dumps(new_layer.get_config())
old_layer = keras.layers.LSTM(input_dim=5, input_length=3,
output_dim=2, name='d', consume_less='mem')
new_layer = keras.layers.LSTM(2, input_shape=[3, 5], name='d', implementation=1)
assert json.dumps(old_layer.get_config()) == json.dumps(new_layer.get_config())
old_layer = keras.layers.LSTM(input_dim=5,
output_dim=2, name='d', consume_less='mem')
new_layer = keras.layers.LSTM(2, input_shape=[None, 5], name='d', implementation=1)
assert json.dumps(old_layer.get_config()) == json.dumps(new_layer.get_config())
old_layer = keras.layers.LSTM(input_shape=[3, 5], output_dim=2, name='d', consume_less='gpu')
new_layer = keras.layers.LSTM(2, input_shape=[3, 5], name='d', implementation=2)
assert json.dumps(old_layer.get_config()) == json.dumps(new_layer.get_config())
old_layer = keras.layers.LSTM(2, init='normal',
inner_init='glorot_uniform',
forget_bias_init='one',
inner_activation='hard_sigmoid',
W_regularizer='l1',
U_regularizer='l1',
b_regularizer='l1',
dropout_W=0.1,
dropout_U=0.1,
name='LSTM')
new_layer = keras.layers.LSTM(2, kernel_initializer='normal',
recurrent_initializer='glorot_uniform',
unit_forget_bias=True,
recurrent_activation='hard_sigmoid',
kernel_regularizer='l1',
recurrent_regularizer='l1',
bias_regularizer='l1',
dropout=0.1,
recurrent_dropout=0.1,
name='LSTM')
assert json.dumps(old_layer.get_config()) == json.dumps(new_layer.get_config())
old_layer = keras.layers.LSTM(2, init='normal',
inner_init='glorot_uniform',
forget_bias_init='zero',
inner_activation='hard_sigmoid',
W_regularizer='l1',
U_regularizer='l1',
b_regularizer='l1',
dropout_W=0.1,
dropout_U=0.1,
name='LSTM')
new_layer = keras.layers.LSTM(2, kernel_initializer='normal',
recurrent_initializer='glorot_uniform',
unit_forget_bias=True,
recurrent_activation='hard_sigmoid',
kernel_regularizer='l1',
recurrent_regularizer='l1',
bias_regularizer='l1',
dropout=0.1,
recurrent_dropout=0.1,
name='LSTM')
assert json.dumps(old_layer.get_config()) == json.dumps(new_layer.get_config())
@keras_test
def test_simplernn_legacy_interface():
old_layer = keras.layers.SimpleRNN(input_shape=[3, 5], output_dim=2, name='d')
new_layer = keras.layers.SimpleRNN(2, input_shape=[3, 5], name='d')
assert json.dumps(old_layer.get_config()) == json.dumps(new_layer.get_config())
old_layer = keras.layers.SimpleRNN(2, init='normal',
inner_init='glorot_uniform',
W_regularizer='l1',
U_regularizer='l1',
b_regularizer='l1',
dropout_W=0.1,
dropout_U=0.1,
name='SimpleRNN')
new_layer = keras.layers.SimpleRNN(2, kernel_initializer='normal',
recurrent_initializer='glorot_uniform',
kernel_regularizer='l1',
recurrent_regularizer='l1',
bias_regularizer='l1',
dropout=0.1,
recurrent_dropout=0.1,
name='SimpleRNN')
assert json.dumps(old_layer.get_config()) == json.dumps(new_layer.get_config())
@keras_test
def test_gru_legacy_interface():
old_layer = keras.layers.GRU(input_shape=[3, 5], output_dim=2, name='d')
new_layer = keras.layers.GRU(2, input_shape=[3, 5], name='d')
assert json.dumps(old_layer.get_config()) == json.dumps(new_layer.get_config())
old_layer = keras.layers.GRU(2, init='normal',
inner_init='glorot_uniform',
inner_activation='hard_sigmoid',
W_regularizer='l1',
U_regularizer='l1',
b_regularizer='l1',
dropout_W=0.1,
dropout_U=0.1,
name='GRU')
new_layer = keras.layers.GRU(2, kernel_initializer='normal',
recurrent_initializer='glorot_uniform',
recurrent_activation='hard_sigmoid',
kernel_regularizer='l1',
recurrent_regularizer='l1',
bias_regularizer='l1',
dropout=0.1,
recurrent_dropout=0.1,
name='GRU')
assert json.dumps(old_layer.get_config()) == json.dumps(new_layer.get_config())
@keras_test
def test_gaussiandropout_legacy_interface():
old_layer = keras.layers.GaussianDropout(p=0.6, name='drop')
new_layer_1 = keras.layers.GaussianDropout(rate=0.6, name='drop')
new_layer_2 = keras.layers.GaussianDropout(0.6, name='drop')
assert json.dumps(old_layer.get_config()) == json.dumps(new_layer_1.get_config())
assert json.dumps(old_layer.get_config()) == json.dumps(new_layer_2.get_config())
@keras_test
def test_maxpooling2d_legacy_interface():
old_layer = keras.layers.MaxPooling2D(pool_size=(2, 2), border_mode='valid', name='maxpool2d')
new_layer = keras.layers.MaxPool2D(pool_size=2, padding='valid', name='maxpool2d')
assert json.dumps(old_layer.get_config()) == json.dumps(new_layer.get_config())
old_layer = keras.layers.MaxPooling2D((2, 2), 2, 'valid', name='maxpool2d')
new_layer = keras.layers.MaxPool2D(pool_size=2, strides=2, padding='valid', name='maxpool2d')
assert json.dumps(old_layer.get_config()) == json.dumps(new_layer.get_config())
old_layer = keras.layers.MaxPooling2D((2, 2), padding='valid', dim_ordering='tf', name='maxpool2d')
new_layer = keras.layers.MaxPool2D(pool_size=2, padding='valid', data_format='channels_last', name='maxpool2d')
assert json.dumps(old_layer.get_config()) == json.dumps(new_layer.get_config())
old_layer = keras.layers.MaxPooling2D((2, 2), padding='valid', dim_ordering='th', name='maxpool2d')
new_layer = keras.layers.MaxPool2D(pool_size=2, padding='valid', data_format='channels_first', name='maxpool2d')
assert json.dumps(old_layer.get_config()) == json.dumps(new_layer.get_config())
old_layer = keras.layers.MaxPooling2D((2, 2), padding='valid', dim_ordering='default', name='maxpool2d')
new_layer = keras.layers.MaxPool2D(pool_size=2, padding='valid', name='maxpool2d')
assert json.dumps(old_layer.get_config()) == json.dumps(new_layer.get_config())
@keras_test
def test_avgpooling2d_legacy_interface():
old_layer = keras.layers.AveragePooling2D(pool_size=(2, 2), border_mode='valid', name='avgpooling2d')
new_layer = keras.layers.AvgPool2D(pool_size=(2, 2), padding='valid', name='avgpooling2d')
assert json.dumps(old_layer.get_config()) == json.dumps(new_layer.get_config())
old_layer = keras.layers.AveragePooling2D((2, 2), (2, 2), 'valid', name='avgpooling2d')
new_layer = keras.layers.AvgPool2D(pool_size=(2, 2), strides=(2, 2), padding='valid', name='avgpooling2d')
assert json.dumps(old_layer.get_config()) == json.dumps(new_layer.get_config())
old_layer = keras.layers.AveragePooling2D((2, 2), padding='valid', dim_ordering='tf', name='avgpooling2d')
new_layer = keras.layers.AvgPool2D(pool_size=2, padding='valid', data_format='channels_last', name='avgpooling2d')
assert json.dumps(old_layer.get_config()) == json.dumps(new_layer.get_config())
old_layer = keras.layers.AveragePooling2D((2, 2), padding='valid', dim_ordering='th', name='avgpooling2d')
new_layer = keras.layers.AvgPool2D(pool_size=2, padding='valid', data_format='channels_first', name='avgpooling2d')
assert json.dumps(old_layer.get_config()) == json.dumps(new_layer.get_config())
old_layer = keras.layers.AveragePooling2D((2, 2), padding='valid', dim_ordering='default', name='avgpooling2d')
new_layer = keras.layers.AvgPool2D(pool_size=2, padding='valid', name='avgpooling2d')
assert json.dumps(old_layer.get_config()) == json.dumps(new_layer.get_config())
@keras_test
def test_maxpooling3d_legacy_interface():
old_layer = keras.layers.MaxPooling3D(pool_size=(2, 2, 2), border_mode='valid', name='maxpool3d')
new_layer = keras.layers.MaxPool3D(pool_size=(2, 2, 2), padding='valid', name='maxpool3d')
assert json.dumps(old_layer.get_config()) == json.dumps(new_layer.get_config())
old_layer = keras.layers.MaxPooling3D((2, 2, 2), (2, 2, 2), 'valid', name='maxpool3d')
new_layer = keras.layers.MaxPool3D(pool_size=(2, 2, 2), strides=(2, 2, 2), padding='valid', name='maxpool3d')
assert json.dumps(old_layer.get_config()) == json.dumps(new_layer.get_config())
old_layer = keras.layers.MaxPooling3D((2, 2, 2), padding='valid', dim_ordering='tf', name='maxpool3d')
new_layer = keras.layers.MaxPool3D(pool_size=(2, 2, 2), padding='valid', data_format='channels_last', name='maxpool3d')
assert json.dumps(old_layer.get_config()) == json.dumps(new_layer.get_config())
old_layer = keras.layers.MaxPooling3D((2, 2, 2), padding='valid', dim_ordering='th', name='maxpool3d')
new_layer = keras.layers.MaxPool3D(pool_size=(2, 2, 2), padding='valid', data_format='channels_first', name='maxpool3d')
assert json.dumps(old_layer.get_config()) == json.dumps(new_layer.get_config())
old_layer = keras.layers.MaxPooling3D((2, 2, 2), padding='valid', dim_ordering='default', name='maxpool3d')
new_layer = keras.layers.MaxPool3D(pool_size=(2, 2, 2), padding='valid', name='maxpool3d')
assert json.dumps(old_layer.get_config()) == json.dumps(new_layer.get_config())
@keras_test
def test_avgpooling3d_legacy_interface():
old_layer = keras.layers.AveragePooling3D(pool_size=(2, 2, 2), border_mode='valid', name='avgpooling3d')
new_layer = keras.layers.AvgPool3D(pool_size=(2, 2, 2), padding='valid', name='avgpooling3d')
assert json.dumps(old_layer.get_config()) == json.dumps(new_layer.get_config())
old_layer = keras.layers.AveragePooling3D((2, 2, 2), (2, 2, 2), 'valid', name='avgpooling3d')
new_layer = keras.layers.AvgPool3D(pool_size=(2, 2, 2), strides=(2, 2, 2), padding='valid', name='avgpooling3d')
assert json.dumps(old_layer.get_config()) == json.dumps(new_layer.get_config())
old_layer = keras.layers.AveragePooling3D((2, 2, 2), padding='valid', dim_ordering='tf', name='avgpooling3d')
new_layer = keras.layers.AvgPool3D(pool_size=(2, 2, 2), padding='valid', data_format='channels_last', name='avgpooling3d')
assert json.dumps(old_layer.get_config()) == json.dumps(new_layer.get_config())
old_layer = keras.layers.AveragePooling3D((2, 2, 2), padding='valid', dim_ordering='th', name='avgpooling3d')
new_layer = keras.layers.AvgPool3D(pool_size=(2, 2, 2), padding='valid', data_format='channels_first', name='avgpooling3d')
assert json.dumps(old_layer.get_config()) == json.dumps(new_layer.get_config())
old_layer = keras.layers.AveragePooling3D((2, 2, 2), padding='valid', dim_ordering='default', name='avgpooling3d')
new_layer = keras.layers.AvgPool3D(pool_size=(2, 2, 2), padding='valid', name='avgpooling3d')
assert json.dumps(old_layer.get_config()) == json.dumps(new_layer.get_config())
@keras_test
def test_global_maxpooling2d_legacy_interface():
old_layer = keras.layers.GlobalMaxPooling2D(dim_ordering='tf', name='global_maxpool2d')
new_layer = keras.layers.GlobalMaxPool2D(data_format='channels_last', name='global_maxpool2d')
assert json.dumps(old_layer.get_config()) == json.dumps(new_layer.get_config())
old_layer = keras.layers.GlobalMaxPooling2D(dim_ordering='th', name='global_maxpool2d')
new_layer = keras.layers.GlobalMaxPool2D(data_format='channels_first', name='global_maxpool2d')
assert json.dumps(old_layer.get_config()) == json.dumps(new_layer.get_config())
old_layer = keras.layers.GlobalMaxPooling2D(dim_ordering='default', name='global_maxpool2d')
new_layer = keras.layers.GlobalMaxPool2D(name='global_maxpool2d')
assert json.dumps(old_layer.get_config()) == json.dumps(new_layer.get_config())
@keras_test
def test_global_avgpooling2d_legacy_interface():
old_layer = keras.layers.GlobalAveragePooling2D(dim_ordering='tf', name='global_avgpool2d')
new_layer = keras.layers.GlobalAvgPool2D(data_format='channels_last', name='global_avgpool2d')
assert json.dumps(old_layer.get_config()) == json.dumps(new_layer.get_config())
old_layer = keras.layers.GlobalAveragePooling2D(dim_ordering='th', name='global_avgpool2d')
new_layer = keras.layers.GlobalAvgPool2D(data_format='channels_first', name='global_avgpool2d')
assert json.dumps(old_layer.get_config()) == json.dumps(new_layer.get_config())
old_layer = keras.layers.GlobalAveragePooling2D(dim_ordering='default', name='global_avgpool2d')
new_layer = keras.layers.GlobalAvgPool2D(name='global_avgpool2d')
assert json.dumps(old_layer.get_config()) == json.dumps(new_layer.get_config())
@keras_test
def test_global_maxpooling3d_legacy_interface():
old_layer = keras.layers.GlobalMaxPooling3D(dim_ordering='tf', name='global_maxpool3d')
new_layer = keras.layers.GlobalMaxPool3D(data_format='channels_last', name='global_maxpool3d')
assert json.dumps(old_layer.get_config()) == json.dumps(new_layer.get_config())
old_layer = keras.layers.GlobalMaxPooling3D(dim_ordering='th', name='global_maxpool3d')
new_layer = keras.layers.GlobalMaxPool3D(data_format='channels_first', name='global_maxpool3d')
assert json.dumps(old_layer.get_config()) == json.dumps(new_layer.get_config())
old_layer = keras.layers.GlobalMaxPooling3D(dim_ordering='default', name='global_maxpool3d')
new_layer = keras.layers.GlobalMaxPool3D(name='global_maxpool3d')
assert json.dumps(old_layer.get_config()) == json.dumps(new_layer.get_config())
@keras_test
def test_global_avgpooling3d_legacy_interface():
old_layer = keras.layers.GlobalAveragePooling3D(dim_ordering='tf', name='global_avgpool3d')
new_layer = keras.layers.GlobalAvgPool3D(data_format='channels_last', name='global_avgpool3d')
assert json.dumps(old_layer.get_config()) == json.dumps(new_layer.get_config())
old_layer = keras.layers.GlobalAveragePooling3D(dim_ordering='th', name='global_avgpool3d')
new_layer = keras.layers.GlobalAvgPool3D(data_format='channels_first', name='global_avgpool3d')
assert json.dumps(old_layer.get_config()) == json.dumps(new_layer.get_config())
old_layer = keras.layers.GlobalAveragePooling3D(dim_ordering='default', name='global_avgpool3d')
new_layer = keras.layers.GlobalAvgPool3D(name='global_avgpool3d')
assert json.dumps(old_layer.get_config()) == json.dumps(new_layer.get_config())
@keras_test
def test_upsampling1d_legacy_interface():
old_layer = keras.layers.UpSampling1D(length=3, name='us1d')
new_layer_1 = keras.layers.UpSampling1D(size=3, name='us1d')
new_layer_2 = keras.layers.UpSampling1D(3, name='us1d')
assert json.dumps(old_layer.get_config()) == json.dumps(new_layer_1.get_config())
assert json.dumps(old_layer.get_config()) == json.dumps(new_layer_2.get_config())
@keras_test
def test_upsampling2d_legacy_interface():
old_layer = keras.layers.UpSampling2D((2, 2), dim_ordering='tf', name='us2d')
new_layer = keras.layers.UpSampling2D((2, 2), data_format='channels_last', name='us2d')
assert json.dumps(old_layer.get_config()) == json.dumps(new_layer.get_config())
@keras_test
def test_upsampling3d_legacy_interface():
old_layer = keras.layers.UpSampling3D((2, 2, 2),
dim_ordering='tf',
name='us3d')
new_layer = keras.layers.UpSampling3D((2, 2, 2),
data_format='channels_last',
name='us3d')
assert json.dumps(old_layer.get_config()) == json.dumps(new_layer.get_config())
@keras_test
def test_conv2d_legacy_interface():
old_layer = keras.layers.Convolution2D(5, 3, 3, name='conv')
new_layer = keras.layers.Conv2D(5, (3, 3), name='conv')
assert json.dumps(old_layer.get_config()) == json.dumps(new_layer.get_config())
old_layer = keras.layers.Convolution2D(5, 3, nb_col=3, name='conv')
new_layer = keras.layers.Conv2D(5, (3, 3), name='conv')
assert json.dumps(old_layer.get_config()) == json.dumps(new_layer.get_config())
old_layer = keras.layers.Convolution2D(5, nb_row=3, nb_col=3, name='conv')
new_layer = keras.layers.Conv2D(5, (3, 3), name='conv')
assert json.dumps(old_layer.get_config()) == json.dumps(new_layer.get_config())
old_layer = keras.layers.Convolution2D(5, 3, 3,
init='normal',
subsample=(2, 2),
border_mode='valid',
dim_ordering='th',
W_regularizer='l1',
b_regularizer='l2',
W_constraint='maxnorm',
b_constraint='unitnorm',
name='conv')
new_layer = keras.layers.Conv2D(5, (3, 3),
kernel_initializer='normal',
strides=(2, 2),
padding='valid',
kernel_regularizer='l1',
bias_regularizer='l2',
kernel_constraint='max_norm',
bias_constraint='unit_norm',
data_format='channels_first',
name='conv')
assert json.dumps(old_layer.get_config()) == json.dumps(new_layer.get_config())
@keras_test
def test_deconv2d_legacy_interface():
old_layer = keras.layers.Deconvolution2D(5, 3, 3, (6, 7, 5), name='deconv')
new_layer = keras.layers.Conv2DTranspose(5, (3, 3), name='deconv')
assert json.dumps(old_layer.get_config()) == json.dumps(new_layer.get_config())
old_layer = keras.layers.Deconvolution2D(5, 3, 3, output_shape=(6, 7, 5), name='deconv')
new_layer = keras.layers.Conv2DTranspose(5, (3, 3), name='deconv')
assert json.dumps(old_layer.get_config()) == json.dumps(new_layer.get_config())
old_layer = keras.layers.Deconvolution2D(5, 3, nb_col=3, output_shape=(6, 7, 5), name='deconv')
new_layer = keras.layers.Conv2DTranspose(5, (3, 3), name='deconv')
assert json.dumps(old_layer.get_config()) == json.dumps(new_layer.get_config())
old_layer = keras.layers.Deconvolution2D(5, nb_row=3, nb_col=3, output_shape=(6, 7, 5), name='deconv')
new_layer = keras.layers.Conv2DTranspose(5, (3, 3), name='deconv')
assert json.dumps(old_layer.get_config()) == json.dumps(new_layer.get_config())
old_layer = keras.layers.Deconvolution2D(5, 3, 3,
output_shape=(6, 7, 5),
init='normal',
subsample=(2, 2),
border_mode='valid',
dim_ordering='th',
W_regularizer='l1',
b_regularizer='l2',
W_constraint='maxnorm',
b_constraint='unitnorm',
name='conv')
new_layer = keras.layers.Conv2DTranspose(
5, (3, 3),
kernel_initializer='normal',
strides=(2, 2),
padding='valid',
kernel_regularizer='l1',
bias_regularizer='l2',
kernel_constraint='max_norm',
bias_constraint='unit_norm',
data_format='channels_first',
name='conv')
assert json.dumps(old_layer.get_config()) == json.dumps(new_layer.get_config())
@keras_test
def test_conv1d_legacy_interface():
old_layer = keras.layers.Convolution1D(5,
filter_length=3,
input_dim=3,
input_length=4,
name='conv')
new_layer = keras.layers.Conv1D(5, 3, name='conv', input_shape=(4, 3))
assert json.dumps(old_layer.get_config()) == json.dumps(new_layer.get_config())
old_layer = keras.layers.Convolution1D(5, 3,
init='normal',
subsample_length=2,
border_mode='valid',
W_regularizer='l1',
b_regularizer='l2',
W_constraint='maxnorm',
b_constraint='unitnorm',
name='conv')
new_layer = keras.layers.Conv1D(5, 3,
kernel_initializer='normal',
strides=2,
padding='valid',
kernel_regularizer='l1',
bias_regularizer='l2',
kernel_constraint='max_norm',
bias_constraint='unit_norm',
name='conv')
assert json.dumps(old_layer.get_config()) == json.dumps(new_layer.get_config())
@keras_test
def test_separable_conv2d_legacy_interface():
old_layer = keras.layers.SeparableConv2D(5, 3, 3, name='conv')
new_layer = keras.layers.SeparableConv2D(5, (3, 3), name='conv')
assert json.dumps(old_layer.get_config()) == json.dumps(new_layer.get_config())
old_layer = keras.layers.SeparableConv2D(5, 3, nb_col=3, name='conv')
new_layer = keras.layers.SeparableConv2D(5, (3, 3), name='conv')
assert json.dumps(old_layer.get_config()) == json.dumps(new_layer.get_config())
old_layer = keras.layers.SeparableConv2D(5, nb_row=3, nb_col=3, name='conv')
new_layer = keras.layers.SeparableConv2D(5, (3, 3), name='conv')
assert json.dumps(old_layer.get_config()) == json.dumps(new_layer.get_config())
old_layer = keras.layers.SeparableConv2D(5, 3, 3,
init='normal',
subsample=(2, 2),
border_mode='valid',
dim_ordering='th',
depthwise_regularizer='l1',
b_regularizer='l2',
depthwise_constraint='maxnorm',
b_constraint='unitnorm',
name='conv')
new_layer = keras.layers.SeparableConv2D(5, (3, 3),
depthwise_initializer='normal',
pointwise_initializer='normal',
strides=(2, 2),
padding='valid',
depthwise_regularizer='l1',
bias_regularizer='l2',
depthwise_constraint='max_norm',
bias_constraint='unit_norm',
data_format='channels_first',
name='conv')
old_config = json.dumps(old_layer.get_config())
new_config = json.dumps(new_layer.get_config())
assert old_config == new_config
@keras_test
def test_conv3d_legacy_interface():
old_layer = keras.layers.Convolution3D(5, 3, 3, 4, name='conv')
new_layer = keras.layers.Conv3D(5, (3, 3, 4), name='conv')
assert json.dumps(old_layer.get_config()) == json.dumps(new_layer.get_config())
old_layer = keras.layers.Convolution3D(5, 3, 3, kernel_dim3=4, name='conv')
new_layer = keras.layers.Conv3D(5, (3, 3, 4), name='conv')
assert json.dumps(old_layer.get_config()) == json.dumps(new_layer.get_config())
old_layer = keras.layers.Convolution3D(5, 3,
kernel_dim2=3,
kernel_dim3=4,
name='conv')
new_layer = keras.layers.Conv3D(5, (3, 3, 4), name='conv')
assert json.dumps(old_layer.get_config()) == json.dumps(new_layer.get_config())
old_layer = keras.layers.Convolution3D(5,
kernel_dim1=3,
kernel_dim2=3,
kernel_dim3=4,
name='conv')
new_layer = keras.layers.Conv3D(5, (3, 3, 4), name='conv')
assert json.dumps(old_layer.get_config()) == json.dumps(new_layer.get_config())
old_layer = keras.layers.Convolution3D(5, 3, 3, 4,
init='normal',
subsample=(2, 2, 2),
border_mode='valid',
dim_ordering='th',
W_regularizer='l1',
b_regularizer='l2',
W_constraint='maxnorm',
b_constraint='unitnorm',
name='conv')
new_layer = keras.layers.Conv3D(5, (3, 3, 4),
kernel_initializer='normal',
strides=(2, 2, 2),
padding='valid',
kernel_regularizer='l1',
bias_regularizer='l2',
kernel_constraint='max_norm',
bias_constraint='unit_norm',
data_format='channels_first',
name='conv')
assert json.dumps(old_layer.get_config()) == json.dumps(new_layer.get_config())
@keras_test
def test_convlstm2d_legacy_interface():
old_layer = keras.layers.ConvLSTM2D(5, 3, 3, name='conv')
new_layer = keras.layers.ConvLSTM2D(5, (3, 3), name='conv')
assert json.dumps(old_layer.get_config()) == json.dumps(new_layer.get_config())
old_layer = keras.layers.ConvLSTM2D(5, 3, nb_col=3, name='conv')
new_layer = keras.layers.ConvLSTM2D(5, (3, 3), name='conv')
assert json.dumps(old_layer.get_config()) == json.dumps(new_layer.get_config())
old_layer = keras.layers.ConvLSTM2D(5, nb_row=3, nb_col=3, name='conv')
new_layer = keras.layers.ConvLSTM2D(5, (3, 3), name='conv')
assert json.dumps(old_layer.get_config()) == json.dumps(new_layer.get_config())
old_layer = keras.layers.ConvLSTM2D(5, 3, 3,
init='normal',
inner_init='uniform',
forget_bias_init='one',
inner_activation='relu',
subsample=(2, 2),
border_mode='valid',
dim_ordering='th',
W_regularizer='l1',
U_regularizer='l2',
b_regularizer='l2',
dropout_W=0.2,
dropout_U=0.1,
name='conv')
new_layer = keras.layers.ConvLSTM2D(5, (3, 3),
kernel_initializer='normal',
recurrent_initializer='uniform',
unit_forget_bias=True,
recurrent_activation='relu',
strides=(2, 2),
padding='valid',
kernel_regularizer='l1',
recurrent_regularizer='l2',
bias_regularizer='l2',
data_format='channels_first',
dropout=0.2,
recurrent_dropout=0.1,
name='conv')
assert json.dumps(old_layer.get_config()) == json.dumps(new_layer.get_config())
@keras_test
def test_batchnorm_legacy_interface():
old_layer = keras.layers.BatchNormalization(mode=0, name='bn')
new_layer = keras.layers.BatchNormalization(name='bn')
assert json.dumps(old_layer.get_config()) == json.dumps(new_layer.get_config())
old_layer = keras.layers.BatchNormalization(mode=0,
beta_init='one',
gamma_init='uniform',
name='bn')
new_layer = keras.layers.BatchNormalization(beta_initializer='ones',
gamma_initializer='uniform',
name='bn')
assert json.dumps(old_layer.get_config()) == json.dumps(new_layer.get_config())
@keras_test
def test_atrousconv1d_legacy_interface():
old_layer = keras.layers.AtrousConvolution1D(5, 3,
init='normal',
subsample_length=2,
border_mode='valid',
W_regularizer='l1',
b_regularizer='l2',
W_constraint='maxnorm',
b_constraint='unitnorm',
atrous_rate=2,
name='conv')
new_layer = keras.layers.Conv1D(5, 3,
kernel_initializer='normal',
strides=2,
padding='valid',
kernel_regularizer='l1',
bias_regularizer='l2',
kernel_constraint='max_norm',
bias_constraint='unit_norm',
dilation_rate=2,
name='conv')
assert json.dumps(old_layer.get_config()) == json.dumps(new_layer.get_config())
@keras_test
def test_atrousconv2d_legacy_interface():
old_layer = keras.layers.AtrousConvolution2D(
5, 3, 3,
atrous_rate=(2, 2),
init='normal',
subsample=(2, 2),
border_mode='valid',
dim_ordering='th',
W_regularizer='l1',
b_regularizer='l2',
W_constraint='maxnorm',
b_constraint='unitnorm',
name='conv')
new_layer = keras.layers.Conv2D(5, (3, 3),
kernel_initializer='normal',
strides=(2, 2),
padding='valid',
kernel_regularizer='l1',
bias_regularizer='l2',
kernel_constraint='max_norm',
bias_constraint='unit_norm',
data_format='channels_first',
dilation_rate=(2, 2),
name='conv')
assert json.dumps(old_layer.get_config()) == json.dumps(new_layer.get_config())
@keras_test
def test_zeropadding2d_legacy_interface():
old_layer = keras.layers.ZeroPadding2D(padding={'right_pad': 4,
'bottom_pad': 2,
'top_pad': 1,
'left_pad': 3},
dim_ordering='tf',
name='zp2d')
new_layer = keras.layers.ZeroPadding2D(((1, 2), (3, 4)),
data_format='channels_last',
name='zp2d')
assert json.dumps(old_layer.get_config()) == json.dumps(new_layer.get_config())
@keras_test
def test_zeropadding3d_legacy_interface():
old_layer = keras.layers.ZeroPadding3D((2, 2, 2),
dim_ordering='tf',
name='zp3d')
new_layer = keras.layers.ZeroPadding3D((2, 2, 2),
data_format='channels_last',
name='zp3d')
assert json.dumps(old_layer.get_config()) == json.dumps(new_layer.get_config())
@keras_test
def test_cropping2d_legacy_interface():
old_layer = keras.layers.Cropping2D(dim_ordering='tf', name='c2d')
new_layer = keras.layers.Cropping2D(data_format='channels_last', name='c2d')
assert json.dumps(old_layer.get_config()) == json.dumps(new_layer.get_config())
@keras_test
def test_cropping3d_legacy_interface():
old_layer = keras.layers.Cropping3D(dim_ordering='tf', name='c3d')
new_layer = keras.layers.Cropping3D(data_format='channels_last', name='c3d')
assert json.dumps(old_layer.get_config()) == json.dumps(new_layer.get_config())
@keras_test
def test_generator_methods_interface():
def train_generator():
x = np.random.randn(2, 2)
y = np.random.randint(0, 2, size=[2, 1])
while True:
yield (x, y)
def val_generator():
x = np.random.randn(2, 2)
y = np.random.randint(0, 2, size=[2, 1])
while True:
yield (x, y)
def pred_generator():
x = np.random.randn(1, 2)
while True:
yield x
x = keras.layers.Input(shape=(2, ))
y = keras.layers.Dense(2)(x)
model = keras.models.Model(inputs=x, outputs=y)
model.compile(optimizer='rmsprop',
loss='sparse_categorical_crossentropy',
metrics=['accuracy'])
model.fit_generator(generator=train_generator(),
samples_per_epoch=1,
validation_data=val_generator(),
nb_val_samples=1,
nb_worker=1)
model.evaluate_generator(generator=train_generator(),
val_samples=2,
nb_worker=1)
model.predict_generator(generator=pred_generator(),
val_samples=2,
nb_worker=1)
def test_spatialdropout1d_legacy_interface():
old_layer = keras.layers.SpatialDropout1D(p=0.6, name='sd1d')
new_layer_1 = keras.layers.SpatialDropout1D(rate=0.6, name='sd1d')
new_layer_2 = keras.layers.SpatialDropout1D(0.6, name='sd1d')
assert json.dumps(old_layer.get_config()) == json.dumps(new_layer_1.get_config())
assert json.dumps(old_layer.get_config()) == json.dumps(new_layer_2.get_config())
@keras_test
def test_spatialdropout2d_legacy_interface():
old_layer = keras.layers.SpatialDropout2D(p=0.5,
dim_ordering='tf',
name='sd2d')
new_layer_1 = keras.layers.SpatialDropout2D(rate=0.5,
data_format='channels_last',
name='sd2d')
new_layer_2 = keras.layers.SpatialDropout2D(0.5,
data_format='channels_last',
name='sd2d')
assert json.dumps(old_layer.get_config()) == json.dumps(new_layer_1.get_config())
assert json.dumps(old_layer.get_config()) == json.dumps(new_layer_2.get_config())
@keras_test
def test_spatialdropout3d_legacy_interface():
old_layer = keras.layers.SpatialDropout3D(p=0.5,
dim_ordering='tf',
name='sd3d')
new_layer_1 = keras.layers.SpatialDropout3D(rate=0.5,
data_format='channels_last',
name='sd3d')
new_layer_2 = keras.layers.SpatialDropout3D(0.5,
data_format='channels_last',
name='sd3d')
assert json.dumps(old_layer.get_config()) == json.dumps(new_layer_1.get_config())
assert json.dumps(old_layer.get_config()) == json.dumps(new_layer_2.get_config())
if __name__ == '__main__':
pytest.main([__file__])
-317
Ver Arquivo
@@ -1,317 +0,0 @@
import pytest
from keras.utils.test_utils import keras_test
from keras.utils.test_utils import layer_test
from keras.legacy import layers as legacy_layers
from keras import layers
from keras import models
from keras import regularizers
from keras import constraints
from keras import backend as K
import numpy as np
@keras_test
def test_highway():
layer_test(legacy_layers.Highway,
kwargs={},
input_shape=(3, 2))
layer_test(legacy_layers.Highway,
kwargs={'W_regularizer': regularizers.l2(0.01),
'b_regularizer': regularizers.l1(0.01),
'activity_regularizer': regularizers.l2(0.01),
'W_constraint': constraints.MaxNorm(1),
'b_constraint': constraints.MaxNorm(1)},
input_shape=(3, 2))
@keras_test
def test_maxout_dense():
layer_test(legacy_layers.MaxoutDense,
kwargs={'output_dim': 3},
input_shape=(3, 2))
layer_test(legacy_layers.MaxoutDense,
kwargs={'output_dim': 3,
'W_regularizer': regularizers.l2(0.01),
'b_regularizer': regularizers.l1(0.01),
'activity_regularizer': regularizers.l2(0.01),
'W_constraint': constraints.MaxNorm(1),
'b_constraint': constraints.MaxNorm(1)},
input_shape=(3, 2))
@keras_test
def test_merge():
# test modes: 'sum', 'mul', 'concat', 'ave', 'cos', 'dot'.
input_shapes = [(3, 2), (3, 2)]
inputs = [np.random.random(shape) for shape in input_shapes]
# test functional API
for mode in ['sum', 'mul', 'concat', 'ave', 'max']:
print(mode)
input_a = layers.Input(shape=input_shapes[0][1:])
input_b = layers.Input(shape=input_shapes[1][1:])
merged = legacy_layers.merge([input_a, input_b], mode=mode)
model = models.Model([input_a, input_b], merged)
model.compile('rmsprop', 'mse')
expected_output_shape = model.compute_output_shape(input_shapes)
actual_output_shape = model.predict(inputs).shape
assert expected_output_shape == actual_output_shape
config = model.get_config()
model = models.Model.from_config(config)
model.compile('rmsprop', 'mse')
# test Merge (#2460)
merged = legacy_layers.Merge(mode=mode)([input_a, input_b])
model = models.Model([input_a, input_b], merged)
model.compile('rmsprop', 'mse')
expected_output_shape = model.compute_output_shape(input_shapes)
actual_output_shape = model.predict(inputs).shape
assert expected_output_shape == actual_output_shape
# test lambda with output_shape lambda
input_a = layers.Input(shape=input_shapes[0][1:])
input_b = layers.Input(shape=input_shapes[1][1:])
merged = legacy_layers.merge(
[input_a, input_b],
mode=lambda tup: K.concatenate([tup[0], tup[1]]),
output_shape=lambda tup: tup[0][:-1] + (tup[0][-1] + tup[1][-1],))
model = models.Model([input_a, input_b], merged)
expected_output_shape = model.compute_output_shape(input_shapes)
actual_output_shape = model.predict(inputs).shape
assert expected_output_shape == actual_output_shape
config = model.get_config()
model = models.Model.from_config(config)
model.compile('rmsprop', 'mse')
# test function with output_shape function
def fn_mode(tup):
x, y = tup
return K.concatenate([x, y], axis=1)
def fn_output_shape(tup):
s1, s2 = tup
return (s1[0], s1[1] + s2[1]) + s1[2:]
input_a = layers.Input(shape=input_shapes[0][1:])
input_b = layers.Input(shape=input_shapes[1][1:])
merged = legacy_layers.merge([input_a, input_b],
mode=fn_mode,
output_shape=fn_output_shape)
model = models.Model([input_a, input_b], merged)
expected_output_shape = model.compute_output_shape(input_shapes)
actual_output_shape = model.predict(inputs).shape
assert expected_output_shape == actual_output_shape
config = model.get_config()
model = models.Model.from_config(config)
model.compile('rmsprop', 'mse')
# test function with output_mask function
# time dimension is required for masking
input_shapes = [(4, 3, 2), (4, 3, 2)]
inputs = [np.random.random(shape) for shape in input_shapes]
def fn_output_mask(tup):
x_mask, y_mask = tup
return K.concatenate([x_mask, y_mask])
input_a = layers.Input(shape=input_shapes[0][1:])
input_b = layers.Input(shape=input_shapes[1][1:])
a = layers.Masking()(input_a)
b = layers.Masking()(input_b)
merged = legacy_layers.merge([a, b], mode=fn_mode, output_shape=fn_output_shape, output_mask=fn_output_mask)
model = models.Model([input_a, input_b], merged)
expected_output_shape = model.compute_output_shape(input_shapes)
actual_output_shape = model.predict(inputs).shape
assert expected_output_shape == actual_output_shape
config = model.get_config()
model = models.Model.from_config(config)
model.compile('rmsprop', 'mse')
mask_inputs = (np.zeros(input_shapes[0][:-1]), np.ones(input_shapes[1][:-1]))
expected_mask_output = np.concatenate(mask_inputs, axis=-1)
mask_input_placeholders = [K.placeholder(shape=input_shape[:-1]) for input_shape in input_shapes]
mask_output = model.layers[-1]._output_mask(mask_input_placeholders)
assert np.all(K.function(mask_input_placeholders, [mask_output])(mask_inputs)[0] == expected_mask_output)
# test lambda with output_mask lambda
input_a = layers.Input(shape=input_shapes[0][1:])
input_b = layers.Input(shape=input_shapes[1][1:])
a = layers.Masking()(input_a)
b = layers.Masking()(input_b)
merged = legacy_layers.merge(
[a, b], mode=lambda tup: K.concatenate([tup[0], tup[1]], axis=1),
output_shape=lambda tup: (tup[0][0], tup[0][1] + tup[1][1]) + tup[0][2:],
output_mask=lambda tup: K.concatenate([tup[0], tup[1]]))
model = models.Model([input_a, input_b], merged)
expected_output_shape = model.compute_output_shape(input_shapes)
actual_output_shape = model.predict(inputs).shape
assert expected_output_shape == actual_output_shape
config = model.get_config()
model = models.Model.from_config(config)
model.compile('rmsprop', 'mse')
mask_output = model.layers[-1]._output_mask(mask_input_placeholders)
assert np.all(K.function(mask_input_placeholders, [mask_output])(mask_inputs)[0] == expected_mask_output)
# test with arguments
input_shapes = [(3, 2), (3, 2)]
inputs = [np.random.random(shape) for shape in input_shapes]
def fn_mode(tup, a, b):
x, y = tup
return x * a + y * b
input_a = layers.Input(shape=input_shapes[0][1:])
input_b = layers.Input(shape=input_shapes[1][1:])
merged = legacy_layers.merge([input_a, input_b], mode=fn_mode, output_shape=lambda s: s[0], arguments={'a': 0.7, 'b': 0.3})
model = models.Model([input_a, input_b], merged)
output = model.predict(inputs)
config = model.get_config()
model = models.Model.from_config(config)
assert np.all(model.predict(inputs) == output)
@keras_test
def test_merge_mask_2d():
rand = lambda *shape: np.asarray(np.random.random(shape) > 0.5, dtype='int32')
# inputs
input_a = layers.Input(shape=(3,))
input_b = layers.Input(shape=(3,))
# masks
masked_a = layers.Masking(mask_value=0)(input_a)
masked_b = layers.Masking(mask_value=0)(input_b)
# three different types of merging
merged_sum = legacy_layers.merge([masked_a, masked_b], mode='sum')
merged_concat = legacy_layers.merge([masked_a, masked_b], mode='concat', concat_axis=1)
merged_concat_mixed = legacy_layers.merge([masked_a, input_b], mode='concat', concat_axis=1)
# test sum
model_sum = models.Model([input_a, input_b], [merged_sum])
model_sum.compile(loss='mse', optimizer='sgd')
model_sum.fit([rand(2, 3), rand(2, 3)], [rand(2, 3)], epochs=1)
# test concatenation
model_concat = models.Model([input_a, input_b], [merged_concat])
model_concat.compile(loss='mse', optimizer='sgd')
model_concat.fit([rand(2, 3), rand(2, 3)], [rand(2, 6)], epochs=1)
# test concatenation with masked and non-masked inputs
model_concat = models.Model([input_a, input_b], [merged_concat_mixed])
model_concat.compile(loss='mse', optimizer='sgd')
model_concat.fit([rand(2, 3), rand(2, 3)], [rand(2, 6)], epochs=1)
@keras_test
def test_merge_mask_3d():
rand = lambda *shape: np.asarray(np.random.random(shape) > 0.5, dtype='int32')
# embeddings
input_a = layers.Input(shape=(3,), dtype='int32')
input_b = layers.Input(shape=(3,), dtype='int32')
embedding = layers.Embedding(3, 4, mask_zero=True)
embedding_a = embedding(input_a)
embedding_b = embedding(input_b)
# rnn
rnn = layers.SimpleRNN(3, return_sequences=True)
rnn_a = rnn(embedding_a)
rnn_b = rnn(embedding_b)
# concatenation
merged_concat = legacy_layers.merge([rnn_a, rnn_b], mode='concat', concat_axis=-1)
model = models.Model([input_a, input_b], [merged_concat])
model.compile(loss='mse', optimizer='sgd')
model.fit([rand(2, 3), rand(2, 3)], [rand(2, 3, 6)])
@keras_test
def test_sequential_regression():
# start with a basic example of using a Sequential model
# inside the functional API
seq = models.Sequential()
seq.add(layers.Dense(10, input_shape=(10,)))
x = layers.Input(shape=(10,))
y = seq(x)
model = models.Model(x, y)
model.compile('rmsprop', 'mse')
weights = model.get_weights()
# test serialization
config = model.get_config()
model = models.Model.from_config(config)
model.compile('rmsprop', 'mse')
model.set_weights(weights)
# more advanced model with multiple branches
branch_1 = models.Sequential(name='branch_1')
branch_1.add(layers.Embedding(input_dim=100,
output_dim=10,
input_length=2,
name='embed_1'))
branch_1.add(layers.LSTM(32, name='lstm_1'))
branch_2 = models.Sequential(name='branch_2')
branch_2.add(layers.Dense(32, input_shape=(8,), name='dense_2'))
branch_3 = models.Sequential(name='branch_3')
branch_3.add(layers.Dense(32, input_shape=(6,), name='dense_3'))
branch_1_2 = models.Sequential([legacy_layers.Merge([branch_1, branch_2], mode='concat')], name='branch_1_2')
branch_1_2.add(layers.Dense(16, name='dense_1_2-0'))
# test whether impromtu input_shape breaks the model
branch_1_2.add(layers.Dense(16, input_shape=(16,), name='dense_1_2-1'))
model = models.Sequential([legacy_layers.Merge([branch_1_2, branch_3], mode='concat')], name='final')
model.add(layers.Dense(16, name='dense_final'))
model.compile(optimizer='rmsprop',
loss='categorical_crossentropy',
metrics=['accuracy'])
model.summary()
x = (100 * np.random.random((100, 2))).astype('int32')
y = np.random.random((100, 8))
z = np.random.random((100, 6))
labels = np.random.random((100, 16))
model.fit([x, y, z], labels, epochs=1)
# test if Sequential can be called in the functional API
a = layers.Input(shape=(2,), dtype='int32')
b = layers.Input(shape=(8,))
c = layers.Input(shape=(6,))
o = model([a, b, c])
outer_model = models.Model([a, b, c], o)
outer_model.compile(optimizer='rmsprop',
loss='categorical_crossentropy',
metrics=['accuracy'])
outer_model.fit([x, y, z], labels, epochs=1)
# test serialization
config = outer_model.get_config()
outer_model = models.Model.from_config(config)
outer_model.compile(optimizer='rmsprop',
loss='categorical_crossentropy',
metrics=['accuracy'])
outer_model.fit([x, y, z], labels, epochs=1)
if __name__ == '__main__':
pytest.main([__file__])
-279
Ver Arquivo
@@ -1,279 +0,0 @@
from __future__ import absolute_import
from __future__ import print_function
import pytest
import os
import numpy as np
from keras.models import Sequential
from keras.layers import Dense, Activation
from keras.legacy.layers import Merge
from keras.utils import np_utils
from keras.utils.test_utils import get_test_data, keras_test
from keras.models import model_from_json, model_from_yaml
input_dim = 16
num_hidden = 8
num_class = 4
batch_size = 32
epochs = 1
def _get_test_data():
np.random.seed(1234)
train_samples = 100
test_samples = 50
(x_train, y_train), (x_test, y_test) = get_test_data(num_train=train_samples,
num_test=test_samples,
input_shape=(input_dim,),
classification=True,
num_classes=4)
y_test = np_utils.to_categorical(y_test)
y_train = np_utils.to_categorical(y_train)
return (x_train, y_train), (x_test, y_test)
@keras_test
def test_merge_sum():
(x_train, y_train), (x_test, y_test) = _get_test_data()
left = Sequential()
left.add(Dense(num_hidden, input_shape=(input_dim,)))
left.add(Activation('relu'))
right = Sequential()
right.add(Dense(num_hidden, input_shape=(input_dim,)))
right.add(Activation('relu'))
model = Sequential()
model.add(Merge([left, right], mode='sum'))
model.add(Dense(num_class))
model.add(Activation('softmax'))
model.compile(loss='categorical_crossentropy', optimizer='rmsprop')
model.fit([x_train, x_train], y_train, batch_size=batch_size, epochs=epochs, verbose=0, validation_data=([x_test, x_test], y_test))
model.fit([x_train, x_train], y_train, batch_size=batch_size, epochs=epochs, verbose=0, validation_split=0.1)
model.fit([x_train, x_train], y_train, batch_size=batch_size, epochs=epochs, verbose=0)
model.fit([x_train, x_train], y_train, batch_size=batch_size, epochs=epochs, verbose=0, shuffle=False)
loss = model.evaluate([x_test, x_test], y_test, verbose=0)
model.predict([x_test, x_test], verbose=0)
model.predict_classes([x_test, x_test], verbose=0)
model.predict_proba([x_test, x_test], verbose=0)
# test weight saving
fname = 'test_merge_sum_temp.h5'
model.save_weights(fname, overwrite=True)
left = Sequential()
left.add(Dense(num_hidden, input_shape=(input_dim,)))
left.add(Activation('relu'))
right = Sequential()
right.add(Dense(num_hidden, input_shape=(input_dim,)))
right.add(Activation('relu'))
model = Sequential()
model.add(Merge([left, right], mode='sum'))
model.add(Dense(num_class))
model.add(Activation('softmax'))
model.load_weights(fname)
os.remove(fname)
model.compile(loss='categorical_crossentropy', optimizer='rmsprop')
nloss = model.evaluate([x_test, x_test], y_test, verbose=0)
assert(loss == nloss)
# test serialization
config = model.get_config()
Sequential.from_config(config)
model.summary()
json_str = model.to_json()
model_from_json(json_str)
yaml_str = model.to_yaml()
model_from_yaml(yaml_str)
@keras_test
def test_merge_dot():
(x_train, y_train), (x_test, y_test) = _get_test_data()
left = Sequential()
left.add(Dense(num_hidden, input_shape=(input_dim,)))
left.add(Activation('relu'))
right = Sequential()
right.add(Dense(num_hidden, input_shape=(input_dim,)))
right.add(Activation('relu'))
model = Sequential()
model.add(Merge([left, right], mode='dot', dot_axes=1))
model.add(Dense(num_class))
model.add(Activation('softmax'))
model.compile(loss='categorical_crossentropy', optimizer='rmsprop')
left = Sequential()
left.add(Dense(num_hidden, input_shape=(input_dim,)))
left.add(Activation('relu'))
right = Sequential()
right.add(Dense(num_hidden, input_shape=(input_dim,)))
right.add(Activation('relu'))
model = Sequential()
model.add(Merge([left, right], mode='dot', dot_axes=[1, 1]))
model.add(Dense(num_class))
model.add(Activation('softmax'))
model.compile(loss='categorical_crossentropy', optimizer='rmsprop')
@keras_test
def test_merge_concat():
(x_train, y_train), (x_test, y_test) = _get_test_data()
left = Sequential(name='branch_1')
left.add(Dense(num_hidden, input_shape=(input_dim,), name='dense_1'))
left.add(Activation('relu', name='relu_1'))
right = Sequential(name='branch_2')
right.add(Dense(num_hidden, input_shape=(input_dim,), name='dense_2'))
right.add(Activation('relu', name='relu_2'))
model = Sequential(name='merged_branches')
model.add(Merge([left, right], mode='concat', name='merge'))
model.add(Dense(num_class, name='final_dense'))
model.add(Activation('softmax', name='softmax'))
model.compile(loss='categorical_crossentropy', optimizer='rmsprop')
model.fit([x_train, x_train], y_train, batch_size=batch_size, epochs=epochs, verbose=0, validation_data=([x_test, x_test], y_test))
model.fit([x_train, x_train], y_train, batch_size=batch_size, epochs=epochs, verbose=0, validation_split=0.1)
model.fit([x_train, x_train], y_train, batch_size=batch_size, epochs=epochs, verbose=0)
model.fit([x_train, x_train], y_train, batch_size=batch_size, epochs=epochs, verbose=0, shuffle=False)
loss = model.evaluate([x_test, x_test], y_test, verbose=0)
model.predict([x_test, x_test], verbose=0)
model.predict_classes([x_test, x_test], verbose=0)
model.predict_proba([x_test, x_test], verbose=0)
model.get_config()
fname = 'test_merge_concat_temp.h5'
model.save_weights(fname, overwrite=True)
model.fit([x_train, x_train], y_train, batch_size=batch_size, epochs=epochs, verbose=0)
model.load_weights(fname)
os.remove(fname)
nloss = model.evaluate([x_test, x_test], y_test, verbose=0)
assert(loss == nloss)
@keras_test
def test_merge_recursivity():
(x_train, y_train), (x_test, y_test) = _get_test_data()
left = Sequential()
left.add(Dense(num_hidden, input_shape=(input_dim,)))
left.add(Activation('relu'))
right = Sequential()
right.add(Dense(num_hidden, input_shape=(input_dim,)))
right.add(Activation('relu'))
righter = Sequential()
righter.add(Dense(num_hidden, input_shape=(input_dim,)))
righter.add(Activation('relu'))
intermediate = Sequential()
intermediate.add(Merge([left, right], mode='sum'))
intermediate.add(Dense(num_hidden))
intermediate.add(Activation('relu'))
model = Sequential()
model.add(Merge([intermediate, righter], mode='sum'))
model.add(Dense(num_class))
model.add(Activation('softmax'))
model.compile(loss='categorical_crossentropy', optimizer='rmsprop')
model.fit([x_train, x_train, x_train], y_train, batch_size=batch_size, epochs=epochs, verbose=0, validation_data=([x_test, x_test, x_test], y_test))
model.fit([x_train, x_train, x_train], y_train, batch_size=batch_size, epochs=epochs, verbose=0, validation_split=0.1)
model.fit([x_train, x_train, x_train], y_train, batch_size=batch_size, epochs=epochs, verbose=0)
model.fit([x_train, x_train, x_train], y_train, batch_size=batch_size, epochs=epochs, verbose=0, shuffle=False)
loss = model.evaluate([x_test, x_test, x_test], y_test, verbose=0)
model.predict([x_test, x_test, x_test], verbose=0)
model.predict_classes([x_test, x_test, x_test], verbose=0)
model.predict_proba([x_test, x_test, x_test], verbose=0)
fname = 'test_merge_recursivity_temp.h5'
model.save_weights(fname, overwrite=True)
model.load_weights(fname)
os.remove(fname)
nloss = model.evaluate([x_test, x_test, x_test], y_test, verbose=0)
assert(loss == nloss)
# test serialization
config = model.get_config()
Sequential.from_config(config)
model.summary()
json_str = model.to_json()
model_from_json(json_str)
yaml_str = model.to_yaml()
model_from_yaml(yaml_str)
@keras_test
def test_merge_overlap():
(x_train, y_train), (x_test, y_test) = _get_test_data()
left = Sequential()
left.add(Dense(num_hidden, input_shape=(input_dim,)))
left.add(Activation('relu'))
model = Sequential()
model.add(Merge([left, left], mode='sum'))
model.add(Dense(num_class))
model.add(Activation('softmax'))
model.compile(loss='categorical_crossentropy', optimizer='rmsprop')
model.fit(x_train, y_train, batch_size=batch_size, epochs=epochs, verbose=1, validation_data=(x_test, y_test))
model.fit(x_train, y_train, batch_size=batch_size, epochs=epochs, verbose=2, validation_split=0.1)
model.fit(x_train, y_train, batch_size=batch_size, epochs=epochs, verbose=0)
model.fit(x_train, y_train, batch_size=batch_size, epochs=epochs, verbose=1, shuffle=False)
model.train_on_batch(x_train[:32], y_train[:32])
loss = model.evaluate(x_test, y_test, verbose=0)
model.predict(x_test, verbose=0)
model.predict_classes(x_test, verbose=0)
model.predict_proba(x_test, verbose=0)
fname = 'test_merge_overlap_temp.h5'
print(model.layers)
model.save_weights(fname, overwrite=True)
print(model.trainable_weights)
model.load_weights(fname)
os.remove(fname)
nloss = model.evaluate(x_test, y_test, verbose=0)
assert(loss == nloss)
# test serialization
config = model.get_config()
Sequential.from_config(config)
model.summary()
json_str = model.to_json()
model_from_json(json_str)
yaml_str = model.to_yaml()
model_from_yaml(yaml_str)
if __name__ == '__main__':
pytest.main([__file__])
+12 -2
Ver Arquivo
@@ -10,11 +10,13 @@ allobj = [losses.mean_squared_error,
losses.mean_absolute_percentage_error,
losses.mean_squared_logarithmic_error,
losses.squared_hinge,
losses.hinge, losses.categorical_crossentropy,
losses.hinge,
losses.categorical_crossentropy,
losses.binary_crossentropy,
losses.kullback_leibler_divergence,
losses.poisson,
losses.cosine_proximity]
losses.cosine_proximity,
losses.logcosh]
def test_objective_shapes_3d():
@@ -44,5 +46,13 @@ def test_cce_one_hot():
assert K.eval(losses.sparse_categorical_crossentropy(y_a, y_b)).shape == (6,)
def test_categorical_hinge():
y_pred = K.variable(np.array([[0.3, 0.2, 0.1], [0.1, 0.2, 0.7]]))
y_true = K.variable(np.array([[0, 1, 0], [1, 0, 0]]))
expected_loss = ((0.3 - 0.2 + 1) + (0.7 - 0.1 + 1)) / 2.0
loss = K.eval(losses.categorical_hinge(y_true, y_pred))
assert np.isclose(expected_loss, loss)
if __name__ == '__main__':
pytest.main([__file__])
+1
Ver Arquivo
@@ -17,6 +17,7 @@ all_metrics = [
metrics.binary_crossentropy,
metrics.poisson,
metrics.cosine_proximity,
metrics.logcosh,
]
all_sparse_metrics = [
+26
Ver Arquivo
@@ -160,6 +160,32 @@ class TestImage:
assert(sorted(dir_iterator.filenames) == sorted(filenames))
shutil.rmtree(tmp_folder)
def test_directory_iterator_class_mode_input(self):
tmp_folder = tempfile.mkdtemp(prefix='test_images')
os.mkdir(os.path.join(tmp_folder, 'class-1'))
# save the images in the paths
count = 0
for test_images in self.all_test_images:
for im in test_images:
filename = os.path.join(tmp_folder, 'class-1', 'image-{}.jpg'.format(count))
im.save(os.path.join(tmp_folder, filename))
count += 1
# create iterator
generator = image.ImageDataGenerator()
dir_iterator = generator.flow_from_directory(tmp_folder, class_mode='input')
batch = next(dir_iterator)
# check if input and output have the same shape
assert(batch[0].shape == batch[1].shape)
# check if the input and output images are not the same numpy array
input_img = batch[0][0]
output_img = batch[1][0]
output_img[0][0][0] += 1
assert(input_img[0][0][0] != output_img[0][0][0])
shutil.rmtree(tmp_folder)
def test_img_utils(self):
height, width = 10, 8

Alguns arquivos não foram exibidos porque demasiados arquivos foram alterados neste diff Mostrar Mais