当前位置: 首页>>代码示例>>Python>>正文


Python functions.select_item方法代码示例

本文整理汇总了Python中chainer.functions.select_item方法的典型用法代码示例。如果您正苦于以下问题:Python functions.select_item方法的具体用法?Python functions.select_item怎么用?Python functions.select_item使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在chainer.functions的用法示例。


在下文中一共展示了functions.select_item方法的14个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: softmax_cross_entropy

# 需要导入模块: from chainer import functions [as 别名]
# 或者: from chainer.functions import select_item [as 别名]
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 
开发者ID:pfnet-research,项目名称:chainer-compiler,代码行数:23,代码来源:nin.py

示例2: forward

# 需要导入模块: from chainer import functions [as 别名]
# 或者: from chainer.functions import select_item [as 别名]
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 
开发者ID:pfnet-research,项目名称:chainer-compiler,代码行数:25,代码来源:gen_mnist_mlp.py

示例3: softmax_cross_entropy

# 需要导入模块: from chainer import functions [as 别名]
# 或者: from chainer.functions import select_item [as 别名]
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 
开发者ID:pfnet-research,项目名称:chainer-compiler,代码行数:23,代码来源:resnet50.py

示例4: update

# 需要导入模块: from chainer import functions [as 别名]
# 或者: from chainer.functions import select_item [as 别名]
def update(Q, target_Q, opt, samples, gamma=0.99, target_type='double_dqn'):
    """Update a Q-function with given samples and a target Q-function."""
    dtype = chainer.get_dtype()
    xp = Q.xp
    obs = xp.asarray([sample[0] for sample in samples], dtype=dtype)
    action = xp.asarray([sample[1] for sample in samples], dtype=np.int32)
    reward = xp.asarray([sample[2] for sample in samples], dtype=dtype)
    done = xp.asarray([sample[3] for sample in samples], dtype=dtype)
    obs_next = xp.asarray([sample[4] for sample in samples], dtype=dtype)
    # Predicted values: Q(s,a)
    y = F.select_item(Q(obs), action)
    # Target values: r + gamma * max_b Q(s',b)
    with chainer.no_backprop_mode():
        if target_type == 'dqn':
            next_q = F.max(target_Q(obs_next), axis=1)
        elif target_type == 'double_dqn':
            next_q = F.select_item(target_Q(obs_next),
                                   F.argmax(Q(obs_next), axis=1))
        else:
            raise ValueError('Unsupported target_type: {}'.format(target_type))
        target = reward + gamma * (1 - done) * next_q
    loss = mean_clipped_loss(y, target)
    Q.cleargrads()
    loss.backward()
    opt.update() 
开发者ID:chainer,项目名称:chainer,代码行数:27,代码来源:dqn_cartpole.py

示例5: prob

# 需要导入模块: from chainer import functions [as 别名]
# 或者: from chainer.functions import select_item [as 别名]
def prob(self, x):
        return F.select_item(self.all_prob, x) 
开发者ID:chainer,项目名称:chainerrl,代码行数:4,代码来源:distribution.py

示例6: log_prob

# 需要导入模块: from chainer import functions [as 别名]
# 或者: from chainer.functions import select_item [as 别名]
def log_prob(self, x):
        return F.select_item(self.all_log_prob, x) 
开发者ID:chainer,项目名称:chainerrl,代码行数:4,代码来源:distribution.py

示例7: max

# 需要导入模块: from chainer import functions [as 别名]
# 或者: from chainer.functions import select_item [as 别名]
def max(self):
        with chainer.force_backprop_mode():
            return F.select_item(self.q_values, self.greedy_actions) 
开发者ID:chainer,项目名称:chainerrl,代码行数:5,代码来源:action_value.py

示例8: evaluate_actions

# 需要导入模块: from chainer import functions [as 别名]
# 或者: from chainer.functions import select_item [as 别名]
def evaluate_actions(self, actions):
        return F.select_item(self.q_values, actions) 
开发者ID:chainer,项目名称:chainerrl,代码行数:4,代码来源:action_value.py

示例9: sampled_actions_log_probs

# 需要导入模块: from chainer import functions [as 别名]
# 或者: from chainer.functions import select_item [as 别名]
def sampled_actions_log_probs(self):
        return F.select_item(
            self.log_probs,
            chainer.Variable(np.asarray(self.action_indices, dtype=np.int32))) 
开发者ID:muupan,项目名称:async-rl,代码行数:6,代码来源:policy_output.py

示例10: gen_select_item_test

# 需要导入模块: from chainer import functions [as 别名]
# 或者: from chainer.functions import select_item [as 别名]
def gen_select_item_test(test_name):
    input = V(aranges(4, 3))
    indices = V([1, 2, 0, 1])
    output = F.select_item(input, indices)

    node = onnx.helper.make_node(
        'ChainerSelectItem',
        inputs=['input', 'indices'],
        outputs=['output'])
    expect(node, inputs=[input, indices], outputs=[output], name=test_name) 
开发者ID:pfnet-research,项目名称:chainer-compiler,代码行数:12,代码来源:gen_extra_test.py

示例11: check_forward

# 需要导入模块: from chainer import functions [as 别名]
# 或者: from chainer.functions import select_item [as 别名]
def check_forward(self, x_data, t_data):
        x = chainer.Variable(x_data)
        t = chainer.Variable(t_data)
        y = functions.select_item(x, t)
        y_exp = cuda.to_cpu(x_data)[range(t_data.size), cuda.to_cpu(t_data)]

        self.assertEqual(y.data.dtype, self.dtype)
        numpy.testing.assert_equal(cuda.to_cpu(y.data), y_exp) 
开发者ID:chainer,项目名称:chainer,代码行数:10,代码来源:test_select_item.py

示例12: check_backward

# 需要导入模块: from chainer import functions [as 别名]
# 或者: from chainer.functions import select_item [as 别名]
def check_backward(self, x_data, t_data, gy_data):
        gradient_check.check_backward(
            functions.select_item,
            (x_data, t_data), gy_data, eps=0.01, dtype='d',
            **self.check_backward_options) 
开发者ID:chainer,项目名称:chainer,代码行数:7,代码来源:test_select_item.py

示例13: check_double_backward

# 需要导入模块: from chainer import functions [as 别名]
# 或者: from chainer.functions import select_item [as 别名]
def check_double_backward(self, x_data, t_data, gy_data, ggx_data):
        def f(x):
            return functions.select_item(x, t_data)

        gradient_check.check_double_backward(
            f, x_data, gy_data, ggx_data, eps=0.01, dtype='d',
            **self.check_backward_options) 
开发者ID:chainer,项目名称:chainer,代码行数:9,代码来源:test_select_item.py

示例14: check_value_check

# 需要导入模块: from chainer import functions [as 别名]
# 或者: from chainer.functions import select_item [as 别名]
def check_value_check(self, x_data, t_data):
        x = chainer.Variable(x_data)
        t = chainer.Variable(t_data)

        if self.valid:
            # Check if it throws nothing
            functions.select_item(x, t)
        else:
            with self.assertRaises(ValueError):
                functions.select_item(x, t) 
开发者ID:chainer,项目名称:chainer,代码行数:12,代码来源:test_select_item.py


注:本文中的chainer.functions.select_item方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。