-
Notifications
You must be signed in to change notification settings - Fork 269
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #392 from autonomio/daily-dev
* Fix the required parameters discrepancy * Fix hidden_layers complexity * Make @pep8speaks stop crying * Handles PRs #292 and #379 - Implements a custom error if a non-supported optimizer is used with with lr_normalizer #292 - Fixed docs typo #379 * Updated PR template
- Loading branch information
Showing
8 changed files
with
63 additions
and
74 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,80 +1,41 @@ | ||
def hidden_layers(model, params, last_neuron): | ||
from keras.layers import Dense, Dropout | ||
from .network_shape import network_shape | ||
from ..utils.exceptions import TalosParamsError | ||
|
||
|
||
def hidden_layers(model, params, last_neuron): | ||
'''HIDDEN LAYER Generator | ||
NOTE: 'first_neuron', 'dropout', and 'hidden_layers' need | ||
NOTE: 'shapes', 'first_neuron', 'dropout', and 'hidden_layers' need | ||
to be present in the params dictionary. | ||
Hidden layer generation for the cases where number | ||
of layers is used as a variable in the optimization process. | ||
Handles things in a way where any number of layers can be tried | ||
with matching hyperparameters.''' | ||
|
||
from keras.layers import Dense, Dropout | ||
from .network_shape import network_shape | ||
from ..utils.exceptions import TalosParamsError | ||
|
||
try: | ||
kernel_initializer = params['kernel_initializer'] | ||
except KeyError: | ||
kernel_initializer = 'glorot_uniform' | ||
|
||
try: | ||
kernel_regularizer = params['kernel_regularizer'] | ||
except KeyError: | ||
kernel_regularizer = None | ||
|
||
try: | ||
bias_initializer = params['bias_initializer'] | ||
except KeyError: | ||
bias_initializer = 'zeros' | ||
|
||
try: | ||
bias_regularizer = params['bias_regularizer'] | ||
except KeyError: | ||
bias_regularizer = None | ||
|
||
try: | ||
use_bias = params['use_bias'] | ||
except KeyError: | ||
use_bias = True | ||
|
||
try: | ||
activity_regularizer = params['activity_regularizer'] | ||
except KeyError: | ||
activity_regularizer = None | ||
|
||
try: | ||
kernel_constraint = params['kernel_constraint'] | ||
except KeyError: | ||
kernel_constraint = None | ||
|
||
try: | ||
bias_constraint = params['bias_constraint'] | ||
except KeyError: | ||
bias_constraint = None | ||
|
||
# check for the params that are required for hidden_layers | ||
for param in ['shapes', 'first_neuron', 'dropout']: | ||
try: | ||
params[param] | ||
except KeyError as err: | ||
if err.args[0] == param: | ||
raise TalosParamsError("hidden_layers requires '" + param + "' in params") | ||
for param in ['shapes', 'first_neuron', 'dropout', 'hidden_layers']: | ||
if param not in params: | ||
raise TalosParamsError( | ||
"hidden_layers requires '" + param + "' in params") | ||
|
||
layer_neurons = network_shape(params, last_neuron) | ||
|
||
for i in range(params['hidden_layers']): | ||
|
||
model.add(Dense(layer_neurons[i], | ||
activation=params['activation'], | ||
use_bias=use_bias, | ||
kernel_initializer=kernel_initializer, | ||
kernel_regularizer=kernel_regularizer, | ||
bias_initializer=bias_initializer, | ||
bias_regularizer=bias_regularizer, | ||
activity_regularizer=activity_regularizer, | ||
kernel_constraint=kernel_constraint, | ||
bias_constraint=bias_constraint)) | ||
|
||
model.add(Dense( | ||
layer_neurons[i], | ||
kernel_initializer=params.get( | ||
'kernel_initializer', | ||
'glorot_uniform' | ||
), | ||
kernel_regularizer=params.get('kernel_regularizer'), | ||
bias_initializer=params.get('bias_initializer', 'zeros'), | ||
bias_regularizer=params.get('bias_regularizer'), | ||
use_bias=params.get('use_bias', True), | ||
activity_regularizer=params.get('activity_regularizer'), | ||
kernel_constraint=params.get('kernel_constraint'), | ||
bias_constraint=params.get('bias_constraint'), | ||
activation=params.get('activation') | ||
)) | ||
model.add(Dropout(params['dropout'])) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
def test_lr_normalizer(): | ||
'''Test learning rate normalizer to confirm an invalid type is | ||
recognized and throws TalosModelError.''' | ||
|
||
from talos.model.normalizers import lr_normalizer | ||
from talos.utils.exceptions import TalosModelError | ||
|
||
print('Testing lr_normalizer() and invalid optimizer type...') | ||
|
||
# Using string as proxy for any invalid class | ||
# (ex., tensorflow-sourced optimizer) | ||
bad_optimizer = 'test' | ||
|
||
try: | ||
lr_normalizer(1, bad_optimizer) | ||
except TalosModelError: | ||
print('Invalid model optimizer caught successfully!') | ||
pass | ||
else: | ||
print('Invalid (string) model optimizer type not caught.') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters