Is there a way to get the shape automatically for the input_shape parameter in LSTM, then to set that shape to the input_shape parameter. I want to be able to have the Recurrent Neural Network to set the input shape automatically, depending on the shape of the data. Thanks.
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')
If you know what data you're going to be training/testing the model on, this shouldn't be a challenge. You just have to pick one data point from your dataset. If your data is in NumPy/Tensor/Pandas, you can get it's shape using x.shape()
. You don't have to worry about the batch size, this is something that Keras will automatically pick on.
I generally use input_shape=X_train.shape[1:]
. That is assuming your input shape is correct, and can pass through the neural net.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.