Issue
I am using tensorflow==2.9.1 an i am trying to use tf.keras.Model
? to build a model with partially numerical and partially categorical data.
Here a short example of what i would like to do:
import tensorflow==2.9.1
input_layer = tf.keras.layers.InputLayer(input_shape=(1,), dtype=tf.string)
string_lookup_layer = tf.keras.layers.StringLookup(vocabulary=["a", "b"], output_mode="one_hot")(input_layer)
This thorws the following traceback. Sadly if I just use the StringLookup
it requests an InputLayer.
ValueError Traceback (most recent call last)
Cell In [60], line 2
1 input_layer = tf.keras.layers.InputLayer(input_shape=(1,), dtype=tf.string)
----> 2 string_lookup_layer = tf.keras.layers.StringLookup(vocabulary=["a", "b"], output_mode="one_hot")(input_layer)
File ~\AppData\Local\pypoetry\Cache\virtualenvs\dqex-training-server-il8_fjrD-py3.10\lib\site-packages\keras\utils\traceback_utils.py:67, in filter_traceback.<locals>.error_handler(*args, **kwargs)
65 except Exception as e: # pylint: disable=broad-except
66 filtered_tb = _process_traceback_frames(e.__traceback__)
---> 67 raise e.with_traceback(filtered_tb) from None
68 finally:
69 del filtered_tb
File ~\AppData\Local\pypoetry\Cache\virtualenvs\dqex-training-server-il8_fjrD-py3.10\lib\site-packages\tensorflow\python\framework\constant_op.py:102, in convert_to_eager_tensor(value, ctx, dtype)
100 dtype = dtypes.as_dtype(dtype).as_datatype_enum
101 ctx.ensure_initialized()
--> 102 return ops.EagerTensor(value, ctx.device_name, dtype)
ValueError: Exception encountered when calling layer "string_lookup_25" (type StringLookup).
Attempt to convert a value (<keras.engine.input_layer.InputLayer object at 0x000001F46A29FFD0>) with an unsupported type (<class 'keras.engine.input_layer.InputLayer'>) to a Tensor.
I assume, I am missing something. How do I make this work?
Solution
Use tf.keras.layers.Input
instead of tf.keras.layers.InputLayer
and initialize your StringLookup
layer first:
import tensorflow as tf
inputs = tf.keras.layers.Input(shape=(1,), dtype=tf.string)
string_lookup_layer = tf.keras.layers.StringLookup(vocabulary=["a", "b"], output_mode="one_hot")
outputs = string_lookup_layer(inputs)
model = tf.keras.Model(inputs, outputs)
model(tf.constant(["a"]))
<tf.Tensor: shape=(1, 3), dtype=float32, numpy=array([[0., 1., 0.]], dtype=float32)>
Note that you have a 3 elements in the one-hot encoded vector, because of the predefined vocabulary and oov_token='[UNK]'
.
Answered By - AloneTogether
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.