MCPcopy Create free account
hub / github.com/pytorch/examples / evaluate

Function evaluate

word_language_model/main.py:143–160  ·  view source on GitHub ↗
(data_source)

Source from the content-addressed store, hash-verified

141
142
143def evaluate(data_source):
144 # Turn on evaluation mode which disables dropout.
145 model.eval()
146 total_loss = 0.
147 ntokens = len(corpus.dictionary)
148 if args.model != 'Transformer':
149 hidden = model.init_hidden(eval_batch_size)
150 with torch.no_grad():
151 for i in range(0, data_source.size(0) - 1, args.bptt):
152 data, targets = get_batch(data_source, i)
153 if args.model == 'Transformer':
154 output = model(data)
155 output = output.view(-1, ntokens)
156 else:
157 output, hidden = model(data, hidden)
158 hidden = repackage_hidden(hidden)
159 total_loss += len(data) * criterion(output, targets).item()
160 return total_loss / (len(data_source) - 1)
161
162
163def train():

Callers 1

main.pyFile · 0.85

Calls 3

repackage_hiddenFunction · 0.85
init_hiddenMethod · 0.80
get_batchFunction · 0.70

Tested by

no test coverage detected