def forward(self, inputs):
     xp = cuda.get_array_module(*inputs)
     x, t = inputs
     if xp is np:
         self.y, = softmax.Softmax().forward((x, ))
         loss = softmax_crossentropy_cpu_funcs.crossentropy(self.y,
                                                            t) / t.shape[0]
     else:
         self.y, = softmax.Softmax(self.use_cudnn).forward((x, ))
         loss = _get_crossentropyloss_gpu(self.y, t)
     return loss.reshape(()),
Beispiel #2
0
 def forward_gpu(self, inputs):
     x, t = inputs
     self.y, = softmax.Softmax(self.use_cudnn).forward_gpu((x,))
     ret = cuda.reduce(
         'int* t, float* y, int n_channel', '-log(y[i * n_channel + t[i]])',
         'a+b', '0', 'crossent_fwd', numpy.float32
     )(t, self.y, self.y.shape[1])
     ret /= t.size
     return ret,
Beispiel #3
0
 def forward_gpu(self, inputs):
     cupy = cuda.cupy
     x, t = inputs
     self.y, = softmax.Softmax(self.use_cudnn).forward((x, ))
     if getattr(self, 'normalize', True):
         count = x.size // x.shape[1]
     else:
         count = x.shape[0]
     y = cupy.rollaxis(self.y, 1, self.y.ndim)
     ret = cuda.reduce('S t, raw T y, int32 n_channel, T inv_count',
                       'T out', 'log(y[_j * n_channel + t])', 'a + b',
                       'out = a * inv_count', '0',
                       'crossent_fwd')(t, y.reduced_view(), y.shape[-1],
                                       -1.0 / count)
     return ret,
Beispiel #4
0
    def forward_cpu(self, inputs):
        x, t = inputs
        self.y, = softmax.Softmax().forward((x, ))
        yd = numpy.rollaxis(self.y, 1)
        yd = yd.reshape(len(yd), -1).T

        p = yd[six.moves.range(t.size), t.flat]
        # deal with the case where the SoftmaxCrossEntropy is
        # unpickled from the old version
        if getattr(self, 'normalize', True):
            count = x.size // x.shape[1]
        else:
            count = x.shape[0]
        y = numpy.log(p).sum(keepdims=True) * (-1.0 / count)
        return y.reshape(()),
 def forward_gpu(self, inputs):
     cupy = cuda.cupy
     x, t = inputs
     self.y, = softmax.Softmax(self.use_cudnn).forward((x, ))
     n_unit = int(numpy.prod(self.y.shape[2:]))
     if getattr(self, 'normalize', True):
         count = t.shape[0] * n_unit
     else:
         count = t.shape[0]
     y = cupy.rollaxis(self.y, 1, len(self.y))
     ret = cuda.reduce('S t, raw T y, int32 n_channel, T inv_count',
                       'T out', 'log(y[_j * n_channel + t])', 'a + b',
                       'out = a * inv_count', '0',
                       'crossent_fwd')(t, y, y.shape[-1], -1.0 / count)
     return ret,
 def forward_gpu(self, inputs):
     x, t = inputs
     self.y, = softmax.Softmax(self.use_cudnn).forward_gpu((x,))
     n_unit = int(numpy.prod(self.y.shape[2:]))
     # the map_expr is equivalent to the pseudo code -log(y[n, c, m]),
     # where n = i / n_unit, c = t[i], and m = i % n_unit
     ret = cuda.reduce(
         'int* t, float* y, int n_channel, int n_unit',
         '-log(y[n_unit * ((i / n_unit) * n_channel + t[i])'
         '       + (i % n_unit)])',
         'a+b', '0', 'crossent_fwd', numpy.float32
     )(t, self.y, self.y.shape[1], n_unit)
     if getattr(self, 'normalize', True):
         n_unit = int(numpy.prod(self.y.shape[2:]))
         count = t.shape[0] * n_unit
     else:
         count = t.shape[0]
     ret /= count
     return ret,
Beispiel #7
0
    def forward_gpu(self, inputs):
        cupy = cuda.cupy
        x, t = inputs
        self.y, = softmax.Softmax(self.use_cudnn).forward((x,))
        if getattr(self, 'normalize', True):
            count = float((t != self.ignore_label).sum())
        else:
            count = t.shape[0]
        self.count = count

        if count == 0:
            return cupy.zeros((), dtype=x.dtype),

        y = cupy.rollaxis(self.y, 1, self.y.ndim)
        ret = cuda.reduce(
            'S t, raw T y, int32 n_channel, T inv_count', 'T out',
            't == -1 ? 0 : log(y[_j * n_channel + t])',
            'a + b', 'out = a * inv_count', '0', 'crossent_fwd'
        )(t, y.reduced_view(), y.shape[-1], -1.0 / count)
        return ret,
Beispiel #8
0
    def forward_cpu(self, inputs):
        x, t = inputs
        self.y, = softmax.Softmax().forward((x,))
        yd = numpy.rollaxis(self.y, 1)
        yd = yd.reshape(len(yd), -1).T

        p = yd[six.moves.range(t.size), numpy.maximum(t.flat, 0)]
        # deal with the case where the SoftmaxCrossEntropy is
        # unpickled from the old version
        if getattr(self, 'normalize', True):
            count = (t != self.ignore_label).sum()
        else:
            count = x.shape[0]
        self.count = count

        if count == 0:
            return numpy.zeros((), dtype=x.dtype),

        y = (numpy.log(p) * (t.flat != self.ignore_label)).sum(keepdims=True) \
            * (-1.0 / count)
        return y.reshape(()),
Beispiel #9
0
 def forward_cpu(self, inputs):
     x, t = inputs
     self.y, = softmax.Softmax().forward_cpu((x,))
     p = self.y[six.moves.range(len(t)), t]
     y = -numpy.log(p).sum(keepdims=True) / t.size
     return y.reshape(()),