import tensorflow as tf
import numpy as np
import pandas as pd
from sklearn.model_selection import train_test_split

x= pd.read_excel('Synthetic Data Feature.xlsx',header=None)
#x=x.T
#print(x)
y= pd.read_excel('Synthetic Data Label.xlsx',header=None)
#y= pd.read_excel('A.xlsx',header=None)

#x_train,y_test, y_train, y_test=train_test_split(x,y,test_size=0.2, random_state=0)

def weight_variable(shape):
    initial=tf.truncated_normal(shape,stddev=0.1)
    return tf.Variable(initial)

def bias_variable(shape):
    initial=tf.constant(0.1,shape=shape)
    return tf.Variable(initial)

def conv1d(x,W):
    #strides=[1,x_movement,y_movement,1]
    
    return tf.nn.conv1d(x,W,stride=1,padding='SAME')

def pool(x):
    #strides=[1,x_movement,y_movement,1]
    return tf.nn.pool(x,window_shape=[2],pooling_type='MAX',padding='SAME',strides=[2])

#define placeholder for inputs to network
xs=tf.placeholder(tf.float32,[None,25])
ys=tf.placeholder(tf.float32,[None,1])
#A=tf.placeholder(tf.float32,[None,7])
#print(xs)
#print(ys)
keep_prob=tf.placeholder(tf.float32)
x_image=tf.reshape(xs,[-1,25,1])
#print(x_image.shape)#[n_samples,784,1]


##conv1 layer
W_conv1=weight_variable([6,1,16])#patch  5,in size 1, out size 8,[filter,neurons,output]
b_conv1=bias_variable([16])
h_conv1=tf.nn.relu(conv1d(x_image,W_conv1)+b_conv1)#output size25*8
h_pool1=pool(h_conv1)#output size 13*8

##conv2 layer
W_conv2=weight_variable([4,16,32])#patch  5,in size 8, out size 16
b_conv2=bias_variable([32])
h_conv2=tf.nn.relu(conv1d(h_pool1,W_conv2)+b_conv2)#output size 13*16
h_pool2=pool(h_conv2)#output size 7*16


#conv3 layer

W_conv3=weight_variable([4,32,64])#patch  5,in size 16, out size 32
b_conv3=bias_variable([64])
h_conv3=tf.nn.relu(conv1d(h_pool2,W_conv3)+b_conv3)#output size 7*32
h_pool3=pool(h_conv3)#output size 4*32



##func1 layer
W_fc1=weight_variable([4*64,1024])
b_fc1=bias_variable([1024])
#[n_sample,7,7,64]->>[n_sample,7*7*64]
h_pool2_flat=tf.reshape(h_pool3,[-1,4*64])
h_fc1=tf.nn.relu(tf.matmul(h_pool2_flat,W_fc1)+b_fc1)
h_fc1_drop=tf.nn.dropout(h_fc1,keep_prob)

##func2 layer
W_fc2=weight_variable([1024,1])
b_fc2=bias_variable([1])
prediction=tf.matmul(h_fc1_drop,W_fc2)+b_fc2
#prediction=np.array(prediction)
#ys=np.array(ys)

#s=np.ones((8000,7))
#for i in range(8000):
        #s[i][2]=10*s[i][2]
        #s[i][5]=10*s[i][5]
        #print(s)
#A=np.zeros((10000,7))        
#for h in range(10000):
    #A[h]=np.array([ys[h][0]-prediction[h][0],ys[h][1]-prediction[h][1],3*(ys[h][2]-prediction[h][2]),
       #ys[h][3]-prediction[h][3],ys[h][4]-prediction[h][4],3*(ys[h][5]-prediction[h][5]),ys[h][6]-prediction[h][6]])
    #print(A)

t=np.array([[1]])
loss=tf.reduce_mean(tf.reduce_sum(tf.square((ys-prediction)*t),reduction_indices=[1]))
#train_step=tf.train.AdamOptimizer(0.005).minimize(loss)
#the error between prediction and real data
#cross_entropy=tf.reduce_mean(-tf.reduce_sum(ys*tf.log(prediction),reduction_indices=[1]))
#train_step=tf.train.AdamOptimizer(1e-4).minimize(cross_entropy)

#loss=tf.reduce_mean(tf.reduce_sum(tf.square((ys-prediction)),reduction_indices=[1]))
    
#train_step=tf.train.GradientDescentOptimizer(0.006).minimize(loss)
train_step=tf.train.AdamOptimizer(0.006).minimize(loss)





saver=tf.train.Saver()
sess=tf.Session()
sess.run(tf.global_variables_initializer())



for i in range(10000):
    #batch_xs,batch_ys=mnist.train.next_batch(100)
   
    x_train,x_test, y_train, y_test=train_test_split(x,y,test_size=0.3, random_state=None)
    sess.run(train_step,feed_dict={xs:x_train,ys:y_train,keep_prob:1})
    if i % 10==0:
       
        print(sess.run(loss,feed_dict={xs:x_test,ys:y_test,keep_prob:1}),flush=True)

        
        
        #print(compute_accuracy(mnist.test.images,mnist.test.labels))

#saver_path=saver.save(sess,"my_model\saver_bet.ckpt")
saver_path=saver.save(sess,"SyncScreener\saver_bet.ckpt")
print("save to path:",saver_path)



