MCPcopy Create free account
hub / github.com/baidu/DDParser / epoch_evaluate

Function epoch_evaluate

ddparser/parser/model.py:139–175  ·  view source on GitHub ↗

Evaluate in one epoch

(args, model, loader, puncts)

Source from the content-addressed store, hash-verified

137
138@dygraph.no_grad
139def epoch_evaluate(args, model, loader, puncts):
140 """Evaluate in one epoch"""
141 model.eval()
142 total_loss, metric = 0, Metric()
143 pad_index = args.pad_index
144 bos_index = args.bos_index
145 eos_index = args.eos_index
146
147 for batch, inputs in enumerate(loader(), start=1):
148 if args.encoding_model.startswith("ernie"):
149 words, arcs, rels = inputs
150 s_arc, s_rel, words = model(words)
151 else:
152 words, feats, arcs, rels = inputs
153 s_arc, s_rel, words = model(words, feats)
154 mask = layers.logical_and(
155 layers.logical_and(words != pad_index, words != bos_index),
156 words != eos_index,
157 )
158 loss = loss_function(s_arc, s_rel, arcs, rels, mask)
159 arc_preds, rel_preds = decode(args, s_arc, s_rel, mask)
160 # ignore all punctuation if not specified
161 if not args.punct:
162 punct_mask = layers.reduce_all(
163 layers.expand(layers.unsqueeze(words, -1),
164 (1, 1, puncts.shape[0])) != layers.expand(layers.reshape(puncts,
165 (1, 1, -1)), words.shape + [1]),
166 dim=-1)
167
168 mask = layers.logical_and(mask, punct_mask)
169
170 metric(arc_preds, rel_preds, arcs, rels, mask)
171 total_loss += loss.numpy().item()
172
173 total_loss /= len(loader)
174
175 return total_loss, metric
176
177
178@dygraph.no_grad

Callers 2

trainFunction · 0.90
evaluateFunction · 0.90

Calls 4

MetricClass · 0.90
loss_functionFunction · 0.85
decodeFunction · 0.85
evalMethod · 0.80

Tested by

no test coverage detected