Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to load vocfile for Albert #17

Closed
gargomeiste opened this issue Oct 30, 2019 · 1 comment
Closed

How to load vocfile for Albert #17

gargomeiste opened this issue Oct 30, 2019 · 1 comment

Comments

@gargomeiste
Copy link

Hello,

I tried to implement your library in order to fine tune Albert:

def create_model(max_seq_len):
    """Creates a classification model."""
    albert_model_name = "albert_base"
    albert_dir = bert.fetch_tfhub_albert_model(albert_model_name, ".models")

    albert_params = bert.albert_params(albert_model_name)
    l_bert = bert.BertModelLayer.from_params(albert_params, name="albert")
        
    input_ids      = keras.layers.Input(shape=(max_seq_len,), dtype='int32', name="input_ids")
    token_type_ids = keras.layers.Input(shape=(max_seq_len,), dtype='int32', name="token_type_ids")
    output         = l_bert([input_ids, token_type_ids])

    print("bert shape", output.shape)
    output = keras.layers.Lambda(lambda x: x[:, 0, :])(output)
    output = keras.layers.Dense(1, activation="sigmoid")(output)

    model = keras.Model(inputs=[input_ids, token_type_ids], outputs=output)
    model.build(input_shape=[(None, max_seq_len)])


    for weight in l_bert.weights:
        print(weight.name)


    model.compile(optimizer=keras.optimizers.Adam(),
        loss="binary_crossentropy",
        metrics=["accuracy", Precision(), Recall()])

    bert.load_albert_weights(l_bert, albert_dir)
    
    model.summary()
        
    return model

When training, I have an index issue regarding the embeddings (my assumption is that the vocab / vocab_size is different between bert and albert). I tried to import it from tf_hub but wasn't able to find it. Am I missing something?

Thank you in advance! You are doing an amazing work!

@kpe
Copy link
Owner

kpe commented Oct 30, 2019

@antoinegargot - ALBERT is using sentencepiece for tokenization, so you might need something like:

!pip install sentencepiece
import sentencepiece as spm

sp_model = tf.io.gfile.glob(os.path.join(albert_dir, "assets/*"))[0]
sp = spm.SentencePieceProcessor()
sp.load(sp_model)

tokens = sp.encode_as_pieces("Hello, World!")
token_ids = list(map(sp.piece_to_id, tokens))
# or
token_ids = sp.encode_as_ids("Hello, World!")


@kpe kpe closed this as completed Oct 30, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants