def test_exponent_logarithm_operators(): a = 2 * nd.ones(shape=LARGE_X) # exponent result = nd.exp(a) assert result[-1] == 7.389056 assert result.shape == a.shape # exponent minus 1 result = nd.expm1(a) assert result[-1] == 6.389056 assert result.shape == a.shape # log2 result = nd.log2(a) assert result[-1] == 1 assert result.shape == a.shape # log10 result = nd.log10(a) assert result[-1] == 0.30103 assert result.shape == a.shape # log1p result = nd.log1p(a) assert result[-1] == 1.0986123 assert result.shape == a.shape # log result = nd.log(a) assert result[-1] == 0.6931472 assert result.shape == a.shape
def loss(x): d = x + 3 * 1.0 predict = nd.array([[0, x / d, 1 / d, 1 / d, 1 / d],]) return crit( nd.log1p(predict), nd.array([1]))
true_dist = nd.where(target_mask, nd.zeros_like(true_dist) + self.confidence, true_dist) true_dist[:, self.padding_idx] = 0 mask = nd.equal(target,self.padding_idx) if len(mask.shape) > 0: true_dist = nd.where( nd.squeeze(mask), nd.zeros_like(true_dist) ,true_dist ) self.true_dist = true_dist return self.criterion(x, true_dist.as_in_context(cfg.ctx)) if cfg.DEBUG_ON: crit = LabelSmoothing(5, 0, 0.4) predict = nd.array([[0, 0.2, 0.7, 0.1, 0], [0, 0.2, 0.7, 0.1, 0], [0, 0.2, 0.7, 0.1, 0]]) v = crit( nd.log1p(predict), nd.array([2,1,0])) # Show the target distributions expected by the system. plt.imshow(crit.true_dist.asnumpy()) if cfg.DEBUG_ON: crit = LabelSmoothing(5, 0, 0.1) def loss(x): d = x + 3 * 1.0 predict = nd.array([[0, x / d, 1 / d, 1 / d, 1 / d],]) return crit( nd.log1p(predict), nd.array([1])) plt.figure() L = [loss(x).asnumpy()[0] for x in range(1, 100)] plt.plot(np.arange(1, 100),L)