Keras Model提供1.0的测试精度

时间:2017-11-05 13:21:43

标签: machine-learning neural-network keras

下面是预测第二天是关闭还是关闭的代码(Up = 1,down = 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 model

def 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 model


window = 5
X_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 1

model.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 :(得分:1)

(由于你没有澄清它,我在这里假设你正在讨论测试准确性 - 列车准确度确实可以是1.0,具体取决于你的数据的详细信息&模型。)

嗯,当一个人弄乱问题,损失和指标时,通常会出现这样的问题 - 当binary_crossentropy被用作多类分类问题的Keras损失时,请参阅this answer of mine类似的混淆。

在尝试任何补救措施之前,请尝试手动预测几个示例(即使用model.predict代替model.evaluate);我不能自己做,因为我没有你的数据,但我敢打赌你得到的结果将 符合model.evaluate结果所暗示的完美准确性。

问题的核心:由于您遇到了二元分类问题,因此您应该肯定在模型编译中询问loss='binary_crossentropy',而不是mse

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