Skip to content

Commit 3208ee5

Browse files
committed
Finished NER training but missing run_evaluate function.
1 parent 664e1c7 commit 3208ee5

1 file changed

Lines changed: 30 additions & 10 deletions

File tree

‎test/TensorFlowNET.Examples/TextProcess/NER/LstmCrfNer.cs‎

Lines changed: 30 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,9 @@ public class LstmCrfNer : IExample
3939
Tensor labels_tensor;
4040
Tensor dropout_tensor;
4141
Tensor lr_tensor;
42+
Operation train_op;
43+
Tensor loss;
44+
Tensor merged;
4245

4346
public bool Run()
4447
{
@@ -47,13 +50,19 @@ public bool Run()
4750

4851
tf.train.import_meta_graph("graph/lstm_crf_ner.meta");
4952

53+
float loss_value = 0f;
54+
55+
//add_summary();
5056
word_ids_tensor = graph.OperationByName("word_ids");
5157
sequence_lengths_tensor = graph.OperationByName("sequence_lengths");
5258
char_ids_tensor = graph.OperationByName("char_ids");
5359
word_lengths_tensor = graph.OperationByName("word_lengths");
5460
labels_tensor = graph.OperationByName("labels");
5561
dropout_tensor = graph.OperationByName("dropout");
5662
lr_tensor = graph.OperationByName("lr");
63+
train_op = graph.OperationByName("train_step/Adam");
64+
loss = graph.OperationByName("Mean");
65+
//merged = graph.OperationByName("Merge/MergeSummary");
5766

5867
var init = tf.global_variables_initializer();
5968

@@ -63,24 +72,28 @@ public bool Run()
6372

6473
foreach (var epoch in range(hp.epochs))
6574
{
66-
print($"Epoch {epoch + 1} out of {hp.epochs}");
67-
run_epoch(train, dev, epoch);
75+
Console.Write($"Epoch {epoch + 1} out of {hp.epochs}, ");
76+
loss_value = run_epoch(sess, train, dev, epoch);
77+
print($"train loss: {loss_value}");
6878
}
69-
7079
});
7180

72-
return true;
81+
return loss_value < 0.1;
7382
}
7483

75-
private void run_epoch(CoNLLDataset train, CoNLLDataset dev, int epoch)
84+
private float run_epoch(Session sess, CoNLLDataset train, CoNLLDataset dev, int epoch)
7685
{
77-
int i = 0;
86+
NDArray results = null;
87+
7888
// iterate over dataset
7989
var batches = minibatches(train, hp.batch_size);
8090
foreach (var(words, labels) in batches)
8191
{
82-
get_feed_dict(words, labels, hp.lr, hp.dropout);
92+
var (fd, _) = get_feed_dict(words, labels, hp.lr, hp.dropout);
93+
results = sess.run(new ITensorOrOperation[] { train_op, loss }, feed_dict: fd);
8394
}
95+
96+
return results[1];
8497
}
8598

8699
private IEnumerable<((int[][], int[])[], int[][])> minibatches(CoNLLDataset data, int minibatch_size)
@@ -115,7 +128,7 @@ private void run_epoch(CoNLLDataset train, CoNLLDataset dev, int epoch)
115128
/// <param name="labels">list of ids</param>
116129
/// <param name="lr">learning rate</param>
117130
/// <param name="dropout">keep prob</param>
118-
private FeedItem[] get_feed_dict((int[][], int[])[] words, int[][] labels, float lr = 0f, float dropout = 0f)
131+
private (FeedItem[], int[]) get_feed_dict((int[][], int[])[] words, int[][] labels, float lr = 0f, float dropout = 0f)
119132
{
120133
int[] sequence_lengths;
121134
int[][] word_lengths;
@@ -140,14 +153,21 @@ private FeedItem[] get_feed_dict((int[][], int[])[] words, int[][] labels, float
140153
feeds.Add(new FeedItem(word_lengths_tensor, np.array(word_lengths)));
141154
}
142155

143-
throw new NotImplementedException("get_feed_dict");
156+
(labels, _) = pad_sequences(labels, 0);
157+
feeds.Add(new FeedItem(labels_tensor, np.array(labels)));
158+
159+
feeds.Add(new FeedItem(lr_tensor, lr));
160+
161+
feeds.Add(new FeedItem(dropout_tensor, dropout));
162+
163+
return (feeds.ToArray(), sequence_lengths);
144164
}
145165

146166
public void PrepareData()
147167
{
148168
hp = new HyperParams("LstmCrfNer")
149169
{
150-
epochs = 15,
170+
epochs = 50,
151171
dropout = 0.5f,
152172
batch_size = 20,
153173
lr_method = "adam",

0 commit comments

Comments
 (0)