Comments (6)
#7 Sentence Embedding
GlobalMaxPool1D
doesn't support masking. Following is a modification that suits this case:
keras-bert/keras_bert/layers/pooling.py
Lines 5 to 21 in b7ecdc3
I've added a demo for sentence embedding with pooling:
keras-bert/demo/load_model/load_and_pool.py
Lines 20 to 39 in 02c7eb2
from keras-bert.
I forgot to return a None
mask in MaskedGlobalMaxPool1D
. I've fixed it and made a release.
from keras-bert.
It looks that the author provided a demo:
inputs, output_layer = get_model( #
output_layeris the last feature extraction layer (the last transformer) ... training=False, # The input layers and output layer will be returned if
trainingis
False)
Then Any classifier can be added on top of this `output_layer' (which is embeddings), such as LSTM or Logistic Regression.
Make sure "training=False"
You may have to rewrite the token dictionary since your dataset may not be exactly like MRPC.
from keras-bert.
@CyberZHG Oh sweet! Will check this out. Going through the paper for BERT cleared my masking queries.
I meanwhile got the job done with the official tensorflow-hub module.
from keras-bert.
Thanks. The MaskedGlobalMaxPool1D
itself works well in the demo you gave, but it looks it does not fit if we add a classification layer (such as Dense
) on top of it:
This code gives error:
model = load_trained_model_from_checkpoint('bert_config.json', 'bert_model.ckpt')
def get_custermized_model(model):
pool_layer = MaskedGlobalMaxPool1D(name='Pooling')(model.output)
x = pool_layer
x = Dense(units=1, activation='sigmoid')(x)
print(model.inputs[0])
custermized_model = Model(inputs=model.inputs, outputs=x)
custermized_model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['acc'])
return custermized_model
custermized_model = get_custermized_model(model=model)
history = custermized_model.fit(x=X_train, y=train_labels, epochs=1, validation_split=0.3)
InvalidArgumentError: Incompatible shapes: [32] vs. [32,512]
[[{{node metrics_4/acc/mul}} = Mul[T=DT_FLOAT, _device="/job:localhost/replica:0/task:0/device:GPU:0"](metrics_4/acc/Mean, metrics_4/acc/Cast_1)]]
[[{{node metrics_4/acc/Mean_2/_1745}} = _Recvclient_terminated=false, recv_device="/job:localhost/replica:0/task:0/device:CPU:0", send_device="/job:localhost/replica:0/task:0/device:GPU:0", send_device_incarnation=1, tensor_name="edge_4979_metrics_4/acc/Mean_2", tensor_type=DT_FLOAT, _device="/job:localhost/replica:0/task:0/device:CPU:0"]]
Would you help
- add a
Dense
layer on top of "MaskedGlobalMaxPool1D
" and try how it works ? Thanks
PS:
I noticed that the token_input in your demo has shape (1, 512) , and the above error happens when I feed a batch of data with shape (64, 512) where 64 is batch size.
However, if I add dimension and feed (64, 1, 512) to the model, it complains of input shape errors.
from keras-bert.
@CyberZHG
Hi cyberzhg
I notice in the code above
`
(1) model = load_trained_model_from_checkpoint(config_path, checkpoint_path)
(2) model = load_trained_model_from_checkpoint(config_path, checkpoint_path, training=True, seq_len=seq_len)
`
if I build an classifier with bilstm on top of that ,
which means finetune?
Thanks
weiizhen
from keras-bert.
Related Issues (20)
- keras-bert load pre model failed in keras 2.2.4, maybe it is a bug
- could you provide an albert loader ? HOT 1
- 如何实现在自己的数据上很少的预训练? HOT 1
- 如何提取长度一致的bert词向量? HOT 1
- Input sentence for extract_embeddings HOT 1
- List index out of range for example code HOT 2
- 在哪里可以找到之前的版本呢? HOT 2
- Keras Bert with character embeddings HOT 1
- The method get_model to get the bert model is right? HOT 4
- API reference list
- which version of keras-bert is compatible for tensorflow 1.13? HOT 2
- 关于显存巨量占用的疑惑 HOT 1
- Improve Tokenizer for uppercase text HOT 1
- Error in getting attention map HOT 3
- Masked LM pre-training HOT 1
- AttributeError: module 'keras' has no attribute 'applications' HOT 5
- module 'tensorflow.compat.v2' has no attribute '__internal__' HOT 1
- load_trained_model_from_checkpoint(config_path,checkpoint_file=checkpoint_path) AttributeError: 'tuple' object has no attribute 'layer'
- load_trained_model_from_checkpoint(config_path,checkpoint_file=checkpoint_path)
- 'adamwarmup' object has no attribute '_set_hyper'
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from keras-bert.