You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

test_loss.py 6.7 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. # -*- coding: utf-8 -*-
  2. import numpy as np
  3. import pytest
  4. import megengine.functional as F
  5. import megengine.tensor as Tensor
  6. def test_cross_entropy_with_logits():
  7. data = Tensor([[0, 50], [0, -150]]).astype(np.float32)
  8. label = Tensor([1, 0]).astype(np.int32)
  9. loss = F.nn.cross_entropy(data, label)
  10. np.testing.assert_allclose(loss.numpy(), 0.0)
  11. label = Tensor([0, 1]).astype(np.int32)
  12. loss = F.nn.cross_entropy(data, label)
  13. np.testing.assert_allclose(loss.numpy(), 100)
  14. label = np.array([1, 0])
  15. loss = F.nn.cross_entropy(data, label)
  16. np.testing.assert_allclose(loss.numpy(), 0.0)
  17. def test_cross_entropy():
  18. def softmax(x):
  19. x = np.exp(x)
  20. x /= x.sum(1, keepdims=True)
  21. return x
  22. def ref(x, y):
  23. return np.mean([-np.log(x[i, y[i]]) for i in range(len(y))])
  24. x = (np.random.rand(5, 10) - 0.5) * 4
  25. y = np.random.randint(10, size=(5,))
  26. for i in range(len(x)):
  27. x[i, y[i]] += np.random.rand() * 2
  28. x = softmax(x)
  29. l_ref = ref(x, y)
  30. l = F.nn.cross_entropy(Tensor(x, "float32"), Tensor(y, "int32"), with_logits=False)
  31. np.testing.assert_allclose(l.numpy(), l_ref, 1e-6, 1e-6)
  32. l1 = F.nn.cross_entropy(
  33. Tensor(x, "float32"), Tensor(y, "int32"), axis=-1, with_logits=False
  34. )
  35. np.testing.assert_allclose(l1.numpy(), l_ref, 1e-6, 1e-6)
  36. def test_cross_entropy_reduction():
  37. logits = np.random.randn(16, 10)
  38. label = np.random.randint(10, size=[16])
  39. logits = Tensor(logits, dtype="float32")
  40. label = Tensor(label, dtype="int32")
  41. perm = np.random.permutation(16)
  42. logits_perm = Tensor(logits[perm], dtype="float32")
  43. label_perm = Tensor(label[perm], dtype="int32")
  44. loss = F.nn.cross_entropy(logits, label, reduction="none")
  45. loss_perm = F.nn.cross_entropy(logits_perm, label_perm, reduction="none")
  46. np.testing.assert_allclose(loss.numpy()[perm], loss_perm.numpy())
  47. loss_sum = F.nn.cross_entropy(logits, label, reduction="sum")
  48. np.testing.assert_allclose(loss.numpy().sum(), loss_sum.numpy(), rtol=2e-7)
  49. loss_mean = F.nn.cross_entropy(logits, label, reduction="mean")
  50. np.testing.assert_allclose(loss_mean.numpy(), loss_sum.numpy() / 16)
  51. loss_ls = F.nn.cross_entropy(logits, label, reduction="mean", label_smooth=0.1)
  52. loss_ls_none_reduce = F.nn.cross_entropy(
  53. logits, label, reduction="none", label_smooth=0.1
  54. )
  55. np.testing.assert_allclose(
  56. loss_ls.numpy(), loss_ls_none_reduce.numpy().mean(), rtol=2e-7
  57. )
  58. with pytest.raises(ValueError):
  59. F.nn.cross_entropy(logits, label, reduction="MEAN")
  60. with pytest.raises(ValueError):
  61. F.nn.cross_entropy(logits, label, reduction="max")
  62. def ctc_nll_naive_npy(
  63. pred,
  64. pred_lengths,
  65. label,
  66. label_lengths,
  67. blank=0,
  68. reduction="mean",
  69. time_major=False,
  70. ):
  71. """naive :func:`ctc_nll` using numpy arrays. Used for testing and helping
  72. our user to understand how CTC works. Only ``LABEL_COMPACT`` mode is
  73. supported."""
  74. pred = np.asarray(pred, dtype=np.float32)
  75. pred_lengths = np.asarray(pred_lengths, dtype=np.int8)
  76. label = np.asarray(label, dtype=np.int32)
  77. label_lengths = np.asarray(label_lengths, dtype=np.int32)
  78. if time_major:
  79. pred = np.transpose(pred, (1, 0, 2))
  80. # pred in (N, T, P) format
  81. batch_size, time_len, nr_class = pred.shape
  82. assert pred_lengths.shape == (batch_size,) and pred_lengths.max() <= pred.shape[1]
  83. assert label_lengths.shape == (batch_size,)
  84. assert label.shape == (label_lengths.sum(),) and label.max() < nr_class
  85. ret = np.empty((batch_size,), dtype=np.float32)
  86. label_start = 0
  87. for i in range(batch_size):
  88. label_end = label_start + label_lengths[i]
  89. ret[i] = _ctc_npy_single_seq(
  90. pred[i][: pred_lengths[i]], label[label_start:label_end], blank
  91. )
  92. label_start = label_end
  93. if reduction == "mean":
  94. return (ret / label_lengths).mean()
  95. elif reduction == "sum":
  96. return ret.sum()
  97. elif reduction == "none":
  98. return ret
  99. else:
  100. raise ValueError("{} is not a valid value for reduction".format(reduction))
  101. def _ctc_npy_single_seq(pred, label, blank):
  102. def safelog(x):
  103. eps = np.finfo(x.dtype).tiny
  104. return np.log(np.maximum(x, eps))
  105. def log_sum_exp(x, y):
  106. x, y = np.maximum(x, y), np.minimum(x, y)
  107. return x + np.log1p(np.exp(y - x))
  108. len_pred, alphabet_size = pred.shape
  109. (len_label,) = label.shape
  110. len_ex_label = len_label * 2 + 1
  111. ex_label = (np.zeros(len_ex_label)).astype(np.int32) + blank
  112. ex_label[1::2] = label
  113. prob = np.zeros(len_ex_label, dtype=np.float32)
  114. prob[0] = pred[0][ex_label[0]]
  115. prob[1] = pred[0][ex_label[1]]
  116. prob = safelog(prob) # compute on log scale
  117. ex_label_pmask = ex_label[2:] != ex_label[:-2]
  118. for t in range(1, len_pred):
  119. # enter loop: prob[i] = log(p(pred[:t+1], label[:i+1]))
  120. new_prob = prob.copy()
  121. new_prob[1:] = log_sum_exp(new_prob[1:], prob[:-1])
  122. new_prob[2:] = (
  123. new_prob[2:] * (1 - ex_label_pmask)
  124. + log_sum_exp(new_prob[2:], prob[:-2]) * ex_label_pmask
  125. )
  126. new_prob += safelog(pred[t, ex_label])
  127. prob = new_prob
  128. return -log_sum_exp(prob[-1], prob[-2])
  129. def test_ctc_loss():
  130. def test_func(T, C, N):
  131. input = np.random.randn(T, N, C)
  132. input = F.softmax(Tensor(input), axis=-1).numpy()
  133. # replace nan to 0.2
  134. input = np.nan_to_num(input, copy=True, nan=0.2)
  135. input_lengths = np.ones(N, dtype=np.int32) * T
  136. target_lengths = np.random.randint(low=1, high=T + 1, size=(N,), dtype=np.int32)
  137. target = np.random.randint(
  138. low=1, high=C, size=(sum(target_lengths)), dtype=np.int32
  139. )
  140. input_mge = Tensor(input)
  141. input_lengths_mge = Tensor(input_lengths)
  142. target_mge = Tensor(target)
  143. target_lengths_mge = Tensor(target_lengths)
  144. blank = np.random.randint(C)
  145. for method in ["mean", "sum", "none"]:
  146. np_out = ctc_nll_naive_npy(
  147. input,
  148. input_lengths,
  149. target,
  150. target_lengths,
  151. blank=blank,
  152. reduction=method,
  153. time_major=True,
  154. )
  155. mge_out = F.nn.ctc_loss(
  156. input_mge,
  157. input_lengths_mge,
  158. target_mge,
  159. target_lengths_mge,
  160. blank=blank,
  161. reduction=method,
  162. )
  163. np.testing.assert_allclose(mge_out.numpy(), np_out, rtol=2e-6)
  164. cases = [[1, 2, 1], [100, 50, 200], [100, 5, 1]]
  165. for case in cases:
  166. test_func(*case)