# -*- encoding:utf-8 -*-
import numpy
from tensorflow.keras.optimizers import SGD, Adam
from keras.models import Sequential
from keras.layers import Dense
from keras.layers import Dropout
from keras.layers import Flatten
from keras.utils import np_utils
from keras import backend as K
from keras.utils import np_utils
import numpy as np
import random



def read_data(path):
	with open(path) as f :
		lines=f.readlines()
	lines=random.sample(lines,int(len(lines)/10))
	lines=[eval(line.strip()) for line in lines]
	X,Y=zip(*lines)
	X=np.array(X)
	X=1.0*X/256
	X=X.reshape(-1,28*28)#784
	Y=np.array(Y)
	Y=np_utils.to_categorical(Y,num_classes)
	return X,Y
		
num_classes=11
train_x,train_y=read_data("train_data")
train_x_10,train_y_10=read_data("train_data_10")#reject data
train_x=np.concatenate((train_x,train_x_10))
train_y=np.concatenate((train_y,train_y_10))
model = Sequential()
#input_shape=(输入长，输入宽，输入通道数)
model.add(Dense(units=256,input_dim=28*28, activation='relu',use_bias=True))
model.add(Dense(units=128,input_dim=256,activation='relu',use_bias=True))
model.add(Dense(units=128,input_dim=256,activation='relu',use_bias=True))
model.add(Dense(units=num_classes,input_dim=128,use_bias=True, activation='softmax'))
# Compile model
model.compile(loss='categorical_crossentropy', optimizer="adam", metrics=['accuracy'])

model.fit(train_x, train_y, batch_size=50, epochs=10,shuffle=True)
model.save("model")



