# Path: sequencing/utils/algebra_ops.py
# class LinearOp(object):
#     def __init__(self, input_size, output_size, init_scale=None,
#                  name='linear_op'):
#         if not init_scale:
#             init_scale = (6./(input_size + output_size)) ** 0.5
# 
#         with tf.variable_scope(name):
#             self.W = tf.get_variable('weights',
#                                      shape=[input_size, output_size],
#                                      initializer=tf.random_uniform_initializer(
#                                          -init_scale,
#                                          init_scale),
#                                      dtype=DTYPE)
#             self.b = tf.get_variable('biases', shape=[output_size], dtype=DTYPE)
# 
#     def __call__(self, inputs):
#         return tf.add(tf.matmul(inputs, self.W), self.b)


import collections
import tensorflow as tf
from contextlib import ExitStack
from .algebra_ops import LinearOp
from tensorflow.contrib.rnn import LSTMStateTuple
        if graph is not None:
            stack.enter_context(graph.as_default())
        reader = tf.train.NewCheckpointReader(save_file)
        saved_shapes = reader.get_variable_to_shape_map()
        var_names = sorted(
            [(var.name, var.name.split(':')[0]) for var in tf.global_variables()
             if var.name.split(':')[0] in saved_shapes])
        restore_vars = []
        name2var = dict(
            zip(map(lambda x: x.name.split(':')[0], tf.global_variables()),
                tf.global_variables()))
        with tf.variable_scope('', reuse=True):
            for var_name, saved_var_name in var_names:
                curr_var = name2var[saved_var_name]
                var_shape = curr_var.get_shape().as_list()
                if var_shape == saved_shapes[saved_var_name]:
                    restore_vars.append(curr_var)

            saver = tf.train.Saver(restore_vars)
            saver.restore(session, save_file)
        tf.logging.info('restored {} variables: {}'.format(len(restore_vars),
                                                           restore_vars))

class EncoderDecoderBridge(object):

    def __init__(self, state_size, decoder_rnn_params):
        self.drnn_state_is_tuple = decoder_rnn_params['cell_name'].endswith('LSTMCell')
        self.drnn_num_layers = decoder_rnn_params['num_layers']
        self.drnn_state_size = decoder_rnn_params['state_size']
        if self.drnn_state_is_tuple:

# Path: sequencing/utils/algebra_ops.py
# class LinearOp(object):
#     def __init__(self, input_size, output_size, init_scale=None,
#                  name='linear_op'):
#         if not init_scale:
#             init_scale = (6./(input_size + output_size)) ** 0.5
# 
#         with tf.variable_scope(name):
#             self.W = tf.get_variable('weights',
#                                      shape=[input_size, output_size],
#                                      initializer=tf.random_uniform_initializer(
#                                          -init_scale,
#                                          init_scale),
#                                      dtype=DTYPE)
#             self.b = tf.get_variable('biases', shape=[output_size], dtype=DTYPE)
# 
#     def __call__(self, inputs):
#         return tf.add(tf.matmul(inputs, self.W), self.b)


# Path: sequencing/utils/misc.py
import collections
import tensorflow as tf
from contextlib import ExitStack
from .algebra_ops import LinearOp
from tensorflow.contrib.rnn import LSTMStateTuple

        if graph is not None:
            stack.enter_context(graph.as_default())
        reader = tf.train.NewCheckpointReader(save_file)
        saved_shapes = reader.get_variable_to_shape_map()
        var_names = sorted(
            [(var.name, var.name.split(':')[0]) for var in tf.global_variables()
             if var.name.split(':')[0] in saved_shapes])
        restore_vars = []
        name2var = dict(
            zip(map(lambda x: x.name.split(':')[0], tf.global_variables()),
                tf.global_variables()))
        with tf.variable_scope('', reuse=True):
            for var_name, saved_var_name in var_names:
                curr_var = name2var[saved_var_name]
                var_shape = curr_var.get_shape().as_list()
                if var_shape == saved_shapes[saved_var_name]:
                    restore_vars.append(curr_var)

            saver = tf.train.Saver(restore_vars)
            saver.restore(session, save_file)
        tf.logging.info('restored {} variables: {}'.format(len(restore_vars),
                                                           restore_vars))

class EncoderDecoderBridge(object):

    def __init__(self, state_size, decoder_rnn_params):
        self.drnn_state_is_tuple = decoder_rnn_params['cell_name'].endswith('LSTMCell')
        self.drnn_num_layers = decoder_rnn_params['num_layers']
        self.drnn_state_size = decoder_rnn_params['state_size']
        if self.drnn_state_is_tuple:

            self.init_transfer = LinearOp(state_size,