简体   繁体   中英

Regression with LSTM - python and Keras

I am trying to use a LSTM network in Keras to make predictions of timeseries data one step into the future. The data I have is of 5 dimensions, and I am trying to use the previous 3 periods of readings to predict the a future value in the next period. I have normalised the data and removed all NaN etc, and this is the code I am trying to use to train the network:

def Network_ii(IN, OUT, TIME_PERIOD, EPOCHS, BATCH_SIZE, LTSM_SHAPE):
 
    length = len(OUT)
    train_x = IN[:int(0.9 * length)]
    validation_x = IN[int(0.9 * length):]
    train_y = OUT[:int(0.9 * length)]
    validation_y = OUT[int(0.9 * length):]

    # Define Network & callback:
    train_x = train_x.reshape(train_x.shape[0],3, 5)
    validation_x = validation_x.reshape(validation_x.shape[0],3, 5)
    

    model = Sequential()
    model.add(LSTM(units=128, return_sequences= True, input_shape=(train_x.shape[1],3)))
    model.add(LSTM(units=128))
    model.add(Dense(units=1))
    model.compile(optimizer='adam', loss='mean_squared_error')

    train_y = np.asarray(train_y)
    validation_y = np.asarray(validation_y)
    history = model.fit(train_x, train_y, batch_size=BATCH_SIZE, epochs=EPOCHS, validation_data=(validation_x, validation_y))

    # Score model
    score = model.evaluate(validation_x, validation_y, verbose=0)
    print('Test loss:', score)
    # Save model
    model.save(f"models/new_model")

I am attempting to roughly follow the steps outlined here- https://machinelearningmastery.com/multivariate-time-series-forecasting-lstms-keras/

However, no matter what adjustments I have made in terms of changing the number of dimensions used to train the network or the length of the time period I cannot get the output of the model to give predictions that are not either 1 or 0. This is even though the target data, in the array 'OUT' is made up of data continuous on [0,1].

I think there may be something wrong with how I am setting up the .Sequential() function, but I cannot see what to adjust. I am relatively new to this so any help would be greatly appreciated.

You are probably using a prediction function that is not the standard. Maybe you are using predict_classes ?

The one that is well documented and the standard is model.predict .

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.

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