Reputation: 342
I'm trying to use this module (https://tfhub.dev/google/wiki40b-lm-nl/1) loading it with KerasLayer
, but not sure why this error is raised.
import tensorflow as tf
import tensorflow_hub as hub
import tensorflow_text
hub_url = "https://tfhub.dev/google/wiki40b-lm-nl/1"
embed = hub.KerasLayer(hub_url, input_shape=[],
dtype=tf.string)
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-37-4e8ab0d5082c> in <module>()
5 hub_url = "https://tfhub.dev/google/wiki40b-lm-nl/1"
6 embed = hub.KerasLayer(hub_url, input_shape=[],
----> 7 dtype=tf.string)
1 frames
/usr/local/lib/python3.7/dist-packages/tensorflow_hub/keras_layer.py in _get_callable(self)
300 if self._signature not in self._func.signatures:
301 raise ValueError("Unknown signature %s in %s (available signatures: %s)."
--> 302 % (self._signature, self._handle, self._func.signatures))
303 f = self._func.signatures[self._signature]
304 if not callable(f):
ValueError: Unknown signature default in https://tfhub.dev/google/wiki40b-lm-nl/1 (available signatures: _SignatureMap({'neg_log_likelihood': <ConcreteFunction pruned(text) at 0x7F3044A93210>, 'tokenization': <ConcreteFunction pruned(text) at 0x7F3040B7D190>, 'token_neg_log_likelihood': <ConcreteFunction pruned(token) at 0x7F3040D14810>, 'word_embeddings': <ConcreteFunction pruned(text) at 0x7F303D3FF2D0>, 'activations': <ConcreteFunction pruned(text) at 0x7F303D3FFF50>, 'prediction': <ConcreteFunction pruned(mem_4, mem_5, mem_6, mem_7, mem_8, mem_9, mem_10, mem_11, input_tokens, mem_0, mem_1, mem_2, mem_3) at 0x7F303C189090>, 'detokenization': <ConcreteFunction pruned(token_ids) at 0x7F3039860790>, 'token_word_embeddings': <ConcreteFunction pruned(token) at 0x7F3038FC2110>, 'token_activations': <ConcreteFunction pruned(token) at 0x7F303BAF9150>})).
I tried to set the signature signature="word_embeddings", signature_outputs_as_dict=True
, but it turns out that the embeddings do not accept strings as input, only a tensor.
TypeError Traceback (most recent call last)
<ipython-input-36-e98cfe451175> in <module>()
----> 1 embed('ik')
5 frames
/usr/local/lib/python3.7/dist-packages/tensorflow/python/eager/function.py in _call_with_flat_signature(self, args, kwargs, cancellation_manager)
1733 raise TypeError("{}: expected argument #{}(zero-based) to be a Tensor; "
1734 "got {} ({})".format(self._flat_signature_summary(), i,
-> 1735 type(arg).__name__, str(arg)))
1736 return self._call_flat(args, self.captured_inputs, cancellation_manager)
1737
TypeError: pruned(text): expected argument #0(zero-based) to be a Tensor; got str (ik)
My question is, how to use this embedding with str
as input, as they point out in the module's page (section Inputs)?
Upvotes: 0
Views: 560
Reputation: 603
Passing the text wrapped in a tf.constant
to embed()
and setting the output_key
keyword should make it work:
import tensorflow as tf
import tensorflow_hub as hub
import tensorflow_text
embed = hub.KerasLayer("https://tfhub.dev/google/wiki40b-lm-nl/1",
signature="word_embeddings",
output_key="word_embeddings")
embed(tf.constant("\n_START_ARTICLE_\n1001 vrouwen uit de Nederlandse "
"geschiedenis\n_START_SECTION_\nSelectie van vrouwen"
"\n_START_PARAGRAPH_\nDe 'oudste' biografie in het boek "
"is gewijd aan de beschermheilige"))
(tested with TF 2.4.1 and tensorflow_hub 0.11.0)
Upvotes: 2