88from config import config
99from itertools import islice
1010#from scipy.sparse import csr_matrix
11- from util import data_generator , data_generator_tst
11+ from util import data_generator_ss , data_generator_tst
1212
1313## Training Params
1414def main ():
@@ -36,23 +36,28 @@ def main():
3636 x_idxs = tf .placeholder (tf .int64 , shape = [None ,2 ])
3737 x_vals = tf .placeholder (tf .float32 , shape = [None ])
3838 x = tf .SparseTensor (x_idxs , x_vals , [batch_size ,feature_dim ])
39- y = tf .placeholder (tf .float32 , shape = [None ,n_classes ])
39+ y = tf .placeholder (tf .float32 , shape = [None ,max_label ])
4040 #
4141 W1 = tf .Variable (tf .truncated_normal ([feature_dim ,hidden_dim ], stddev = 2.0 / math .sqrt (feature_dim + hidden_dim )))
4242 b1 = tf .Variable (tf .truncated_normal ([hidden_dim ], stddev = 2.0 / math .sqrt (feature_dim + hidden_dim )))
4343 layer_1 = tf .nn .relu (tf .sparse_tensor_dense_matmul (x ,W1 )+ b1 )
4444 #
45- W2 = tf .Variable (tf .truncated_normal ([hidden_dim ,n_classes ], stddev = 2.0 / math .sqrt (hidden_dim + n_classes )))
46- b2 = tf .Variable (tf .truncated_normal ([n_classes ], stddev = 2.0 / math .sqrt (n_classes + hidden_dim )))
47- logits = tf .matmul (layer_1 ,W2 )+ b2
45+ if max_label > 1 : # an extra node for padding a dummy class
46+ W2 = tf .Variable (tf .truncated_normal ([hidden_dim ,n_classes + 1 ], stddev = 2.0 / math .sqrt (hidden_dim + n_classes )))
47+ b2 = tf .Variable (tf .truncated_normal ([n_classes + 1 ], stddev = 2.0 / math .sqrt (n_classes + hidden_dim )))
48+ logits = tf .matmul (layer_1 ,W2 [:,:- 1 ])+ b2 [:- 1 ]
49+ else :
50+ W2 = tf .Variable (tf .truncated_normal ([hidden_dim ,n_classes ], stddev = 2.0 / math .sqrt (hidden_dim + n_classes )))
51+ b2 = tf .Variable (tf .truncated_normal ([n_classes ], stddev = 2.0 / math .sqrt (n_classes + hidden_dim )))
52+ logits = tf .matmul (layer_1 ,W2 )+ b2
4853 #
4954 k = 1
5055 if k == 1 :
5156 top_idxs = tf .argmax (logits , axis = 1 )
5257 else :
5358 top_idxs = tf .nn .top_k (logits , k = k , sorted = False )[1 ]
5459 #
55- loss = tf .reduce_mean (tf .nn .sampled_softmax_loss (tf .transpose (W2 ),b2 ,tf . reshape ( y ,[ - 1 , max_label ]), layer_1 ,n_samples ,n_classes ,remove_accidental_hits = False , num_true = max_label ,partition_strategy = 'div' ))
60+ loss = tf .reduce_mean (tf .nn .sampled_softmax_loss (tf .transpose (W2 ), b2 , y , layer_1 , n_samples , n_classes , remove_accidental_hits = False , num_true = max_label , partition_strategy = 'div' ))
5661 #
5762 train_step = tf .train .AdamOptimizer (lr ).minimize (loss )
5863 #
@@ -65,7 +70,7 @@ def main():
6570 sess = tf .Session (config = Config )
6671 sess .run (tf .global_variables_initializer ())
6772 #
68- training_data_generator = data_generator (train_files , batch_size , n_classes )
73+ training_data_generator = data_generator_ss (train_files , batch_size , n_classes , max_label )
6974 steps_per_epoch = n_train // batch_size
7075 n_steps = n_epochs * steps_per_epoch
7176 n_check = 500
@@ -94,7 +99,7 @@ def main():
9499 sess .run (train_step , feed_dict = {x_idxs :idxs_batch , x_vals :vals_batch , y :labels_batch })
95100 if i % steps_per_epoch == steps_per_epoch - 1 :
96101 total_time += time .time ()- begin_time
97- print ('Finished ' ,i ,' steps. Time elapsed for last 100 batches = ' ,time .time ()- begin_time )
102+ print ('Finished ' ,i ,' steps. Time elapsed for last' , i % n_check , ' batches = ' ,time .time ()- begin_time )
98103 n_steps_val = n_test // batch_size
99104 test_data_generator = data_generator_tst (test_files , batch_size )
100105 num_batches = 0
0 commit comments