Tensorflow keras模型无法预测

时间:2017-08-15 14:36:46

标签: python tensorflow keras tensor

我对keras模型的tensorflow有问题。

我的目标是预测未来5天的美元/土耳其里拉价格,如果它提高了" will_raise_5_s"需要为1,如果它不会提高" will_raise_5_s"值将为0.

所以,我有14维数组,0或1作为预测值。

所以我为此创建了一个模型,但这个模型并没有学习。损失和val_acc永远不会改变。

我看到每个时代都有这个

loss: 7.5000 - acc: 0.5347 - val_loss: 6.5712 - val_acc: 0.5923

以下是我的数据模型:https://api.myjson.com/bins/12qaq1

这是我的代码:

import urllib
import json
import pandas as pd
import stockstats as sdf
import numpy as np

def download_data():

    with open('data.json', 'r') as content_file:
        content = content_file.read()

    return content

def reformat_data_for_stockstats(data):

    #Boş verileri drop ediyoruz.
    data = data.drop('aort',axis=1).drop('hacimtl',axis=1).drop('sembolid',axis=1)
    data = data.rename(columns={
        'acilis':'Open',
        'dusuk':'Low',
        'hacimlot':'Volume',
        'kapanis':'Close',
        'yuksek':'High',
        'tarih':'Date'
    })

    return data


def apply_stock_to_data(data, stock):
    data['rsi_9'] = stock['rsi_9']
    data['rsi_14'] = stock['rsi_14']
    data['rsi_28'] = stock['rsi_28']
    data['close_-2_s'] = stock['close_-2_s']
    data['close_5_s'] = stock['close_5_s']
    data['wr_10'] = stock['wr_10']
    data['boll'] = stock['boll']
    data['boll_ub'] = stock['boll_ub']
    data['boll_lb'] = stock['boll_lb']

    return data

def apply_classification_data(data):
    #data['real_date'] = data.apply(lambda row: datetime.datetime.strptime(row['Date']).date(), axis=1)

    data['yesterday_raised'] = data.apply(lambda row: 1 if row['close_-1_s'] > row['close_-2_s'] else 0,
                                          axis=1)
    data['today_raised'] = data.apply(lambda row: 1 if row['close'] > row['open'] else 0, axis=1)
    data['will_raise_5_s'] = data.apply(lambda row: 1 if row['close_5_s'] > row['close'] else 0, axis=1)

    data['boll_diff'] = data.apply(lambda row: (row['close'] - row['boll']) if (np.isnan(row['boll'])==False) else 0,axis=1)
    data['boll_ub_diff'] = data.apply(lambda row: (row['close'] - row['boll_ub']) if (np.isnan(row['boll_ub'])==False) else 0,axis=1)
    data['boll_lb_diff'] = data.apply(lambda row: (row['close'] - row['boll_lb']) if (np.isnan(row['boll_lb'])==False) else 0,axis=1)

    data['open_diff'] = data.apply(lambda row: (row['close'] - row['open']) if (np.isnan(row['open'])==False) else 0,axis=1)
    data['low_diff'] = data.apply(lambda row: (row['close'] - row['low']) if (np.isnan(row['low'])==False) else 0,axis=1)
    data['high_diff'] = data.apply(lambda row: (row['close'] - row['high']) if (np.isnan(row['high'])==False) else 0,axis=1)


    data['close_20_sma_diff'] = data.apply(lambda row: (row['close'] - row['close_20_sma']) if (np.isnan(row['close_20_sma'])==False) else 0,axis=1)



    return data


def clear_non_required_fields(data):
    return data.\
        drop('open',axis=1).\
        drop('low',axis=1).\
        drop('high',axis=1).\
        drop('close',axis=1).\
        drop('close_-1_s',axis=1).\
        drop('close_-2_s',axis=1).\
        drop('close_5_s',axis=1).\
        drop('close_20_sma',axis=1).\
        drop('close_20_mstd',axis=1).\
        drop('rs_9',axis=1).\
        drop('rs_14',axis=1).\
        drop('rs_28',axis=1).\
        drop('boll',axis=1).\
        drop('boll_ub',axis=1).\
        drop('boll_lb',axis=1).\
        drop('volume',axis=1)

def clear_not_valid_data(data):
    data = data.shift(-2)
    data = data[:-7]

    return data

data = json.loads(download_data())
data = pd.DataFrame.from_dict(data)
data = reformat_data_for_stockstats(data)
stock = sdf.StockDataFrame.retype(data)

data = apply_stock_to_data(data ,stock)
data = apply_classification_data(data)
data = clear_non_required_fields(data)
data = clear_not_valid_data(data)


girisx = np.array(data.drop('will_raise_5_s',axis=1))
cikisx = np.array(data['will_raise_5_s'].values.reshape(-1,1))

giris = girisx[:,0:13]
cikis = cikisx

import keras
from keras.layers import Dense, Activation, Dropout
from keras.models import Sequential
from keras.utils.np_utils import to_categorical


model = Sequential()
model.add(Dense(units=2048,input_dim=13))
model.add(Activation('relu'))
model.add(Dropout(0.5))


model.add(Dense(units=2048))
model.add(Activation('relu'))
model.add(Dropout(0.5))

model.add(Dense(units=2))
model.add(Activation('softmax'))

//this line is garbage
optimizer = keras.optimizers.sgd(lr=1)

model.compile(optimizer='adam',loss='sparse_categorical_crossentropy', metrics=['accuracy'])
model.fit(giris, cikis, epochs=50, batch_size=32, validation_split=0.20)

1 个答案:

答案 0 :(得分:0)

请看一下Learning_rate:lr = 1看起来不正常恕我直言。我不确定这种情况,但根据我的经验:当模型停止学习时,这意味着lr太高了。尝试0.01 - 1e-5。希望,这有帮助。

当模型停止学习时,你也可以使用Keras回调处理学习率(参见文档)。