import keras
# -*- encoding:utf-8 -*-
import numpy as np
from keras.models import Sequential
# 优化方法选用Adam(其实可选项有很多，如SGD)
from keras.optimizers import Adam
import random
from keras.models import load_model
from imblearn.over_sampling import RandomOverSampler
from keras.utils import np_utils
# 用于模型初始化，Conv2D模型初始化、Activation激活函数，MaxPooling2D是池化层
# Flatten作用是将多位输入进行一维化
# Dense是全连接层
from keras.layers import Conv2D, Activation, MaxPool2D, Flatten, Dense


def read_data(path):
    lines = []
    with open(path) as f:
        for x in range(20000):
            lines.append(eval(f.readline().strip()))

    random.shuffle(lines)
    print('读取数据完毕')

    d=int(0.95*len(lines))

    train_x=[s[:-2] for s in lines[0:d]]
    train_y=[s[-1] for s in lines[0:d]]
    test_x=[s[:-2] for s in lines[d:]]
    test_y=[s[-1] for s in lines[d:]]

    print('转换数据完毕')

    ros = RandomOverSampler(random_state=0)
    X_resampled, y_resampled = ros.fit_sample(np.array(train_x), np.array(train_y))

    print('数据重采样完毕')

    return X_resampled,y_resampled,np.array(test_x),np.array(test_y)

train_x,train_y,test_x,test_y=read_data("D:\\data\\quantization\\stock6.log")
train_x = train_x.reshape(train_x.shape[0], 1,80,5)
test_x = test_x.reshape(test_x.shape[0], 1,80,5)


# 加载数据
# (X_train,Y_train),(X_test,Y_test) = mnist.load_data()

model = Sequential()
# model.add(Dense(units=635, input_dim=400,  activation='relu'))
# model.add(Dense(units=3, activation='softmax'))
# model.compile(loss='categorical_crossentropy', optimizer="adam",metrics=['accuracy'])

# 模型卷积层设计
model.add(Conv2D(
    nb_filter=32,  # 第一层设置32个滤波器
    nb_row=3,
    nb_col=20,  # 设置滤波器的大小为5*5
    padding='same',  # 选择滤波器的扫描方式，即是否考虑边缘
    input_shape=(1,80,5),  # 设置输入的形状
    # batch_input_shape=(64, 1, 28, 28),
))
# 选择激活函数
model.add(Activation('relu'))

# 设置下采样(池化层）
model.add(MaxPool2D(
    pool_size=(4,1),  # 下采样格为2*2
    strides=(2,2),  # 向右向下的步长
    padding='same', # padding mode is 'same'
))

# 使用Flatten函数，将输入数据扁平化（因为输入数据是一个多维的形式，需要将其扁平化）
model.add(Flatten())  # 将多维的输入一维化
model.add(Dense(1024))  # 全连接层1024个点
model.add(Activation('relu'))

# 在建设一层
model.add(Dense(3))  # 输入是个类别
model.add(Activation('softmax'))  # 用于分类的softmax函数

adam = Adam()  # 学习速率lr=0.0001

model.compile(optimizer=adam,
    loss='categorical_crossentropy',
    metrics=['accuracy'])

print("Starting training ")
h=model.fit(train_x, train_y, batch_size=64, epochs=10, shuffle=True)
score = model.evaluate(test_x, test_y)
print(score)
print('Test score:', score[0])
print('Test accuracy:', score[1])

path="15min_cnn_seq.h5"
model.save(path)
model=None


model=load_model(path)
result=model.predict(test_x)
print(result)
print(test_y)