deeppavlov/models/elmo/train_utils.py
def dump_weights(tf_save_dir, outfile, options):
    """
    Dump the trained weights from a model to a HDF5 file.
    """

    def _get_outname(tf_name):
        outname = re.sub(':0$', '', tf_name)
        outname = outname.lstrip('lm/')
        outname = re.sub('/rnn/', '/RNN/', outname)
        outname = re.sub('/multi_rnn_cell/', '/MultiRNNCell/', outname)
        outname = re.sub('/cell_', '/Cell', outname)
        outname = re.sub('/lstm_cell/', '/LSTMCell/', outname)
        if '/RNN/' in outname:
            if 'projection' in outname:
                outname = re.sub('projection/kernel', 'W_P_0', outname)
            else:
                outname = re.sub('/kernel', '/W_0', outname)
                outname = re.sub('/bias', '/B', outname)
        return outname

    ckpt_file = tf.train.latest_checkpoint(tf_save_dir)

    config = tf.ConfigProto(allow_soft_placement=True)
    with tf.Graph().as_default():
        with tf.Session(config=config) as sess:
            with tf.variable_scope('lm'):
                LanguageModel(options, False)  # Create graph
                # we use the "Saver" class to load the variables
                loader = tf.train.Saver()
                loader.restore(sess, ckpt_file)

            with h5py.File(outfile, 'w') as fout:
                for v in tf.trainable_variables():
                    if v.name.find('softmax') >= 0:
                        # don't dump these
                        continue
                    outname = _get_outname(v.name)
                    # print("Saving variable {0} with name {1}".format(
                    #     v.name, outname))
                    shape = v.get_shape().as_list()
                    dset = fout.create_dataset(outname, shape, dtype='float32')
                    values = sess.run([v])[0]
                    dset[...] = values
def dump_weights(tf_save_dir, outfile, options):
    """
    Dump the trained weights from a model to a HDF5 file.
    """

    def _get_outname(tf_name):
        outname = re.sub(':0$', '', tf_name)
        outname = outname.lstrip('lm/')
        outname = re.sub('/rnn/', '/RNN/', outname)
        outname = re.sub('/multi_rnn_cell/', '/MultiRNNCell/', outname)
        outname = re.sub('/cell_', '/Cell', outname)
        outname = re.sub('/lstm_cell/', '/LSTMCell/', outname)
        if '/RNN/' in outname:
            if 'projection' in outname:
                outname = re.sub('projection/kernel', 'W_P_0', outname)
            else:
                outname = re.sub('/kernel', '/W_0', outname)
                outname = re.sub('/bias', '/B', outname)
        return outname

    ckpt_file = tf.train.latest_checkpoint(tf_save_dir)

    config = tf.ConfigProto(allow_soft_placement=True)
    with tf.Graph().as_default():
        with tf.Session(config=config) as sess:
            with tf.variable_scope('lm'):
                LanguageModel(options, False)  # Create graph
                # we use the "Saver" class to load the variables
                loader = tf.train.Saver()
                loader.restore(sess, ckpt_file)

            with h5py.File(outfile, 'w') as fout:
                for v in tf.trainable_variables():
                    if v.name.find('softmax') >= 0:
                        # don't dump these
                        continue
                    outname = _get_outname(v.name)
                    # print("Saving variable {0} with name {1}".format(
                    #     v.name, outname))
                    shape = v.get_shape().as_list()
                    dset = fout.create_dataset(outname, shape, dtype='float32')
                    values = sess.run([v])[0]
                    dset[...] = values
Dump the trained weights from a model to a HDF5 file.