11using System ;
22using System . Collections ;
33using System . Collections . Generic ;
4+ using System . Diagnostics ;
45using System . IO ;
56using System . Linq ;
67using System . Text ;
@@ -52,7 +53,7 @@ public bool Run()
5253 protected virtual bool RunWithImportedGraph ( Session sess , Graph graph )
5354 {
5455 Console . WriteLine ( "Building dataset..." ) ;
55- var ( x , y , alphabet_size ) = DataHelpers . build_char_dataset ( "train" , model_name , CHAR_MAX_LEN , DataLimit ) ;
56+ var ( x , y , alphabet_size ) = DataHelpers . build_char_dataset ( "train" , model_name , CHAR_MAX_LEN , DataLimit = null ) ;
5657 Console . WriteLine ( "\t DONE" ) ;
5758
5859 var ( train_x , valid_x , train_y , valid_y ) = train_test_split ( x , y , test_size : 0.15f ) ;
@@ -76,12 +77,13 @@ protected virtual bool RunWithImportedGraph(Session sess, Graph graph)
7677 Tensor optimizer = graph . get_operation_by_name ( "loss/optimizer" ) ;
7778 Tensor global_step = graph . get_operation_by_name ( "global_step" ) ;
7879 Tensor accuracy = graph . get_operation_by_name ( "accuracy/accuracy" ) ;
79-
80+ var stopwatch = Stopwatch . StartNew ( ) ;
8081 int i = 0 ;
81- foreach ( var ( x_batch , y_batch ) in train_batches )
82+ foreach ( var ( x_batch , y_batch , total ) in train_batches )
8283 {
8384 i ++ ;
84- Console . WriteLine ( "Training on batch " + i ) ;
85+ var estimate = TimeSpan . FromSeconds ( ( stopwatch . Elapsed . TotalSeconds / i ) * total ) ;
86+ Console . WriteLine ( $ "Training on batch { i } /{ total } . Estimated training time: { estimate } ") ;
8587 var train_feed_dict = new Hashtable
8688 {
8789 [ model_x ] = x_batch ,
@@ -90,8 +92,7 @@ protected virtual bool RunWithImportedGraph(Session sess, Graph graph)
9092 } ;
9193 // original python:
9294 //_, step, loss = sess.run([model.optimizer, model.global_step, model.loss], feed_dict = train_feed_dict)
93- var result = sess . run ( new Tensor [ ] { optimizer , global_step , loss } , train_feed_dict ) ;
94- // exception here, loss value seems like a float[]
95+ var result = sess . run ( new ITensorOrOperation [ ] { optimizer , global_step , loss } , train_feed_dict ) ;
9596 //loss_value = result[2];
9697 var step = result [ 1 ] ;
9798 if ( step % 10 == 0 )
@@ -102,7 +103,7 @@ protected virtual bool RunWithImportedGraph(Session sess, Graph graph)
102103 // # Test accuracy with validation data for each epoch.
103104 var valid_batches = batch_iter ( valid_x , valid_y , BATCH_SIZE , 1 ) ;
104105 var ( sum_accuracy , cnt ) = ( 0 , 0 ) ;
105- foreach ( var ( valid_x_batch , valid_y_batch ) in valid_batches )
106+ foreach ( var ( valid_x_batch , valid_y_batch , total_validation_batches ) in valid_batches )
106107 {
107108 // valid_feed_dict = {
108109 // model.x: valid_x_batch,
@@ -170,16 +171,19 @@ protected virtual bool RunWithBuiltGraph(Session session, Graph graph)
170171 return ( train_x , valid_x , train_y , valid_y ) ;
171172 }
172173
173- private IEnumerable < ( NDArray , NDArray ) > batch_iter ( NDArray inputs , NDArray outputs , int batch_size , int num_epochs )
174+ private IEnumerable < ( NDArray , NDArray , int ) > batch_iter ( NDArray inputs , NDArray outputs , int batch_size , int num_epochs )
174175 {
175- var num_batches_per_epoch = ( len ( inputs ) - 1 ) ; // batch_size + 1
176+ var num_batches_per_epoch = ( len ( inputs ) - 1 ) / batch_size ;
177+ var total_batches = num_batches_per_epoch * num_epochs ;
176178 foreach ( var epoch in range ( num_epochs ) )
177179 {
178180 foreach ( var batch_num in range ( num_batches_per_epoch ) )
179181 {
180182 var start_index = batch_num * batch_size ;
181183 var end_index = Math . Min ( ( batch_num + 1 ) * batch_size , len ( inputs ) ) ;
182- yield return ( inputs [ new Slice ( start_index , end_index ) ] , outputs [ new Slice ( start_index , end_index ) ] ) ;
184+ if ( end_index <= start_index )
185+ break ;
186+ yield return ( inputs [ new Slice ( start_index , end_index ) ] , outputs [ new Slice ( start_index , end_index ) ] , total_batches ) ;
183187 }
184188 }
185189 }
0 commit comments