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.

55 lines
1.7 KiB

4 years ago
  1. import time
  2. import argparse
  3. import torch
  4. import sklearn
  5. import numpy as np
  6. import dataset
  7. import util
  8. if __name__ == "__main__":
  9. parser = argparse.ArgumentParser()
  10. parser.add_argument('--model', choices=['intent', 'slot', 'joint'])
  11. parser.add_argument('--dataset', choices=['atis', 'snips'])
  12. parser.add_argument('--filters', type=int)
  13. parser.add_argument('--runs', type=int, default=15)
  14. parser.add_argument('--gpu', action="store_true") # pass for CUDA, otherwise will run on CPU
  15. args = parser.parse_args()
  16. args.dropout = 0
  17. train, valid, test, num_words, num_intent, num_slot, wordvecs = dataset.load(args.dataset, batch_size=8, seq_len=50)
  18. # change the model below
  19. for sparsity in [0, 20, 40, 60, 80, 90, 95, 99]:
  20. filters = 300 - 300 * (sparsity / 100)
  21. model = util.load_model(args.model, num_words, num_intent, num_slot, args.dropout, wordvecs, 100, int(filters))
  22. if args.gpu:
  23. model = model.cuda()
  24. print(f"sparsity {sparsity}, params {sum(p.numel() for p in model.parameters() if p.requires_grad)}")
  25. times = []
  26. criterion = torch.nn.CrossEntropyLoss(ignore_index=-1)
  27. for i in range(args.runs):
  28. start_time = time.time()
  29. if args.model == 'intent':
  30. util.valid_intent(model, test, criterion, args.gpu)
  31. elif args.model == 'slot':
  32. util.valid_slot(model, test, criterion, args.gpu)
  33. elif args.model == 'joint':
  34. util.valid_joint(model, test, criterion, args.gpu, 0.2)
  35. end_time = time.time()
  36. elapsed_time = end_time - start_time
  37. times.append(elapsed_time)
  38. print(f"mean: {sum(times) / float(len(times)):.5f} sec, std deviation: {np.std(times):.5f}")