make other meta-learning algorithms implemented in l2l.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

learnToLearnTest.py 2.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. import os
  2. import torch
  3. import pickle
  4. import random
  5. from options import config, states
  6. from torch.nn import functional as F
  7. from torch.nn import L1Loss
  8. import matchzoo as mz
  9. import numpy as np
  10. from fast_adapt import fast_adapt
  11. def test(embedding,head, total_dataset, batch_size, num_epoch):
  12. test_set_size = len(total_dataset)
  13. random.shuffle(total_dataset)
  14. a, b, c, d = zip(*total_dataset)
  15. losses_q = []
  16. ndcgs1 = []
  17. ndcgs3 = []
  18. for iterator in range(test_set_size):
  19. try:
  20. supp_xs = a[iterator].cuda()
  21. supp_ys = b[iterator].cuda()
  22. query_xs = c[iterator].cuda()
  23. query_ys = d[iterator].cuda()
  24. except IndexError:
  25. print("index error in test method")
  26. continue
  27. num_local_update = config['inner']
  28. learner = head.clone()
  29. temp_sxs = embedding(supp_xs)
  30. temp_qxs = embedding(query_xs)
  31. evaluation_error,predictions = fast_adapt(learner,
  32. temp_sxs,
  33. temp_qxs,
  34. supp_ys,
  35. query_ys,
  36. config['inner'],
  37. get_predictions=True
  38. )
  39. l1 = L1Loss(reduction='mean')
  40. loss_q = l1(predictions.view(-1), query_ys)
  41. # print("testing - iterator:{} - l1:{} ".format(iterator,loss_q))
  42. losses_q.append(float(loss_q))
  43. y_true = query_ys.cpu().detach().numpy()
  44. y_pred = predictions.cpu().detach().numpy()
  45. ndcgs1.append(float(mz.metrics.NormalizedDiscountedCumulativeGain(k=1)(y_true, y_pred)))
  46. ndcgs3.append(float(mz.metrics.NormalizedDiscountedCumulativeGain(k=3)(y_true, y_pred)))
  47. del supp_xs, supp_ys, query_xs, query_ys, predictions, y_true, y_pred, loss_q
  48. torch.cuda.empty_cache()
  49. # calculate metrics
  50. # print("======================================")
  51. # losses_q = torch.stack(losses_q).mean(0)
  52. losses_q = np.array(losses_q).mean()
  53. print("mean of mse: ", losses_q)
  54. # print("======================================")
  55. n1 = np.array(ndcgs1).mean()
  56. print("nDCG1: ", n1)
  57. n3 = np.array(ndcgs3).mean()
  58. print("nDCG3: ", n3)