TODO: 训练精度更高的模型

This commit is contained in:
eson 2021-03-23 18:12:32 +08:00
parent 68d7c5d2c9
commit 36b30982f7
6 changed files with 70 additions and 27 deletions

40
data.py
View File

@ -8,6 +8,7 @@ import pymysql
import pickle
import os
import numpy
import time, datetime
def get_collect():
collect = {}
@ -22,9 +23,10 @@ def get_collect():
# 使用 cursor() 方法创建一个游标对象 cursor
cursor = db.cursor()
today = time.strftime("%Y-%m-%d", time.localtime())
# 使用 execute() 方法执行 SQL 查询
print(cursor.execute('''SELECT coin, extra_coins, pay_users, create_at from pay_items_hour pih where region = "all" and platform="all"'''))
cursor.execute('''SELECT coin, extra_coins, pay_users, create_at from pay_items_hour pih where region = "all" and platform="all" and create_at <= %s''', today)
collect_pay = {}
for row in cursor.fetchall():
# print(row)
@ -84,7 +86,7 @@ def get_collect():
def load_pay_data(textNum = 80):
collect = get_collect()
# TODO: 处理gift pay的波动关系
x_train = []
@ -101,17 +103,20 @@ def load_pay_data(textNum = 80):
total_coin = 0
last_total_coin = 0
for v2 in lastday_v:
last_total_coin += v2[0] + v2[1]
count = 0
for v1, v2 in zip(cur_v,lastday_v):
total_coin += v1[0] + v1[1]
# print(v1[3])
last_total_coin += v2[0] + v2[1]
# last_total_coin += v2[0] + v2[1]
# print(v2[3])
compare = float(total_coin - last_total_coin) / float(last_total_coin)
# compare = float(total_coin - last_total_coin) / float(last_total_coin)
# print(compare)
x_train.append([count, total_coin , (total_coin - last_total_coin) , v1[2] , v2[2]])
x_train.append([count, total_coin, total_coin/last_total_coin])
count+=1
for i in range(count):
@ -119,15 +124,15 @@ def load_pay_data(textNum = 80):
lastday_v = cur_v
x_train = numpy.reshape(x_train, (len(x_train) , 5, 1))
x_train = numpy.reshape(x_train, (len(x_train) , 3, 1))
y_train = numpy.reshape(y_train, (len(y_train)))
# max_features = 1024
tx_train = x_train[len(x_train) - textNum:]
ty_train = y_train[len(y_train) - textNum:]
x_train = x_train[:len(x_train) - textNum]
y_train = y_train[:len(y_train) - textNum]
# x_train = x_train[:len(x_train) - textNum]
# y_train = y_train[:len(y_train) - textNum]
return x_train, y_train, tx_train, ty_train
@ -150,23 +155,26 @@ def load_gift_data(textNum = 80):
last_total_coin = 0
users = 0
for v2 in lastday_v:
last_total_coin += v2[0]
f = 20000000.0
count = 0
count = 1
for v1, v2 in zip(cur_v,lastday_v):
total_coin += v1[0]
# print(v1[3])
last_total_coin += v2[0]
# last_total_coin += v2[0]
users += v1[1]
# print(v2[3])
compare = float(total_coin - last_total_coin) / float(last_total_coin)
# compare = float(total_coin - last_total_coin) / float(last_total_coin)
# print(compare)
x_train.append([count, total_coin, compare, users ])
x_train.append([count, total_coin, total_coin / last_total_coin, users ])
count+=1
for i in range(count):
for i in range(count - 1):
y_train.append(total_coin)
lastday_v = cur_v
@ -178,7 +186,7 @@ def load_gift_data(textNum = 80):
tx_train = x_train[len(x_train) - textNum:]
ty_train = y_train[len(y_train) - textNum:]
x_train = x_train[:len(x_train) - textNum]
y_train = y_train[:len(y_train) - textNum]
# x_train = x_train[:len(x_train) - textNum]
# y_train = y_train[:len(y_train) - textNum]
return x_train, y_train, tx_train, ty_train

24
predict.proto Normal file
View File

@ -0,0 +1,24 @@
syntax = "proto3";
service Predict {
rpc PayDay(RequestPay) returns (Reply) {}
rpc GiftDay(RequestGift) returns (Reply) {}
}
// Request Request请求
message RequestPay {
// count, total_coin , (total_coin - last_total_coin) , v1[2] , v2[2]] // 24
int32 Hour = 1;
int64 Coin = 2;
int64 YesterdayCoin = 3;
}
// RequestGift Request请求
message RequestGift {
}
// Reply Reply
message Reply {
}

View File

@ -2,6 +2,9 @@ import numpy
from keras.models import load_model
from data import load_pay_data, load_gift_data
import matplotlib.pyplot as plt
# x_train, y_train, tx_train, ty_train = load_pay_data(160)
# model = load_model("./predict_pay")
@ -13,7 +16,6 @@ from data import load_pay_data, load_gift_data
# print("测结果:", p_data[i][0], "测:", tx_train[i], "真实:", ty_train[i])
x_train, y_train, tx_train, ty_train = load_gift_data(160)
model = load_model("./predict_gift")
p_data = model.predict(tx_train)
@ -23,6 +25,10 @@ for i in range(len(p_data)):
if abs(comp) >= 0.1:
print("测结果:", p_data[i][0], "测:", tx_train[i], "真实:", ty_train[i])
plt.plot(ty_train)
plt.plot(p_data)
plt.show()
# data = numpy.reshape([[15, 2359688 / 10000000, 255968 / 1000000, 10 / 10000]],(1, 4, 1))
# print( model.predict(data))

4
requirements.txt Normal file
View File

@ -0,0 +1,4 @@
tensorflow
keras
numpy
pymysql

View File

@ -18,23 +18,24 @@ if __name__ == "__main__":
x_train, y_train, tx_train, ty_train = load_gift_data()
model = Sequential()
units = 256
units = 400
model.add(LSTM(units, activation='relu', input_shape=(4,1)))
model.add(Dropout(0.3))
model.add(LSTM(units, activation='relu', input_shape=(4,1) ))
model.add(Dropout(0.2))
model.add(Dense(1))
model.summary()
model.compile(loss='mse', optimizer='adam')
model.fit(x_train, y_train, batch_size=32, epochs=1000)
model.fit(x_train, y_train, batch_size=1, epochs=50)
model.save("./predict_gift")
p_data = model.predict(tx_train)
for i in range(len(p_data)):
comp = (p_data[i][0] - ty_train[i]) / ty_train[i]
print(comp, p_data[i][0], ty_train[i])
if abs(comp) >= 0.2:
if abs(comp) >= 0.1:
print("测结果:", p_data[i][0], "测:", tx_train[i], "真实:", ty_train[i])

View File

@ -16,18 +16,18 @@ from data import load_pay_data
if __name__ == "__main__":
x_train, y_train, tx_train, ty_train = load_pay_data()
x_train, y_train, tx_train, ty_train = load_pay_data(80)
model = Sequential()
units = 500
model.add(LSTM(units, activation='relu', input_shape=(5,1)))
model.add(Dropout(0.1))
units = 500
model.add(LSTM(units, activation='relu', input_shape=(3,1)))
model.add(Dropout(0.3))
model.add(Dense(1))
model.summary()
model.compile(loss='mse', optimizer='adam')
model.fit(x_train, y_train, batch_size=128, epochs=1500)
model.fit(x_train, y_train, batch_size=1, epochs=50)
model.save("./predict_pay")
p_data = model.predict(tx_train)