获取input_ shape参数LSTM的形状



有没有一种方法可以自动获得LSTM中input_shape参数的形状,然后将该形状设置为input_shape参数。我希望能够让递归神经网络根据数据的形状自动设置输入形状。谢谢

dataset_train = pd.read_csv(dataset_path)

training_set = dataset_train.iloc[:, :].values
from sklearn.preprocessing import MinMaxScaler
sc = MinMaxScaler(feature_range=(0, 1))
training_set_scaled = sc.fit_transform(x)

print(len(training_set_scaled))
print(len(dataset_train))
X_train = []
y_train = []
for i in range(past_days, len(training_set_scaled) - future_days + 1):
X_train.append(training_set_scaled[i - past_days:i, 0])
y_train.append(training_set_scaled[i + future_days - 1:i + future_days, 0])
X_train, y_train = np.array(X_train), np.array(y_train)

## Building and Training the RNN
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense
from tensorflow.keras.layers import LSTM
from tensorflow.keras.layers import Dropout
### Initialising the RNN

regressor = Sequential()
### Adding the first LSTM layer and some Dropout regularisation
regressor.add(LSTM(units=50, input_shape= (?) , return_sequences=True))
regressor.add(Dropout(0.2))
### Adding a second LSTM layer and some Dropout regularisation
regressor.add(LSTM(units=50, return_sequences=True))
regressor.add(Dropout(0.2))
### Adding a third LSTM layer and some Dropout regularisation
regressor.add(LSTM(units=50, return_sequences=True))
regressor.add(Dropout(0.2))
### Adding a fourth LSTM layer and some Dropout regularisation
regressor.add(LSTM(units=50))
regressor.add(Dropout(0.2))
### Adding the output layer
regressor.add(Dense(units=1))
### Compiling the RNN
regressor.compile(optimizer='adam', loss='mean_squared_error')

如果您知道要在什么数据上训练/测试模型,这应该不是一个挑战。您只需要从数据集中选择一个数据点。如果你的数据是NumPy/Tensor/Pandas,你可以使用x.shape()得到它的形状。你不必担心批量大小,这是Keras会自动选择的。

我通常使用input_shape=X_train.shape[1:]。这是假设你的输入形状是正确的,并且可以通过神经网络。

最新更新