0

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 ??

whitebear
  • 11,200
  • 24
  • 114
  • 237

1 Answers1

1

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)
AloneTogether
  • 25,814
  • 5
  • 20
  • 39
  • However if you use `None` as your batch size you could use batches of any size both during training and inference. – rudolfovic Dec 13 '21 at 07:29
  • mmm input size must be `(batch_size, 3, 128)`? I used `(165737,3,50)` for learning data though.... This aims to predict next [50dimention], from three succession of [50dimention],[50dimention],[50dimention] items. – whitebear Dec 13 '21 at 08:39
  • In my understanding in learning process train data shape is `(165737, 3, 50)` , then use `(1,3,50)` for predict purpose. – whitebear Dec 13 '21 at 08:41
  • I added `model.fit()` in the article – whitebear Dec 13 '21 at 08:55
  • Must have been a misunderstanding. Updated answer. Your code seems to work fine with the example I provided. – AloneTogether Dec 13 '21 at 09:08
  • 1
    I tried your code and it works Thanks @AloneTogether I need to sort out the shape ideas. – whitebear Dec 13 '21 at 09:36