@@ -128,9 +128,9 @@ def get_parameters():
128128 'graph embeddings and doc2vec.' )
129129
130130 parser .add_argument ('-training_corpus' ,
131- help = "Path to a training corpus contaning both positive and negative examples"
132- " in the format -- positive sample, starting location of the ambigous word, "
133- "endinglocation of the ambigous word , true_url, context, negative_samples_urls -- "
131+ help = "Path for a training corpus containing both positive and negative examples"
132+ " in the format -- positive sample, starting location of the ambiguous word, "
133+ "ending location of the ambiguous word , true_url, context, negative_samples_urls -- "
134134 "seperated by the tabs in text form." ,
135135 default = '/Users/sevgili/Ozge-PhD/DBpedia-datasets/training-datasets/csv/negative_samples_filtered_randomly_3.tsv' )
136136
@@ -151,8 +151,8 @@ def get_parameters():
151151 "and values contain the long abstracts of each entity." ,
152152 default = '/Users/sevgili/Ozge-PhD/DBpedia-datasets/outputs/databases/long_abstracts.db' )
153153
154- parser .add_argument ('-gpu' , help = "The decision of cpu and gpu, if you would like to use gpu please give only number, e.g. -gpu 0 "
155- "(default cpu)." ,
154+ parser .add_argument ('-gpu' , help = "The decision of cpu and gpu, if you would like to use gpu please "
155+ "give only number, e.g. -gpu 0 (default cpu)." ,
156156 default = '/cpu' )
157157
158158 parser .add_argument ('-learning_rate' , help = "Learning rate for the optimizer in neural network (default 0.005)." ,
@@ -189,6 +189,9 @@ def get_parameters():
189189 parser .add_argument ('-l2_beta' , help = "The beta parameter of L2 loss (default 0, means no L2 loss!)" ,
190190 default = 0.0 , type = float )
191191
192+ parser .add_argument ('-dropout' , help = "The keep probability of dropout (default 1.0, means dropout!)" ,
193+ default = 1.0 , type = float )
194+
192195 args = parser .parse_args ()
193196
194197 device = args .gpu
@@ -207,7 +210,7 @@ def get_parameters():
207210
208211 return args .training_corpus , args .graph_embedding , args .doc2vec , args .lookup_db , args .long_abstracts_db , \
209212 args .learning_rate , args .training_epochs , args .h1_size , args .h2_size , h3_size , h4_size , h5_size ,\
210- args .h6_size , device , args .l2_beta
213+ args .h6_size , device , args .l2_beta , args . dropout
211214
212215
213216def forward_propagation (x_size , y_size , h1_size , h2_size , h3_size , h4_size , h5_size , h6_size , l2_beta ):
@@ -232,12 +235,12 @@ def forward_propagation(x_size, y_size, h1_size, h2_size, h3_size, h4_size, h5_s
232235 }
233236
234237 # Forward propagation
235- h1 = tf .add (tf .matmul (X , weights ['w1' ]), biases ['b1' ])
236- h2 = tf .add (tf .matmul (h1 , weights ['w2' ]), biases ['b2' ])
237- h3 = tf .add (tf .matmul (h2 , weights ['w3' ]), biases ['b3' ])
238- h4 = tf .add (tf .matmul (h3 , weights ['w4' ]), biases ['b4' ])
239- h5 = tf .add (tf .matmul (h4 , weights ['w5' ]), biases ['b5' ])
240- h6 = tf .add (tf .matmul (h5 , weights ['w6' ]), biases ['b6' ])
238+ h1 = tf .nn . dropout ( tf . add (tf .matmul (X , weights ['w1' ]), biases ['b1' ]), keep_prob )
239+ h2 = tf .nn . dropout ( tf . add (tf .matmul (h1 , weights ['w2' ]), biases ['b2' ]), keep_prob )
240+ h3 = tf .nn . dropout ( tf . add (tf .matmul (h2 , weights ['w3' ]), biases ['b3' ]), keep_prob )
241+ h4 = tf .nn . dropout ( tf . add (tf .matmul (h3 , weights ['w4' ]), biases ['b4' ]), keep_prob )
242+ h5 = tf .nn . dropout ( tf . add (tf .matmul (h4 , weights ['w5' ]), biases ['b5' ]), keep_prob )
243+ h6 = tf .nn . dropout ( tf . add (tf .matmul (h5 , weights ['w6' ]), biases ['b6' ]), keep_prob )
241244
242245 # if h6 is specified
243246 if h6_size :
@@ -286,17 +289,18 @@ def forward_propagation(x_size, y_size, h1_size, h2_size, h3_size, h4_size, h5_s
286289 return yhat , l2_loss
287290
288291
289- def train (sess , optimizer , loss , train_inputs , train_outputs , training_epochs ):
292+ def train (sess , optimizer , loss , train_inputs , train_outputs , training_epochs , keep_probability ):
290293 sess .run (tf .global_variables_initializer ())
291294
292295 for epoch in range (training_epochs ):
293- _ , current_loss = sess .run ([optimizer , loss ], feed_dict = {X : train_inputs , y : train_outputs })
296+ _ , current_loss = sess .run ([optimizer , loss ], feed_dict = {X : train_inputs ,
297+ y : train_outputs , keep_prob :keep_probability })
294298
295299
296300if __name__ == "__main__" :
297301 training_corpus , path_graphembed , path_doc2vec , path_lookupdb , path_longabsdb , \
298302 learning_rate , training_epochs , h1_size , h2_size , h3_size , h4_size , h5_size , h6_size , \
299- device , l2_beta = get_parameters ()
303+ device , l2_beta , dropout = get_parameters ()
300304
301305 print ('hidden sizes:' , h1_size , h2_size , h3_size , h4_size , h5_size , h6_size )
302306
@@ -314,6 +318,7 @@ def train(sess, optimizer, loss, train_inputs, train_outputs, training_epochs):
314318 # Symbols
315319 X = tf .placeholder (tf .float32 , shape = [None , x_size ])
316320 y = tf .placeholder (tf .float32 , shape = [None , y_size ])
321+ keep_prob = tf .placeholder (tf .float32 )
317322
318323 # Forward propagation
319324 yhat , l2_loss = forward_propagation (x_size , y_size , h1_size , h2_size , h3_size , h4_size , h5_size , h6_size , l2_beta )
@@ -370,7 +375,7 @@ def train(sess, optimizer, loss, train_inputs, train_outputs, training_epochs):
370375 val_outputs = outputs [val_id ]
371376
372377 train (sess = sess , optimizer = optimizer , loss = loss , train_inputs = train_inputs ,
373- train_outputs = train_outputs , training_epochs = training_epochs )
378+ train_outputs = train_outputs , training_epochs = training_epochs , keep_probability = dropout )
374379
375380 '''
376381 for epoch in range(training_epochs):
@@ -385,10 +390,10 @@ def train(sess, optimizer, loss, train_inputs, train_outputs, training_epochs):
385390 summary_str = sess.run(train_summary_op, feed_dict={X: train_inputs, y: train_outputs})
386391 summary_writer.add_summary(summary_str, epoch)
387392 '''
388- cv_accuracy , cv_precision , cv_recall , cv_f1 = accuracy .eval ({X : val_inputs , y : val_outputs }), \
389- precision .eval ({X : val_inputs , y : val_outputs }), \
390- recall .eval ({X : val_inputs , y : val_outputs }), \
391- f1 .eval ({X : val_inputs , y : val_outputs })
393+ cv_accuracy , cv_precision , cv_recall , cv_f1 = accuracy .eval ({X : val_inputs , y : val_outputs , keep_prob : 1 }), \
394+ precision .eval ({X : val_inputs , y : val_outputs , keep_prob : 1 }), \
395+ recall .eval ({X : val_inputs , y : val_outputs , keep_prob : 1 }), \
396+ f1 .eval ({X : val_inputs , y : val_outputs , keep_prob : 1 })
392397 cv_accuracies .append (cv_accuracy )
393398 cv_precisions .append (cv_precision )
394399 cv_recalls .append (cv_recall )
@@ -401,24 +406,25 @@ def train(sess, optimizer, loss, train_inputs, train_outputs, training_epochs):
401406
402407 # Train model
403408 train (sess = sess , optimizer = optimizer , loss = loss , train_inputs = inputs ,
404- train_outputs = outputs , training_epochs = training_epochs )
409+ train_outputs = outputs , training_epochs = training_epochs , keep_probability = dropout )
405410
406411 # Test model
407412 pred = tf .nn .sigmoid (yhat )
408413 correct_prediction = tf .equal (tf .round (pred ), y )
409414
410415 print ("Training Report" , classification_report (y_true = outputs ,
411- y_pred = tf .round (pred ).eval ({X : inputs , y : outputs })))
416+ y_pred = tf .round (pred ).eval ({X : inputs , y : outputs , keep_prob : dropout })))
412417
413418 print ("Test Report" , classification_report (y_true = test_outputs ,
414- y_pred = tf .round (pred ).eval ({X : test_inputs , y : test_outputs })))
419+ y_pred = tf .round (pred ).eval ({X : test_inputs , y : test_outputs , keep_prob : 1 })))
415420
416421 accuracy = tf .reduce_mean (tf .cast (correct_prediction , tf .float32 ))
417- print ("Training Accuracy:" , accuracy .eval ({X : inputs , y : outputs }))
418- print ("Training Precision:" , precision .eval ({X : inputs , y : outputs }),
419- "Training Recall:" , recall .eval ({X : inputs , y : outputs }), "Training F1:" , f1 .eval ({X : inputs , y : outputs }))
420-
421- print ("Test Accuracy:" , accuracy .eval ({X : test_inputs , y : test_outputs }))
422- print ("Test Precision:" , precision .eval ({X : test_inputs , y : test_outputs }),
423- "Test Recall:" , recall .eval ({X : test_inputs , y : test_outputs }), " Test F1:" ,
424- f1 .eval ({X : test_inputs , y : test_outputs }))
422+ print ("Training Accuracy:" , accuracy .eval ({X : inputs , y : outputs , keep_prob :dropout }))
423+ print ("Training Precision:" , precision .eval ({X : inputs , y : outputs , keep_prob :dropout }),
424+ "Training Recall:" , recall .eval ({X : inputs , y : outputs , keep_prob :dropout }),
425+ "Training F1:" , f1 .eval ({X : inputs , y : outputs , keep_prob :dropout }))
426+
427+ print ("Test Accuracy:" , accuracy .eval ({X : test_inputs , y : test_outputs , keep_prob :1 }))
428+ print ("Test Precision:" , precision .eval ({X : test_inputs , y : test_outputs , keep_prob :1 }),
429+ "Test Recall:" , recall .eval ({X : test_inputs , y : test_outputs , keep_prob :1 }), " Test F1:" ,
430+ f1 .eval ({X : test_inputs , y : test_outputs , keep_prob :1 }))
0 commit comments