(model, config, tf_checkpoint_path)
return model
Load tf checkpoints in a pytorch model.
Load tf checkpoints in a pytorch model.
Load tf checkpoints in a pytorch model .
def load_tf_weights_in_albert(model, config, tf_checkpoint_path):
    """ Load tf checkpoints in a pytorch model.
    """
    try:
        import re
        import numpy as np
        import tensorflow as tf
    except ImportError:
        logger.error("Loading a TensorFlow model in PyTorch, requires TensorFlow to be installed. Please see "
                     "https://www.tensorflow.org/install/ for installation instructions.")
        raise
    tf_path = os.path.abspath(tf_checkpoint_path)
    logger.info("Converting TensorFlow checkpoint from {}".format(tf_path))
    # Load weights from TF model
    init_vars = tf.train.list_variables(tf_path)
    names = []
    arrays = []
    for name, shape in init_vars:
        logger.info("Loading TF weight {} with shape {}".format(name, shape))
        array = tf.train.load_variable(tf_path, name)
        names.append(name)
        arrays.append(array)

    for name, array in zip(names, arrays):
        name = name.split('/')
        # adam_v and adam_m are variables used in AdamWeightDecayOptimizer to calculated m and v
        # which are not required for using pretrained model
        if any(n in ["adam_v", "adam_m", "global_step"] for n in name):
            logger.info("Skipping {}".format("/".join(name)))
            continue
        pointer = model
        for m_name in name:
            if re.fullmatch(r'[A-Za-z]+_\d+', m_name):
                l = re.split(r'_(\d+)', m_name)
            else:
                l = [m_name]
            if l[0] == 'kernel' or l[0] == 'gamma':
                pointer = getattr(pointer, 'weight')
            elif l[0] == 'output_bias' or l[0] == 'beta':
                pointer = getattr(pointer, 'bias')
            elif l[0] == 'output_weights':
                pointer = getattr(pointer, 'weight')
            elif l[0] == 'squad':
                pointer = getattr(pointer, 'classifier')
            else:
                try:
                    pointer = getattr(pointer, l[0])
                except AttributeError:
                    logger.info("Skipping {}".format("/".join(name)))
                    continue
            if len(l) >= 2:
                num = int(l[1])
                pointer = pointer[num]
        if m_name[-11:] == '_embeddings':
            pointer = getattr(pointer, 'weight')
        elif m_name[-13:] == '_embeddings_2':
            pointer = getattr(pointer, 'weight')
            array = np.transpose(array)
        elif m_name == 'kernel':
            array = np.transpose(array)
        try:
            assert pointer.shape == array.shape
        except AssertionError as e:
            e.args += (pointer.shape, array.shape)
            raise
        logger.info("Initialize PyTorch weight {}".format(name))
        pointer.data = torch.from_numpy(array)
    return model
def load_tf_weights_in_albert ( model , config , tf_checkpoint_path ) : try : import re import numpy as np import tensorflow as tf except ImportError : logger . error ( "Loading a TensorFlow model in PyTorch, requires TensorFlow to be installed. Please see " "https://www.tensorflow.org/install/ for installation instructions." ) raise tf_path = os . path . abspath ( tf_checkpoint_path ) logger . info ( "Converting TensorFlow checkpoint from {}" . format ( tf_path ) ) # Load weights from TF model init_vars = tf . train . list_variables ( tf_path ) names = [ ] arrays = [ ] for name , shape in init_vars : logger . info ( "Loading TF weight {} with shape {}" . format ( name , shape ) ) array = tf . train . load_variable ( tf_path , name ) names . append ( name ) arrays . append ( array ) for name , array in zip ( names , arrays ) : name = name . split ( '/' ) # adam_v and adam_m are variables used in AdamWeightDecayOptimizer to calculated m and v # which are not required for using pretrained model if any ( n in [ "adam_v" , "adam_m" , "global_step" ] for n in name ) : logger . info ( "Skipping {}" . format ( "/" . join ( name ) ) ) continue pointer = model for m_name in name : if re . fullmatch ( r'[A-Za-z]+_\d+' , m_name ) : l = re . split ( r'_(\d+)' , m_name ) else : l = [ m_name ] if l [ 0 ] == 'kernel' or l [ 0 ] == 'gamma' : pointer = getattr ( pointer , 'weight' ) elif l [ 0 ] == 'output_bias' or l [ 0 ] == 'beta' : pointer = getattr ( pointer , 'bias' ) elif l [ 0 ] == 'output_weights' : pointer = getattr ( pointer , 'weight' ) elif l [ 0 ] == 'squad' : pointer = getattr ( pointer , 'classifier' ) else : try : pointer = getattr ( pointer , l [ 0 ] ) except AttributeError : logger . info ( "Skipping {}" . format ( "/" . join ( name ) ) ) continue if len ( l ) >= 2 : num = int ( l [ 1 ] ) pointer = pointer [ num ] if m_name [ - 11 : ] == '_embeddings' : pointer = getattr ( pointer , 'weight' ) elif m_name [ - 13 : ] == '_embeddings_2' : pointer = getattr ( pointer , 'weight' ) array = np . transpose ( array ) elif m_name == 'kernel' : array = np . transpose ( array ) try : assert pointer . shape == array . shape except AssertionError as e : e . args += ( pointer . shape , array . shape ) raise logger . info ( "Initialize PyTorch weight {}" . format ( name ) ) pointer . data = torch . from_numpy ( array ) return model