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
101
  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.task = test_utils.TestTranslationTask.setup_task(self.args, self.d, self.d)
  41. self.model = self.task.build_model(self.args)
  42. def test_nll_loss(self):
  43. self.args.label_smoothing = 0.1
  44. nll_crit = CrossEntropyCriterion(self.args, self.task)
  45. smooth_crit = LabelSmoothedCrossEntropyCriterion(self.args, self.task)
  46. nll_loss, nll_sample_size, nll_logging_output = nll_crit(self.model, self.sample)
  47. smooth_loss, smooth_sample_size, smooth_logging_output = smooth_crit(self.model, self.sample)
  48. self.assertLess(abs(nll_loss - nll_logging_output['loss']), 1e-6)
  49. self.assertLess(abs(nll_loss - smooth_logging_output['nll_loss']), 1e-6)
  50. def test_padding(self):
  51. self.args.label_smoothing = 0.1
  52. crit = LabelSmoothedCrossEntropyCriterion(self.args, self.task)
  53. loss, _, logging_output = crit(self.model, self.sample)
  54. def get_one_no_padding(idx):
  55. # create a new sample with just a single batch item so that there's
  56. # no padding
  57. sample1 = next(test_utils.dummy_dataloader([self.data[idx]]))
  58. args1 = copy.copy(self.args)
  59. args1.probs = args1.probs[idx, :, :].unsqueeze(0)
  60. model1 = self.task.build_model(args1)
  61. loss1, _, _ = crit(model1, sample1)
  62. return loss1
  63. loss1 = get_one_no_padding(0)
  64. loss2 = get_one_no_padding(1)
  65. self.assertAlmostEqual(loss, loss1 + loss2)
  66. def test_reduction(self):
  67. self.args.label_smoothing = 0.1
  68. crit = LabelSmoothedCrossEntropyCriterion(self.args, self.task)
  69. loss, _, logging_output = crit(self.model, self.sample, reduce=True)
  70. unreduced_loss, _, _ = crit(self.model, self.sample, reduce=False)
  71. self.assertAlmostEqual(loss, unreduced_loss.sum())
  72. def test_zero_eps(self):
  73. self.args.label_smoothing = 0.0
  74. nll_crit = CrossEntropyCriterion(self.args, self.task)
  75. smooth_crit = LabelSmoothedCrossEntropyCriterion(self.args, self.task)
  76. nll_loss, nll_sample_size, nll_logging_output = nll_crit(self.model, self.sample)
  77. smooth_loss, smooth_sample_size, smooth_logging_output = smooth_crit(self.model, self.sample)
  78. self.assertAlmostEqual(nll_loss, smooth_loss)
  79. def assertAlmostEqual(self, t1, t2):
  80. self.assertEqual(t1.size(), t2.size(), "size mismatch")
  81. self.assertLess((t1 - t2).abs().max(), 1e-6)
  82. if __name__ == '__main__':
  83. unittest.main()
Tip!

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

Comments

Loading...