Home > database >  My TensorFlow prediction model show simple line
My TensorFlow prediction model show simple line

Time:12-29

I try to predict LTC coin price on 100 steps in future. All code works good, I create model architecture, save model, load model, load 1000 records for training.

Everising work normal, but after several cycles model show just simple line.

Here is code:

df = df[['y', 'h', 'o', 'l']]  # ,'t'
    df2 = df.values

    print(len(df))
    training = int(np.ceil(len(df) * .95))
    print(training)
    # quit()
    # prepere data for tensorflow
    # MinMaxScaler expecting like 1 feature
    scaler = MinMaxScaler(feature_range=(0, 1))
    scaled_data = scaler.fit_transform(df)
    print(f"scaled_data {len(scaled_data)}")

    # How many past days of data we want to use to predict the next day price
    prediction_days = 500
    train_data = scaled_data[0:int(training), :]
    print(f"train_data {len(train_data)}")
    # Preparing the Training data
    X_train = []
    y_train = []

    X_test = []
    y_test = []

    for x in range(prediction_days, len(train_data)):
        X_train.append(scaled_data[x - prediction_days:x, 0])
        y_train.append(scaled_data[x, 0])

        X_test.append(scaled_data[x - prediction_days:x, 0])
        y_test.append(scaled_data[x, 0])



    X_train, y_train = np.array(X_train), np.array(y_train)
    X_test, y_test = np.array(X_test), np.array(y_test)
    # Reshaping so that it will work in Neural net
    X_train = np.reshape(X_train, (X_train.shape[0], X_train.shape[1], 1))
    X_test = np.reshape(X_test, (X_test.shape[0], X_test.shape[1], 1))
    print(X_train.shape)
    print(y_train.shape)
    print("Files")
    print(os.path.isfile('model.h5'))
    if os.path.isfile('model.h5') is False:
        model = Sequential()
        model.add(LSTM(units=50, return_sequences=True, input_shape=(X_train.shape[1], 1)))
        model.add(Dropout(0.2))
        model.add(LSTM(units=50, return_sequences=True))
        model.add(Dropout(0.2))
        model.add(LSTM(units=50))
        model.add(Dropout(0.2))
        model.add(Dense(units=100))
        # define the optimization algorithm
        # best learning rate for
        opt = Adam(lr=0.01, beta_1=0.9, beta_2=0.999, epsilon=1e-08, decay=0.0)
        model.compile(optimizer=opt, loss='mean_squared_error')
        model.fit(X_train, y_train,  epochs=5, validation_data=(X_test, y_test)) #, callbacks=[keras.callbacks.LearningRateScheduler(lambda epoch: 1e-8 * 10 ** (epoch / 30))]

        # evaluate the model

        model.save('model.h5')
        #del model
        model = load_model('model.h5')
        # loss, accuracy = my_model.evaluate(X_test, y_test)
        # print(f"accuracy: {accuracy * 100:.2f}%")

        model_json = model.to_json()
        with open("model.json", "w") as json_file:
            json_file.write(model_json)

        model.save_weights('model_weight.h5')
        model.load_weights('model_weight.h5')


    else:

        # load json and create model
        json_file = open('model.json', 'r')
        loaded_model_json = json_file.read()
        json_file.close()
        model = model_from_json(loaded_model_json)

        # load weights into new model
        model.load_weights("model.h5")
        print("Loaded model from disk")
        opt = Adam(lr=0.01, beta_1=0.9, beta_2=0.999, epsilon=1e-08, decay=0.0)
        model.compile(optimizer=opt, loss='mean_squared_error')
        # train the model, iterating on the data in batches
        model.fit(X_train, y_train, epochs=5, validation_data=(X_test, y_test)) # callbacks=[keras.callbacks.LearningRateScheduler(lambda epoch: 1e-8 * 10 ** (epoch / 30))]
        # evaluate the model


        model_json = model.to_json()
        with open("model.json", "w") as json_file:
            json_file.write(model_json)

        model.save_weights('model_weight.h5')
        model.load_weights('model_weight.h5')


    test_data = 60
    # actual_prices = df.values

    total_dataset = df.values
    print(f"Counter")
    model_inputs = total_dataset[len(total_dataset) - test_data - prediction_days:]
    model_inputs = model_inputs.reshape(-1, 1)
    model_inputs = scaler.fit_transform( model_inputs)  # ValueError: X has 1 features, but MinMaxScaler is expecting 4 features as input.


    real_data = [model_inputs[len(model_inputs) - prediction_days: len(model_inputs)   1, 0]]

    # len of real_data = 101
    real_data = np.array(real_data)
    real_data = np.reshape(real_data, (real_data.shape[0], real_data.shape[1], 1))
    # reshape real_data to (100, 100, 1)

    prediction = model.predict(real_data)
    # evaluation metrics

    prediction = scaler.inverse_transform(prediction)
    prediction = prediction.reshape(-1, 1)
    # plot
    # plt.plot(df, color='green')
    plt.plot(prediction, color='green')
    plt.legend()
    plt.show()

Here is first cycle of model:

enter image description here

Here is 7-10 cycle of model:

enter image description here

Maybe some problem with learning process?

CodePudding user response:

Need to use not linear optimizer

from keras.optimizers import SGD 
....
sgd = SGD(learning_rate=0.01)
model.compile(loss='mse', optimizer=sgd)

  • Related