简体   繁体   English

获取 input_shape 参数 LSTM 的形状

[英]getting the shape for the input_shape parameter LSTM

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.有没有办法自动获取 LSTM 中 input_shape 参数的形状,然后将该形状设置为 input_shape 参数。 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.如果您知道要在 model 上训练/测试哪些数据,这应该不是挑战。 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() .如果您的数据在 NumPy/Tensor/Pandas 中,您可以使用x.shape()获取它的形状。 You don't have to worry about the batch size, this is something that Keras will automatically pick on.您不必担心批量大小,这是 Keras 会自动选择的。

I generally use input_shape=X_train.shape[1:] .我通常使用input_shape=X_train.shape[1:] That is assuming your input shape is correct, and can pass through the neural net.那是假设您的输入形状是正确的,并且可以通过神经网络。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM