【问题标题】:getting the shape for the input_shape parameter LSTM获取 input_shape 参数 LSTM 的形状
【发布时间】:2020-12-10 08:04:56
【问题描述】:

有没有办法自动获取 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')

【问题讨论】:

    标签: python machine-learning keras deep-learning recurrent-neural-network


    【解决方案1】:

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

    【讨论】:

      【解决方案2】:

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

      【讨论】:

      • 我收到以下错误:str(x.shape.as_list())) ValueError: Input 0 of layer lstm is in compatible with the layer: expected ndim=3, found ndim=2.收到的完整形状:[无,0]
      • 所以你输入的形状是0?就像我说的,这只有在你有正确的输入形状时才有效
      • 你的输入形状是什么?
      • 这是你的问题。您还没有准备好自动推断形状,因为您的输入错误
      • 再一次,您的输入是错误的。不管你在input_shape里面放什么都会报错
      猜你喜欢
      • 1970-01-01
      • 2020-01-22
      • 1970-01-01
      • 2020-01-20
      • 1970-01-01
      • 2019-06-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多