我们从Python开源项目中,提取了以下9个代码示例,用于说明如何使用theano.tensor.log1p()。
def _log_add(a, b): # TODO: move functions like this to utils max_ = tensor.maximum(a, b) result = (max_ + tensor.log1p(tensor.exp(a + b - 2 * max_))) return T.switch(T.isnan(result), max_, result)
def log_add(a, b): max_ = T.maximum(a, b) return (max_ + T.log1p(T.exp(a + b - 2 * max_)))
def logsumexp(x, y): max = T.switch(x > y, x, y) min = T.switch(x > y, y, x) return T.log1p(T.exp(min - max)) + max
def log1p(x): """ Elemwise log(1 + `x`). """ # see decorator for function body
def test_log_add(): m = theano.config.mode if m == 'FAST_COMPILE': m = 'FAST_RUN' m = compile.mode.get_mode(m) m = m.excluding('fusion') m = copy.copy(m) # No need to put them back as we have a new object m.check_isfinite = False # check some basic cases x = dvector() y = dvector() f = function([x, y], T.log(T.exp(x) + T.exp(y)), mode=m) f([10000], [10000]) # causes overflow if handled incorrectly assert numpy.isfinite(f([10000], [10000])) utt.assert_allclose(f([10000], [10000]), 10000 + numpy.log1p(1)) # test that it give the same result when it don't overflow f([10], [10]) # don't causes overflow utt.assert_allclose(f([10], [10]), 10 + numpy.log1p(1)) # test that it also works with more than two args, (this currently fails) x = dvector() y = dvector() f = function([x, y], T.log(T.exp(x) + T.exp(y) + T.exp(x - y) + T.exp( x + y)), mode=m) try: f([10000], [10000]) # causes overflow if handled incorrectly utt.assert_allclose(f([10000], [10000]), 20000) except utt.WrongValue: raise SkipTest("log(add(exp)) is not stabilized when adding " "more than 2 elements, see #623") # TODO: test that the optimization works in the presence of broadcasting. # TODO: (write and) test that the optimization works with Sum in addition to working with Add.
def log_add(lna, lnb): """ Compute the ln(a+b) given {lna,lnb} :param :return: ln(a+b) """ max_ = tensor.maximum(lna, lnb) result = (max_ + tensor.log1p(tensor.exp(lna + lnb - 2 * max_))) #log1p(x) = log(1+x) return tensor.switch(tensor.isnan(result), max_, result)
def apply_log_domain(self, l, probs, l_len=None, probs_mask=None): # Does the same computation as apply, but alpha is in the log domain # This avoids numerical underflow issues that were not corrected in the previous version. def _log(a): return tensor.log(tensor.clip(a, 1e-12, 1e12)) def _log_add(a, b): maximum = tensor.maximum(a, b) return (maximum + tensor.log1p(tensor.exp(a + b - 2 * maximum))) def _log_mul(a, b): return a + b # See comments above B = probs.shape[1] C = probs.shape[2]-1 L = l.shape[0] S = 2*L+1 l_blk = C * tensor.ones((S, B), dtype='int32') l_blk = tensor.set_subtensor(l_blk[1::2,:], l) l_blk = l_blk.T # now l_blk is B x S alpha0 = tensor.concatenate([ tensor.ones((B, 1)), tensor.zeros((B, S-1)) ], axis=1) alpha0 = _log(alpha0) l_blk_2 = tensor.concatenate([-tensor.ones((B,2)), l_blk[:,:-2]], axis=1) l_case2 = tensor.neq(l_blk, C) * tensor.neq(l_blk, l_blk_2) def recursion(p, p_mask, prev_alpha): prev_alpha_1 = tensor.concatenate([tensor.zeros((B,1)),prev_alpha[:,:-1]], axis=1) prev_alpha_2 = tensor.concatenate([tensor.zeros((B,2)),prev_alpha[:,:-2]], axis=1) alpha_bar1 = tensor.set_subtensor(prev_alpha[:,1:], _log_add(prev_alpha[:,1:],prev_alpha[:,:-1])) alpha_bar2 = tensor.set_subtensor(alpha_bar1[:,2:], _log_add(alpha_bar1[:,2:],prev_alpha[:,:-2])) alpha_bar = tensor.switch(l_case2, alpha_bar2, alpha_bar1) probs = _log(p[tensor.arange(B)[:,None].repeat(S,axis=1).flatten(), l_blk.flatten()].reshape((B,S))) next_alpha = _log_mul(alpha_bar, probs) next_alpha = tensor.switch(p_mask[:,None], next_alpha, prev_alpha) return next_alpha alpha, _ = scan(fn=recursion, sequences=[probs, probs_mask], outputs_info=[alpha0]) last_alpha = alpha[-1] # last_alpha = theano.printing.Print('a-1')(last_alpha) prob = _log_add(last_alpha[tensor.arange(B), 2*l_len.astype('int32')-1], last_alpha[tensor.arange(B), 2*l_len.astype('int32')]) # return the negative log probability of the labellings return -prob
def test_log1p(): m = theano.config.mode if m == 'FAST_COMPILE': m = 'FAST_RUN' m = compile.mode.get_mode(m) m = m.excluding('fusion') # check some basic cases x = dvector() f = function([x], T.log(1 + (x)), mode=m) assert [node.op for node in f.maker.fgraph.toposort()] == [T.log1p] f = function([x], T.log(1 + (-x)), mode=m) assert [node.op for node in f.maker.fgraph.toposort()] == [ T.neg, inplace.log1p_inplace] f = function([x], -T.log(1 + (-x)), mode=m) assert [node.op for node in f.maker.fgraph.toposort()] == [ T.neg, inplace.log1p_inplace, inplace.neg_inplace] # check trickier cases (and use different dtype) y = fmatrix() f = function([x, y], T.log(tensor.fill(y, 1) + (x)), mode=m) # the first three ops are Shape_i, Shape_i, and Dimshuffle assert [node.op for node in f.maker.fgraph.toposort()][3:] == [ T.log1p, tensor.alloc] f = function([x, y], T.log(0 + (x) + tensor.fill(y, 1.0)), mode=m) assert [node.op for node in f.maker.fgraph.toposort()][3:] == [ T.log1p, tensor.alloc] f = function([x, y], T.log(2 + (x) - tensor.fill(y, 1.0)), mode=m) assert [node.op for node in f.maker.fgraph.toposort()][3:] \ == [T.log1p, tensor.alloc] f([1e-7, 10], [[0, 0], [0, 0]]) # debugmode will verify values if 0: # at one point this worked, but it has been broken since # the constant up-casting made 1 -> 1.0+0.0j # I was never sure if this optimization should work on complex numbers or not. z = tensor.zmatrix() f = function([z], T.log(1 + (z)), mode=m) assert [node.op for node in f.maker.fgraph.toposort()] == [T.log1p] if 1: # should work for int z = tensor.imatrix() f = function([z], T.log(1 + (z)), mode=m) assert [node.op for node in f.maker.fgraph.toposort()] == [T.log1p]