Register
Login
Resources
Docs Blog Datasets Glossary Case Studies Tutorials & Webinars
Product
Data Engine LLMs Platform Enterprise
Pricing Explore
Connect to our Discord channel

test_label_smoothing.py 4.1 KB

You have to be logged in to leave a comment. Sign In
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
  1. # Copyright (c) 2017-present, Facebook, Inc.
  2. # All rights reserved.
  3. #
  4. # This source code is licensed under the license found in the LICENSE file in
  5. # the root directory of this source tree. An additional grant of patent rights
  6. # can be found in the PATENTS file in the same directory.
  7. import argparse
  8. import copy
  9. import unittest
  10. import torch
  11. from fairseq.criterions.cross_entropy import CrossEntropyCriterion
  12. from fairseq.criterions.label_smoothed_cross_entropy import LabelSmoothedCrossEntropyCriterion
  13. import tests.utils as test_utils
  14. class TestLabelSmoothing(unittest.TestCase):
  15. def setUp(self):
  16. # build dictionary
  17. self.d = test_utils.dummy_dictionary(3)
  18. vocab = len(self.d)
  19. self.assertEqual(vocab, 4 + 3) # 4 special + 3 tokens
  20. self.assertEqual(self.d.pad(), 1)
  21. self.assertEqual(self.d.eos(), 2)
  22. self.assertEqual(self.d.unk(), 3)
  23. pad, eos, unk, w1, w2, w3 = 1, 2, 3, 4, 5, 6 # noqa: F841
  24. # build dataset
  25. self.data = [
  26. # the first batch item has padding
  27. {'source': torch.LongTensor([w1, eos]), 'target': torch.LongTensor([w1, eos])},
  28. {'source': torch.LongTensor([w1, eos]), 'target': torch.LongTensor([w1, w1, eos])},
  29. ]
  30. self.sample = next(test_utils.dummy_dataloader(self.data))
  31. # build model
  32. self.args = argparse.Namespace()
  33. self.args.sentence_avg = False
  34. self.args.probs = torch.FloatTensor([
  35. # pad eos unk w1 w2 w3
  36. [0.05, 0.05, 0.1, 0.05, 0.3, 0.4, 0.05],
  37. [0.05, 0.10, 0.2, 0.05, 0.2, 0.3, 0.10],
  38. [0.05, 0.15, 0.3, 0.05, 0.1, 0.2, 0.15],
  39. ]).unsqueeze(0).expand(2, 3, 7) # add batch dimension
  40. self.model = test_utils.TestModel.build_model(self.args, self.d, self.d)
  41. def test_nll_loss(self):
  42. self.args.label_smoothing = 0.1
  43. nll_crit = CrossEntropyCriterion(self.args, self.d, self.d)
  44. smooth_crit = LabelSmoothedCrossEntropyCriterion(self.args, self.d, self.d)
  45. nll_loss, nll_sample_size, nll_logging_output = nll_crit(self.model, self.sample)
  46. smooth_loss, smooth_sample_size, smooth_logging_output = smooth_crit(self.model, self.sample)
  47. self.assertLess(abs(nll_loss - nll_logging_output['loss']), 1e-6)
  48. self.assertLess(abs(nll_loss - smooth_logging_output['nll_loss']), 1e-6)
  49. def test_padding(self):
  50. self.args.label_smoothing = 0.1
  51. crit = LabelSmoothedCrossEntropyCriterion(self.args, self.d, self.d)
  52. loss, _, logging_output = crit(self.model, self.sample)
  53. def get_one_no_padding(idx):
  54. # create a new sample with just a single batch item so that there's
  55. # no padding
  56. sample1 = next(test_utils.dummy_dataloader([self.data[idx]]))
  57. args1 = copy.copy(self.args)
  58. args1.probs = args1.probs[idx, :, :].unsqueeze(0)
  59. model1 = test_utils.TestModel.build_model(args1, self.d, self.d)
  60. loss1, _, _ = crit(model1, sample1)
  61. return loss1
  62. loss1 = get_one_no_padding(0)
  63. loss2 = get_one_no_padding(1)
  64. self.assertAlmostEqual(loss, loss1 + loss2)
  65. def test_reduction(self):
  66. self.args.label_smoothing = 0.1
  67. crit = LabelSmoothedCrossEntropyCriterion(self.args, self.d, self.d)
  68. loss, _, logging_output = crit(self.model, self.sample, reduce=True)
  69. unreduced_loss, _, _ = crit(self.model, self.sample, reduce=False)
  70. self.assertAlmostEqual(loss, unreduced_loss.sum())
  71. def test_zero_eps(self):
  72. self.args.label_smoothing = 0.0
  73. nll_crit = CrossEntropyCriterion(self.args, self.d, self.d)
  74. smooth_crit = LabelSmoothedCrossEntropyCriterion(self.args, self.d, self.d)
  75. nll_loss, nll_sample_size, nll_logging_output = nll_crit(self.model, self.sample)
  76. smooth_loss, smooth_sample_size, smooth_logging_output = smooth_crit(self.model, self.sample)
  77. self.assertAlmostEqual(nll_loss, smooth_loss)
  78. def assertAlmostEqual(self, t1, t2):
  79. self.assertEqual(t1.size(), t2.size(), "size mismatch")
  80. self.assertLess((t1 - t2).abs().max(), 1e-6)
  81. if __name__ == '__main__':
  82. unittest.main()
Tip!

Press p or to see the previous file or, n or to see the next file

Comments

Loading...