import keras

from keras.model import Sequential

from keras.layers import Dense,Dropout

from keras.optimizers import RMSprop

import scipy.io as sio

import matplotlib.pyplot as plt

batch_size =50

epochs =200

input_shape=(30000,)#此處為3*100*100的影象資料平鋪為30000*1的列向量

data=sio.loadmat('data_yuanshuju.mat')

train_x=data_yuanshuju['train_x']

train_y=data_yuanshuju['train_y']

test_x=data_yuanshuju['test_x']

test_y=data_yuanshuju['test_y']

train_x =np.reshape(train_x,(5000,30000))

test_x=np.reshape(test_x,(2000,30000))

train_x =train_x.astype('float32')

train_y =train_y.astype('float32')

test_x =test_x.astype('float32')

test_y =test.astype('float32')

train_x /=255

test_x /=255

print(train_x.shape[0],'train samples')

print(test_x.shape[0],'test samples')

train_y =(train_y-min(train_y))/(max(train_y)-min(train_y))
test_y =(test_y-min(test_y))/(max(test_y)-min(test_y))

#建立模型

model =Sequential()

model.add(Dense(256,activation='sigmoid',input_shape=input_shape))

model.add(Dense(128,activation='sigmoid'))

model.add(Dense(64,activation='sigmoid'))

model.add(Dense(1,activation='sigmoid'))

model.summary()

model.compile(loss='mse',optimizer=RMSprop(),metrics=['accuracy'])

history=model.fit(train_x,train_y,

                            batch_size=batch_size,

                            epochs=epochs,

                            verbose=1,

                             validation_data=(test_x,test_y) )

score = model.evaluate(test_x,test_y,verbose=0)

Y_predict =model.predict(test_x)

print('Test loss',score[0])

print('Test accuracy',score[1])

plt.plot(test_y.'b-')

plt.plot(Y_predict,'r--')