Git Product home page Git Product logo

Comments (3)

kpe avatar kpe commented on August 12, 2024 1

ohh... I see, you are trying to replace/extend the default embeddings layer, cool!

I believe, because the l_bert instance is not part of the graph, actually because, the weights get instantiated here (out of any context/scope):

output = bert_layer.encoders_layer(new_emb)  

the prefix/name_scope is missing. As a workaround, you could put the relevant peaces (or everything) in a name_scope like this:

from tensorflow.python.keras import backend as K

# https://github.com/tensorflow/tensorflow/issues/27298
with K.get_graph().as_default(), K.name_scope('bert'):
  emb_mask = bert_layer.embeddings_layer(mask_ids)  # shape(1, seq_len, emb_size)
  output = bert_layer.encoders_layer(new_emb)

as a minimal example:

import os
import bert

from tensorflow import keras
from tensorflow.python.keras import backend as K

model_name = "chinese_L-12_H-768_A-12"
model_dir = bert.fetch_google_bert_model(model_name, ".models")
model_ckpt = os.path.join(model_dir, "bert_model.ckpt")

bert_params = bert.params_from_pretrained_ckpt(model_dir)

# https://github.com/tensorflow/tensorflow/issues/27298
with K.get_graph().as_default(), K.name_scope('bert'):
    l_bert = bert.BertModelLayer.from_params(bert_params, name="bert")
    inp_ids = keras.layers.Input(shape=(128,), dtype='int32')
    new_emb = l_bert.embeddings_layer(inp_ids)
    output = l_bert.encoders_layer(new_emb)
    output = keras.layers.Dense(3, activation='softmax')(output + new_emb)
    model = keras.models.Model(inp_ids, output, name='bert')

bert.load_bert_weights(l_bert, model_ckpt)
model.summary()

as an alternative consider extending BertModelLayer, and overriding the relevant methods (i.e. call(),build(),...).

from bert-for-tf2.

kpe avatar kpe commented on August 12, 2024

I'm not able to reproduce, can you try posting a minimal but complete executable example, i.e. something like:

import os
import bert

from tensorflow import keras

model_name = "chinese_L-12_H-768_A-12"
model_dir = bert.fetch_google_bert_model(model_name, ".models")
model_ckpt = os.path.join(model_dir, "bert_model.ckpt")

bert_params = bert.params_from_pretrained_ckpt(model_dir)
l_bert = bert.BertModelLayer.from_params(bert_params, name="bert")

# use in Keras Model here, and call model.build()
model = keras.models.Sequential([
    keras.layers.InputLayer(input_shape=(128,)),
    l_bert,
    keras.layers.Lambda(lambda x: x[:, 0, :]),
    keras.layers.Dense(2)
])
model.build(input_shape=(None, 128))

bert.load_bert_weights(l_bert, model_ckpt)
model.summary()

from bert-for-tf2.

yangxudong avatar yangxudong commented on August 12, 2024

the prefix/name_scope is missing

Thanks for your reply. And yes, it is because of missing the prefix/name_scope. Your example is feasible. Cool!

from bert-for-tf2.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.