(epoch, train_adj, train_fea, idx_train, val_adj=None, val_fea=None)
| 160 | |
| 161 | # define the training function. |
| 162 | def train(epoch, train_adj, train_fea, idx_train, val_adj=None, val_fea=None): |
| 163 | if val_adj is None: |
| 164 | val_adj = train_adj |
| 165 | val_fea = train_fea |
| 166 | |
| 167 | t = time.time() |
| 168 | model.train() |
| 169 | optimizer.zero_grad() |
| 170 | output = model(train_fea, train_adj) |
| 171 | # special for reddit |
| 172 | if sampler.learning_type == "inductive": |
| 173 | loss_train = F.nll_loss(output, labels[idx_train]) |
| 174 | acc_train = accuracy(output, labels[idx_train]) |
| 175 | else: |
| 176 | loss_train = F.nll_loss(output[idx_train], labels[idx_train]) |
| 177 | acc_train = accuracy(output[idx_train], labels[idx_train]) |
| 178 | |
| 179 | loss_train.backward() |
| 180 | optimizer.step() |
| 181 | train_t = time.time() - t |
| 182 | val_t = time.time() |
| 183 | # We can not apply the fastmode for the reddit dataset. |
| 184 | # if sampler.learning_type == "inductive" or not args.fastmode: |
| 185 | |
| 186 | if args.early_stopping > 0 and sampler.dataset != "reddit": |
| 187 | loss_val = F.nll_loss(output[idx_val], labels[idx_val]).item() |
| 188 | early_stopping(loss_val, model) |
| 189 | |
| 190 | if not args.fastmode: |
| 191 | # # Evaluate validation set performance separately, |
| 192 | # # deactivates dropout during validation run. |
| 193 | model.eval() |
| 194 | output = model(val_fea, val_adj) |
| 195 | loss_val = F.nll_loss(output[idx_val], labels[idx_val]).item() |
| 196 | acc_val = accuracy(output[idx_val], labels[idx_val]).item() |
| 197 | if sampler.dataset == "reddit": |
| 198 | early_stopping(loss_val, model) |
| 199 | else: |
| 200 | loss_val = 0 |
| 201 | acc_val = 0 |
| 202 | |
| 203 | if args.lradjust: |
| 204 | scheduler.step() |
| 205 | |
| 206 | val_t = time.time() - val_t |
| 207 | return (loss_train.item(), acc_train.item(), loss_val, acc_val, get_lr(optimizer), train_t, val_t) |
| 208 | |
| 209 | |
| 210 | def test(test_adj, test_fea): |
no test coverage detected