def tensorflow_to_theano_model(in_path, out_path): config = load_config_from_json_file(in_path) th2tf = construct_parameter_map(config) keys, values = list(zip(*list(th2tf.items()))) with tf.Session() as sess: new_saver = tf.train.import_meta_graph(in_path + '.meta') new_saver.restore(sess, in_path) params = {} for th_name, tf_name in list(th2tf.items()): if tf_name is not None: try: v = sess.run( tf.get_default_graph().get_tensor_by_name(tf_name)) except: logging.info("Skipping {} because it was not " \ "found".format(tf_name)) continue else: if th_name == 'history_errs': v = [] elif th_name == 'decoder_c_tt': v = np.zeros(1, dtype=np.float32) else: assert False, 'Need to handle {}'.format(th_name) assert th_name not in params, '{} is repeated!'.format(th_name) params[th_name] = v np.savez(out_path, **params) logging.info('Saved {} params to {}'.format(len(params), out_path))
def tensorflow_to_theano_model(in_path, out_path): config = load_config_from_json_file(in_path) th2tf = construct_parameter_map(config) keys, values = list(zip(*list(th2tf.items()))) with tf.Session() as sess: new_saver = tf.train.import_meta_graph(in_path + '.meta') new_saver.restore(sess, in_path) params = {} for th_name, tf_name in list(th2tf.items()): if tf_name is not None: try: v = sess.run(tf.get_default_graph().get_tensor_by_name(tf_name)) except: logging.info("Skipping {} because it was not " \ "found".format(tf_name)) continue else: if th_name == 'history_errs': v = [] elif th_name == 'decoder_c_tt': v = np.zeros(1, dtype=np.float32) else: assert False, 'Need to handle {}'.format(th_name) assert th_name not in params, '{} is repeated!'.format(th_name) params[th_name] = v np.savez(out_path, **params) logging.info('Saved {} params to {}'.format(len(params), out_path))
def main(settings): """ Translates a source language file (or STDIN) into a target language file (or STDOUT). """ # Start logging. level = logging.DEBUG if settings.verbose else logging.INFO logging.basicConfig(level=level, format='%(levelname)s: %(message)s') # Create the TensorFlow session. tf_config = tf.ConfigProto() tf_config.allow_soft_placement = True session = tf.Session(config=tf_config) # Load config file for each model. configs = [] for model in settings.models: config = load_config_from_json_file(model) setattr(config, 'reload', model) configs.append(config) # Create the model graphs and restore their variables. logging.debug("Loading models\n") models = [] # ============= 19/8/16 KP ============ warning('='*20 + 'Model Config to Load') warning(settings.models) # ===================================== for i, config in enumerate(configs): with tf.variable_scope("model%d" % i) as scope: if config.model_type == "transformer": model = TransformerModel(config) else: model = rnn_model.RNNModel(config) saver = model_loader.init_or_restore_variables(config, session, ensemble_scope=scope) model.sampling_utils = SamplingUtils(settings) models.append(model) # ============= 19/8/16 KP ============ model_summary() # ===================================== # TODO Ensembling is currently only supported for RNNs, so if # TODO len(models) > 1 then check models are all rnn # Translate the source file. inference.translate_file(input_file=settings.input, output_file=settings.output, session=session, models=models, configs=configs, beam_size=settings.beam_size, nbest=settings.n_best, minibatch_size=settings.minibatch_size, maxibatch_size=settings.maxibatch_size, normalization_alpha=settings.normalization_alpha)
def main(settings): """ Translates a source language file (or STDIN) into a target language file (or STDOUT). """ # Create the TensorFlow session. tf_config = tf.ConfigProto() tf_config.allow_soft_placement = True session = tf.Session(config=tf_config) # Load config file for each model. configs = [] for model in settings.models: config = load_config_from_json_file(model) setattr(config, 'reload', model) configs.append(config) # Create the model graphs. logging.debug("Loading models\n") models = [] for i, config in enumerate(configs): with tf.variable_scope("model%d" % i) as scope: if config.model_type == "transformer": model = TransformerModel(config) else: model = rnn_model.RNNModel(config) model.sampling_utils = SamplingUtils(settings) models.append(model) # Add smoothing variables (if the models were trained with smoothing). #FIXME Assumes either all models were trained with smoothing or none were. if configs[0].exponential_smoothing > 0.0: smoothing = ExponentialSmoothing(configs[0].exponential_smoothing) # Restore the model variables. for i, config in enumerate(configs): with tf.variable_scope("model%d" % i) as scope: _ = model_loader.init_or_restore_variables(config, session, ensemble_scope=scope) # Swap-in the smoothed versions of the variables. if configs[0].exponential_smoothing > 0.0: session.run(fetches=smoothing.swap_ops) # TODO Ensembling is currently only supported for RNNs, so if # TODO len(models) > 1 then check models are all rnn # Translate the source file. inference.translate_file(input_file=settings.input, output_file=settings.output, session=session, models=models, configs=configs, beam_size=settings.beam_size, nbest=settings.n_best, minibatch_size=settings.minibatch_size, maxibatch_size=settings.maxibatch_size, normalization_alpha=settings.normalization_alpha)
def main(source_file, nbest_file, output_file, rescorer_settings): # load model model_options options = [] for model in rescorer_settings.models: config = load_config_from_json_file(model) setattr(config, 'reload', model) options.append(config) rescore(source_file, nbest_file, output_file, rescorer_settings, options)
def main(source_file, target_file, output_file, scorer_settings): # load model model_options options = [] for model in scorer_settings.models: config = load_config_from_json_file(model) setattr(config, 'reload', model) options.append(config) scores = score_model(source_file, target_file, scorer_settings, options) write_scores(source_file, target_file, scores, output_file, scorer_settings)
def _load_model_options(self): """ Loads config options for each model. """ self._options = [] for model in self._models: config = load_config_from_json_file(model) setattr(config, 'reload', model) self._options.append(config) _, _, _, self._num_to_target = util.load_dictionaries(self._options[0])
def main(settings): """ Translates a source language file (or STDIN) into a target language file (or STDOUT). """ # Start logging. level = logging.DEBUG if settings.verbose else logging.INFO logging.basicConfig(level=level, format='%(levelname)s: %(message)s') # Create the TensorFlow session. tf_config = tf.ConfigProto() tf_config.allow_soft_placement = True session = tf.Session(config=tf_config) # Load config file for each model. configs = [] for model in settings.models: config = load_config_from_json_file(model) setattr(config, 'reload', model) configs.append(config) # Create the model graphs and restore their variables. logging.debug("Loading models\n") models = [] for i, config in enumerate(configs): with tf.variable_scope("model%d" % i) as scope: if config.model_type == "transformer": model = TransformerModel(config) else: model = rnn_model.RNNModel(config) saver = model_loader.init_or_restore_variables(config, session, ensemble_scope=scope) models.append(model) # TODO Ensembling is currently only supported for RNNs, so if # TODO len(models) > 1 then check models are all rnn # Translate the source file. inference.translate_file(input_file=settings.input, output_file=settings.output, session=session, models=models, configs=configs, beam_size=settings.beam_size, nbest=settings.n_best, minibatch_size=settings.minibatch_size, maxibatch_size=settings.maxibatch_size, normalization_alpha=settings.normalization_alpha)
def theano_to_tensorflow_config(model_path): config = load_config_from_json_file(model_path) setattr(config, 'reload', None) setattr(config, 'prior_model', None) return config
def main(settings): """ Translates a source language file (or STDIN) into a target language file (or STDOUT). """ # Create the TensorFlow session. g = tf.Graph() with g.as_default(): tf_config = tf.compat.v1.ConfigProto() tf_config.allow_soft_placement = True session = tf.compat.v1.Session(config=tf_config) # Load config file for each model. configs = [] for model in settings.models: config = load_config_from_json_file(model) setattr(config, 'reload', model) setattr(config, 'translation_maxlen', settings.translation_maxlen) configs.append(config) # Create the model graphs. logging.debug("Loading models\n") models = [] for i, config in enumerate(configs): with tf.compat.v1.variable_scope("model%d" % i) as scope: if config.model_type == "transformer": model = TransformerModel( config, consts_config_str=settings.config_str) else: model = rnn_model.RNNModel(config) model.sampling_utils = SamplingUtils(settings) models.append(model) # Add smoothing variables (if the models were trained with smoothing). # FIXME Assumes either all models were trained with smoothing or none were. if configs[0].exponential_smoothing > 0.0: smoothing = ExponentialSmoothing(configs[0].exponential_smoothing) # Restore the model variables. for i, config in enumerate(configs): with tf.compat.v1.variable_scope("model%d" % i) as scope: _ = model_loader.init_or_restore_variables( config, session, ensemble_scope=scope) # Swap-in the smoothed versions of the variables. if configs[0].exponential_smoothing > 0.0: session.run(fetches=smoothing.swap_ops) max_translation_len = settings.translation_maxlen # Create a BeamSearchSampler / RandomSampler. if settings.translation_strategy == 'beam_search': sampler = BeamSearchSampler(models, configs, settings.beam_size) else: assert settings.translation_strategy == 'sampling' sampler = RandomSampler(models, configs, settings.beam_size) # Warn about the change from neg log probs to log probs for the RNN. if settings.n_best: model_types = [config.model_type for config in configs] if 'rnn' in model_types: logging.warn( 'n-best scores for RNN models have changed from ' 'positive to negative (as of commit 95793196...). ' 'If you are using the scores for reranking etc, then ' 'you may need to update your scripts.') # Translate the source file. translate_utils.translate_file( input_file=settings.input, output_file=settings.output, session=session, sampler=sampler, config=configs[0], max_translation_len=max_translation_len, normalization_alpha=settings.normalization_alpha, consts_config_str=settings.config_str, nbest=settings.n_best, minibatch_size=settings.minibatch_size, maxibatch_size=settings.maxibatch_size)