1- using System ;
1+ using NumSharp ;
2+ using System ;
23using System . Collections . Generic ;
34using System . IO ;
45using System . Linq ;
78using Tensorflow . Estimator ;
89using TensorFlowNET . Examples . Utility ;
910using static Tensorflow . Python ;
11+ using static TensorFlowNET . Examples . DataHelpers ;
1012
1113namespace TensorFlowNET . Examples . Text . NER
1214{
@@ -27,17 +29,32 @@ public class LstmCrfNer : IExample
2729
2830 HyperParams hp ;
2931
30- Dictionary < string , int > vocab_tags = new Dictionary < string , int > ( ) ;
3132 int nwords , nchars , ntags ;
3233 CoNLLDataset dev , train ;
3334
35+ Tensor word_ids_tensor ;
36+ Tensor sequence_lengths_tensor ;
37+ Tensor char_ids_tensor ;
38+ Tensor word_lengths_tensor ;
39+ Tensor labels_tensor ;
40+ Tensor dropout_tensor ;
41+ Tensor lr_tensor ;
42+
3443 public bool Run ( )
3544 {
3645 PrepareData ( ) ;
3746 var graph = tf . Graph ( ) . as_default ( ) ;
3847
3948 tf . train . import_meta_graph ( "graph/lstm_crf_ner.meta" ) ;
4049
50+ word_ids_tensor = graph . OperationByName ( "word_ids" ) ;
51+ sequence_lengths_tensor = graph . OperationByName ( "sequence_lengths" ) ;
52+ char_ids_tensor = graph . OperationByName ( "char_ids" ) ;
53+ word_lengths_tensor = graph . OperationByName ( "word_lengths" ) ;
54+ labels_tensor = graph . OperationByName ( "labels" ) ;
55+ dropout_tensor = graph . OperationByName ( "dropout" ) ;
56+ lr_tensor = graph . OperationByName ( "lr" ) ;
57+
4158 var init = tf . global_variables_initializer ( ) ;
4259
4360 with ( tf . Session ( ) , sess =>
@@ -47,13 +64,85 @@ public bool Run()
4764 foreach ( var epoch in range ( hp . epochs ) )
4865 {
4966 print ( $ "Epoch { epoch + 1 } out of { hp . epochs } ") ;
67+ run_epoch ( train , dev , epoch ) ;
5068 }
5169
5270 } ) ;
5371
5472 return true ;
5573 }
5674
75+ private void run_epoch ( CoNLLDataset train , CoNLLDataset dev , int epoch )
76+ {
77+ int i = 0 ;
78+ // iterate over dataset
79+ var batches = minibatches ( train , hp . batch_size ) ;
80+ foreach ( var ( words , labels ) in batches )
81+ {
82+ get_feed_dict ( words , labels , hp . lr , hp . dropout ) ;
83+ }
84+ }
85+
86+ private IEnumerable < ( ( int [ ] [ ] , int [ ] ) [ ] , int [ ] [ ] ) > minibatches ( CoNLLDataset data , int minibatch_size )
87+ {
88+ var x_batch = new List < ( int [ ] [ ] , int [ ] ) > ( ) ;
89+ var y_batch = new List < int [ ] > ( ) ;
90+ foreach ( var ( x , y ) in data . GetItems ( ) )
91+ {
92+ if ( len ( y_batch ) == minibatch_size )
93+ {
94+ yield return ( x_batch . ToArray ( ) , y_batch . ToArray ( ) ) ;
95+ x_batch . Clear ( ) ;
96+ y_batch . Clear ( ) ;
97+ }
98+
99+ var x3 = ( x . Select ( x1 => x1 . Item1 ) . ToArray ( ) , x . Select ( x2 => x2 . Item2 ) . ToArray ( ) ) ;
100+ x_batch . Add ( x3 ) ;
101+ y_batch . Add ( y ) ;
102+ }
103+
104+ if ( len ( y_batch ) > 0 )
105+ yield return ( x_batch . ToArray ( ) , y_batch . ToArray ( ) ) ;
106+ }
107+
108+ /// <summary>
109+ /// Given some data, pad it and build a feed dictionary
110+ /// </summary>
111+ /// <param name="words">
112+ /// list of sentences. A sentence is a list of ids of a list of
113+ /// words. A word is a list of ids
114+ /// </param>
115+ /// <param name="labels">list of ids</param>
116+ /// <param name="lr">learning rate</param>
117+ /// <param name="dropout">keep prob</param>
118+ private FeedItem [ ] get_feed_dict ( ( int [ ] [ ] , int [ ] ) [ ] words , int [ ] [ ] labels , float lr = 0f , float dropout = 0f )
119+ {
120+ int [ ] sequence_lengths ;
121+ int [ ] [ ] word_lengths ;
122+ int [ ] [ ] word_ids ;
123+ int [ ] [ ] [ ] char_ids ;
124+
125+ if ( true ) // use_chars
126+ {
127+ ( char_ids , word_ids ) = ( words . Select ( x => x . Item1 ) . ToArray ( ) , words . Select ( x => x . Item2 ) . ToArray ( ) ) ;
128+ ( word_ids , sequence_lengths ) = pad_sequences ( word_ids , pad_tok : 0 ) ;
129+ ( char_ids , word_lengths ) = pad_sequences ( char_ids , pad_tok : 0 ) ;
130+ }
131+
132+ // build feed dictionary
133+ var feeds = new List < FeedItem > ( ) ;
134+ feeds . Add ( new FeedItem ( word_ids_tensor , np . array ( word_ids ) ) ) ;
135+ feeds . Add ( new FeedItem ( sequence_lengths_tensor , np . array ( sequence_lengths ) ) ) ;
136+
137+ if ( true ) // use_chars
138+ {
139+ feeds . Add ( new FeedItem ( char_ids_tensor , np . array ( char_ids ) ) ) ;
140+ feeds . Add ( new FeedItem ( word_lengths_tensor , np . array ( word_lengths ) ) ) ;
141+ }
142+
143+ throw new NotImplementedException ( "get_feed_dict" ) ;
144+ }
145+
57146 public void PrepareData ( )
58147 {
59148 hp = new HyperParams ( "LstmCrfNer" )
0 commit comments