my RNN is like this below
length_of_sequence = 3
in_out_neurons = 50
n_hidden = 128
model = Sequential()
model.add(LSTM(n_hidden, batch_input_shape=(None, length_of_sequence,in_out_neurons), return_sequences=True))
model.add(Dense(in_out_neurons,activation="linear"))
optimizer = Adam(lr=0.001)
model.compile(loss="mean_squared_error", optimizer=optimizer)
model.summary()
Model: "sequential"
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
lstm (LSTM) (None, 3, 128) 91648
dense (Dense) (None, 3, 50) 6450
=================================================================
Total params: 98,098
Trainable params: 98,098
Non-trainable params: 0
_________________________________________________________________
then try to train and predict
print(final_x.shape) #(165737, 3, 50)
print(final_y.shape) #(165737, 1, 50)
model.fit(
final_x,final_y,
batch_size=300,
epochs=10,
validation_split=0.9
)
print(test_input.shape) # (1, 3, 50)
predicted = model.predict(test_input)
shows the error ValueError: shapes (335476,50) and (3,50) not aligned: 50 (dim 1) != 3 (dim 0)
I am not sure wheere 335476 comes from....
Where should I fix ??
CodePudding user response:
You usually to use the same batch_size that you use to train your original model. More information on this topic and possible workarounds can be found here. However, since you are using None, it should work with a single sample. Here is a working example:
import tensorflow as tf
length_of_sequence = 3
in_out_neurons = 50
n_hidden = 128
model = tf.keras.Sequential()
model.add(tf.keras.layers.LSTM(n_hidden, batch_input_shape=(None, length_of_sequence,in_out_neurons), return_sequences=True))
model.add(tf.keras.layers.Dense(in_out_neurons,activation="linear"))
optimizer = tf.keras.optimizers.Adam(lr=0.001)
model.compile(loss="mean_squared_error", optimizer=optimizer)
model.summary()
final_x = tf.random.normal((100, 3, 50))
final_y = tf.random.normal((100, 3, 50))
model.fit(
final_x,final_y,
batch_size=2,
epochs=10,
validation_split=0.9
)
test_input = tf.random.normal((1, 3, 50))
predicted = model.predict(test_input)
print(predicted.shape)
(1, 3, 50)
