我的RNN如下圖
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
_________________________________________________________________
然后嘗試訓練和預測
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)
顯示錯誤 ValueError: shapes (335476,50) and (3,50) not aligned: 50 (dim 1) != 3 (dim 0)
我不確定 335476 來自哪里......
我應該在哪里修??
uj5u.com熱心網友回復:
您通常batch_size使用與訓練原始模型相同的方法。可以在此處找到有關此主題的更多資訊和可能的解決方法。但是,由于您使用的是None,它應該適用于單個樣本。這是一個作業示例:
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)
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/381033.html
