How to process input and output shape for keras LSTM

前端 未结 1 1638
小蘑菇
小蘑菇 2021-01-25 06:49

I am learning about RNN and I wrote this simple LSTM model in keras (theano) using a sample dataset generated using sklearn.

from sklearn.datasets import make_re         


        
相关标签:
1条回答
  • 2021-01-25 07:07

    If I'm correct, then LSTM expects a 3D input.

    X = np.random.random((100, 10, 64))
    y = np.random.random((100, 2))
    
    model = Sequential()
    model.add(LSTM(32, input_shape=(10, 64)))
    model.add(Dense(2)) 
    model.compile(loss='mean_squared_error', optimizer='adam')
    
    model.fit(X, Y, nb_epoch=1, batch_size=32)
    

    UPDATE: If you want to convert X, Y = make_regression(100, 9, 9, 2) into 3D, then you can use this.

    from sklearn.datasets import make_regression
    from keras.models import Sequential
    from keras.layers import Dense,Activation,LSTM
    
    #creating sample dataset
    X, Y = make_regression(100, 9, 9, 2)
    X = X.reshape(X.shape + (1,))
    
    #creating LSTM model
    model = Sequential()
    model.add(LSTM(32, input_shape=(9, 1)))
    model.add(Dense(2))
    model.compile(loss='mean_squared_error', optimizer='adam')
    
    model.fit(X, Y, nb_epoch=1, batch_size=32)
    
    0 讨论(0)
提交回复
热议问题