003. evaluate and save model # You inspected how to train model in previous lecture # In this lecture,you will complete step of building model, # and up to step of testing model # If you input [1,5,3,7,8,10,12], # and model ouputs [0,0,0,1,0], # you can say model can predict well import tensorflow as tf input_data=[[1,5,3,7,8,10,12], [5,8,10,3,9,7,1]] label_data=[[0,0,0,1,0], [1,0,0,0,0]] INPUT_SIZE=7 HIDDEN1_SIZE=10 HIDDEN2_SIZE=8 CLASSES=5 Learning_rate=0.05 x_placeholder_node=tf.placeholder(tf.float32,shape=[None,INPUT_SIZE]) y_placeholder_node=tf.placeholder(tf.float32,shape=[None,CLASSES]) map_placeholder_and_data={x_placeholder_node:input_data,y_placeholder_node:label_data} W_h1=tf.Variable(tf.truncated_normal(shape=[INPUT_SIZE,HIDDEN1_SIZE]),dtype=tf.float32) b_h1=tf.Variable(tf.zeros(shape=[HIDDEN1_SIZE]),dtype=tf.float32) W_h2=tf.Variable(tf.truncated_normal(shape=[HIDDEN1_SIZE,HIDDEN2_SIZE]),dtype=tf.float32) b_h2=tf.Variable(tf.zeros(shape=[HIDDEN2_SIZE]),dtype=tf.float32) W_o=tf.Variable(tf.truncated_normal(shape=[HIDDEN2_SIZE,CLASSES]),dtype=tf.float32) b_o=tf.Variable(tf.zeros(shape=[CLASSES]),dtype=tf.float32) # At point of finishing configuring weights, # you create saver_object # You can specify parameters you want to save # When you don't specify parameters, # all parameters you use will be saved parameters_to_be_saved_list=[W_h1,b_h1,W_h2,b_h2,W_o,b_o] saver_object=tf.train.Saver() hidden1=tf.sigmoid(tf.matmul(x_placeholder_node,W_h1) + b_h1) hidden2=tf.sigmoid(tf.matmul(hidden1,W_h2) + b_h2) hypothesis_function=tf.sigmoid(tf.matmul(hidden2,W_o) + b_o) cost=tf.reduce_sum(-y_placeholder_node*tf.log(hypothesis_function)-(1-y_placeholder_node)*tf.log(1-hypothesis_function),reduction_indices=1) loss=tf.reduce_mean(cost) train=tf.train.GradientDescentOptimizer(Learning_rate).minimize(cost) # argmax() returns index of maximal value # tf.argmax(input,dimension,name=None) # You extract index of maximal value from prediction by using argmax() # Then,you compare extracted index and index of label by using tf.equal() # Then you can get like this [1,0,1,1] # You calculate [1,0,1,1] to find mean value(3/4) # For example,you will denote maximal value as 1 # index prediction label # 0 [0 0 1 0 0] [0 0 1 0 0] => true => 1 # 1 [1 0 0 0 0] [0 1 0 0 0] => false => 0 # 2 [0 0 0 0 1] [0 0 0 0 1] => true => 1 # 3 [0 0 1 0 0] [0 0 1 0 0] => true => 1 # This code displays accuracy # Since hypothesis_function is vector,you pass 1 comp_pred=tf.equal(tf.argmax(hypothesis_function,1) tf.argmax(y_placeholder_node,1)) accuracy=tf.reduce_mean(tf.cast(comp_pred,tf.float32)) sess_object=tf.Session() sess_object.run(tf.initialize_all_variables()) for i in range(1000): _,loss,acc=sess_object.run([train,cost,accuracy],map_placeholder_and_data) pred=sess_object.run(tf.argmax(hypothesis_function,1),map_placeholder_and_data) if i%100==0: saver_object.save(sess_object,'./tflectcheckpoint') print('----------') print('step: ',i) print('loss: ',loss) print('accuracy: ',acc)