Build Custom Federated Averaging Process With Valueerror: Layer Sequential Expects 1 Inputs, But It Received 3 Input Tensors
Solution 1:
As the error message:
ValueError: Layer sequential expects 1 inputs, but it received 3 input tensors.
says, the Keras model is defined with only a single input (the first layer in the list):
model = tf.keras.models.Sequential([
tf.keras.layers.LSTM(2, input_shape=(1, 2), return_sequences=True),
tf.keras.layers.Dense(256, activation=tf.nn.relu),
tf.keras.layers.Activation(tf.nn.softmax),
])
Try inspecting model.input_spec
to see what objects the model is expecting to be fed as input.
>>> [InputSpec(shape=(None, None, 2), ndim=3)]
Where as the dataset defines and OrderedDict
of 3 tensors for input features:
features = ['time', 'meas_info', 'value']
LABEL_COLUMN = 'counter'
dataset = tf.data.Dataset.from_tensor_slices(
(collections.OrderedDict(df[features].to_dict('list')),
df[LABEL_COLUMN].to_list())
)
Try inspecting the value of dataset.element_spec
to see what objects the dataset will feed the model.
To make them compatible will require either changing the model definition, or the dataset. I'll assume the three features in the dataset are desired, in which case we want to tell Keras we have three features from the OrderedDict
. We'll need to use the Functional model API from Keras.
SEQUENCE_LENGTH = 5
input_dict = {f: tf.keras.layers.Input(shape=(SEQUENCE_LENGTH, 1), name=f) for f in features}
concatenated_inputs = tf.keras.layers.Concatenate()(input_dict.values())
lstm_output = tf.keras.layers.LSTM(2, input_shape=(1, 2), return_sequences=True)(concatenated_inputs)
logits = tf.keras.layers.Dense(256, activation=tf.nn.relu)(lstm_output)
predictions = tf.keras.layers.Activation(tf.nn.softmax)(logits)
model = tf.keras.models.Model(inputs=input_dict, outputs=predictions
Note that for the LSTM layer, I needed to provide and extra SEQUENCE_LENGTH
variable and dimension. The shape=(SEQUENCE_LENGTH, 1)
will need to be modified to fit the shape of the features coming out of the dataset.
To test if the model and dataset and compatible quickly (without all the other machinery), ensure that the following doesn't raise an error:
model(next(iter(dataset))[0])
Post a Comment for "Build Custom Federated Averaging Process With Valueerror: Layer Sequential Expects 1 Inputs, But It Received 3 Input Tensors"