class
MultiModalBertModel
[source]
MultiModalBertModel
(*args
, **kwargs
) ::Model
Model
groups layers into an object with training and inference features.
Arguments:
inputs: The input(s) of the model: a keras.Input
object or list of
keras.Input
objects.
outputs: The output(s) of the model. See Functional API example below.
name: String, the name of the model.
There are two ways to instantiate a Model
:
1 - With the "Functional API", where you start from Input
,
you chain layer calls to specify the model's forward pass,
and finally you create your model from inputs and outputs:
import tensorflow as tf
inputs = tf.keras.Input(shape=(3,))
x = tf.keras.layers.Dense(4, activation=tf.nn.relu)(inputs)
outputs = tf.keras.layers.Dense(5, activation=tf.nn.softmax)(x)
model = tf.keras.Model(inputs=inputs, outputs=outputs)
2 - By subclassing the Model
class: in that case, you should define your
layers in __init__
and you should implement the model's forward pass
in call
.
import tensorflow as tf
class MyModel(tf.keras.Model):
def __init__(self):
super(MyModel, self).__init__()
self.dense1 = tf.keras.layers.Dense(4, activation=tf.nn.relu)
self.dense2 = tf.keras.layers.Dense(5, activation=tf.nn.softmax)
def call(self, inputs):
x = self.dense1(inputs)
return self.dense2(x)
model = MyModel()
If you subclass Model
, you can optionally have
a training
argument (boolean) in call
, which you can use to specify
a different behavior in training and inference:
import tensorflow as tf
class MyModel(tf.keras.Model):
def __init__(self):
super(MyModel, self).__init__()
self.dense1 = tf.keras.layers.Dense(4, activation=tf.nn.relu)
self.dense2 = tf.keras.layers.Dense(5, activation=tf.nn.softmax)
self.dropout = tf.keras.layers.Dropout(0.5)
def call(self, inputs, training=False):
x = self.dense1(inputs)
if training:
x = self.dropout(x, training=training)
return self.dense2(x)
model = MyModel()
Once the model is created, you can config the model with losses and metrics
with model.compile()
, train the model with model.fit()
, or use the model
to do prediction with model.predict()
.
MultiModalBertModel
is transformers model with multi-modal input support. One can use it as a normal keras model.
model = MultiModalBertModel(params=params)
_ = model(one_batch_data)
assert model.get_pooled_output().shape[-1] == 312
assert len(model.get_sequence_output().shape) == 3
404 Client Error: Not Found for url: https://huggingface.co/voidful/albert_chinese_tiny/resolve/main/tf_model.h5 Some weights of the PyTorch model were not used when initializing the TF 2.0 model TFAlbertModel: ['predictions.bias', 'predictions.dense.bias', 'predictions.LayerNorm.bias', 'predictions.LayerNorm.weight', 'predictions.decoder.weight', 'predictions.decoder.bias', 'predictions.dense.weight'] - This IS expected if you are initializing TFAlbertModel from a PyTorch model trained on another task or with another architecture (e.g. initializing a TFBertForSequenceClassification model from a BertForPreTraining model). - This IS NOT expected if you are initializing TFAlbertModel from a PyTorch model that you expect to be exactly identical (e.g. initializing a TFBertForSequenceClassification model from a BertForSequenceClassification model). All the weights of TFAlbertModel were initialized from the PyTorch model. If your task is similar to the task the model of the checkpoint was trained on, you can already use TFAlbertModel for predictions without further training.
INFO:tensorflow:Modal Type id mapping: { "class": 0, "image": 1, "text": 2 } WARNING:tensorflow:AutoGraph could not transform <bound method Socket.send of <zmq.sugar.socket.Socket object at 0x7f6d1e289980>> and will run it as-is. Please report this to the TensorFlow team. When filing the bug, set the verbosity to 10 (on Linux, `export AUTOGRAPH_VERBOSITY=10`) and attach the full output. Cause: module, class, method, function, traceback, frame, or code object was expected, got cython_function_or_method To silence this warning, decorate the function with @tf.autograph.experimental.do_not_convert WARNING: AutoGraph could not transform <bound method Socket.send of <zmq.sugar.socket.Socket object at 0x7f6d1e289980>> and will run it as-is. Please report this to the TensorFlow team. When filing the bug, set the verbosity to 10 (on Linux, `export AUTOGRAPH_VERBOSITY=10`) and attach the full output. Cause: module, class, method, function, traceback, frame, or code object was expected, got cython_function_or_method To silence this warning, decorate the function with @tf.autograph.experimental.do_not_convert
The parameters `output_attentions`, `output_hidden_states` and `use_cache` cannot be updated when calling a model.They have to be set to True/False in the config object (i.e.: `config=XConfig.from_pretrained('name', output_attentions=True)`). The parameter `return_dict` cannot be set in graph mode and will always be set to `True`.
from m3tl.run_bert_multitask import create_tensorspec_from_shape_type
from m3tl.utils import infer_shape_and_type_from_dict
spec_dict = create_tensorspec_from_shape_type(
infer_shape_and_type_from_dict(one_batch_data))
class ServingModule(tf.Module):
def __init__(self):
super(ServingModule, self).__init__()
self.model = model
# @tf.function(input_signature=[v for v in spec_dict.values()])
def serve(self, x):
return self.model.call(x)
# serving_module = ServingModule()
# _ = serving_module.serve(one_batch_data)
# signatures = dict(
# serving_default=serving_module.serve.get_concrete_function(one_batch_data)
# )
signatures = dict(
serving_default=model.call.get_concrete_function(one_batch_data)
)
# tf.saved_model.save(serving_module, os.path.join(params.ckpt_dir, 'serving'), signatures=signatures)
# model.save(os.path.join(params.ckpt_dir, 'serving'), signatures=model.call.get_concrete_function(spec_dict), save_traces=True)
model.save(os.path.join(params.ckpt_dir, 'serving'))
# model.save(params.ckpt_dir, signatures=model.call.get_concrete_function(one_batch_data))
The parameters `output_attentions`, `output_hidden_states` and `use_cache` cannot be updated when calling a model.They have to be set to True/False in the config object (i.e.: `config=XConfig.from_pretrained('name', output_attentions=True)`). The parameter `return_dict` cannot be set in graph mode and will always be set to `True`. The parameters `output_attentions`, `output_hidden_states` and `use_cache` cannot be updated when calling a model.They have to be set to True/False in the config object (i.e.: `config=XConfig.from_pretrained('name', output_attentions=True)`). The parameter `return_dict` cannot be set in graph mode and will always be set to `True`. The parameters `output_attentions`, `output_hidden_states` and `use_cache` cannot be updated when calling a model.They have to be set to True/False in the config object (i.e.: `config=XConfig.from_pretrained('name', output_attentions=True)`). The parameter `return_dict` cannot be set in graph mode and will always be set to `True`. The parameters `output_attentions`, `output_hidden_states` and `use_cache` cannot be updated when calling a model.They have to be set to True/False in the config object (i.e.: `config=XConfig.from_pretrained('name', output_attentions=True)`). The parameter `return_dict` cannot be set in graph mode and will always be set to `True`. The parameters `output_attentions`, `output_hidden_states` and `use_cache` cannot be updated when calling a model.They have to be set to True/False in the config object (i.e.: `config=XConfig.from_pretrained('name', output_attentions=True)`). The parameter `return_dict` cannot be set in graph mode and will always be set to `True`. The parameters `output_attentions`, `output_hidden_states` and `use_cache` cannot be updated when calling a model.They have to be set to True/False in the config object (i.e.: `config=XConfig.from_pretrained('name', output_attentions=True)`). The parameter `return_dict` cannot be set in graph mode and will always be set to `True`. The parameters `output_attentions`, `output_hidden_states` and `use_cache` cannot be updated when calling a model.They have to be set to True/False in the config object (i.e.: `config=XConfig.from_pretrained('name', output_attentions=True)`). The parameter `return_dict` cannot be set in graph mode and will always be set to `True`. The parameters `output_attentions`, `output_hidden_states` and `use_cache` cannot be updated when calling a model.They have to be set to True/False in the config object (i.e.: `config=XConfig.from_pretrained('name', output_attentions=True)`). The parameter `return_dict` cannot be set in graph mode and will always be set to `True`. The parameters `output_attentions`, `output_hidden_states` and `use_cache` cannot be updated when calling a model.They have to be set to True/False in the config object (i.e.: `config=XConfig.from_pretrained('name', output_attentions=True)`). The parameter `return_dict` cannot be set in graph mode and will always be set to `True`. The parameters `output_attentions`, `output_hidden_states` and `use_cache` cannot be updated when calling a model.They have to be set to True/False in the config object (i.e.: `config=XConfig.from_pretrained('name', output_attentions=True)`). The parameter `return_dict` cannot be set in graph mode and will always be set to `True`. The parameters `output_attentions`, `output_hidden_states` and `use_cache` cannot be updated when calling a model.They have to be set to True/False in the config object (i.e.: `config=XConfig.from_pretrained('name', output_attentions=True)`). The parameter `return_dict` cannot be set in graph mode and will always be set to `True`. The parameters `output_attentions`, `output_hidden_states` and `use_cache` cannot be updated when calling a model.They have to be set to True/False in the config object (i.e.: `config=XConfig.from_pretrained('name', output_attentions=True)`). The parameter `return_dict` cannot be set in graph mode and will always be set to `True`. The parameters `output_attentions`, `output_hidden_states` and `use_cache` cannot be updated when calling a model.They have to be set to True/False in the config object (i.e.: `config=XConfig.from_pretrained('name', output_attentions=True)`). The parameter `return_dict` cannot be set in graph mode and will always be set to `True`. The parameters `output_attentions`, `output_hidden_states` and `use_cache` cannot be updated when calling a model.They have to be set to True/False in the config object (i.e.: `config=XConfig.from_pretrained('name', output_attentions=True)`). The parameter `return_dict` cannot be set in graph mode and will always be set to `True`. The parameters `output_attentions`, `output_hidden_states` and `use_cache` cannot be updated when calling a model.They have to be set to True/False in the config object (i.e.: `config=XConfig.from_pretrained('name', output_attentions=True)`). The parameter `return_dict` cannot be set in graph mode and will always be set to `True`. The parameters `output_attentions`, `output_hidden_states` and `use_cache` cannot be updated when calling a model.They have to be set to True/False in the config object (i.e.: `config=XConfig.from_pretrained('name', output_attentions=True)`). The parameter `return_dict` cannot be set in graph mode and will always be set to `True`. The parameters `output_attentions`, `output_hidden_states` and `use_cache` cannot be updated when calling a model.They have to be set to True/False in the config object (i.e.: `config=XConfig.from_pretrained('name', output_attentions=True)`). The parameter `return_dict` cannot be set in graph mode and will always be set to `True`.
INFO:tensorflow:Unsupported signature for serialization: ((TensorSpec(shape=(None, 26), dtype=tf.int64, name='input_ids'), None, None, None, <tensorflow.python.framework.func_graph.UnknownArgument object at 0x7f6c126a4ed0>, False), {}). INFO:tensorflow:Unsupported signature for serialization: ((TensorSpec(shape=(None, 26), dtype=tf.int64, name='input_ids'), None, None, None, <tensorflow.python.framework.func_graph.UnknownArgument object at 0x7f6c12685d90>, True), {}). INFO:tensorflow:Unsupported signature for serialization: ((TensorSpec(shape=(None, 26), dtype=tf.int64, name='input_ids'), None, None, None, <tensorflow.python.framework.func_graph.UnknownArgument object at 0x7f6c126e3310>, True), {}). INFO:tensorflow:Unsupported signature for serialization: ((TensorSpec(shape=(None, 26), dtype=tf.int64, name='input_ids'), None, None, None, <tensorflow.python.framework.func_graph.UnknownArgument object at 0x7f6c126e3790>, False), {}). INFO:tensorflow:Unsupported signature for serialization: ((TensorSpec(shape=(None, 26), dtype=tf.int64, name='input_ids'), None, None, None, <tensorflow.python.framework.func_graph.UnknownArgument object at 0x7f6c126a4ed0>, False), {}). INFO:tensorflow:Unsupported signature for serialization: ((TensorSpec(shape=(None, 26), dtype=tf.int64, name='input_ids'), None, None, None, <tensorflow.python.framework.func_graph.UnknownArgument object at 0x7f6c12685d90>, True), {}). INFO:tensorflow:Unsupported signature for serialization: ((TensorSpec(shape=(None, 26), dtype=tf.int64, name='input_ids'), None, None, None, <tensorflow.python.framework.func_graph.UnknownArgument object at 0x7f6c126e3310>, True), {}). INFO:tensorflow:Unsupported signature for serialization: ((TensorSpec(shape=(None, 26), dtype=tf.int64, name='input_ids'), None, None, None, <tensorflow.python.framework.func_graph.UnknownArgument object at 0x7f6c126e3790>, False), {}). INFO:tensorflow:Unsupported signature for serialization: ((TensorSpec(shape=(None, 26), dtype=tf.int64, name='input_ids'), None, None, None, <tensorflow.python.framework.func_graph.UnknownArgument object at 0x7f6c126e3310>, True), {}). INFO:tensorflow:Unsupported signature for serialization: ((TensorSpec(shape=(None, 26), dtype=tf.int64, name='input_ids'), None, None, None, <tensorflow.python.framework.func_graph.UnknownArgument object at 0x7f6c126e3790>, False), {}).
WARNING:absl:Found untraced functions such as embeddings_layer_call_fn, embeddings_layer_call_and_return_conditional_losses, encoder_layer_call_fn, encoder_layer_call_and_return_conditional_losses, pooler_layer_call_fn while saving (showing 5 of 115). These functions will not be directly callable after loading.
INFO:tensorflow:Unsupported signature for serialization: ((TensorSpec(shape=(None, 26), dtype=tf.int64, name='input_ids'), None, None, None, <tensorflow.python.framework.func_graph.UnknownArgument object at 0x7f6c126a4ed0>, False), {}). INFO:tensorflow:Unsupported signature for serialization: ((TensorSpec(shape=(None, 26), dtype=tf.int64, name='input_ids'), None, None, None, <tensorflow.python.framework.func_graph.UnknownArgument object at 0x7f6c12685d90>, True), {}). INFO:tensorflow:Unsupported signature for serialization: ((TensorSpec(shape=(None, 26), dtype=tf.int64, name='input_ids'), None, None, None, <tensorflow.python.framework.func_graph.UnknownArgument object at 0x7f6c126e3310>, True), {}). INFO:tensorflow:Unsupported signature for serialization: ((TensorSpec(shape=(None, 26), dtype=tf.int64, name='input_ids'), None, None, None, <tensorflow.python.framework.func_graph.UnknownArgument object at 0x7f6c126e3790>, False), {}). INFO:tensorflow:Unsupported signature for serialization: ((TensorSpec(shape=(None, 26), dtype=tf.int64, name='input_ids'), None, None, None, <tensorflow.python.framework.func_graph.UnknownArgument object at 0x7f6c126a4ed0>, False), {}). INFO:tensorflow:Unsupported signature for serialization: ((TensorSpec(shape=(None, 26), dtype=tf.int64, name='input_ids'), None, None, None, <tensorflow.python.framework.func_graph.UnknownArgument object at 0x7f6c12685d90>, True), {}). INFO:tensorflow:Unsupported signature for serialization: ((TensorSpec(shape=(None, 26), dtype=tf.int64, name='input_ids'), None, None, None, <tensorflow.python.framework.func_graph.UnknownArgument object at 0x7f6c126e3310>, True), {}). INFO:tensorflow:Unsupported signature for serialization: ((TensorSpec(shape=(None, 26), dtype=tf.int64, name='input_ids'), None, None, None, <tensorflow.python.framework.func_graph.UnknownArgument object at 0x7f6c126e3790>, False), {}). INFO:tensorflow:Unsupported signature for serialization: ((TensorSpec(shape=(None, 26), dtype=tf.int64, name='input_ids'), None, None, None, <tensorflow.python.framework.func_graph.UnknownArgument object at 0x7f6c126e3310>, True), {}). INFO:tensorflow:Unsupported signature for serialization: ((TensorSpec(shape=(None, 26), dtype=tf.int64, name='input_ids'), None, None, None, <tensorflow.python.framework.func_graph.UnknownArgument object at 0x7f6c126e3790>, False), {}).
WARNING:absl:Found untraced functions such as embeddings_layer_call_fn, embeddings_layer_call_and_return_conditional_losses, encoder_layer_call_fn, encoder_layer_call_and_return_conditional_losses, pooler_layer_call_fn while saving (showing 5 of 115). These functions will not be directly callable after loading.
--------------------------------------------------------------------------- TypeError Traceback (most recent call last) <ipython-input-5-dfeb6144af78> in <module> 21 # tf.saved_model.save(serving_module, os.path.join(params.ckpt_dir, 'serving'), signatures=signatures) 22 # model.save(os.path.join(params.ckpt_dir, 'serving'), signatures=model.call.get_concrete_function(spec_dict), save_traces=True) ---> 23 model.save(os.path.join(params.ckpt_dir, 'serving')) 24 25 # model.save(params.ckpt_dir, signatures=model.call.get_concrete_function(one_batch_data)) /data/anaconda3/lib/python3.7/site-packages/tensorflow/python/keras/engine/training.py in save(self, filepath, overwrite, include_optimizer, save_format, signatures, options, save_traces) 2000 # pylint: enable=line-too-long 2001 save.save_model(self, filepath, overwrite, include_optimizer, save_format, -> 2002 signatures, options, save_traces) 2003 2004 def save_weights(self, /data/anaconda3/lib/python3.7/site-packages/tensorflow/python/keras/saving/save.py in save_model(model, filepath, overwrite, include_optimizer, save_format, signatures, options, save_traces) 155 else: 156 saved_model_save.save(model, filepath, overwrite, include_optimizer, --> 157 signatures, options, save_traces) 158 159 /data/anaconda3/lib/python3.7/site-packages/tensorflow/python/keras/saving/saved_model/save.py in save(model, filepath, overwrite, include_optimizer, signatures, options, save_traces) 87 with distribution_strategy_context._get_default_replica_context(): # pylint: disable=protected-access 88 with utils.keras_option_scope(save_traces): ---> 89 save_lib.save(model, filepath, signatures, options) 90 91 if not include_optimizer: /data/anaconda3/lib/python3.7/site-packages/tensorflow/python/saved_model/save.py in save(obj, export_dir, signatures, options) 1031 1032 _, exported_graph, object_saver, asset_info = _build_meta_graph( -> 1033 obj, signatures, options, meta_graph_def) 1034 saved_model.saved_model_schema_version = constants.SAVED_MODEL_SCHEMA_VERSION 1035 /data/anaconda3/lib/python3.7/site-packages/tensorflow/python/saved_model/save.py in _build_meta_graph(obj, signatures, options, meta_graph_def) 1196 1197 with save_context.save_context(options): -> 1198 return _build_meta_graph_impl(obj, signatures, options, meta_graph_def) /data/anaconda3/lib/python3.7/site-packages/tensorflow/python/saved_model/save.py in _build_meta_graph_impl(obj, signatures, options, meta_graph_def) 1161 1162 object_graph_proto = _serialize_object_graph(saveable_view, -> 1163 asset_info.asset_index) 1164 meta_graph_def.object_graph_def.CopyFrom(object_graph_proto) 1165 /data/anaconda3/lib/python3.7/site-packages/tensorflow/python/saved_model/save.py in _serialize_object_graph(saveable_view, asset_file_def_index) 753 for obj, obj_proto in zip(saveable_view.nodes, proto.nodes): 754 _write_object_proto(obj, obj_proto, asset_file_def_index, --> 755 saveable_view.function_name_map) 756 return proto 757 /data/anaconda3/lib/python3.7/site-packages/tensorflow/python/saved_model/save.py in _write_object_proto(obj, proto, asset_file_def_index, function_name_map) 798 version=versions_pb2.VersionDef( 799 producer=1, min_consumer=1, bad_consumers=[]), --> 800 metadata=obj._tracking_metadata) 801 # pylint:enable=protected-access 802 proto.user_object.CopyFrom(registered_type_proto) /data/anaconda3/lib/python3.7/site-packages/tensorflow/python/keras/engine/base_layer.py in _tracking_metadata(self) 3077 @property 3078 def _tracking_metadata(self): -> 3079 return self._trackable_saved_model_saver.tracking_metadata 3080 3081 def _list_extra_dependencies_for_serialization(self, serialization_cache): /data/anaconda3/lib/python3.7/site-packages/tensorflow/python/keras/saving/saved_model/base_serialization.py in tracking_metadata(self) 53 # TODO(kathywu): check that serialized JSON can be loaded (e.g., if an 54 # object is in the python property) ---> 55 return json_utils.Encoder().encode(self.python_properties) 56 57 def list_extra_dependencies_for_serialization(self, serialization_cache): /data/anaconda3/lib/python3.7/site-packages/tensorflow/python/keras/saving/saved_model/json_utils.py in encode(self, obj) 51 52 def encode(self, obj): ---> 53 return super(Encoder, self).encode(_encode_tuple(obj)) 54 55 /data/anaconda3/lib/python3.7/json/encoder.py in encode(self, o) 197 # exceptions aren't as detailed. The list call should be roughly 198 # equivalent to the PySequence_Fast that ''.join() would do. --> 199 chunks = self.iterencode(o, _one_shot=True) 200 if not isinstance(chunks, (list, tuple)): 201 chunks = list(chunks) /data/anaconda3/lib/python3.7/json/encoder.py in iterencode(self, o, _one_shot) 255 self.key_separator, self.item_separator, self.sort_keys, 256 self.skipkeys, _one_shot) --> 257 return _iterencode(o, 0) 258 259 def _make_iterencode(markers, _default, _encoder, _indent, _floatstr, /data/anaconda3/lib/python3.7/site-packages/tensorflow/python/keras/saving/saved_model/json_utils.py in default(self, obj) 48 items = obj.as_list() if obj.rank is not None else None 49 return {'class_name': 'TensorShape', 'items': items} ---> 50 return serialization.get_json_type(obj) 51 52 def encode(self, obj): /data/anaconda3/lib/python3.7/site-packages/tensorflow/python/util/serialization.py in get_json_type(obj) 77 return obj.__wrapped__ 78 ---> 79 raise TypeError('Not JSON Serializable:', obj) TypeError: ('Not JSON Serializable:', <tf.Tensor: shape=(), dtype=int32, numpy=128>)