This is the error I get on the first epoch when calling model.fit:
Exception has occurred: ValueError
Layer "functional" expects 2 input(s), but it received 1 input tensors. Inputs received: [<tf.Tensor 'data:0' shape=(None, 128) dtype=float32>]
File "D:workspaceMachine Learning 545PSU_classescs445_group_projectcodeKeras Music Genres Classificationencoder_decoder_feature_extractor.py", line 177, in train_encoder_decoder_model
model.fit(x = X_train,
File "D:workspaceMachine Learning 545PSU_classescs445_group_projectcodeKeras Music Genres Classificationencoder_decoder_feature_extractor.py", line 217, in <module>
trained_model = train_encoder_decoder_model(encoder_decoder_model, X_train, y_train, X_test, y_test)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
ValueError: Layer "functional" expects 2 input(s), but it received 1 input tensors. Inputs received: [<tf.Tensor 'data:0' shape=(None, 128) dtype=float32>]"
this is my model:
def define_encoder_decoder_model(num_features):
# Define the encoder
encoder_inputs = Input(shape=(None, num_features))
encoder_hidden1 = Dense(100, activation='relu')(encoder_inputs)
encoder_hidden2 = Dense(50, activation='relu')(encoder_hidden1)
encoder_lstm = LSTM(25, return_state=True)
encoder_outputs, state_h, state_c = encoder_lstm(encoder_hidden2)
encoder_states = [state_h, state_c]
# Define the decoder
decoder_inputs = Input(shape=(None, 25))
decoder_hidden1 = Dense(50, activation='relu')(decoder_inputs)
decoder_hidden2 = Dense(100, activation='relu')(decoder_hidden1)
decoder_lstm = LSTM(num_features, return_sequences=True, return_state=True)
decoder_outputs, _, _ = decoder_lstm(decoder_hidden2, initial_state=encoder_states)
decoder_dense = Dense(num_features, activation='softmax')
decoder_outputs = decoder_dense(decoder_outputs)
# Define the model that will turn encoder_inputs and decoder_inputs into decoder_outputs
model = Model([encoder_inputs, decoder_inputs], decoder_outputs)
# Compile the model
model.compile(optimizer='adam', loss='categorical_crossentropy',
metrics=['accuracy', 'precision', 'recall', 'f1_score'])
# Summary of the model
model.summary()
return modeltype here
this is how I’m calling model.fit:
def train_encoder_decoder_model(model, X_train, y_train, X_test, y_test):
"""
Trains an encoder-decoder model using the provided data.
Args:
model: The encoder-decoder model to train.
X_train: The input training data.
y_train: The target training data.
X_test: The input test data.
y_test: The target test data.
Returns:
The trained encoder-decoder model.
"""
print("shapes: X_train:", np.shape(X_train)," y_train: ", np.shape(y_train)," X_test: ", np.shape(X_test)," y_test: ", np.shape(y_test))
# Train the model
# append to a file the training log for each epoch
file_logger = FileLogger('training.log')
y_train_T = tf.convert_to_tensor(np.array([y_train]).T)
y_test_T = tf.convert_to_tensor(np.array([y_test]).T)
#x_train = tf.convert_to_tensor(X_train)
y_train = X_train
y_test = X_test
model.fit(x = X_train,
y= y_train,
batch_size=100,
epochs=100,
verbose=2,
validation_data=(X_test, y_test),
callbacks=[file_logger])
return model
This is an encoder-decoder model so the X_train dataset is equal to the y_train, same for X_test, y_test. In this first case the shape of the training set is (799,128) and the test data set is (299,128). The features are represented as “float64” values.
I’m running the code under visual studio code. I pre process the data into a normalized and scaled dataset and then divide it into training dataset and test datasets, build my encoder-decoder model (see method above), and then try to train the model. What I get is this output from the model.fit “Epoch 1/100” and the error message shown above.
What is this error and how to resolve it?
elbilo is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.