Python chainer.functions.log_softmax() Examples

The following are 30 code examples of chainer.functions.log_softmax(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module chainer.functions , or try the search function .
Example #1
Source File: nin.py    From chainer-compiler with MIT License 6 votes vote down vote up
def softmax_cross_entropy(self, y, t):
        import numpy as np

        log_softmax = F.log_softmax(y)
        # SelectItem is not supported by onnx-chainer.
        # TODO(hamaji): Support it?
        # log_prob = F.select_item(log_softmax, t)

        # TODO(hamaji): Currently, F.sum with axis=1 cannot be
        # backpropped properly.
        # log_prob = F.sum(log_softmax * t, axis=1)
        # self.batch_size = chainer.Variable(np.array(t.size, np.float32),
        #                                    name='batch_size')
        # return -F.sum(log_prob, axis=0) / self.batch_size
        log_prob = F.sum(log_softmax * t, axis=(0, 1))
        batch_size = chainer.Variable(np.array(t.shape[0], np.float32),
                                      name='batch_size')
        self.extra_inputs = [batch_size]
        loss = -log_prob / batch_size
        loss.name = 'loss'
        return loss 
Example #2
Source File: gen_mnist_mlp.py    From chainer-compiler with MIT License 6 votes vote down vote up
def forward(self, x, t):
        xp = cuda.get_array_module(x)
        y = self.predictor(x)
        log_softmax = F.log_softmax(y)
        # SelectItem is not supported by onnx-chainer.
        # TODO(hamaji): Support it?
        # log_prob = F.select_item(log_softmax, t)

        batch_size = chainer.Variable(xp.array(t.size, xp.float32),
                                      name='batch_size')
        self.extra_inputs = [batch_size]
        # TODO(hamaji): Currently, F.sum with axis=1 cannot be
        # backpropped properly.
        # log_prob = F.sum(log_softmax * t, axis=1)
        # return -F.sum(log_prob, axis=0) / self.batch_size
        log_prob = F.sum(log_softmax * t, axis=(0, 1))
        loss = -log_prob / batch_size
        reporter.report({'loss': loss}, self)
        if self.compute_accuracy:
            acc = accuracy.accuracy(y, xp.argmax(t, axis=1))
            reporter.report({'accuracy': acc}, self)
        loss.name = 'loss'
        return loss 
Example #3
Source File: resnet50.py    From chainer-compiler with MIT License 6 votes vote down vote up
def softmax_cross_entropy(self, y, t):
        import numpy as np

        log_softmax = F.log_softmax(y)
        # SelectItem is not supported by onnx-chainer.
        # TODO(hamaji): Support it?
        # log_prob = F.select_item(log_softmax, t)

        # TODO(hamaji): Currently, F.sum with axis=1 cannot be
        # backpropped properly.
        # log_prob = F.sum(log_softmax * t, axis=1)
        # self.batch_size = chainer.Variable(np.array(t.size, np.float32),
        #                                    name='batch_size')
        # return -F.sum(log_prob, axis=0) / self.batch_size
        log_prob = F.sum(log_softmax * t, axis=(0, 1))
        batch_size = chainer.Variable(self.xp.array(t.shape[0], np.float32),
                                      name='batch_size')
        self.extra_inputs = [batch_size]
        loss = -log_prob / batch_size
        loss.name = 'loss'
        return loss 
Example #4
Source File: alex.py    From chainer-compiler with MIT License 6 votes vote down vote up
def softmax_cross_entropy(self, y, t):
        import numpy as np

        log_softmax = F.log_softmax(y)
        # SelectItem is not supported by onnx-chainer.
        # TODO(hamaji): Support it?
        # log_prob = F.select_item(log_softmax, t)

        # TODO(hamaji): Currently, F.sum with axis=1 cannot be
        # backpropped properly.
        # log_prob = F.sum(log_softmax * t, axis=1)
        # self.batch_size = chainer.Variable(np.array(t.size, np.float32),
        #                                    name='batch_size')
        # return -F.sum(log_prob, axis=0) / self.batch_size
        log_prob = F.sum(log_softmax * t, axis=(0, 1))
        batch_size = chainer.Variable(np.array(t.shape[0], np.float32),
                                      name='batch_size')
        self.extra_inputs = [batch_size]
        loss = -log_prob / batch_size
        loss.name = 'loss'
        return loss 
Example #5
Source File: beam_search.py    From lencon with MIT License 5 votes vote down vote up
def _calc_top_n(self, model, x, state, beam_width):
        o, state = model.decode_once(x, state, train=False)
        o = F.log_softmax(o, use_cudnn=False)
        o = chainer.cuda.to_cpu(o.data[0])

        eos_score = o[self.EOS]
        self._edit_probs(o)

        inds = np.argpartition(o, len(o) - beam_width)
        inds = inds[::-1][:beam_width]
        return inds, o, state, eos_score 
Example #6
Source File: e2e_asr_transformer.py    From espnet with Apache License 2.0 5 votes vote down vote up
def recognize(self, x_block, recog_args, char_list=None, rnnlm=None):
        """E2E recognition function.

        Args:
            x (ndarray): Input acouctic feature (B, T, D) or (T, D).
            recog_args (Namespace): Argment namespace contraining options.
            char_list (List[str]): List of characters.
            rnnlm (chainer.Chain): Language model module defined at
            `espnet.lm.chainer_backend.lm`.

        Returns:
            List: N-best decoding results.

        """
        with chainer.no_backprop_mode(), chainer.using_config("train", False):
            # 1. encoder
            ilens = [x_block.shape[0]]
            batch = len(ilens)
            xs, _, _ = self.encoder(x_block[None, :, :], ilens)

            # calculate log P(z_t|X) for CTC scores
            if recog_args.ctc_weight > 0.0:
                lpz = self.ctc.log_softmax(xs.reshape(batch, -1, self.dims)).data[0]
            else:
                lpz = None
            # 2. decoder
            if recog_args.lm_weight == 0.0:
                rnnlm = None
            y = self.recognize_beam(xs, lpz, recog_args, char_list, rnnlm)

        return y 
Example #7
Source File: ctc.py    From espnet with Apache License 2.0 5 votes vote down vote up
def log_softmax(self, hs):
        """Log_softmax of frame activations.

        Args:
            hs (list of chainer.Variable | N-dimension array):
                Input variable from encoder.

        Returns:
            chainer.Variable: A n-dimension float array.

        """
        y_hat = self.ctc_lo(F.pad_sequence(hs), n_batch_axes=2)
        return F.log_softmax(y_hat.reshape(-1, y_hat.shape[-1])).reshape(y_hat.shape) 
Example #8
Source File: ctc.py    From espnet with Apache License 2.0 5 votes vote down vote up
def log_softmax(self, hs):
        """Log_softmax of frame activations.

        Args:
            hs (list of chainer.Variable | N-dimension array):
                Input variable from encoder.

        Returns:
            chainer.Variable: A n-dimension float array.

        """
        y_hat = self.ctc_lo(F.pad_sequence(hs), n_batch_axes=2)
        return F.log_softmax(y_hat.reshape(-1, y_hat.shape[-1])).reshape(y_hat.shape) 
Example #9
Source File: label_smoothing_loss.py    From espnet with Apache License 2.0 5 votes vote down vote up
def forward(self, ys_block, ys_pad):
        """Forward Loss.

        Args:
            ys_block (chainer.Variable): Predicted labels.
            ys_pad (chainer.Variable): Target (true) labels.

        Returns:
            float: Training loss.

        """
        # Output (all together at once for efficiency)
        batch, length, dims = ys_block.shape
        concat_logit_block = ys_block.reshape(-1, dims)

        # Target reshape
        concat_t_block = ys_pad.reshape((batch * length))
        ignore_mask = concat_t_block >= 0
        n_token = ignore_mask.sum()
        normalizer = n_token if self.normalize_length else batch

        if not self.use_label_smoothing:
            loss = F.softmax_cross_entropy(concat_logit_block, concat_t_block)
            loss = loss * n_token / normalizer
        else:
            log_prob = F.log_softmax(concat_logit_block)
            broad_ignore_mask = self.xp.broadcast_to(
                ignore_mask[:, None], concat_logit_block.shape
            )
            pre_loss = (
                ignore_mask * log_prob[self.xp.arange(batch * length), concat_t_block]
            )
            loss = -F.sum(pre_loss) / normalizer
            label_smoothing = broad_ignore_mask * -1.0 / self.n_target_vocab * log_prob
            label_smoothing = F.sum(label_smoothing) / normalizer
            loss = self.confidence * loss + self.smoothing * label_smoothing
        return loss 
Example #10
Source File: decoder.py    From espnet with Apache License 2.0 5 votes vote down vote up
def recognize(self, e, yy_mask, source):
        """Process recognition function."""
        e = self.forward(e, source, yy_mask)
        return F.log_softmax(e, axis=-1) 
Example #11
Source File: ctc.py    From espnet with Apache License 2.0 5 votes vote down vote up
def log_softmax(self, hs):
        """Log_softmax of frame activations.

        Args:
            hs (list of chainer.Variable | N-dimension array):
                Input variable from encoder.

        Returns:
            chainer.Variable: A n-dimension float array.

        """
        y_hat = self.ctc_lo(F.pad_sequence(hs), n_batch_axes=2)
        return F.log_softmax(y_hat.reshape(-1, y_hat.shape[-1])).reshape(y_hat.shape) 
Example #12
Source File: extlm.py    From espnet with Apache License 2.0 5 votes vote down vote up
def final(self, state):
        clm_state, wlm_state, wlm_logprobs, node, log_y, clm_logprob = state
        if node is not None and node[1] >= 0:  # check if the node is word end
            w = self.xp.full(1, node[1], "i")
        else:  # this node is not a word end, which means <unk>
            w = self.xp_word_unk
        wlm_state, z_wlm = self.wordlm(wlm_state, w)
        return F.log_softmax(z_wlm).data[:, self.word_eos]


# Definition of a look-ahead word language model 
Example #13
Source File: extlm.py    From espnet with Apache License 2.0 5 votes vote down vote up
def final(self, state):
        wlm_state, cumsum_probs, node = state
        if node is not None and node[1] >= 0:  # check if the node is word end
            w = self.xp.full(1, node[1], "i")
        else:  # this node is not a word end, which means <unk>
            w = self.xp_word_unk
        wlm_state, z_wlm = self.wordlm(wlm_state, w)
        return F.log_softmax(z_wlm).data[:, self.word_eos] 
Example #14
Source File: lm.py    From espnet with Apache License 2.0 5 votes vote down vote up
def predict(self, state, x):
        """Predict log probabilities for given state and input x using the predictor

        :param state : the state
        :param x : the input
        :return a tuple (state, log prob vector)
        :rtype cupy/numpy array
        """
        if hasattr(self.predictor, "normalized") and self.predictor.normalized:
            return self.predictor(state, x)
        else:
            state, z = self.predictor(state, x)
            return state, F.log_softmax(z).data 
Example #15
Source File: nvdm.py    From lda2vec with MIT License 5 votes vote down vote up
def decode(self, sample, bow):
        """ Decode latent document vectors back into word counts
        (n_docs, n_vocab).
        """
        logprob = F.log_softmax(self.embedding(sample))
        # This is equivalent to a softmax_cross_entropy where instead of
        # guessing 1 of N words we have repeated observations
        # Normal softmax for guessing the next word is:
        # t log softmax(x), where t is 0 or 1
        # Softmax for guessing word counts is simply doing
        # the above more times, so multiply by the count
        # count log softmax(x)
        loss = -F.sum(bow * logprob)
        return loss 
Example #16
Source File: gpu_test_softmax.py    From deep-learning-from-scratch-3 with MIT License 5 votes vote down vote up
def test_forward1(self):
        x = np.array([[-1, 0, 1, 2], [2, 0, 1, -1]], np.float32)
        y = F.log_softmax(x)
        y2 = CF.log_softmax(x)
        res = array_allclose(y.data, y2.data)
        self.assertTrue(res) 
Example #17
Source File: gpu_test_softmax.py    From deep-learning-from-scratch-3 with MIT License 5 votes vote down vote up
def test_backward1(self):
        x = np.array([[-1, 0, 1, 2], [2, 0, 1, -1]])
        f = lambda x: F.log_softmax(x)
        self.assertTrue(gradient_check(f, x)) 
Example #18
Source File: gpu_test_softmax.py    From deep-learning-from-scratch-3 with MIT License 5 votes vote down vote up
def test_backward2(self):
        x = np.random.randn(10, 10)
        f = lambda x: F.log_softmax(x)
        self.assertTrue(gradient_check(f, x)) 
Example #19
Source File: test_softmax.py    From deep-learning-from-scratch-3 with MIT License 5 votes vote down vote up
def test_forward1(self):
        x = np.array([[-1, 0, 1, 2], [2, 0, 1, -1]], np.float32)
        y = F.log_softmax(x)
        y2 = CF.log_softmax(x)
        res = array_allclose(y.data, y2.data)
        self.assertTrue(res) 
Example #20
Source File: test_softmax.py    From deep-learning-from-scratch-3 with MIT License 5 votes vote down vote up
def test_backward1(self):
        x = np.array([[-1, 0, 1, 2], [2, 0, 1, -1]])
        f = lambda x: F.log_softmax(x)
        self.assertTrue(gradient_check(f, x)) 
Example #21
Source File: loss.py    From vat_chainer with MIT License 5 votes vote down vote up
def entropy_y_x(p_logit):
    p = F.softmax(p_logit)
    return - F.sum(p * F.log_softmax(p_logit)) / p_logit.shape[0] 
Example #22
Source File: policy_output.py    From async-rl with MIT License 5 votes vote down vote up
def log_probs(self):
        return F.log_softmax(self.logits) 
Example #23
Source File: lstm_decoder.py    From DSTC6-End-to-End-Conversation-Modeling with MIT License 5 votes vote down vote up
def predict(self, s):
        """Predict single-label log probabilities

        Args:
            s (any): Current (hidden, cell) states.
        Return:
            (~chainer.Variable) log softmax vector
        """
        y = self.out(self.proj(s[2][0]))
        return F.log_softmax(y) 
Example #24
Source File: lstm_decoder.py    From DSTC6-End-to-End-Conversation-Modeling with MIT License 5 votes vote down vote up
def predict(self, s):
        """Predict single-label log probabilities

        Args:
            s (any): Current (hidden, cell) states.
        Return:
            (~chainer.Variable) log softmax vector
        """
        y = self.out(self.proj(s[2][0]))
        return F.log_softmax(y) 
Example #25
Source File: lstm_decoder.py    From DSTC6-End-to-End-Conversation-Modeling with MIT License 5 votes vote down vote up
def predict(self, s):
        """Predict single-label log probabilities

        Args:
            s (any): Current (hidden, cell) states.
        Return:
            (~chainer.Variable) log softmax vector
        """
        y = self.out(self.proj(s[2][0]))
        return F.log_softmax(y) 
Example #26
Source File: lstm_decoder.py    From DSTC6-End-to-End-Conversation-Modeling with MIT License 5 votes vote down vote up
def predict(self, s):
        """Predict single-label log probabilities

        Args:
            s (any): Current (hidden, cell) states.
        Return:
            (~chainer.Variable) log softmax vector
        """
        y = self.out(self.proj(s[2][0]))
        return F.log_softmax(y) 
Example #27
Source File: loss.py    From vat_chainer with MIT License 5 votes vote down vote up
def kl_categorical(p_logit, q_logit):
    if isinstance(p_logit, chainer.Variable):
        xp = cuda.get_array_module(p_logit.data)
    else:
        xp = cuda.get_array_module(p_logit)
    p = F.softmax(p_logit)
    _kl = F.sum(p * (F.log_softmax(p_logit) - F.log_softmax(q_logit)), 1)
    return F.sum(_kl) / xp.prod(xp.array(_kl.shape)) 
Example #28
Source File: loss.py    From vat_chainer with MIT License 5 votes vote down vote up
def cross_entropy(logit, y):
    # y should be one-hot encoded probability
    return - F.sum(y * F.log_softmax(logit)) / logit.shape[0] 
Example #29
Source File: net.py    From models with MIT License 5 votes vote down vote up
def kl_loss(xp, p_logit, q_logit):
    p = F.softmax(p_logit)
    _kl = F.sum(p * (F.log_softmax(p_logit) - F.log_softmax(q_logit)), 1)
    return F.sum(_kl) / xp.prod(xp.array(_kl.shape)) 
Example #30
Source File: test_log_softmax.py    From chainer with MIT License 5 votes vote down vote up
def forward(self, inputs, device):
        x, = inputs
        return functions.log_softmax(x, axis=self.axis),