def init(config_filename, log_verbosity): """ :param str config_filename: filename to config-file :param int log_verbosity: """ rnn.init_better_exchook() rnn.init_thread_join_hack() if config_filename: print("Using config file %r." % config_filename) assert os.path.exists(config_filename) rnn.init_config(config_filename=config_filename, command_line_options=[]) global config config = rnn.config config.set("task", "calculate_wer") config.set("log", None) config.set("log_verbosity", log_verbosity) config.set("use_tensorflow", True) rnn.init_log() print("Returnn calculate-word-error-rate starting up.", file=log.v1) rnn.returnn_greeting() rnn.init_backend_engine() assert Util.BackendEngine.is_tensorflow_selected( ), "this is only for TensorFlow" rnn.init_faulthandler() rnn.init_config_json_network() rnn.print_task_properties()
def init(config_filename, log_verbosity): """ :param str config_filename: filename to config-file :param int log_verbosity: """ rnn.init_better_exchook() rnn.init_thread_join_hack() print("Using config file %r." % config_filename) assert os.path.exists(config_filename) rnn.init_config( config_filename=config_filename, extra_updates={ "use_tensorflow": True, "log": None, "log_verbosity": log_verbosity, "task": __file__, # just extra info for the config }) global config config = rnn.config rnn.init_log() print("Returnn compile-tf-graph starting up.", file=log.v1) rnn.returnn_greeting() rnn.init_backend_engine() assert Util.BackendEngine.is_tensorflow_selected( ), "this is only for TensorFlow" rnn.init_faulthandler() rnn.init_config_json_network()
def init(config_filename, log_verbosity): """ :param str config_filename: filename to config-file :param int log_verbosity: """ rnn.init_better_exchook() rnn.init_thread_join_hack() if config_filename: print("Using config file %r." % config_filename) assert os.path.exists(config_filename) rnn.init_config(config_filename=config_filename, command_line_options=[]) global config config = rnn.config config.set("log", None) config.set("log_verbosity", log_verbosity) config.set("use_tensorflow", True) rnn.init_log() print("Returnn compile-native-op starting up.", file=log.v1) rnn.returnn_greeting() rnn.init_backend_engine() assert Util.BackendEngine.is_tensorflow_selected( ), "this is only for TensorFlow" rnn.init_faulthandler() rnn.init_config_json_network() if 'network' in config.typed_dict: print("Loading network") from TFNetwork import TFNetwork network = TFNetwork(name="root", config=config, rnd_seed=1, train_flag=False, eval_flag=True, search_flag=False) network.construct_from_dict(config.typed_dict["network"])
def demo(): """ Will print out the different network topologies of the specified pretraining scheme. """ import better_exchook better_exchook.install() import rnn import sys if len(sys.argv) <= 1: print("usage: python %s [config] [other options]" % __file__) print("example usage: python %s ++pretrain default ++pretrain_construction_algo from_input" % __file__) rnn.init_config(command_line_options=sys.argv[1:]) # noinspection PyProtectedMember rnn.config._hack_value_reading_debug() rnn.config.update({"log": []}) rnn.init_log() rnn.init_backend_engine() if not rnn.config.value("pretrain", ""): print("config option 'pretrain' not set, will set it for this demo to 'default'") rnn.config.set("pretrain", "default") pretrain = pretrain_from_config(rnn.config) print("pretrain: %s" % pretrain) num_pretrain_epochs = pretrain.get_train_num_epochs() from pprint import pprint for epoch in range(1, 1 + num_pretrain_epochs): print("epoch %i (of %i) network json:" % (epoch, num_pretrain_epochs)) net_json = pretrain.get_network_json_for_epoch(epoch) pprint(net_json) print("done.")
def demo(): """ Will print out the different network topologies of the specified pretraining scheme. """ import better_exchook better_exchook.install() import rnn import sys if len(sys.argv) <= 1: print("usage: python %s [config] [other options]" % __file__) print( "example usage: python %s ++pretrain default ++pretrain_construction_algo from_input" % __file__) rnn.init_config(command_line_options=sys.argv[1:]) # noinspection PyProtectedMember rnn.config._hack_value_reading_debug() rnn.config.update({"log": []}) rnn.init_log() rnn.init_backend_engine() if not rnn.config.value("pretrain", ""): print( "config option 'pretrain' not set, will set it for this demo to 'default'" ) rnn.config.set("pretrain", "default") pretrain = pretrain_from_config(rnn.config) print("pretrain: %s" % pretrain) num_pretrain_epochs = pretrain.get_train_num_epochs() from pprint import pprint for epoch in range(1, 1 + num_pretrain_epochs): print("epoch %i (of %i) network json:" % (epoch, num_pretrain_epochs)) net_json = pretrain.get_network_json_for_epoch(epoch) pprint(net_json) print("done.")
def init(config_filename, log_verbosity, remaining_args=()): """ :param str config_filename: filename to config-file :param int log_verbosity: :param list[str] remaining_args: """ rnn.init_better_exchook() rnn.init_thread_join_hack() print("Using config file %r." % config_filename) assert os.path.exists(config_filename) rnn.init_config( config_filename=config_filename, command_line_options=remaining_args, extra_updates={ "use_tensorflow": True, "log": None, "log_verbosity": log_verbosity, "task": "search", }, default_config={ "debug_print_layer_output_template": True, } ) global config config = rnn.config rnn.init_log() print("Returnn %s starting up." % os.path.basename(__file__), file=log.v1) rnn.returnn_greeting() rnn.init_backend_engine() assert Util.BackendEngine.is_tensorflow_selected(), "this is only for TensorFlow" rnn.init_faulthandler() better_exchook.replace_traceback_format_tb() # makes some debugging easier rnn.init_config_json_network()
def init(config_filename, log_verbosity): """ :param str config_filename: filename to config-file :param int log_verbosity: """ rnn.init_better_exchook() rnn.init_thread_join_hack() print("Using config file %r." % config_filename) assert os.path.exists(config_filename) rnn.init_config(config_filename=config_filename, command_line_options=[]) global config config = rnn.config config.set("log", None) config.set("log_verbosity", log_verbosity) config.set("use_tensorflow", True) rnn.init_log() print("Returnn compile-native-op starting up.", file=log.v1) rnn.returnn_greeting() rnn.init_backend_engine() assert Util.BackendEngine.is_tensorflow_selected(), "this is only for TensorFlow" rnn.init_faulthandler() rnn.init_config_json_network()
def demo(): """ Demo run. Given some learning rate file (with scores / existing lrs), will calculate how lrs would have been set, given some config. """ import better_exchook better_exchook.install() import rnn import sys if len(sys.argv) <= 1: print("usage: python %s [config] [other options] [++check_learning_rates 1]" % __file__) print( ("example usage: " "python %s ++learning_rate_control newbob ++learning_rate_file newbob.data ++learning_rate 0.001") % __file__) rnn.init_config(command_line_options=sys.argv[1:]) # noinspection PyProtectedMember rnn.config._hack_value_reading_debug() rnn.config.update({"log": []}) rnn.init_log() rnn.init_backend_engine() check_lr = rnn.config.bool("check_learning_rates", False) from Pretrain import pretrain_from_config pretrain = pretrain_from_config(rnn.config) first_non_pretrain_epoch = 1 pretrain_learning_rate = None if pretrain: first_non_pretrain_epoch = pretrain.get_train_num_epochs() + 1 log.initialize(verbosity=[5]) control = load_learning_rate_control_from_config(rnn.config) print("LearningRateControl: %r" % control) if not control.epoch_data: print("No epoch data so far.") return first_epoch = min(control.epoch_data.keys()) if first_epoch != 1: print("Strange, first epoch from epoch data is %i." % first_epoch) print("Error key: %s from %r" % (control.get_error_key(epoch=first_epoch), control.epoch_data[first_epoch].error)) if pretrain: pretrain_learning_rate = rnn.config.float('pretrain_learning_rate', control.default_learning_rate) max_epoch = max(control.epoch_data.keys()) for epoch in range(1, max_epoch + 2): # all epochs [1..max_epoch+1] old_learning_rate = None if epoch in control.epoch_data: old_learning_rate = control.epoch_data[epoch].learning_rate if epoch < first_non_pretrain_epoch: learning_rate = pretrain_learning_rate s = "Pretrain epoch %i, fixed learning rate: %s (was: %s)" % (epoch, learning_rate, old_learning_rate) elif 1 < first_non_pretrain_epoch == epoch: learning_rate = control.default_learning_rate s = "First epoch after pretrain, epoch %i, fixed learning rate: %s (was %s)" % ( epoch, learning_rate, old_learning_rate) else: learning_rate = control.calc_new_learning_rate_for_epoch(epoch) s = "Calculated learning rate for epoch %i: %s (was: %s)" % (epoch, learning_rate, old_learning_rate) if learning_rate < control.min_learning_rate: learning_rate = control.min_learning_rate s += ", clipped to %s" % learning_rate s += ", previous relative error: %s" % control.calc_relative_error(epoch - 2, epoch - 1) if hasattr(control, "_calc_recent_mean_relative_error"): # noinspection PyProtectedMember s += ", previous mean relative error: %s" % control._calc_recent_mean_relative_error(epoch) print(s) if check_lr and old_learning_rate is not None: if old_learning_rate != learning_rate: print("Learning rate is different in epoch %i!" % epoch) sys.exit(1) # Overwrite new learning rate so that the calculation for further learning rates stays consistent. if epoch in control.epoch_data: control.epoch_data[epoch].learning_rate = learning_rate else: control.epoch_data[epoch] = control.EpochData(learningRate=learning_rate) print("Finished, last stored epoch was %i." % max_epoch)
def _init_base(configfile=None, target_mode=None, epoch=None, sprint_opts=None): """ :param str|None configfile: filename, via init(), this is set :param str|None target_mode: "forward" or so. via init(), this is set :param int epoch: via init(), this is set :param dict[str,str]|None sprint_opts: optional parameters to override values in configfile """ global isInitialized isInitialized = True # Run through in any case. Maybe just to set targetMode. if not getattr(sys, "argv", None): # Set some dummy. Some code might want this (e.g. TensorFlow). sys.argv = [__file__] global config if not config: # Some subset of what we do in rnn.init(). rnn.init_better_exchook() rnn.init_thread_join_hack() if configfile is None: configfile = DefaultSprintCrnnConfig assert os.path.exists(configfile) rnn.init_config(config_filename=configfile, extra_updates={"task": target_mode}) assert rnn.config config = rnn.config if sprint_opts is not None: config.update(sprint_opts) rnn.init_log() rnn.returnn_greeting(config_filename=configfile) rnn.init_backend_engine() rnn.init_faulthandler(sigusr1_chain=True) rnn.init_config_json_network() global Engine if BackendEngine.is_tensorflow_selected(): # Use TFEngine.Engine class instead of Engine.Engine. from TFEngine import Engine elif BackendEngine.is_theano_selected(): from Engine import Engine import atexit atexit.register(_at_exit_handler) if target_mode: set_target_mode(target_mode) _init_dataset() if target_mode and target_mode == "forward" and epoch: model_filename = config.value('model', '') fns = [ EngineBase.epoch_model_filename(model_filename, epoch, is_pretrain) for is_pretrain in [False, True] ] fn_postfix = "" if BackendEngine.is_tensorflow_selected(): fn_postfix += ".meta" fns_existing = [fn for fn in fns if os.path.exists(fn + fn_postfix)] assert len(fns_existing) == 1, "%s not found" % fns model_epoch_filename = fns_existing[0] config.set('load', model_epoch_filename) assert EngineBase.get_epoch_model(config)[1] == model_epoch_filename, ( "%r != %r" % (EngineBase.get_epoch_model(config), model_epoch_filename)) global engine if not engine: devices = rnn.init_theano_devices() rnn.print_task_properties(devices) rnn.init_engine(devices) engine = rnn.engine assert isinstance(engine, Engine)
def _init_base(configfile=None, target_mode=None, epoch=None, sprint_opts=None): """ :param str|None configfile: filename, via init(), this is set :param str|None target_mode: "forward" or so. via init(), this is set :param int epoch: via init(), this is set :param dict[str,str]|None sprint_opts: optional parameters to override values in configfile """ global isInitialized isInitialized = True # Run through in any case. Maybe just to set targetMode. if not getattr(sys, "argv", None): # Set some dummy. Some code might want this (e.g. TensorFlow). sys.argv = [__file__] global config if not config: # Some subset of what we do in rnn.init(). rnn.init_better_exchook() rnn.init_thread_join_hack() if configfile is None: configfile = DefaultSprintCrnnConfig assert os.path.exists(configfile) rnn.init_config(config_filename=configfile, extra_updates={"task": target_mode}) config = rnn.config if sprint_opts is not None: config.update(sprint_opts) rnn.init_log() rnn.returnn_greeting(config_filename=configfile) rnn.init_backend_engine() rnn.init_faulthandler(sigusr1_chain=True) rnn.init_config_json_network() global Engine if BackendEngine.is_tensorflow_selected(): # Use TFEngine.Engine class instead of Engine.Engine. from TFEngine import Engine elif BackendEngine.is_theano_selected(): from Engine import Engine import atexit atexit.register(_at_exit_handler) if target_mode: set_target_mode(target_mode) _init_dataset() if target_mode and target_mode == "forward" and epoch: model_filename = config.value('model', '') fns = [EngineBase.epoch_model_filename(model_filename, epoch, is_pretrain) for is_pretrain in [False, True]] fn_postfix = "" if BackendEngine.is_tensorflow_selected(): fn_postfix += ".meta" fns_existing = [fn for fn in fns if os.path.exists(fn + fn_postfix)] assert len(fns_existing) == 1, "%s not found" % fns model_epoch_filename = fns_existing[0] config.set('load', model_epoch_filename) assert EngineBase.get_epoch_model(config)[1] == model_epoch_filename, ( "%r != %r" % (EngineBase.get_epoch_model(config), model_epoch_filename)) global engine if not engine: devices = rnn.init_theano_devices() rnn.print_task_properties(devices) rnn.init_engine(devices) engine = rnn.engine assert isinstance(engine, Engine)
def demo(): """ Demo run. Given some learning rate file (with scores / existing lrs), will calculate how lrs would have been set, given some config. """ import better_exchook better_exchook.install() import rnn import sys if len(sys.argv) <= 1: print("usage: python %s [config] [other options] [++check_learning_rates 1]" % __file__) print( ("example usage: " "python %s ++learning_rate_control newbob ++learning_rate_file newbob.data ++learning_rate 0.001") % __file__) rnn.init_config(command_line_options=sys.argv[1:]) # noinspection PyProtectedMember rnn.config._hack_value_reading_debug() rnn.config.update({"log": []}) rnn.init_log() rnn.init_backend_engine() check_lr = rnn.config.bool("check_learning_rates", False) from Pretrain import pretrain_from_config pretrain = pretrain_from_config(rnn.config) first_non_pretrain_epoch = 1 pretrain_learning_rate = None if pretrain: first_non_pretrain_epoch = pretrain.get_train_num_epochs() + 1 log.initialize(verbosity=[5]) control = load_learning_rate_control_from_config(rnn.config) print("LearningRateControl: %r" % control) if not control.epoch_data: print("No epoch data so far.") return first_epoch = min(control.epoch_data.keys()) if first_epoch != 1: print("Strange, first epoch from epoch data is %i." % first_epoch) print("Error key: %s from %r" % (control.get_error_key(epoch=first_epoch), control.epoch_data[first_epoch].error)) if pretrain: pretrain_learning_rate = rnn.config.float('pretrain_learning_rate', control.default_learning_rate) max_epoch = max(control.epoch_data.keys()) for epoch in range(1, max_epoch + 2): # all epochs [1..max_epoch+1] old_learning_rate = None if epoch in control.epoch_data: old_learning_rate = control.epoch_data[epoch].learning_rate if epoch < first_non_pretrain_epoch: learning_rate = pretrain_learning_rate s = "Pretrain epoch %i, fixed learning rate: %s (was: %s)" % (epoch, learning_rate, old_learning_rate) elif 1 < first_non_pretrain_epoch == epoch: learning_rate = control.default_learning_rate s = "First epoch after pretrain, epoch %i, fixed learning rate: %s (was %s)" % ( epoch, learning_rate, old_learning_rate) else: learning_rate = control.calc_new_learning_rate_for_epoch(epoch) s = "Calculated learning rate for epoch %i: %s (was: %s)" % (epoch, learning_rate, old_learning_rate) if learning_rate < control.min_learning_rate: learning_rate = control.min_learning_rate s += ", clipped to %s" % learning_rate s += ", previous relative error: %s" % control.calc_relative_error(epoch - 2, epoch - 1) if hasattr(control, "_calc_recent_mean_relative_error"): # noinspection PyProtectedMember s += ", previous mean relative error: %s" % control._calc_recent_mean_relative_error(epoch) print(s) if check_lr and old_learning_rate is not None: if old_learning_rate != learning_rate: print("Learning rate is different in epoch %i!" % epoch) sys.exit(1) # Overwrite new learning rate so that the calculation for further learning rates stays consistent. if epoch in control.epoch_data: control.epoch_data[epoch].learning_rate = learning_rate else: control.epoch_data[epoch] = control.EpochData(learningRate=learning_rate) print("Finished, last stored epoch was %i." % max_epoch)