Python chainer.functions.mean() Examples

The following are 30 code examples of chainer.functions.mean(). 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: test_group_normalization.py    From chainer with MIT License 6 votes vote down vote up
def _simple_group_normalization(x, groups, gamma, beta, eps=1e-5):
    batch_size, channels = x.shape[:2]
    x_reshape = x.reshape(batch_size, groups, channels // groups, -1)

    mean = numpy.mean(x_reshape, axis=(2, 3), keepdims=True)
    var = numpy.var(x_reshape, axis=(2, 3), keepdims=True)
    std = numpy.sqrt(var + eps, dtype=x.dtype)

    x_hat = (x_reshape - mean) / std
    x_hat = x_hat.reshape(x.shape)

    for i in six.moves.xrange(x.ndim):
        if i != 1:  # except for channel dim
            gamma = numpy.expand_dims(gamma, i)
            beta = numpy.expand_dims(beta, i)

    return x_hat * gamma + beta 
Example #2
Source File: mdn.py    From models with MIT License 6 votes vote down vote up
def get_gaussian_params(self, x):
        h = F.tanh(self.l1(x))
        h = self.l2(h)

        pi = h[:, :self.gaussian_mixtures]
        mu_var_dim = self.gaussian_mixtures * self.input_dim
        mu = h[:, self.gaussian_mixtures:self.gaussian_mixtures + mu_var_dim]
        log_var = h[:, self.gaussian_mixtures + mu_var_dim:]

        n_batch = x.shape[0]

        # mixing coefficients
        pi = F.reshape(pi, (n_batch, self.gaussian_mixtures))
        pi = F.softmax(pi, axis=1)

        # mean
        mu = F.reshape(mu, (n_batch, self.gaussian_mixtures, self.input_dim))

        # log variance
        log_var = F.reshape(
            log_var, (n_batch, self.gaussian_mixtures, self.input_dim))

        return pi, mu, log_var 
Example #3
Source File: ppo.py    From chainerrl with MIT License 6 votes vote down vote up
def _update_recurrent(self, dataset):
        """Update both the policy and the value function."""

        flat_dataset = list(itertools.chain.from_iterable(dataset))
        if self.obs_normalizer:
            self._update_obs_normalizer(flat_dataset)

        xp = self.model.xp

        assert 'state' in flat_dataset[0]
        assert 'v_teacher' in flat_dataset[0]

        if self.standardize_advantages:
            all_advs = xp.array([b['adv'] for b in flat_dataset])
            mean_advs = xp.mean(all_advs)
            std_advs = xp.std(all_advs)
        else:
            mean_advs = None
            std_advs = None

        for _ in range(self.epochs):
            random.shuffle(dataset)
            for minibatch in _yield_subset_of_sequences_with_fixed_number_of_items(  # NOQA
                    dataset, self.minibatch_size):
                self._update_once_recurrent(minibatch, mean_advs, std_advs) 
Example #4
Source File: test_rasterize.py    From neural_renderer with MIT License 6 votes vote down vote up
def test_forward_case3(self):
        """Whether a silhouette by neural renderer matches that by Blender."""

        # load teapot
        vertices, faces, textures = utils.load_teapot_batch()

        # create renderer
        renderer = neural_renderer.Renderer()
        renderer.image_size = 256
        renderer.anti_aliasing = False
        renderer.light_intensity_ambient = 1.0
        renderer.light_intensity_directional = 0.0

        images = renderer.render(vertices, faces, textures)
        images = images.data.get()
        image = images[2].mean(0)

        # load reference image by blender
        ref = scipy.misc.imread('./tests/data/teapot_blender.png')
        ref = ref.astype('float32')
        ref = (ref.min(-1) != 255).astype('float32')

        chainer.testing.assert_allclose(ref, image) 
Example #5
Source File: train_mnist.py    From models with MIT License 6 votes vote down vote up
def pretraining(optimizer):
    logger.info('pretraining')
    copy_grand_opt = copy.deepcopy(optimizer.grand_optimizer)
    losses = []
    for _ in range(10):
        x = optimizer.optnet.xp.random.normal(
            scale=10., size=(10000, 1)).astype('f')
        g = optimizer.optnet.step(x)
        # loss forcing g's sign to be the flip of input's sign
        # theta = theta - c*gradient
        # theta = theta + g
        loss = F.mean(F.clip(g, 0, 100) * (x > 0)
                      + F.clip(-g, 0, 100) * (x < 0))
        optimizer.optnet.cleargrads()
        loss.backward()
        optimizer.meta_update()
        optimizer.optnet.reset_state()
        losses.append(loss.item())
    logger.info('finished pretraining. losses {}'.format(losses))
    optimizer.release_all()
    # reset adam state
    optimizer = nets.optnets.OptimizerByNet(optimizer.optnet, copy_grand_opt)
    return optimizer, copy_grand_opt 
Example #6
Source File: fcn8s_matting.py    From portrait_matting with GNU General Public License v3.0 6 votes vote down vote up
def __call__(self, x, t=None, w=None):
        # t, w is on host.

        # Forward network
        alpha = self.forward(x)

        if t is None:
            assert not chainer.config.train
            return

        # Weighted mean squared error
        # TODO: Do more tests
#         loss = F.mean(F.squared_error(alpha, t) * w)
        loss = F.mean_squared_error(alpha, t)

        if np.isnan(float(loss.data)):
            raise ValueError('Loss is nan.')
        chainer.report({'loss': loss}, self)

        return loss 
Example #7
Source File: categorical_dqn.py    From chainerrl with MIT License 6 votes vote down vote up
def compute_weighted_value_loss(eltwise_loss, batch_size, weights,
                                batch_accumulator='mean'):
    """Compute a loss for value prediction problem.

    Args:
        eltwise_loss (Variable): Element-wise loss per example per atom
        weights (ndarray): Weights for y, t.
        batch_accumulator (str): 'mean' will divide loss by batchsize
    Returns:
        (Variable) scalar loss
    """
    assert batch_accumulator in ('mean', 'sum')

    # eltwise_loss is (batchsize, n_atoms) array of losses
    # weights is an array of shape (batch_size)
    # sum loss across atoms and then apply weight per example in batch
    loss_sum = F.matmul(F.sum(eltwise_loss, axis=1), weights)
    if batch_accumulator == 'mean':
        loss = loss_sum / batch_size
    elif batch_accumulator == 'sum':
        loss = loss_sum
    return loss 
Example #8
Source File: dqfd.py    From baselines with MIT License 6 votes vote down vote up
def _compute_target_values(self, exp_batch):
        batch_next_state = exp_batch['next_state']

        with chainer.using_config('train', False), state_kept(self.q_function):
            next_qout = self.q_function(batch_next_state)

        target_next_qout = self.target_q_function(batch_next_state)

        next_q_max = target_next_qout.evaluate_actions(
            next_qout.greedy_actions)
        next_q_max = F.mean(next_q_max, axis=1)

        batch_rewards = exp_batch['reward']
        batch_terminal = exp_batch['is_state_terminal']
        discount = exp_batch['discount']

        return batch_rewards + discount * (1.0 - batch_terminal) * next_q_max 
Example #9
Source File: categorical_dqn.py    From chainerrl with MIT License 6 votes vote down vote up
def compute_value_loss(eltwise_loss, batch_accumulator='mean'):
    """Compute a loss for value prediction problem.

    Args:
        eltwise_loss (Variable): Element-wise loss per example per atom
        batch_accumulator (str): 'mean' or 'sum'. 'mean' will use the mean of
            the loss values in a batch. 'sum' will use the sum.
    Returns:
        (Variable) scalar loss
    """
    assert batch_accumulator in ('mean', 'sum')

    if batch_accumulator == 'sum':
        loss = F.sum(eltwise_loss)
    else:
        loss = F.mean(F.sum(eltwise_loss, axis=1))
    return loss 
Example #10
Source File: iqn.py    From chainerrl with MIT License 6 votes vote down vote up
def compute_weighted_value_loss(eltwise_loss, weights,
                                batch_accumulator='mean'):
    """Compute a loss for value prediction problem.

    Args:
        eltwise_loss (Variable): Element-wise loss per example
        weights (ndarray): Weights for y, t.
        batch_accumulator (str): 'mean' will divide loss by batchsize
    Returns:
        (Variable) scalar loss
    """
    batch_size = eltwise_loss.shape[0]
    assert batch_accumulator in ('mean', 'sum')
    assert eltwise_loss.ndim == 3
    # eltwise_loss is (batchsize, n , n') array of losses
    # weights is an array of shape (batch_size)
    # apply weights per example in batch
    loss_sum = F.matmul(F.sum(F.mean(eltwise_loss, axis=2), axis=1), weights)
    if batch_accumulator == 'mean':
        loss = loss_sum / batch_size
    elif batch_accumulator == 'sum':
        loss = loss_sum
    return loss 
Example #11
Source File: iqn.py    From chainerrl with MIT License 6 votes vote down vote up
def _compute_loss(self, exp_batch, errors_out=None):
        """Compute a loss.

        Returns:
            Returns:
                chainer.Variable: Scalar loss.
        """
        y, taus = self._compute_y_and_taus(exp_batch)
        with chainer.no_backprop_mode():
            t = self._compute_target_values(exp_batch)

        eltwise_loss = compute_eltwise_huber_quantile_loss(y, t, taus)
        if errors_out is not None:
            del errors_out[:]
            delta = F.mean(eltwise_loss, axis=(1, 2))
            errors_out.extend(cuda.to_cpu(delta.array))

        if 'weights' in exp_batch:
            return compute_weighted_value_loss(
                eltwise_loss, exp_batch['weights'],
                batch_accumulator=self.batch_accumulator)
        else:
            return compute_value_loss(
                eltwise_loss, batch_accumulator=self.batch_accumulator) 
Example #12
Source File: updater.py    From pixcaler with MIT License 5 votes vote down vote up
def loss_func_adv_dis_real(self, y_real):
        return F.mean(F.softplus(-y_real)) 
Example #13
Source File: bbox_plotter.py    From kiss with GNU General Public License v3.0 5 votes vote down vote up
def render_objectness_scores(self, objectness_scores, dest_image, image, bottom=False):
        if len(objectness_scores) == 0:
            return dest_image

        if isinstance(objectness_scores, tuple) and self.plot_objectness_classification_result:
            objectness_scores = objectness_scores[1]
            objectness_scores = F.softmax(objectness_scores)
            objectness_classification = map(lambda x: "{:.2f}".format(float(x)), objectness_scores[:, 1].data)
        elif isinstance(objectness_scores, tuple) and not self.plot_objectness_classification_result:
            objectness_scores = objectness_scores[0]
            objectness_classification = map(lambda x: "{:.2f}".format(float(F.mean(x).data)), objectness_scores[:, 0].data)
        elif objectness_scores.shape[-1] == 2:
            objectness_scores = F.softmax(objectness_scores)
            objectness_classification = map(lambda x: "{:.2f}".format(float(x)), objectness_scores[:, 1].data)
        else:
            objectness_classification = map(lambda x: "{:.2f}".format(float(F.mean(x).data)), objectness_scores[:, 0].data)

        for i, char in enumerate(objectness_classification, start=1):
            label_image = Image.new(dest_image.mode, dest_image.size)
            draw = ImageDraw.Draw(label_image)
            paste_width = (i + 1) * image.width
            text_width, text_height = draw.textsize(char, self.font)
            insert_height = image.height - text_height - 1 if bottom else 0
            draw.rectangle([paste_width - text_width - 1, insert_height, paste_width, insert_height + text_height], fill=(255, 255, 255, 160))
            draw.text((paste_width - text_width - 1, insert_height), char, fill='green', font=self.font)
            dest_image = Image.alpha_composite(dest_image, label_image)
        return dest_image 
Example #14
Source File: updater.py    From pixcaler with MIT License 5 votes vote down vote up
def loss_func_adv_gen(self, y_fake):
        return F.mean(F.softplus(-y_fake)) 
Example #15
Source File: updater.py    From pixcaler with MIT License 5 votes vote down vote up
def loss_func_adv_dis_real(self, y_real):
        return F.mean(F.softplus(-y_real)) 
Example #16
Source File: updater.py    From pixcaler with MIT License 5 votes vote down vote up
def loss_func_adv_dis_fake(self, y_fake):
        return F.mean(F.softplus(y_fake)) 
Example #17
Source File: updater.py    From pixcaler with MIT License 5 votes vote down vote up
def loss_func_adv_dis_real_ls(self, y_real):
        return 0.5 * F.mean((y_real - 1.0) ** 2) 
Example #18
Source File: updater.py    From pixcaler with MIT License 5 votes vote down vote up
def loss_func_adv_dis_fake_ls(self, y_fake):
        return 0.5 * F.mean(y_fake ** 2) 
Example #19
Source File: updater.py    From pixcaler with MIT License 5 votes vote down vote up
def loss_func_adv_gen(self, y_fake):
        return F.mean(F.softplus(-y_fake)) 
Example #20
Source File: text_recognizer.py    From kiss with GNU General Public License v3.0 5 votes vote down vote up
def calc_loss(self, predictions, labels):
        recognition_losses = []
        assert predictions.shape[1] == labels.shape[1], "Number of boxes is not equal in predictions and labels"
        for box, box_labels in zip(F.separate(predictions, axis=1), F.separate(labels, axis=1)):
            assert box.shape[1] == box_labels.shape[1], "Number of predicted chars is not equal to number of chars in label"
            box_losses = [
                F.softmax_cross_entropy(char, char_label, reduce="no")
                for char, char_label in zip(F.separate(box, axis=1), F.separate(box_labels, axis=1))
            ]
            recognition_losses.append(F.stack(box_losses))
        return F.mean(F.stack(recognition_losses)) 
Example #21
Source File: utils.py    From kiss with GNU General Public License v3.0 5 votes vote down vote up
def calc_loss(self, grids, image_size, **kwargs):
        normalize = kwargs.get('normalize', True)
        width, height = self.get_bbox_side_lengths(grids, image_size)

        # penalize aspect ratios that are higher than wide, and penalize aspect ratios that are tooo wide
        aspect_ratio = height / F.maximum(width, self.xp.ones_like(width))
        # do not give an incentive to bboxes with a width that is 2x the height of the box
        aspect_loss = F.maximum(aspect_ratio - 0.5, self.xp.zeros_like(aspect_ratio))

        return F.mean(aspect_loss) 
Example #22
Source File: test_rasterize.py    From neural_renderer with MIT License 5 votes vote down vote up
def test_backward_case2(self):
        """Backward if non-zero gradient is on a face."""

        vertices = [
            [0.8, 0.8, 1.],
            [-0.5, -0.8, 1.],
            [0.8, -0.8, 1.]]
        faces = [[0, 1, 2]]
        pyi = 40
        pxi = 50
        grad_ref = [
            [0.98646867, 1.04628897, 0.],
            [-1.03415668, - 0.10403691, 0.],
            [3.00094461, - 1.55173182, 0.],
        ]

        renderer = neural_renderer.Renderer()
        renderer.image_size = 64
        renderer.anti_aliasing = False
        renderer.perspective = False
        renderer.light_intensity_ambient = 1.0
        renderer.light_intensity_directional = 0.0

        vertices = cp.array(vertices, 'float32')
        faces = cp.array(faces, 'int32')
        textures = cp.ones((faces.shape[0], 4, 4, 4, 3), 'float32')
        grad_ref = cp.array(grad_ref, 'float32')
        vertices, faces, textures, grad_ref = utils.to_minibatch((vertices, faces, textures, grad_ref))
        vertices = chainer.Variable(vertices)

        images = renderer.render(vertices, faces, textures)
        images = cf.mean(images, axis=1)
        loss = cf.sum(cf.absolute(images[:, pyi, pxi]))
        loss.backward()

        grad_ref = cp.array(grad_ref, 'float32')
        chainer.testing.assert_allclose(vertices.grad, grad_ref, rtol=1e-2) 
Example #23
Source File: test_rasterize.py    From neural_renderer with MIT License 5 votes vote down vote up
def test_backward_case1(self):
        """Backward if non-zero gradient is out of a face."""

        vertices = [
            [0.8, 0.8, 1.],
            [0.0, -0.5, 1.],
            [0.2, -0.4, 1.]]
        faces = [[0, 1, 2]]
        pxi = 35
        pyi = 25
        grad_ref = [
            [1.6725862, -0.26021874, 0.],
            [1.41986704, -1.64284933, 0.],
            [0., 0., 0.],
        ]

        renderer = neural_renderer.Renderer()
        renderer.image_size = 64
        renderer.anti_aliasing = False
        renderer.perspective = False
        renderer.light_intensity_ambient = 1.0
        renderer.light_intensity_directional = 0.0

        vertices = cp.array(vertices, 'float32')
        faces = cp.array(faces, 'int32')
        textures = cp.ones((faces.shape[0], 4, 4, 4, 3), 'float32')
        grad_ref = cp.array(grad_ref, 'float32')
        vertices, faces, textures, grad_ref = utils.to_minibatch((vertices, faces, textures, grad_ref))
        vertices = chainer.Variable(vertices)

        images = renderer.render(vertices, faces, textures)
        images = cf.mean(images, axis=1)
        loss = cf.sum(cf.absolute(images[:, pyi, pxi] - 1))
        loss.backward()

        chainer.testing.assert_allclose(vertices.grad, grad_ref, rtol=1e-2) 
Example #24
Source File: train_mnist.py    From models with MIT License 5 votes vote down vote up
def evaluate_optimizer(args, train, optimizer):
    if isinstance(optimizer, nets.optnets.LSTMOptNet):
        optimizer.release_all()
    device = chainer.get_device(args.gpu)
    device.use()
    n_evaluation_runs = args.evaluation_runs  # 5?
    max_iter_of_meta = args.iter_meta  # 100

    all_losses = []
    for _ in range(n_evaluation_runs):
        train_iter = chainer.iterators.SerialIterator(train, args.batchsize)
        losses = []
        model = nets.images.MLPforMNIST()
        model.to_device(device)
        optimizer.setup(model)

        iteration = 0
        while iteration < max_iter_of_meta:
            # routine
            iteration += 1
            batch = train_iter.next()
            batch = convert.concat_examples(batch, device=device)
            x, t = batch
            with chainer.using_config('train', True):
                loss, acc = model(x, t, get_accuracy=True)
            model.cleargrads()
            loss.backward(retain_grad=False)  # False
            optimizer.update(train_optnet=False)
            losses.append(loss.item())  # log
        all_losses.append(losses)
    # TODO: use losses in only last half iterations?
    last10_mean = np.mean([losses[-10:] for losses in all_losses])
    return last10_mean, all_losses 
Example #25
Source File: net.py    From models with MIT License 5 votes vote down vote up
def __call__(self, xs):
        y_hat, input_embeds = self.predict(xs)
        loss = 0.5 * F.sum((y_hat - input_embeds) ** 2, axis=1)
        loss = F.mean(loss)
        reporter.report({'loss': loss.data}, self)
        return loss 
Example #26
Source File: model.py    From models with MIT License 5 votes vote down vote up
def square_loss(ys, ts):
    # return F.mean(F.sqrt((ys - ts) ** 2 + 1e-5), axis=(0, 2))
    return F.mean((ys - ts) ** 2 + 1e-5, axis=(0, 2)) 
Example #27
Source File: model_py.py    From models with MIT License 5 votes vote down vote up
def __call__(self, x):
        # chainer requires explicit broadcast for avoiding latent bugs
        u = F.mean(x, -1, keepdims=True)
        u = F.broadcast_to(u, x.shape)
        s = F.mean((x - u) ** 2, -1, keepdims=True)
        s = F.broadcast_to(s, x.shape)
        x = (x - u) / F.sqrt(s + self.e)
        return F.bias(F.scale(x, self.g, axis=2), self.b, axis=2) 
Example #28
Source File: mdn.py    From models with MIT License 5 votes vote down vote up
def negative_log_likelihood(self, x, y):
        pi, mu, log_var = self.get_gaussian_params(x)

        # Likelihood over different Gaussians
        y = F.tile(y[:, None, :], (1, self.gaussian_mixtures, 1))
        pi = F.tile(F.expand_dims(pi, 2), (1, 1, self.input_dim))
        
        squared_sigma = F.exp(log_var)
        sigma = F.sqrt(squared_sigma)
        prob = F.sum(pi * distributions.Normal(mu, sigma).prob(y), axis=1)
        
        negative_log_likelihood = -F.log(prob)
        return F.mean(negative_log_likelihood) 
Example #29
Source File: test_group_normalization.py    From chainer with MIT License 5 votes vote down vote up
def forward(self, inputs, device):
        x, y = inputs

        mean = functions.mean(x, axis=1)
        d = x - mean[:, None]
        var = functions.mean(d * d, axis=1)
        inv_std = functions.rsqrt(var + self.eps)

        dummy_gamma = self.backend_config.xp.ones(
            self.shape[0], dtype=self.dtype)

        return gn_module._MulInvStd(
            self.eps, mean.array, inv_std.array, dummy_gamma).apply((x, y)) 
Example #30
Source File: net.py    From chainer with MIT License 5 votes vote down vote up
def __call__(self, x):
        q_z = self.encoder(x)
        z = q_z.sample(self.k)
        p_x = self.decoder(z)
        p_z = self.prior()

        reconstr = F.mean(p_x.log_prob(
            F.broadcast_to(x[None, :], (self.k,) + x.shape)))
        kl_penalty = F.mean(chainer.kl_divergence(q_z, p_z))
        loss = - (reconstr - self.beta * kl_penalty)
        reporter.report({'loss': loss}, self)
        reporter.report({'reconstr': reconstr}, self)
        reporter.report({'kl_penalty': kl_penalty}, self)
        return loss