I’m trying to train a Keras model where the input is a matrix, and I want the output to be a single value. However, I’m encountering an issue with the code where it throws an error related to the number of samples in the input and output arrays.
Here’s a simplified version of my code:
import numpy as np
from keras.models import Sequential
from keras.layers import Dense
# Input (matrix)
X = np.array([[1, 2, 3, 4],
[2, 3, 4, 5],
[3, 4, 5, 6],
[4, 5, 6, 7],
[5, 6, 7, 8]])
# Output (single value)
Y = np.array([-1])
# Creating model
model = Sequential()
model.add(Dense(10, input_dim=4, activation='relu'))
model.add(Dense(1, activation='linear'))
# Compiling
model.compile(loss='mean_squared_error', optimizer='adam')
# Training
model.fit(X, Y, epochs=10, batch_size=1, verbose=1)
# Predictions
new_data = np.array([[6, 7, 8, 9]])
pred = model.predict(new_data)
print("Prediction:", pred)
I’m receiving the following error message: "x sizes: 5, y sizes: 1. Make sure all arrays contain the same number of samples." I understand that the issue is related to the number of samples in the input and output arrays.
My goal is to convert the matrix input into a single value as the output. How can I modify the code to achieve this?
Any help or guidance would be greatly appreciated. Thank you!
>Solution :
like this?
import numpy as np
from keras.models import Sequential
from keras.layers import Dense
# Input (matrix)
X = np.array([[[1, 2, 3, 4],
[2, 3, 4, 5],
[3, 4, 5, 6],
[4, 5, 6, 7],
[5, 6, 7, 8]],
[[4, 7, 3, 4],
[2, 4, 5, 2],
[6, 1, 3, 9],
[1, 2, 5, 3],
[9, 4, 2, 1]]])
# Output (single value)
Y = np.array([1, 3]) # Adjust the output shape to (num_samples,)
# Creating model
model = Sequential()
model.add(Dense(10, input_shape=(5, 4), activation='relu')) # Adjust input_shape
model.add(Dense(1, activation='linear'))
# Compiling
model.compile(loss='mean_squared_error', optimizer='adam')
# Training
model.fit(X, Y, epochs=10, batch_size=1, verbose=1)
# Predictions
new_data = np.array([[[6, 7, 8, 9],
[9, 8, 7, 6],
[5, 4, 3, 2],
[2, 3, 4, 5],
[1, 1, 1, 1]]])
pred = model.predict(new_data)
print("Prediction:", pred)
X is a 3-dimensional array with shape (num_samples, 5, 4), and Y is a 1-dimensional array with shape (num_samples,).
output is:
Prediction: [[[0.85670656]
[1.1437894 ]
[0.555161 ]
[0.3494279 ]
[0.20106974]]]