Skip to content

Commit

Permalink
WIP- #1987 generate schemas for callbacks
Browse files Browse the repository at this point in the history
  • Loading branch information
umesh-timalsina committed Jan 27, 2021
1 parent cc96c7f commit afb8271
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 8 deletions.
30 changes: 23 additions & 7 deletions src/visualizers/widgets/TrainKeras/schemas/generate-schemas.py
Original file line number Diff line number Diff line change
@@ -1,43 +1,58 @@
import tensorflow as tf
from tensorflow import keras
import inspect
import json

def is_class_name(n, skip_names=[]):
from tensorflow import keras


def is_class_name(n, skip_names=None):
if skip_names is None:
skip_names = []
return n[0].isupper() and n not in skip_names


def parse_schema(mod_name, module, name):
class_ = getattr(module, name)
spec = inspect.getfullargspec(class_)
spec = inspect.getfullargspec(class_.__init__)
ctor_args = spec.args[1:]
kw_arg_start_index = len(ctor_args)-len(spec.defaults)
kw_args = list(zip(ctor_args[kw_arg_start_index:], spec.defaults))
kw_arg_start_index = -1
if spec.defaults is not None:
kw_arg_start_index = len(ctor_args)-len(spec.defaults)
kw_args = list(zip(ctor_args[kw_arg_start_index:], spec.defaults if spec.defaults is not None else []))
pos_args = list(zip(ctor_args[0:kw_arg_start_index]))
args = [ (name, None) for name in pos_args ]
args.extend(kw_args)

return {
'name': name,
#'docstring': inspect.getdoc(class_),
'arguments': [ {'name': n, 'default': d} for (n, d) in args ],
#'url': f'https://keras.io/api/{mod_name}/{name.lower()}/'
}

def parse_module_schemas(module, skip_names=[]):

def parse_module_schemas(module, skip_names=None):
if skip_names is None:
skip_names = []

mod_name = module.__name__.split('.')[-1]
mod_names = ( n for n in dir(module) if is_class_name(n, skip_names) )
class_names = ( n for n in mod_names if True ) # type(getattr(module, n)) is type)
schemas = ( parse_schema(mod_name, module, n) for n in class_names )
return [ schema for schema in schemas if schema is not None ]


all_schemas = {}
all_schemas['optimizers'] = parse_module_schemas(keras.optimizers, ['Optimizer'])
all_schemas['losses'] = parse_module_schemas(keras.losses, ['Loss', 'Reduction', 'KLD', 'MAE', 'MAPE', 'MSE', 'MSLE'])
all_schemas['reductions'] = [ getattr(keras.losses.Reduction, name) for name in dir(keras.losses.Reduction) if name[0].isupper() ]
all_schemas['callbacks'] = parse_module_schemas(keras.callbacks, ['BaseLogger', 'Callback', 'CallbackList', 'ProgbarLogger'])


def is_regression(loss_name):
other_losses = ['CosineSimilarity', 'LogCosh', 'Huber']
return 'Error' in loss_name or loss_name in other_losses


def add_loss_category(loss):
if 'Hinge' in loss['name']:
category = 'Hinge'
Expand All @@ -48,6 +63,7 @@ def add_loss_category(loss):

loss['category'] = category + ' losses'


for loss in all_schemas['losses']:
add_loss_category(loss)

Expand Down
2 changes: 1 addition & 1 deletion src/visualizers/widgets/TrainKeras/schemas/index.json
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"optimizers": [{"name": "Adadelta", "arguments": [{"name": "learning_rate", "default": 0.001}, {"name": "rho", "default": 0.95}, {"name": "epsilon", "default": 1e-07}, {"name": "name", "default": "Adadelta"}]}, {"name": "Adagrad", "arguments": [{"name": "learning_rate", "default": 0.001}, {"name": "initial_accumulator_value", "default": 0.1}, {"name": "epsilon", "default": 1e-07}, {"name": "name", "default": "Adagrad"}]}, {"name": "Adam", "arguments": [{"name": "learning_rate", "default": 0.001}, {"name": "beta_1", "default": 0.9}, {"name": "beta_2", "default": 0.999}, {"name": "epsilon", "default": 1e-07}, {"name": "amsgrad", "default": false}, {"name": "name", "default": "Adam"}]}, {"name": "Adamax", "arguments": [{"name": "learning_rate", "default": 0.001}, {"name": "beta_1", "default": 0.9}, {"name": "beta_2", "default": 0.999}, {"name": "epsilon", "default": 1e-07}, {"name": "name", "default": "Adamax"}]}, {"name": "Ftrl", "arguments": [{"name": "learning_rate", "default": 0.001}, {"name": "learning_rate_power", "default": -0.5}, {"name": "initial_accumulator_value", "default": 0.1}, {"name": "l1_regularization_strength", "default": 0.0}, {"name": "l2_regularization_strength", "default": 0.0}, {"name": "name", "default": "Ftrl"}, {"name": "l2_shrinkage_regularization_strength", "default": 0.0}]}, {"name": "Nadam", "arguments": [{"name": "learning_rate", "default": 0.001}, {"name": "beta_1", "default": 0.9}, {"name": "beta_2", "default": 0.999}, {"name": "epsilon", "default": 1e-07}, {"name": "name", "default": "Nadam"}]}, {"name": "RMSprop", "arguments": [{"name": "learning_rate", "default": 0.001}, {"name": "rho", "default": 0.9}, {"name": "momentum", "default": 0.0}, {"name": "epsilon", "default": 1e-07}, {"name": "centered", "default": false}, {"name": "name", "default": "RMSprop"}]}, {"name": "SGD", "arguments": [{"name": "learning_rate", "default": 0.01}, {"name": "momentum", "default": 0.0}, {"name": "nesterov", "default": false}, {"name": "name", "default": "SGD"}]}], "losses": [{"name": "BinaryCrossentropy", "arguments": [{"name": "from_logits", "default": false}, {"name": "label_smoothing", "default": 0}, {"name": "reduction", "default": "auto"}, {"name": "name", "default": "binary_crossentropy"}], "category": "Probabilistic losses"}, {"name": "CategoricalCrossentropy", "arguments": [{"name": "from_logits", "default": false}, {"name": "label_smoothing", "default": 0}, {"name": "reduction", "default": "auto"}, {"name": "name", "default": "categorical_crossentropy"}], "category": "Probabilistic losses"}, {"name": "CategoricalHinge", "arguments": [{"name": "reduction", "default": "auto"}, {"name": "name", "default": "categorical_hinge"}], "category": "Hinge losses"}, {"name": "CosineSimilarity", "arguments": [{"name": "axis", "default": -1}, {"name": "reduction", "default": "auto"}, {"name": "name", "default": "cosine_similarity"}], "category": "Regression losses"}, {"name": "Hinge", "arguments": [{"name": "reduction", "default": "auto"}, {"name": "name", "default": "hinge"}], "category": "Hinge losses"}, {"name": "Huber", "arguments": [{"name": "delta", "default": 1.0}, {"name": "reduction", "default": "auto"}, {"name": "name", "default": "huber_loss"}], "category": "Regression losses"}, {"name": "KLDivergence", "arguments": [{"name": "reduction", "default": "auto"}, {"name": "name", "default": "kullback_leibler_divergence"}], "category": "Probabilistic losses"}, {"name": "LogCosh", "arguments": [{"name": "reduction", "default": "auto"}, {"name": "name", "default": "logcosh"}], "category": "Regression losses"}, {"name": "MeanAbsoluteError", "arguments": [{"name": "reduction", "default": "auto"}, {"name": "name", "default": "mean_absolute_error"}], "category": "Regression losses"}, {"name": "MeanAbsolutePercentageError", "arguments": [{"name": "reduction", "default": "auto"}, {"name": "name", "default": "mean_absolute_percentage_error"}], "category": "Regression losses"}, {"name": "MeanSquaredError", "arguments": [{"name": "reduction", "default": "auto"}, {"name": "name", "default": "mean_squared_error"}], "category": "Regression losses"}, {"name": "MeanSquaredLogarithmicError", "arguments": [{"name": "reduction", "default": "auto"}, {"name": "name", "default": "mean_squared_logarithmic_error"}], "category": "Regression losses"}, {"name": "Poisson", "arguments": [{"name": "reduction", "default": "auto"}, {"name": "name", "default": "poisson"}], "category": "Probabilistic losses"}, {"name": "SparseCategoricalCrossentropy", "arguments": [{"name": "from_logits", "default": false}, {"name": "reduction", "default": "auto"}, {"name": "name", "default": "sparse_categorical_crossentropy"}], "category": "Probabilistic losses"}, {"name": "SquaredHinge", "arguments": [{"name": "reduction", "default": "auto"}, {"name": "name", "default": "squared_hinge"}], "category": "Hinge losses"}], "reductions": ["auto", "none", "sum", "sum_over_batch_size"]}
{"optimizers": [{"name": "Adadelta", "arguments": [{"name": "learning_rate", "default": 0.001}, {"name": "rho", "default": 0.95}, {"name": "epsilon", "default": 1e-07}, {"name": "name", "default": "Adadelta"}]}, {"name": "Adagrad", "arguments": [{"name": "learning_rate", "default": 0.001}, {"name": "initial_accumulator_value", "default": 0.1}, {"name": "epsilon", "default": 1e-07}, {"name": "name", "default": "Adagrad"}]}, {"name": "Adam", "arguments": [{"name": "learning_rate", "default": 0.001}, {"name": "beta_1", "default": 0.9}, {"name": "beta_2", "default": 0.999}, {"name": "epsilon", "default": 1e-07}, {"name": "amsgrad", "default": false}, {"name": "name", "default": "Adam"}]}, {"name": "Adamax", "arguments": [{"name": "learning_rate", "default": 0.001}, {"name": "beta_1", "default": 0.9}, {"name": "beta_2", "default": 0.999}, {"name": "epsilon", "default": 1e-07}, {"name": "name", "default": "Adamax"}]}, {"name": "Ftrl", "arguments": [{"name": "learning_rate", "default": 0.001}, {"name": "learning_rate_power", "default": -0.5}, {"name": "initial_accumulator_value", "default": 0.1}, {"name": "l1_regularization_strength", "default": 0.0}, {"name": "l2_regularization_strength", "default": 0.0}, {"name": "name", "default": "Ftrl"}, {"name": "l2_shrinkage_regularization_strength", "default": 0.0}]}, {"name": "Nadam", "arguments": [{"name": "learning_rate", "default": 0.001}, {"name": "beta_1", "default": 0.9}, {"name": "beta_2", "default": 0.999}, {"name": "epsilon", "default": 1e-07}, {"name": "name", "default": "Nadam"}]}, {"name": "RMSprop", "arguments": [{"name": "learning_rate", "default": 0.001}, {"name": "rho", "default": 0.9}, {"name": "momentum", "default": 0.0}, {"name": "epsilon", "default": 1e-07}, {"name": "centered", "default": false}, {"name": "name", "default": "RMSprop"}]}, {"name": "SGD", "arguments": [{"name": "learning_rate", "default": 0.01}, {"name": "momentum", "default": 0.0}, {"name": "nesterov", "default": false}, {"name": "name", "default": "SGD"}]}], "losses": [{"name": "BinaryCrossentropy", "arguments": [{"name": "from_logits", "default": false}, {"name": "label_smoothing", "default": 0}, {"name": "reduction", "default": "auto"}, {"name": "name", "default": "binary_crossentropy"}], "category": "Probabilistic losses"}, {"name": "CategoricalCrossentropy", "arguments": [{"name": "from_logits", "default": false}, {"name": "label_smoothing", "default": 0}, {"name": "reduction", "default": "auto"}, {"name": "name", "default": "categorical_crossentropy"}], "category": "Probabilistic losses"}, {"name": "CategoricalHinge", "arguments": [{"name": "reduction", "default": "auto"}, {"name": "name", "default": "categorical_hinge"}], "category": "Hinge losses"}, {"name": "CosineSimilarity", "arguments": [{"name": "axis", "default": -1}, {"name": "reduction", "default": "auto"}, {"name": "name", "default": "cosine_similarity"}], "category": "Regression losses"}, {"name": "Hinge", "arguments": [{"name": "reduction", "default": "auto"}, {"name": "name", "default": "hinge"}], "category": "Hinge losses"}, {"name": "Huber", "arguments": [{"name": "delta", "default": 1.0}, {"name": "reduction", "default": "auto"}, {"name": "name", "default": "huber_loss"}], "category": "Regression losses"}, {"name": "KLDivergence", "arguments": [{"name": "reduction", "default": "auto"}, {"name": "name", "default": "kl_divergence"}], "category": "Probabilistic losses"}, {"name": "LogCosh", "arguments": [{"name": "reduction", "default": "auto"}, {"name": "name", "default": "log_cosh"}], "category": "Regression losses"}, {"name": "MeanAbsoluteError", "arguments": [{"name": "reduction", "default": "auto"}, {"name": "name", "default": "mean_absolute_error"}], "category": "Regression losses"}, {"name": "MeanAbsolutePercentageError", "arguments": [{"name": "reduction", "default": "auto"}, {"name": "name", "default": "mean_absolute_percentage_error"}], "category": "Regression losses"}, {"name": "MeanSquaredError", "arguments": [{"name": "reduction", "default": "auto"}, {"name": "name", "default": "mean_squared_error"}], "category": "Regression losses"}, {"name": "MeanSquaredLogarithmicError", "arguments": [{"name": "reduction", "default": "auto"}, {"name": "name", "default": "mean_squared_logarithmic_error"}], "category": "Regression losses"}, {"name": "Poisson", "arguments": [{"name": "reduction", "default": "auto"}, {"name": "name", "default": "poisson"}], "category": "Probabilistic losses"}, {"name": "SparseCategoricalCrossentropy", "arguments": [{"name": "from_logits", "default": false}, {"name": "reduction", "default": "auto"}, {"name": "name", "default": "sparse_categorical_crossentropy"}], "category": "Probabilistic losses"}, {"name": "SquaredHinge", "arguments": [{"name": "reduction", "default": "auto"}, {"name": "name", "default": "squared_hinge"}], "category": "Hinge losses"}], "reductions": ["auto", "none", "sum", "sum_over_batch_size"], "callbacks": [{"name": "CSVLogger", "arguments": [{"name": ["filename"], "default": null}, {"name": "separator", "default": ","}, {"name": "append", "default": false}]}, {"name": "EarlyStopping", "arguments": [{"name": "monitor", "default": "val_loss"}, {"name": "min_delta", "default": 0}, {"name": "patience", "default": 0}, {"name": "verbose", "default": 0}, {"name": "mode", "default": "auto"}, {"name": "baseline", "default": null}, {"name": "restore_best_weights", "default": false}]}, {"name": "History", "arguments": []}, {"name": "LambdaCallback", "arguments": [{"name": "on_epoch_begin", "default": null}, {"name": "on_epoch_end", "default": null}, {"name": "on_batch_begin", "default": null}, {"name": "on_batch_end", "default": null}, {"name": "on_train_begin", "default": null}, {"name": "on_train_end", "default": null}]}, {"name": "LearningRateScheduler", "arguments": [{"name": ["schedule"], "default": null}, {"name": "verbose", "default": 0}]}, {"name": "ModelCheckpoint", "arguments": [{"name": ["filepath"], "default": null}, {"name": "monitor", "default": "val_loss"}, {"name": "verbose", "default": 0}, {"name": "save_best_only", "default": false}, {"name": "save_weights_only", "default": false}, {"name": "mode", "default": "auto"}, {"name": "save_freq", "default": "epoch"}, {"name": "options", "default": null}]}, {"name": "ReduceLROnPlateau", "arguments": [{"name": "monitor", "default": "val_loss"}, {"name": "factor", "default": 0.1}, {"name": "patience", "default": 10}, {"name": "verbose", "default": 0}, {"name": "mode", "default": "auto"}, {"name": "min_delta", "default": 0.0001}, {"name": "cooldown", "default": 0}, {"name": "min_lr", "default": 0}]}, {"name": "RemoteMonitor", "arguments": [{"name": "root", "default": "http://localhost:9000"}, {"name": "path", "default": "/publish/epoch/end/"}, {"name": "field", "default": "data"}, {"name": "headers", "default": null}, {"name": "send_as_json", "default": false}]}, {"name": "TensorBoard", "arguments": [{"name": "log_dir", "default": "logs"}, {"name": "histogram_freq", "default": 0}, {"name": "write_graph", "default": true}, {"name": "write_images", "default": false}, {"name": "update_freq", "default": "epoch"}, {"name": "profile_batch", "default": 2}, {"name": "embeddings_freq", "default": 0}, {"name": "embeddings_metadata", "default": null}]}, {"name": "TerminateOnNaN", "arguments": []}]}

0 comments on commit afb8271

Please sign in to comment.