lm1b/model/model_nodes.py
def restore_original_lm1b(sess, run_config):
  """
  Var mapping shenanigans to restore the pre-trained model to the current graph
  :param sess:
  :param run_config:
  :return:
  """

  def create_lm1b_restoration_var_map(char_embedding_vars, lstm_vars, softmax_vars):
    var_map = {}

    # Map char embedding vars
    var_map = merge(var_map, dict(map(lambda x: (x.op.name, x), char_embedding_vars)))

    # Map lstm embedding vars
    var_map_regexes = {r"^(" + LSTM_SCOPE_PREFIX + "\d)/lstm_cell/projection/kernel/part_(\d).*": r"\1/W_P_\2",
                       r"^(" + LSTM_SCOPE_PREFIX + "\d)/lstm_cell/kernel/part_(\d).*": r"\1/W_\2",
                       r"^(" + LSTM_SCOPE_PREFIX + "\d)/lstm_cell/bias.*": r"\1/B",
                       r"^(" + LSTM_SCOPE_PREFIX + "\d)/lstm_cell/w_([fio])_diag.*":
                         lambda match: match.group(1) + "/W_" + match.group(
                           2).upper() + "_diag",
                       }
    for r_match, r_replace in var_map_regexes.items():
      matching_variables = filter(lambda x: re.match(r_match, x.name), lstm_vars)
      for v in matching_variables:
        var_map[re.sub(r_match, r_replace, v.name)] = v

    # Map softmax embedding vars
    var_map = merge(var_map, dict(map(lambda x: (x.op.name, x), softmax_vars)))

    return var_map

  var_map = create_lm1b_restoration_var_map(
    char_embedding_vars=tf.get_collection(tf.GraphKeys.GLOBAL_VARIABLES, scope=CHAR_EMBEDDING_SCOPE),
    lstm_vars=tf.get_collection(tf.GraphKeys.GLOBAL_VARIABLES, scope=LSTM_SCOPE_PREFIX),
    softmax_vars=tf.get_collection(tf.GraphKeys.GLOBAL_VARIABLES, scope=SOFTMAX_SCOPE)
  )

  saver = tf.train.Saver(var_list=var_map)
  saver.restore(sess, os.path.join(run_config['model_dir_path_original'], "ckpt-*"))

  Var mapping shenanigans to restore the pre-trained model to the current graph
  :param sess:
  :param run_config:
  :return:
  
Var mapping shenanigans to restore the pre-trained model to the current graph
Var mapping shenanigans to restore the pre - trained model to the current graph
def restore_original_lm1b(sess, run_config):
  def create_lm1b_restoration_var_map(char_embedding_vars, lstm_vars, softmax_vars):
    var_map = {}
    var_map = merge(var_map, dict(map(lambda x: (x.op.name, x), char_embedding_vars)))
    var_map_regexes = {r"^(" + LSTM_SCOPE_PREFIX + "\d)/lstm_cell/projection/kernel/part_(\d).*": r"\1/W_P_\2",
                       r"^(" + LSTM_SCOPE_PREFIX + "\d)/lstm_cell/kernel/part_(\d).*": r"\1/W_\2",
                       r"^(" + LSTM_SCOPE_PREFIX + "\d)/lstm_cell/bias.*": r"\1/B",
                       r"^(" + LSTM_SCOPE_PREFIX + "\d)/lstm_cell/w_([fio])_diag.*":
                         lambda match: match.group(1) + "/W_" + match.group(
                           2).upper() + "_diag",
                       }
    for r_match, r_replace in var_map_regexes.items():
      matching_variables = filter(lambda x: re.match(r_match, x.name), lstm_vars)
      for v in matching_variables:
        var_map[re.sub(r_match, r_replace, v.name)] = v
    var_map = merge(var_map, dict(map(lambda x: (x.op.name, x), softmax_vars)))
    return var_map
  var_map = create_lm1b_restoration_var_map(
    char_embedding_vars=tf.get_collection(tf.GraphKeys.GLOBAL_VARIABLES, scope=CHAR_EMBEDDING_SCOPE),
    lstm_vars=tf.get_collection(tf.GraphKeys.GLOBAL_VARIABLES, scope=LSTM_SCOPE_PREFIX),
    softmax_vars=tf.get_collection(tf.GraphKeys.GLOBAL_VARIABLES, scope=SOFTMAX_SCOPE)
  )
  saver = tf.train.Saver(var_list=var_map)
  saver.restore(sess, os.path.join(run_config['model_dir_path_original'], "ckpt-*"))
def restore_original_lm1b ( sess , run_config ) : def create_lm1b_restoration_var_map ( char_embedding_vars , lstm_vars , softmax_vars ) : var_map = { } var_map = merge ( var_map , dict ( map ( lambda x : ( x . op . name , x ) , char_embedding_vars ) ) ) var_map_regexes = { r"^(" + LSTM_SCOPE_PREFIX + "\d)/lstm_cell/projection/kernel/part_(\d).*" : r"\1/W_P_\2" , r"^(" + LSTM_SCOPE_PREFIX + "\d)/lstm_cell/kernel/part_(\d).*" : r"\1/W_\2" , r"^(" + LSTM_SCOPE_PREFIX + "\d)/lstm_cell/bias.*" : r"\1/B" , r"^(" + LSTM_SCOPE_PREFIX + "\d)/lstm_cell/w_([fio])_diag.*" : lambda match : match . group ( 1 ) + "/W_" + match . group ( 2 ) . upper ( ) + "_diag" , } for r_match , r_replace in var_map_regexes . items ( ) : matching_variables = filter ( lambda x : re . match ( r_match , x . name ) , lstm_vars ) for v in matching_variables : var_map [ re . sub ( r_match , r_replace , v . name ) ] = v var_map = merge ( var_map , dict ( map ( lambda x : ( x . op . name , x ) , softmax_vars ) ) ) return var_map var_map = create_lm1b_restoration_var_map ( char_embedding_vars = tf . get_collection ( tf . GraphKeys . GLOBAL_VARIABLES , scope = CHAR_EMBEDDING_SCOPE ) , lstm_vars = tf . get_collection ( tf . GraphKeys . GLOBAL_VARIABLES , scope = LSTM_SCOPE_PREFIX ) , softmax_vars = tf . get_collection ( tf . GraphKeys . GLOBAL_VARIABLES , scope = SOFTMAX_SCOPE ) ) saver = tf . train . Saver ( var_list = var_map ) saver . restore ( sess , os . path . join ( run_config [ 'model_dir_path_original' ] , "ckpt-*" ) )
Var mapping shenanigans to restore the pre-trained model to the current graph
Var mapping shenanigans to restore the pre - trained model to the current graph
"""
  Var mapping shenanigans to restore the pre-trained model to the current graph
  :param sess:
  :param run_config:
  :return:
  """ # Map char embedding vars # Map lstm embedding vars # Map softmax embedding vars
None
sess
None
run_config
None