(data_source)
| 141 | |
| 142 | |
| 143 | def 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 | |
| 163 | def train(): |
no test coverage detected