我正在试验LSTM编码器-解码器。我不清楚应该重塑输入数据的人。
我使用了以下代码:
import keras
import random
import numpy as np
from random import randint
from numpy import array
from numpy import argmax
from pandas import DataFrame
from pandas import concat
from keras.models import Sequential
from keras.layers import LSTM
from keras.layers import Dense
from keras.layers import TimeDistributed
from keras.layers import RepeatVector
cardinality= 10
n_steps=10
n_steps_y=3
n_features=1
def getRandomInt():
return getOneHotEncoded(random.randint(1,cardinality),cardinality)
def getOneHotEncoded(value, cardinality):
encoded = [0 for _ in range(cardinality+1)]
encoded[value] = 1
return encoded
def generateXY():
X, y = list(), list()
for q in range(100):
x_temp = [getRandomInt() for _ in range(10)]
y_temp = x_temp[-3:]
X.append(x_temp)
y.append(y_temp)
return np.array(X), np.array(y)
def getModel(n_steps=n_steps,n_features=n_features):
model = Sequential()
model.add(LSTM(12, input_shape=(n_steps,n_features)))
model.add(RepeatVector(n_steps_y))
model.add(LSTM(5, return_sequences=True))
model.add(TimeDistributed(Dense(1)))
model.compile(loss='categorical_crossentropy',optimizer='adam')
print(model.summary())
return model
X,y = generateXY()
model=getModel()
model.fit(X,y, epochs=10, batch_size=10,verbose=1)
出现输入形状错误。
ValueError:检查输入时出错:预期lstm_1_input具有 形状(10,1),但数组的形状为(10,11)
如何针对此代码适当调整输入的形状?