I'm getting the following error when I try to pass multiple ragged tensors to my model:
ValueError: Layer "sequential_3" expects 1 input(s), but it received 2 input tensors
I suspect it has something to do with the "shape" argument in the Input layer of the model.
Yes, I've reviewed the ragged tensor documentation.
Yes, I've spent many hours scouring the Google-net.
Yes, I've browsed stackoverflow.com related articles.
Yes, I'm most likely noobing it up right now and there is a simple solution.
Below is a reproducible example (tensorflow 2.7; keras 2.7; python 3.7)
dependencies
import tensorflow as tf
import pandas as pd
import keras
import keras.layers
Dummy Dataset
d0 = pd.DataFrame(data={
"id":[
1,
2, 2,
3, 3, 3,
4, 4, 4, 4,
5, 5, 5, 5, 5,
6, 6, 6, 6, 6, 6
],
"date":[
pd.to_datetime("2008-03-31"),
pd.to_datetime("2008-03-31"), pd.to_datetime("2008-06-30"),
pd.to_datetime("2008-03-31"), pd.to_datetime("2008-06-30"), pd.to_datetime("2008-09-30"),
pd.to_datetime("2008-03-31"), pd.to_datetime("2008-06-30"), pd.to_datetime("2008-09-30"), pd.to_datetime("2008-12-31"),
pd.to_datetime("2008-03-31"), pd.to_datetime("2008-06-30"), pd.to_datetime("2008-09-30"), pd.to_datetime("2008-12-31"), pd.to_datetime("2009-03-31"),
pd.to_datetime("2008-03-31"), pd.to_datetime("2008-06-30"), pd.to_datetime("2008-09-30"), pd.to_datetime("2008-12-31"), pd.to_datetime("2009-03-31"), pd.to_datetime("2009-06-30")
],
"date2":[
1,
1, 2,
1, 2, 3,
1, 2, 3, 4,
1, 2, 3, 4, 5,
1, 2, 3, 4, 5, 6
],
"input":[
10,
11, 12,
13, 14, 15,
16, 17, 18, 19,
20, 21, 22, 23, 24,
25, 26, 27, 28, 29, 30
],
"target":[
60,
61, 62,
63, 64, 65,
66, 67, 68, 69,
70, 71, 72, 73, 74,
75, 76, 77, 78, 79, 80
],
})
inputs
inputs = []
inputs.append(tf.ragged.constant(d0.groupby("id")["input"].apply(list)))
inputs.append(tf.ragged.constant(d0.groupby("id")["date2"].apply(list)))
target
target = tf.ragged.constant(d0.groupby("id")["target"].apply(list))
model
mod1 = keras.Sequential([
keras.layers.Input(shape=(None, 4), dtype=tf.float32, batch_size=1, ragged=True),
keras.layers.LSTM(units=32, dtype=tf.float32, return_sequences=True, use_bias=False),
keras.layers.Dense(units=32),
keras.layers.Dense(units=1)
])
compile
mod1.compile(
optimizer="adam",
loss="mse",
metrics=["accuracy"]
)
fit
history = mod1.fit(
x=inputs,
y=target,
epochs=1
)
The error occurs during the fit step.