Unable to serialize/save TF2.0 Bert model
See original GitHub issueEnvironment info
transformers
version: 3.1.0- Platform: colab
- Python version: 3.7.0
- Tensorflow version (GPU?): 2.3.0
- Using GPU in script?: yes
Who can help
Information
Model I am using (Bert):
The problem arises when using:
- the official example scripts: (give details below)
- my own modified scripts: (give details below)
The tasks I am working on is:
- an official GLUE/SQUaD task: (give the name)
- my own task or dataset: (give details below)
To reproduce
# define model via functional api
def build_model(item_dim, num_layers, num_heads, max_len):
config = BertConfig(hidden_size=item_dim, num_hidden_layers=num_layers,
num_attention_heads=num_heads, intermediate_size=item_dim*4,
max_position_embeddings=max_len)
bert = TFBertMainLayer(config=config)
inputs_embeds = Input(shape=(max_len, item_dim), dtype=tf.float32, name='inputs')
inputs = {"inputs_embeds": inputs_embeds}
# pre-training vectors to bert
seq_emb = bert(inputs)[0]
last_token_emb = seq_emb[:, -1, :]
outputs = Dense(1, activation='sigmoid')(last_token_emb)
model = Model(inputs=inputs, outputs=outputs)
return model
model = build_model(item_dim, num_layers, num_heads, max_len)
model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['acc'])
model.fit(X, y,
epochs=2, batch_size=128,
verbose=2
)
model.save('./my_model')
Errors with
TypeError: ('Not JSON Serializable:', BertConfig { "attention_probs_dropout_prob": 0.1, "gradient_checkpointing": false, "hidden_act": "gelu", "hidden_dropout_prob": 0.1, "hidden_size": 32, "initializer_range": 0.02, "intermediate_size": 128, "layer_norm_eps": 1e-12, "max_position_embeddings": 9, "model_type": "bert", "num_attention_heads": 1, "num_hidden_layers": 1, "pad_token_id": 0, "type_vocab_size": 2, "vocab_size": 30522 } )
Expected behavior
- Successfully saved keras model.
Issue Analytics
- State:
- Created 3 years ago
- Comments:6 (3 by maintainers)
Top Results From Across the Web
Using EfficientNetB0 and save model will result `Unable to ...
Hello, I'm trying to use EfficientNetB0 to create a model and save the model to my local disk. However, when saving it, it...
Read more >Can't load TF transformer model with keras ... - Stack Overflow
Use model.save() or tf.keras.models.save_model() to save Keras model and to load model using tf.keras.models.
Read more >Find Answers to AWS Questions about AWS Inferentia
Difference in pytorch BERT model output vs neuron ... 20764:22801 ERROR TDRV:copy_and_stage_mr_one_channel Failed to allocate aligned (0) buffer in MLA DRAM ...
Read more >Serialization and saving - Keras
Introduction. A Keras model consists of multiple components: The architecture, or configuration, which specifies what layers the model contain, ...
Read more >Save, Load and Inference From TensorFlow Frozen Graph
TensorFlow model saving has become easier than it was in the ... platform-neutral extensible mechanism for serializing structured data.
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
I can confirm the bug. As a workaround you can use
model.save_weights
instead. I cannot give you a specific date for the fix, but I will update the thread.ok, thanks!