Keras模型测试准确率达到1.0

以下是预测下一交易日是否上涨或下跌的代码(上涨=1,下跌=0)

我所做的是创建一个数据框,并仅使用PriceChange(今日收盘价 – 昨日收盘价)来预测下一交易日的价格变化是否上涨或下跌(下一交易日收盘价 – 今日收盘价)

df['PriceChange'] = (df['Close'] > df['Close'].shift(1)).astype(int)df['Closeupnextday'] = (df['Close'].shift(-1) > df['Close']).astype(int)

因此,数据框看起来像这样:

            PriceChange  Closeupnextday    0             0               1    1             1               1    2             1               1    3             1               1    4             1               0    5             0               0    6             0               0    7             0               1

它总是给我1.000的准确率,公平地说,应该只有50+%的准确率。我认为代码中有些地方出了问题,但找不到问题所在。

我应该补充的是,在第20/500个周期后,它总是给我1.000的准确率

请问有什么建议吗?

def load_data(stock, seq_len):    amount_of_features = len(stock.columns)    data = stock.as_matrix() #pd.DataFrame(stock)    sequence_length = seq_len + 1    result = []    for index in range(len(data) - sequence_length):        result.append(data[index: index + sequence_length])    result = np.array(result)    row = round(0.9 * result.shape[0])    train = result[:int(row), :]    x_train = train[:, :-1]    y_train = train[:, -1][:,-1]    x_test = result[int(row):, :-1]    y_test = result[int(row):, -1][:,-1]    x_train = np.reshape(x_train, (x_train.shape[0], x_train.shape[1], amount_of_features))    x_test = np.reshape(x_test, (x_test.shape[0], x_test.shape[1], amount_of_features))      return [x_train, y_train, x_test, y_test]def build_model(layers):    model = Sequential()    model.add(LSTM(        input_dim=layers[0],        output_dim=layers[1],        return_sequences=True))    model.add(Dropout(0.0))    model.add(LSTM(        layers[2],        return_sequences=False))    model.add(Dropout(0.0))    model.add(Dense(        output_dim=layers[2]))    model.add(Activation("linear"))    start = time.time()    model.compile(loss="mse", optimizer="rmsprop",metrics=['accuracy'])    print("Compilation Time : ", time.time() - start)    return modeldef build_model2(layers):        d = 0.2        model = Sequential()        model.add(LSTM(128, input_shape=(layers[1], layers[0]), return_sequences=True))        model.add(Dropout(d))        model.add(LSTM(64, input_shape=(layers[1], layers[0]), return_sequences=False))        model.add(Dropout(d))        model.add(Dense(16, activation="relu", kernel_initializer="uniform"))                model.add(Dense(1, activation="relu", kernel_initializer="uniform"))        model.compile(loss='mse',optimizer='adam',metrics=['accuracy'])        return modelwindow = 5X_train, y_train, X_test, y_test = load_data(df[::-1], window)print("X_train", X_train.shape)print("y_train", y_train.shape)print("X_test", X_test.shape)print("y_test", y_test.shape) # model = build_model([3,lag,1])model = build_model2([len(df.columns),window,1]) #11 = Dataframe axis 1model.fit(    X_train,    y_train,    batch_size=512,    epochs=500,    validation_split=0.1,    verbose=1)trainScore = model.evaluate(X_train, y_train, verbose=0)print('Train Score: %.2f MSE (%.2f RMSE)' % (trainScore[0], math.sqrt(trainScore[0])))testScore = model.evaluate(X_test, y_test, verbose=0)print('Test Score: %.2f MSE (%.2f RMSE)' % (testScore[0], math.sqrt(testScore[0])))# print(X_test[-1])diff=[]ratio=[]p = model.predict(X_test)for u in range(len(y_test)):    pr = p[u][0]    ratio.append((y_test[u]/pr)-1)    diff.append(abs(y_test[u]- pr))    #print(u, y_test[u], pr, (y_test[u]/pr)-1, abs(y_test[u]- pr))print(p)print(y_test)

回答:

(由于您没有澄清,我假设这里您讨论的是测试准确率 – 训练准确率确实可以达到1.0,这取决于您的数据和模型的具体细节。)

当问题、损失和指标混淆时,这种情况很常见 – 请参阅我在此处的回答,了解在多类别分类问题中使用binary_crossentropy作为Keras中的损失时的类似混淆情况。

在尝试任何解决方法之前,请尝试手动预测几个例子(即使用model.predict而不是model.evaluate);由于我没有您的数据,我无法自己完成,但我敢打赌,您将得到的结果将符合您的model.evaluate结果所暗示的完美准确率。

针对您的问题核心:由于您有一个二元分类问题,您应该绝对在模型编译中请求loss='binary_crossentropy',而不是mse

我无法确定您从model.evaluate得到的1.0值的确切含义,但正如我在上面链接的回答中所展示的,Keras为使用metrics=['accuracy']编译的模型返回的评估指标高度依赖于相应的loss条目;即使我最终能够弄清楚那个问题中的问题是什么,我甚至无法开始想象在这里发生了什么,您请求accuracy(即一个分类指标)用于一个回归损失(mse)…

Related Posts

使用LSTM在Python中预测未来值

这段代码可以预测指定股票的当前日期之前的值,但不能预测…

如何在gensim的word2vec模型中查找双词组的相似性

我有一个word2vec模型,假设我使用的是googl…

dask_xgboost.predict 可以工作但无法显示 – 数据必须是一维的

我试图使用 XGBoost 创建模型。 看起来我成功地…

ML Tuning – Cross Validation in Spark

我在https://spark.apache.org/…

如何在React JS中使用fetch从REST API获取预测

我正在开发一个应用程序,其中Flask REST AP…

如何分析ML.NET中多类分类预测得分数组?

我在ML.NET中创建了一个多类分类项目。该项目可以对…

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注