Home > other >  Error when fit tf.keras.layers.Input into tf.strings.split
Error when fit tf.keras.layers.Input into tf.strings.split

Time:05-06

I am new the tf, and the question maybe quite naive. I am trying to understand why the Test2 throughs error.

# Test 1
x = tf.constant([["aa,bbb,cc"], ["dd,,"]])
tf.strings.split(x, sep=',')
=> <tf.RaggedTensor [[[b'aa', b'bbb', b'cc']], [[b'dd', b'', b'']]]>

# Test2
x = tf.keras.layers.Input(shape=(1,), dtype=tf.string)
tf.strings.split(x, sep=",")

...
ValueError: Tensor conversion requested dtype int32 for Tensor with dtype int64: <tf.Tensor 'Cumsum:0' shape=(None,) dtype=int64>

CodePudding user response:

IIUC, this should work as a simple model:

import tensorflow as tf

inputs = tf.keras.layers.Input(shape=(1,), dtype=tf.string)
outputs = tf.strings.split(inputs, sep=",")
# ... your additional layers
model = tf.keras.Model(inputs, outputs)

x = tf.constant([["aa,bbb,cc"], ["dd,,"]])
print(model(x))
<tf.RaggedTensor [[[b'aa', b'bbb', b'cc']],

 [[b'dd', b'', b'']]]>

I am assuming your error is actually coming from a type mismatch after converting the strings to integers using a StringLookup or TextVectorization layer, because this runs:

x = tf.keras.layers.Input(shape=(1,), dtype=tf.string)
tf.strings.split(x, sep=",")
# <KerasTensor: type_spec=RaggedTensorSpec(TensorShape([1, 1, None]), tf.string, 2, tf.int64) (created by layer 'tf.strings.split_2')>

Here is an example with a TextVectorization layer:

import tensorflow as tf

x = tf.constant([["aa,bbb,cc"], ["dd,,"]])

vectorizer_layer = tf.keras.layers.TextVectorization(standardize=None)
vectorizer_layer.adapt(['aa' 'bbb', 'cc', 'dd', ''])
embedding_layer = tf.keras.layers.Embedding(10, 5)

inputs = tf.keras.layers.Input(shape=(1,), dtype=tf.string)
l = tf.keras.layers.Lambda(lambda x: tf.squeeze(tf.strings.split(x, sep=","), axis=1).to_tensor(), name='split')(inputs)
outputs = vectorizer_layer(tf.reshape(l, (tf.reduce_prod(tf.shape(l)), 1)))
outputs = tf.reshape(outputs, tf.shape(l))
outputs = embedding_layer(outputs)
model = tf.keras.Model(inputs, outputs)
print(model(x))
tf.Tensor(
[[[ 0.01325693  0.04536256 -0.04319841 -0.01714526 -0.03821129]
  [ 0.01325693  0.04536256 -0.04319841 -0.01714526 -0.03821129]
  [ 0.04633341 -0.02162067  0.04285793 -0.03961723 -0.04530612]]

 [[ 0.04202544 -0.04769113  0.00436096 -0.04809079 -0.0097675 ]
  [-0.00061743 -0.03051994  0.02737813 -0.04842547  0.03567551]
  [-0.00061743 -0.03051994  0.02737813 -0.04842547  0.03567551]]], shape=(2, 3, 5), dtype=float32)
  • Related