Issue
I am trying to feed a Sequential model in batches. To reproduce my example, suppose my data is:
X = np.random.rand(432,24,1)
Y = np.random.rand(432,24,1)
My goal is to feed the model in batches. 24 points at a time (24 x 1 vector), 432 times.
I built my model as:
X_train, X_test, y_train, y_test = train_test_split(X, Y, test_size=0.3, random_state=12)
model = keras.Sequential([
#keras.layers.Flatten(batch_input_shape=(None, 432, 2)),
keras.layers.Dense(64, activation=tf.nn.relu),
keras.layers.Dense(2, activation=tf.nn.sigmoid),
])
model.compile(optimizer='adam', loss='binary_crossentropy', metrics=['accuracy'])
history = model.fit(X_train, y_train, epochs=200, batch_size=32, validation_split=0.3)
test_loss, test_acc = model.evaluate(X_test, y_test)
print('Model loss:', test_loss, 'Model accuracy: ', test_acc)
However, I get this error:
ValueError: Input 0 of layer dense_25 is incompatible with the layer: expected axis -1 of input shape to have value 864 but received input with shape (None, 432)
Solution
I am not really too sure what you want to do, but here is a working example:
import tensorflow as tf
from sklearn.model_selection import train_test_split
X = np.random.rand(432, 24)
Y = np.random.randint(2, size=(432, 2))
X_train, X_test, y_train, y_test = train_test_split(X, Y, test_size=0.3, random_state=12)
model = tf.keras.Sequential([
tf.keras.layers.Dense(64, activation=tf.nn.relu),
tf.keras.layers.Dense(2, activation=tf.nn.sigmoid),
])
model.compile(optimizer='adam', loss='binary_crossentropy', metrics=['accuracy'])
history = model.fit(X_train, y_train, epochs=200, batch_size=32, validation_split=0.3)
test_loss, test_acc = model.evaluate(X_test, y_test)
print('Model loss:', test_loss, 'Model accuracy: ', test_acc)
Note that your data X
has the shape (432, 24)
and your labels Y
has the shape (432, 2)
. I removed your Flatten
layer as it doesn't make much sense if your data has the shape (432, 24)
. You can make a prediction after training your model like this:
X_new = np.random.rand(1, 24)
Y_new = model.predict(X_new)
print(Y_new)
Answered By - AloneTogether
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.