Python chainer.functions 模块,exp() 实例源码

我们从Python开源项目中,提取了以下35个代码示例,用于说明如何使用chainer.functions.exp()

项目:chainerrl    作者:chainer    | 项目源码 | 文件源码
def __call__(self, state):
        h = state
        for layer in self.hidden_layers:
            h = F.relu(layer(h))
        v = self.v(h)
        mu = self.mu(h)

        if self.scale_mu:
            mu = scale_by_tanh(mu, high=self.action_space.high,
                               low=self.action_space.low)

        mat_diag = F.exp(self.mat_diag(h))
        if hasattr(self, 'mat_non_diag'):
            mat_non_diag = self.mat_non_diag(h)
            tril = lower_triangular_matrix(mat_diag, mat_non_diag)
            mat = matmul_v3(tril, tril, transb=True)
        else:
            mat = F.expand_dims(mat_diag ** 2, axis=2)
        return QuadraticActionValue(
            mu, mat, v, min_action=self.action_space.low,
            max_action=self.action_space.high)
项目:chainerrl    作者:chainer    | 项目源码 | 文件源码
def __call__(self, state):
        h = self.hidden_layers(state)
        v = self.v(h)
        mu = self.mu(h)

        if self.scale_mu:
            mu = scale_by_tanh(mu, high=self.action_space.high,
                               low=self.action_space.low)

        mat_diag = F.exp(self.mat_diag(h))
        if hasattr(self, 'mat_non_diag'):
            mat_non_diag = self.mat_non_diag(h)
            tril = lower_triangular_matrix(mat_diag, mat_non_diag)
            mat = matmul_v3(tril, tril, transb=True)
        else:
            mat = F.expand_dims(mat_diag ** 2, axis=2)
        return QuadraticActionValue(
            mu, mat, v, min_action=self.action_space.low,
            max_action=self.action_space.high)
项目:teras    作者:chantera    | 项目源码 | 文件源码
def __call__(self, x, hs):
        batch, dim = x.shape
        alphas = 0
        _sum = 0
        for h in F.transpose_sequence(hs[:batch]):
            size = h.shape[0]
            if size < batch:
                h = F.vstack([h, variable.Variable(
                    self.xp.zeros((batch - size, h.shape[1]), dtype='f'))])
            score = self._score_func(x, h)
            e = F.exp(score)
            _sum += e
            alphas += batch_matmul(h, e)
        c = F.reshape(batch_matmul(F.reshape(alphas, (batch, dim)),
                                   (1 / _sum)), (batch, dim))
        return c
项目:sg-mcmc-survey    作者:delta2323    | 项目源码 | 文件源码
def gaussian_likelihood(x, mu, var):
    """Returns likelihood of ``x``, or ``N(x; mu, var)``

    Args:
        x(float, numpy.ndarray or chainer.Variable): sample data
        mu(float or chainer.Variable): mean of Gaussian
        var(float): variance of Gaussian
    Returns:
        chainer.Variable: Variable holding likelihood ``N(x; mu, var)``
        whose shape is same as that of ``x``
    """

    if numpy.isscalar(x):
        x = numpy.array(x)
    if isinstance(x, numpy.ndarray):
        x = chainer.Variable(x.astype(numpy.float32))

    if numpy.isscalar(mu):
        mu = numpy.array(mu)
    if isinstance(mu, numpy.ndarray):
        mu = chainer.Variable(mu.astype(numpy.float32))

    x, mu = F.broadcast(x, mu)
    return F.exp(-(x - mu) ** 2 / var / 2) / numpy.sqrt(2 * numpy.pi * var)
项目:unrolled-gan    作者:musyoku    | 项目源码 | 文件源码
def __call__(self, x):
        xp = chainer.cuda.get_array_module(x.data)
        batchsize = x.shape[0]
        if self.train_weights == False and self.initial_T is not None:
            self.T.W.data = self.initial_T

        M = F.reshape(self.T(x), (-1, self.num_kernels, self.ndim_kernel))
        M = F.expand_dims(M, 3)
        M_T = F.transpose(M, (3, 1, 2, 0))
        M, M_T = F.broadcast(M, M_T)

        norm = F.sum(abs(M - M_T), axis=2)
        eraser = F.broadcast_to(xp.eye(batchsize, dtype=x.dtype).reshape((batchsize, 1, batchsize)), norm.shape)
        c_b = F.exp(-(norm + 1e6 * eraser))
        o_b = F.sum(c_b, axis=2)

        if self.train_weights == False:
            self.initial_T = self.T.W.data

        return F.concat((x, o_b), axis=1)
项目:chainer-cf-nade    作者:dsanno    | 项目源码 | 文件源码
def ordinal_loss(y, mask):
    xp = cuda.get_array_module(y.data)
    volatile = y.volatile
    b, c, n = y.data.shape
    max_y = F.broadcast_to(F.max(y, axis=1, keepdims=True), y.data.shape)
    y = y - max_y
    sum_y = F.broadcast_to(F.expand_dims(F.sum(y, axis=1), 1), y.data.shape)
    down_tri = np.tri(c, dtype=np.float32)
    up_tri = down_tri.T
    w1 = Variable(xp.asarray(down_tri.reshape(c, c, 1, 1)), volatile=volatile)
    w2 = Variable(xp.asarray(up_tri.reshape(c, c, 1, 1)), volatile=volatile)
    h = F.exp(F.expand_dims(y, -1))
    h1 = F.convolution_2d(h, w1)
    h1 = F.convolution_2d(F.log(h1), w1)
    h2 = F.convolution_2d(h, w2)
    h2 = F.convolution_2d(F.log(h2), w2)
    h = F.reshape(h1 + h2, (b, c, n))
    return F.sum((h - sum_y - y) * mask) / b
项目:LSGAN    作者:musyoku    | 项目源码 | 文件源码
def __call__(self, x):
        xp = chainer.cuda.get_array_module(x.data)
        batchsize = x.shape[0]
        if self.train_weights == False and self.initial_T is not None:
            self.T.W.data = self.initial_T

        M = F.reshape(self.T(x), (-1, self.num_kernels, self.ndim_kernel))
        M = F.expand_dims(M, 3)
        M_T = F.transpose(M, (3, 1, 2, 0))
        M, M_T = F.broadcast(M, M_T)

        norm = F.sum(abs(M - M_T), axis=2)
        eraser = F.broadcast_to(xp.eye(batchsize, dtype=x.dtype).reshape((batchsize, 1, batchsize)), norm.shape)
        c_b = F.exp(-(norm + 1e6 * eraser))
        o_b = F.sum(c_b, axis=2)

        if self.train_weights == False:
            self.initial_T = self.T.W.data

        return F.concat((x, o_b), axis=1)
项目:adgm    作者:musyoku    | 项目源码 | 文件源码
def __call__(self, x):
        xp = chainer.cuda.get_array_module(x.data)
        batchsize = x.shape[0]
        if self.train_weights == False and self.initial_T is not None:
            self.T.W.data = self.initial_T

        M = F.reshape(self.T(x), (-1, self.num_kernels, self.ndim_kernel))
        M = F.expand_dims(M, 3)
        M_T = F.transpose(M, (3, 1, 2, 0))
        M, M_T = F.broadcast(M, M_T)

        norm = F.sum(abs(M - M_T), axis=2)
        eraser = F.broadcast_to(xp.eye(batchsize, dtype=x.dtype).reshape((batchsize, 1, batchsize)), norm.shape)
        c_b = F.exp(-(norm + 1e6 * eraser))
        o_b = F.sum(c_b, axis=2)

        if self.train_weights == False:
            self.initial_T = self.T.W.data

        return F.concat((x, o_b), axis=1)
项目:vaelm    作者:TatsuyaShirakawa    | 项目源码 | 文件源码
def __call__(self, h, train=True, dpratio=0.5):
        h = F.dropout(h, train=train, ratio=dpratio)
        for i in range(self.num_layers):
            h = F.tanh(self.get_l(i)(h))
        return (self.lmu(h), F.exp(self.lsigma(h)))
项目:chainer-object-detection    作者:dsanno    | 项目源码 | 文件源码
def predict(self, input_x):
        if isinstance(input_x, chainer.Variable):
            device = cuda.get_device(input_x.data)
        else:
            device = cuda.get_device(input_x)
        xp = self.predictor.xp
        with device:
            output = self.predictor(input_x)
            batch_size, input_channel, input_h, input_w = input_x.shape
            batch_size, _, grid_h, grid_w = output.shape
            x, y, w, h, conf, prob = F.split_axis(F.reshape(output, (batch_size, self.predictor.n_boxes, self.predictor.n_classes+5, grid_h, grid_w)), (1, 2, 3, 4, 5), axis=2)
            x = F.sigmoid(x)
            y = F.sigmoid(y)
            conf = F.sigmoid(conf)
            prob = F.transpose(prob, (0, 2, 1, 3, 4))
            prob = F.softmax(prob)
            prob = F.transpose(prob, (0, 2, 1, 3, 4))


            # convert coordinates to those on the image
            x_shift = xp.asarray(np.broadcast_to(np.arange(grid_w, dtype=np.float32), x.shape))
            y_shift = xp.asarray(np.broadcast_to(np.arange(grid_h, dtype=np.float32).reshape(grid_h, 1), y.shape))
            w_anchor = xp.asarray(np.broadcast_to(np.reshape(np.array(self.anchors, dtype=np.float32)[:, 0], (self.predictor.n_boxes, 1, 1, 1)), w.shape))
            h_anchor = xp.asarray(np.broadcast_to(np.reshape(np.array(self.anchors, dtype=np.float32)[:, 1], (self.predictor.n_boxes, 1, 1, 1)), h.shape))
            box_x = (x + x_shift) / grid_w
            box_y = (y + y_shift) / grid_h
            box_w = F.exp(w) * w_anchor / grid_w
            box_h = F.exp(h) * h_anchor / grid_h

            return box_x, box_y, box_w, box_h, conf, prob
项目:chainer-object-detection    作者:dsanno    | 项目源码 | 文件源码
def predict(self, input_x):
        if isinstance(input_x, chainer.Variable):
            device = cuda.get_device(input_x.data)
        else:
            device = cuda.get_device(input_x)
        xp = self.predictor.xp
        with device:
            output = self.predictor(input_x)
            batch_size, input_channel, input_h, input_w = input_x.shape
            batch_size, _, grid_h, grid_w = output.shape
            x, y, w, h, conf, prob = F.split_axis(F.reshape(output, (batch_size, self.predictor.n_boxes, self.predictor.n_classes+5, grid_h, grid_w)), (1, 2, 3, 4, 5), axis=2)
            x = F.sigmoid(x)
            y = F.sigmoid(y)
            conf = F.sigmoid(conf)
            prob = F.transpose(prob, (0, 2, 1, 3, 4))
            prob = F.softmax(prob)
            prob = F.transpose(prob, (0, 2, 1, 3, 4))


            # convert coordinates to those on the image
            x_shift = xp.asarray(np.broadcast_to(np.arange(grid_w, dtype=np.float32), x.shape))
            y_shift = xp.asarray(np.broadcast_to(np.arange(grid_h, dtype=np.float32).reshape(grid_h, 1), y.shape))
            w_anchor = xp.asarray(np.broadcast_to(np.reshape(np.array(self.anchors, dtype=np.float32)[:, 0], (self.predictor.n_boxes, 1, 1, 1)), w.shape))
            h_anchor = xp.asarray(np.broadcast_to(np.reshape(np.array(self.anchors, dtype=np.float32)[:, 1], (self.predictor.n_boxes, 1, 1, 1)), h.shape))
            box_x = (x + x_shift) / grid_w
            box_y = (y + y_shift) / grid_h
            box_w = F.exp(w) * w_anchor / grid_w
            box_h = F.exp(h) * h_anchor / grid_h

            return box_x, box_y, box_w, box_h, conf, prob
项目:chainerrl    作者:chainer    | 项目源码 | 文件源码
def _lossfun(self,
                 distribs, vs_pred, log_probs,
                 vs_pred_old, target_log_probs,
                 advs, vs_teacher):
        prob_ratio = F.exp(log_probs - target_log_probs)
        ent = distribs.entropy

        prob_ratio = F.expand_dims(prob_ratio, axis=-1)
        loss_policy = - F.mean(F.minimum(
            prob_ratio * advs,
            F.clip(prob_ratio, 1-self.clip_eps, 1+self.clip_eps) * advs))

        if self.clip_eps_vf is None:
            loss_value_func = F.mean_squared_error(vs_pred, vs_teacher)
        else:
            loss_value_func = F.mean(F.maximum(
                F.square(vs_pred - vs_teacher),
                F.square(_elementwise_clip(vs_pred,
                                           vs_pred_old - self.clip_eps_vf,
                                           vs_pred_old + self.clip_eps_vf)
                         - vs_teacher)
                ))

        loss_entropy = -F.mean(ent)

        # Update stats
        self.average_loss_policy += (
            (1 - self.average_loss_decay) *
            (cuda.to_cpu(loss_policy.data) - self.average_loss_policy))
        self.average_loss_value_func += (
            (1 - self.average_loss_decay) *
            (cuda.to_cpu(loss_value_func.data) - self.average_loss_value_func))
        self.average_loss_entropy += (
            (1 - self.average_loss_decay) *
            (cuda.to_cpu(loss_entropy.data) - self.average_loss_entropy))

        return (
            loss_policy
            + self.value_func_coef * loss_value_func
            + self.entropy_coef * loss_entropy
            )
项目:chainerrl    作者:chainer    | 项目源码 | 文件源码
def prob(self, x):
        return F.exp(self.log_prob(x))
项目:dgm    作者:ashwindcruz    | 项目源码 | 文件源码
def gaussian_kl_divergence_standard(mu, ln_var):
   """D_{KL}(N(mu,var) | N(0,1))"""
   batch_size = float(mu.data.shape[0])
   S = F.exp(ln_var)
   D = mu.data.size

   KL_sum = 0.5*(F.sum(S, axis=1) + F.sum(mu*mu, axis=1) - F.sum(ln_var, axis=1) - D/batch_size)

   return KL_sum #/ batchsize
项目:dgm    作者:ashwindcruz    | 项目源码 | 文件源码
def gaussian_logp(x, mu, ln_var):
    """log N(x ; mu, var)"""
    batch_size = mu.data.shape[0]
    D = x.data.size
    S = F.exp(ln_var)
    xc = x - mu

    logp_sum = -0.5*(F.sum((xc*xc) / S, axis=1) + F.sum(ln_var, axis=1)
        + D/batch_size*math.log(2.0*math.pi))

    return logp_sum
项目:dgm    作者:ashwindcruz    | 项目源码 | 文件源码
def gaussian_kl_divergence_standard(mu, ln_var):
   """D_{KL}(N(mu,var) | N(0,1))"""
   batch_size = mu.data.shape[0]
   S = F.exp(ln_var)
   D = mu.data.size

   KL_sum = 0.5*(F.sum(S, axis=1) + F.sum(mu*mu, axis=1) - F.sum(ln_var, axis=1) - D/batch_size)

   return KL_sum #/ batchsize
项目:dgm    作者:ashwindcruz    | 项目源码 | 文件源码
def gaussian_logp(x, mu, ln_var):
    """log N(x ; mu, var)"""
    batch_size = mu.data.shape[0]
    D = x.data.size
    S = F.exp(ln_var)
    xc = x - mu

    logp_sum = -0.5*(F.sum((xc*xc) / S, axis=1) + F.sum(ln_var, axis=1)
        + D/batch_size*math.log(2.0*math.pi))


    return logp_sum / batchsize
项目:chainer-deconv    作者:germanRos    | 项目源码 | 文件源码
def test_exp_forward_cpu(self):
        self.check_forward_cpu(F.exp, numpy.exp)
项目:chainer-deconv    作者:germanRos    | 项目源码 | 文件源码
def test_exp_forward_gpu(self):
        self.check_forward_gpu(F.exp, numpy.exp)
项目:chainer-deconv    作者:germanRos    | 项目源码 | 文件源码
def test_exp_backward_cpu(self):
        self.check_backward_cpu(F.exp)
项目:chainer-deconv    作者:germanRos    | 项目源码 | 文件源码
def test_exp_backward_gpu(self):
        self.check_backward_gpu(F.exp)
项目:chainer-deconv    作者:germanRos    | 项目源码 | 文件源码
def test_exp(self):
        self.assertEqual(F.Exp().label, 'exp')
项目:nmtrain    作者:philip30    | 项目源码 | 文件源码
def decode(self):
    arr_sum = None
    a = None
    for i, model in enumerate(self.models):
      output = model.chainer_model.decode()
      output.y = F.log(output.y)
      if i == 0:
        arr_sum = output.y
        if hasattr(output, "a"): a = output.a
      else:
        arr_sum += output.y
    prob = F.exp(F.scale(arr_sum, nmtrain.environment.Variable(self.normalization_constant)))
    return nmtrain.models.decoders.Output(y=prob, a=a)
项目:RankNet    作者:szdr    | 项目源码 | 文件源码
def __call__(self, x_i, x_j, t_i, t_j):
        s_i = self.predictor(x_i)
        s_j = self.predictor(x_j)
        s_diff = s_i - s_j
        if t_i.data > t_j.data:
            S_ij = 1
        elif t_i.data < t_j.data:
            S_ij = -1
        else:
            S_ij = 0
        self.loss = (1 - S_ij) * s_diff / 2. + F.log(1 + F.exp(-s_diff))
        return self.loss
项目:vfm    作者:cemoody    | 项目源码 | 文件源码
def kl_div(mu1, lv1, lv2):
    # KL Divergence between given normal and prior at N(0, sigma_2)
    # Prior assumes mean at zero
    # lns2 - lns1 + (s2^2 + (u1 - u2)**2)/ 2s2**2 - 0.5
    if len(lv1.shape) == 2:
        lv1 = F.expand_dims(lv1, 0)
        mu1 = F.expand_dims(mu1, 0)
    lv2 = F.broadcast_to(lv2, lv1.shape)
    v12 = F.exp(lv1)**2.0
    v22 = F.exp(lv2)**2.0
    return lv2 - lv1 + .5 * v12 / v22 + .5 * mu1**2. / v22 - .5
项目:SeRanet    作者:corochann    | 项目源码 | 文件源码
def free_energy(self, v):
        """
        :param Variable (batch_size, in_channels, image_height, image_width) - input data (training data)
        :return: scalar
        """
        batch_size = v.data.shape[0]
        in_channels = self.in_channels
        real = self.real
        if real == 0:
            '''
            visible layer is 0, 1 (bit)
            vbias_term = 1 * SUM(a(i) * v(i))
            '''
            v_sum = F.sum(v, axis=(2, 3))  # sum over image_height & image_width
            # Originally, it should return sum for each batch.
            # but it returns scalar, which is sum over batches, since sum is used at the end anyway.
            vbias_term = F.sum(F.matmul(v_sum, self.conv.a))
            wx_b = self.conv(v)

        else:
            '''
            visible layer takes real value
            vbias_term = 0.5 * SUM((v(i)-a(i)) * (v(i) - a(i)))
            '''
            #TODO: check
            #m = Variable(xp.ones((batch_size, 1), dtype=xp.float32))
            n = F.reshape(self.conv.a, (1, in_channels, 1, 1))
            xp = cuda.get_array_module(n.data)
            std_ch = xp.reshape(self.std, (1, in_channels, 1, 1))

            #v_ = v - F.matmul(m, n)
            v_ = (v - F.broadcast_to(n, v.data.shape)) / std_ch
            vbias_term = F.sum(0.5 * v_ * v_)
            wx_b = self.conv(v / std_ch)


        hidden_term = F.sum(F.log(1 + F.exp(wx_b)))
        # print('vbias = ', vbias_term.data, ', hidden = ', hidden_term.data, 'F.exp(wx_b) = ', F.exp(wx_b).data)
        return - vbias_term - hidden_term
项目:SeRanet    作者:corochann    | 项目源码 | 文件源码
def sigmoid(x):
        xp = cuda.get_array_module(x.data)
        return 1. / (1 + xp.exp(-x))
项目:adgm    作者:musyoku    | 项目源码 | 文件源码
def gaussian_nll_keepbatch(self, x, mean, ln_var, clip=True):
        if clip:
            clip_min = math.log(0.01)
            clip_max = math.log(10)
            ln_var = F.clip(ln_var, clip_min, clip_max)
        x_prec = F.exp(-ln_var)
        x_diff = x - mean
        x_power = (x_diff * x_diff) * x_prec * 0.5
        # print "nll"
        # print cuda.cupy.amax(x.data), cuda.cupy.amin(x.data)
        # print cuda.cupy.amax(ln_var.data), cuda.cupy.amin(ln_var.data)
        # print cuda.cupy.amax(x_prec.data), cuda.cupy.amin(x_prec.data)
        # print cuda.cupy.amax(x_power.data), cuda.cupy.amin(x_power.data)
        return F.sum((math.log(2.0 * math.pi) + ln_var) * 0.5 + x_power, axis=1)
项目:adgm    作者:musyoku    | 项目源码 | 文件源码
def gaussian_kl_divergence_keepbatch(self, mean, ln_var):
        var = F.exp(ln_var)
        kld = F.sum(mean * mean + var - ln_var - 1, axis=1) * 0.5
        return kld
项目:variational-autoencoder    作者:musyoku    | 项目源码 | 文件源码
def gaussian_nll_keepbatch(self, x, mean, ln_var, clip=True):
        if clip:
            clip_min = math.log(0.001)
            clip_max = math.log(10)
            ln_var = F.clip(ln_var, clip_min, clip_max)
        x_prec = F.exp(-ln_var)
        x_diff = x - mean
        x_power = (x_diff * x_diff) * x_prec * 0.5
        return F.sum((math.log(2.0 * math.pi) + ln_var) * 0.5 + x_power, axis=1)
项目:variational-autoencoder    作者:musyoku    | 项目源码 | 文件源码
def gaussian_kl_divergence_keepbatch(self, mean, ln_var):
        var = F.exp(ln_var)
        kld = F.sum(mean * mean + var - ln_var - 1, axis=1) * 0.5
        return kld
项目:variational-autoencoder    作者:musyoku    | 项目源码 | 文件源码
def log_pz(self, z, mean, ln_var, test=False):
        if self.type_pz == "gaussianmarg":
            # \int q(z)logp(z)dz = -(J/2)*log2pi - (1/2)*sum_{j=1}^{J} (mu^2 + var)
            # See Appendix B [Auto-Encoding Variational Bayes](http://arxiv.org/abs/1312.6114)
            log_pz = -0.5 * (math.log(2.0 * math.pi) + mean * mean + F.exp(ln_var))
        elif self.type_pz == "gaussian":
            log_pz = -0.5 * math.log(2.0 * math.pi) - 0.5 * z ** 2
        return F.sum(log_pz, axis=1)

    # this will not be used
项目:variational-autoencoder    作者:musyoku    | 项目源码 | 文件源码
def gaussian_nll_keepbatch(self, x, mean, ln_var):
        x_prec = F.exp(-ln_var)
        x_diff = x - mean
        x_power = x_diff ** 2 * x_prec * 0.5
        return F.sum((math.log(2.0 * math.pi) + ln_var) * 0.5 + x_power, axis=1)
项目:variational-autoencoder    作者:musyoku    | 项目源码 | 文件源码
def gaussian_kl_divergence_keepbatch(self, mean, ln_var):
        var = F.exp(ln_var)
        kld = F.sum(mean ** 2 + var - ln_var - 1, axis=1) * 0.5
        return kld
项目:Multitask-and-Transfer-Learning    作者:AI-ON    | 项目源码 | 文件源码
def normalize_2d(x):
    exp = F.exp(x[0])
    sums = F.sum(F.sum(exp, axis=-1), axis=-1)
    expanded = F.expand_dims(F.expand_dims(sums, axis=-1), axis=-1)
    denominator = F.tile(expanded, (1, 160, 210))
    return exp / denominator