def main(): stock_sql = StockSQL() if VERSION == 1: model = load_dnn_model_v1(weight_path=WEIGHT_PATH) ret = load_and_pred_data_v1(stock_sql, model) elif VERSION == 2: model = load_dnn_model_v2(weight_path=WEIGHT_PATH) ret = load_and_pred_data_v2(stock_sql, model) elif VERSION == 3: model = load_dnn_model_v3(weight_path=WEIGHT_PATH) ret = load_and_pred_data_v3(stock_sql, model) elif VERSION == 4: model = load_dnn_model_v4(weight_path=WEIGHT_PATH) ret = load_and_pred_data_v4(stock_sql, model) elif VERSION == 5: model = load_dnn_model_v5(weight_path=WEIGHT_PATH) ret = load_and_pred_data_v5(stock_sql, model) else: model = load_dnn_model_v6(weight_path=WEIGHT_PATH) ret = load_and_pred_data_v6(stock_sql, model) print("上傳資料中.....") # 上傳預測結果 stock_sql.write_multi_line("INSERT INTO `AI__分析a` VALUES (%s,%s,%s,%s,%s)", ret) print("Finish")
# Create X Input 3 x_data_3 = np.array(values)[:, 13:16].astype(np.float) x_data_3 = (x_data_3 - mean_dict_3[stock_id]) / std_dict_3[stock_id] # Create X Input 4 x_data_4 = np.array(values)[:, 16:20].astype(np.float) x_data_4 = (x_data_4 - mean_dict_4[stock_id]) / std_dict_4[stock_id] # Create X Input 5 x_data_5 = np.array(values)[:, 20:].astype(np.float) x_data_5 = (x_data_5 - mean_dict_5[stock_id]) / std_dict_5[stock_id] # Predict outputs = model.predict((x_data_1, x_data_2, x_data_3, x_data_4, x_data_5)) # Save predict result dates = [str(v[0]) for v in values] ret += package_outputs(stock_id, dates, outputs, version) print('Preparing Stock: {}, Number: {}'.format(stock_id, len(values))) return ret if __name__ == '__main__': from units.sql import StockSQL # 連線SQL資料庫 stock_sql_test = StockSQL() # 載入資料 load_data(stock_sql_test, load_dir='1723')
from datetime import datetime from flask import Flask, render_template from flask_bootstrap import Bootstrap from flask_moment import Moment from units.draw import * from units.sql import StockSQL from flask_wtf import FlaskForm from wtforms import SubmitField, DateField, SelectMultipleField, IntegerField, StringField from wtforms.validators import DataRequired app = Flask(__name__) app.config['SECRET_KEY'] = 'woodylin7158517' bootstrap = Bootstrap(app) moment = Moment(app) stock_sql = StockSQL() class StocksForm(FlaskForm): start_date = DateField('開始時間', id='datepick', validators=[DataRequired()]) end_date = DateField('結束時間', validators=[DataRequired()]) stock_id = IntegerField('股票代號', validators=[DataRequired()]) submit = SubmitField('送出') @app.errorhandler(404) def page_not_found(e): return render_template('404.html'), 404 @app.errorhandler(500)
def main(): # 初始話參數 if TEST_MODE: today = 'test' else: dt = datetime.datetime.today() today = "{}-{}-{}".format(dt.year, dt.month, dt.day) model_dir = os.path.join( 'outputs', '{}-{}-version{}.h5'.format(today, TRAINING_DATA, VERSION)) log_dir = os.path.join( 'logs', '{}-{}-version{}'.format(today, TRAINING_DATA, VERSION)) if os.path.isdir(log_dir) or os.path.isfile(model_dir): print("檔案已經存在") exit() # 連線SQL資料庫 stock_sql = StockSQL() # 載入資料 & 創建Model if VERSION == 1: x_train, y_train, x_val, y_val = load_data_v1(stock_sql, load_dir=TRAINING_DATA) model = load_dnn_model_v1() elif VERSION == 2: x_train, y_train, x_val, y_val = load_data_v2(stock_sql, load_dir=TRAINING_DATA) model = load_dnn_model_v2() elif VERSION == 3: x_train, y_train, x_val, y_val = load_data_v3(stock_sql, load_dir=TRAINING_DATA) model = load_dnn_model_v3() elif VERSION == 4: x_train, y_train, x_val, y_val = load_data_v4(stock_sql, load_dir=TRAINING_DATA) model = load_dnn_model_v4() elif VERSION == 5: x_train, y_train, x_val, y_val = load_data_v5(stock_sql, load_dir=TRAINING_DATA) model = load_dnn_model_v5() elif VERSION == 6: x_train, y_train, x_val, y_val = load_data_v6(stock_sql, load_dir=TRAINING_DATA) model = load_dnn_model_v6() else: x_train, y_train, x_val, y_val = load_data_v7(stock_sql, load_dir=TRAINING_DATA) model = load_dnn_model_v7() # Training model.compile( tf.keras.optimizers.Adam(0.001), loss=binary_focal_loss, metrics=[keras.metrics.BinaryAccuracy(), OverKill(), UnderKill()], run_eagerly=True) model_tcbk = keras.callbacks.TensorBoard(log_dir=log_dir) model_mckp = SaveBastOverKillAddUnderKillModel(model_dir) model_acbk = OverKillAddUnderKill(log_dir) model.fit(x_train, y_train, batch_size=64, epochs=50, validation_data=(x_val, y_val), callbacks=[model_tcbk, model_mckp, model_acbk])