log1pexp_to_softplus = gof.PatternSub( (tensor.log1p, (tensor.exp, "x")), (softplus, "x"), values_eq_approx=values_eq_approx_remove_inf, allow_multiple_clients=True, ) log1p_neg_sigmoid = gof.PatternSub( (tensor.log1p, (tensor.neg, (sigmoid, "x"))), (tensor.neg, (softplus, "x")), values_eq_approx=values_eq_approx_remove_inf, allow_multiple_clients=True, ) opt.register_stabilize(logsigm_to_softplus, name="logsigm_to_softplus") opt.register_stabilize(log1msigm_to_softplus, name="log1msigm_to_softplus") opt.register_stabilize(log1pexp_to_softplus, name="log1pexp_to_softplus") opt.register_stabilize(log1p_neg_sigmoid, name="log1p_neg_sigmoid,") def is_1pexp(t, only_process_constants=True): """ Returns ------- object If 't' is of the form (1+exp(x)), return (False, x). Else return None. """
log1msigm_to_softplus = gof.PatternSub( (tensor.log, (tensor.sub, dict(pattern='y', constraint=_is_1), (sigmoid, 'x'))), (tensor.neg, (softplus, 'x')), allow_multiple_clients=True, skip_identities_fn=_skip_mul_1) log1pexp_to_softplus = gof.PatternSub( (tensor.log1p, (tensor.exp, 'x')), (softplus, 'x'), allow_multiple_clients=True) opt.register_stabilize(logsigm_to_softplus, name='logsigm_to_softplus') opt.register_stabilize(log1msigm_to_softplus, name='log1msigm_to_softplus') opt.register_stabilize(log1pexp_to_softplus, name='log1pexp_to_softplus') def is_1pexp(t): """ If 't' is of the form (1+exp(x)), return (False, x). Else return None. """ if t.owner and t.owner.op == tensor.add: scalars, scalar_inputs, nonconsts = \ opt.scalarconsts_rest(t.owner.inputs) # scalar_inputs are potentially dimshuffled and fill'd scalars if len(nonconsts) == 1: maybe_exp = nonconsts[0]
v = opt.get_scalar_constant_value(expr) return numpy.allclose(v, 1) except tensor.NotScalarConstantError: return False log1msigm_to_softplus = gof.PatternSub( (tensor.log, (tensor.sub, dict(pattern="y", constraint=_is_1), (sigmoid, "x"))), (tensor.neg, (softplus, "x")), allow_multiple_clients=True, skip_identities_fn=_skip_mul_1, ) log1pexp_to_softplus = gof.PatternSub((tensor.log1p, (tensor.exp, "x")), (softplus, "x"), allow_multiple_clients=True) opt.register_stabilize(logsigm_to_softplus, name="logsigm_to_softplus") opt.register_stabilize(log1msigm_to_softplus, name="log1msigm_to_softplus") opt.register_stabilize(log1pexp_to_softplus, name="log1pexp_to_softplus") def is_1pexp(t): """ Returns ------- object If 't' is of the form (1+exp(x)), return (False, x). Else return None. """ if t.owner and t.owner.op == tensor.add:
log1pexp_to_softplus = gof.PatternSub( (tensor.log1p, (tensor.exp, 'x')), (softplus, 'x'), values_eq_approx=values_eq_approx_remove_inf, allow_multiple_clients=True) log1p_neg_sigmoid = gof.PatternSub( (tensor.log1p, (tensor.neg, (sigmoid, 'x'))), (tensor.neg, (softplus, 'x')), values_eq_approx=values_eq_approx_remove_inf, allow_multiple_clients=True) opt.register_stabilize(logsigm_to_softplus, name='logsigm_to_softplus') opt.register_stabilize(log1msigm_to_softplus, name='log1msigm_to_softplus') opt.register_stabilize(log1pexp_to_softplus, name='log1pexp_to_softplus') opt.register_stabilize(log1p_neg_sigmoid, name='log1p_neg_sigmoid,') def is_1pexp(t, only_process_constants=True): """ Returns ------- object If 't' is of the form (1+exp(x)), return (False, x). Else return None. """