def serving_input_fn():
with tf.variable_scope("bert_model"):
feature_spec = {
"input_ids": tf.FixedLenFeature([MAX_SEQ_LENGTH], tf.int64),
"input_mask": tf.FixedLenFeature([MAX_SEQ_LENGTH], tf.int64),
"segment_ids": tf.FixedLenFeature([MAX_SEQ_LENGTH], tf.int64),
"label_ids": tf.FixedLenFeature([], tf.int64),
}
serialized_tf_example = tf.placeholder(dtype=tf.string,
shape=[None],
name='input_example_tensor')
receiver_tensors = {'examples': serialized_tf_example}
features = tf.parse_example(serialized_tf_example, feature_spec)
return tf.estimator.export.ServingInputReceiver(features, receiver_tensors)
MODEL_DIR = 'gs://{}/bert/models_servable/{}'.format(BUCKET,'bert')
tf.gfile.MakeDirs(MODEL_DIR)
estimator._export_to_tpu = False
model_file = os.path.join(MODEL_DIR, "bert_model")
path = estimator.export_savedmodel(model_file, serving_input_fn)
print(path)
并显示以下错误:
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-106-aaf5ee490ed7> in <module>()
21 model_file = os.path.join(MODEL_DIR, "bert_model")
22 print(model_file)
---> 23 path = estimator.export_savedmodel(model_file, serving_input_fn)
24 print(path)
/usr/local/lib/python3.6/dist-packages/tensorflow_estimator/python/estimator/estimator.py in export_savedmodel(self, export_dir_base, serving_input_receiver_fn, assets_extra, as_text, checkpoint_path, strip_default_attrs)
1643 as_text=as_text,
1644 checkpoint_path=checkpoint_path,
-> 1645 experimental_mode=model_fn_lib.ModeKeys.PREDICT)
1646
1647 def export_saved_model(
/usr/local/lib/python3.6/dist-packages/tensorflow_estimator/python/estimator/estimator.py in export_saved_model(self, export_dir_base, serving_input_receiver_fn, assets_extra, as_text, checkpoint_path, experimental_mode)
721 assets_extra=assets_extra,
722 as_text=as_text,
--> 723 checkpoint_path=checkpoint_path)
724
725 def experimental_export_all_saved_models(
/usr/local/lib/python3.6/dist-packages/tensorflow_estimator/python/estimator/estimator.py in experimental_export_all_saved_models(self, export_dir_base, input_receiver_fn_map, assets_extra, as_text, checkpoint_path)
825 self._add_meta_graph_for_mode(
826 builder, input_receiver_fn_map, checkpoint_path,
--> 827 save_variables, mode=model_fn_lib.ModeKeys.PREDICT)
828 save_variables = False
829
/usr/local/lib/python3.6/dist-packages/tensorflow_estimator/python/estimator/estimator.py in _add_meta_graph_for_mode(self, builder, input_receiver_fn_map, checkpoint_path, save_variables, mode, export_tags, check_variables)
895 labels=getattr(input_receiver, 'labels', None),
896 mode=mode,
--> 897 config=self.config)
898
899 export_outputs = model_fn_lib.export_outputs_for_mode(
/usr/local/lib/python3.6/dist-packages/tensorflow_estimator/python/estimator/estimator.py in _call_model_fn(self, features, labels, mode, config)
1110
1111 logging.info('Calling model_fn.')
-> 1112 model_fn_results = self._model_fn(features=features, **kwargs)
1113 logging.info('Done calling model_fn.')
1114
<ipython-input-90-119a3167bf33> in model_fn(features, labels, mode, params)
72 else:
73 (predicted_labels, log_probs) = create_model(
---> 74 is_predicting, input_ids, input_mask, segment_ids, label_ids, num_labels)
75
76 predictions = {
<ipython-input-89-0f7bd7d1be35> in create_model(is_predicting, input_ids, input_mask, segment_ids, labels, num_labels)
13 inputs=bert_inputs,
14 signature="tokens",
---> 15 as_dict=True)
16
17 # Use "pooled_output" for classification tasks on an entire sentence.
/usr/local/lib/python3.6/dist-packages/tensorflow_hub/module.py in __call__(self, inputs, _sentinel, signature, as_dict)
240 dict_inputs = _convert_dict_inputs(
241 inputs, self._spec.get_input_info_dict(signature=signature,
--> 242 tags=self._tags))
243
244 dict_outputs = self._impl.create_apply_graph(
/usr/local/lib/python3.6/dist-packages/tensorflow_hub/module.py in _convert_dict_inputs(inputs, tensor_info_map)
442 dict_inputs = _prepare_dict_inputs(inputs, tensor_info_map)
443 return tensor_info.convert_dict_to_compatible_tensor(dict_inputs,
--> 444 tensor_info_map)
445
446
/usr/local/lib/python3.6/dist-packages/tensorflow_hub/tensor_info.py in convert_dict_to_compatible_tensor(values, targets)
146 for key, value in sorted(values.items()):
147 result[key] = _convert_to_compatible_tensor(
--> 148 value, targets[key], error_prefix="Can't convert %r" % key)
149 return result
150
/usr/local/lib/python3.6/dist-packages/tensorflow_hub/tensor_info.py in _convert_to_compatible_tensor(value, target, error_prefix)
115 """
116 try:
--> 117 tensor = tf.convert_to_tensor_or_indexed_slices(value, target.dtype)
118 except TypeError as e:
119 raise TypeError("%s: %s" % (error_prefix, e))
/usr/local/lib/python3.6/dist-packages/tensorflow/python/framework/ops.py in convert_to_tensor_or_indexed_slices(value, dtype, name)
1296 """
1297 return internal_convert_to_tensor_or_indexed_slices(
-> 1298 value=value, dtype=dtype, name=name, as_ref=False)
1299
1300
/usr/local/lib/python3.6/dist-packages/tensorflow/python/framework/ops.py in internal_convert_to_tensor_or_indexed_slices(value, dtype, name, as_ref)
1330 raise ValueError(
1331 "Tensor conversion requested dtype %s for Tensor with dtype %s: %r" %
-> 1332 (dtypes.as_dtype(dtype).name, value.dtype.name, str(value)))
1333 return value
1334 else:
ValueError: Tensor conversion requested dtype int32 for Tensor with dtype string: 'Tensor("bert_model/ParseExample/ParseExample:0", shape=(?, 128), dtype=string)'
int32强制用于一种或所有功能:input_id,input_mask,segment_id和label_id。
模型代码仅具有int32转换,这似乎不是导致此转换的原因
one_hot_labels = tf.one_hot(labels, depth=num_labels, dtype=tf.float32)
predicted_labels = tf.squeeze(tf.argmax(log_probs, axis=-1, output_type=tf.int32))
int32强制在哪里发生以及如何解决?预先感谢!
答案 0 :(得分:0)
伯特模型需要input_ids,input_mask和segment_ids作为tf.int32的类型。为了修复该错误,您必须按如下所示将它们从tf.int64转换为tf.int32
def create_model(is_predicting, input_ids, input_mask, segment_ids, labels, num_labels):
"""Creates a classification model."""
input_ids = tf.cast(input_ids, tf.int32)
input_mask = tf.cast(input_mask, tf.int32)
segment_ids = tf.cast(segment_ids, tf.int32)
bert_module = hub.Module(
BERT_MODEL_HUB,
trainable=True)