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


Python chainer.functions方法代码示例

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


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

示例1: test

# 需要导入模块: import chainer [as 别名]
# 或者: from chainer import functions [as 别名]
def test(self):
        a = chainer.Variable(np.random.rand(1).astype(np.float32))
        b = chainer.Variable(np.random.rand(1).astype(np.float32))

        # No old-style function
        y = 2 * a + b
        old_style_funcs = trpo._find_old_style_function([y])
        self.assertEqual(old_style_funcs, [])

        # One old-style function
        y = 2 * old_style_identity(a) + b
        old_style_funcs = trpo._find_old_style_function([y])
        self.assertEqual(len(old_style_funcs), 1)
        self.assertTrue(all(isinstance(f, OldStyleIdentity)
                            for f in old_style_funcs))

        # Three old-style functions
        y = (2 * old_style_identity(old_style_identity(a))
             + old_style_identity(b))
        old_style_funcs = trpo._find_old_style_function([y])
        self.assertEqual(len(old_style_funcs), 3)
        self.assertTrue(all(isinstance(f, OldStyleIdentity)
                            for f in old_style_funcs)) 
开发者ID:chainer,项目名称:chainerrl,代码行数:25,代码来源:test_trpo.py

示例2: test_second_order

# 需要导入模块: import chainer [as 别名]
# 或者: from chainer import functions [as 别名]
def test_second_order(self):
        # Second order, so its Hessian will be non-zero
        params, y = self._generate_params_and_second_order_output()

        old_style_funcs = trpo._find_old_style_function([y])
        if old_style_funcs:
            self.skipTest("\
Chainer v{} does not support double backprop of these functions: {}.".format(
                chainer.__version__, old_style_funcs))

        def test_hessian_vector_product_nonzero(vec):
            hvp = compute_hessian_vector_product(y, params, vec)
            hessian = compute_hessian(y, params)
            self.assertGreater(np.count_nonzero(hvp), 0)
            self.assertGreater(np.count_nonzero(hessian), 0)
            np.testing.assert_allclose(hvp, hessian.dot(vec), atol=1e-3)

        # Test with two different random vectors, reusing y
        test_hessian_vector_product_nonzero(
            np.random.rand(4).astype(np.float32))
        test_hessian_vector_product_nonzero(
            np.random.rand(4).astype(np.float32)) 
开发者ID:chainer,项目名称:chainerrl,代码行数:24,代码来源:test_trpo.py

示例3: _test_call

# 需要导入模块: import chainer [as 别名]
# 或者: from chainer import functions [as 别名]
def _test_call(self, gpu):
        nonlinearity = getattr(F, self.nonlinearity)
        mlp = chainerrl.links.MLPBN(
            in_size=self.in_size,
            out_size=self.out_size,
            hidden_sizes=self.hidden_sizes,
            normalize_input=self.normalize_input,
            normalize_output=self.normalize_output,
            nonlinearity=nonlinearity,
            last_wscale=self.last_wscale,
        )
        batch_size = 7
        x = np.random.rand(batch_size, self.in_size).astype(np.float32)
        if gpu >= 0:
            mlp.to_gpu(gpu)
            x = chainer.cuda.to_gpu(x)
        y = mlp(x)
        self.assertEqual(y.shape, (batch_size, self.out_size))
        self.assertEqual(chainer.cuda.get_array_module(y),
                         chainer.cuda.get_array_module(x)) 
开发者ID:chainer,项目名称:chainerrl,代码行数:22,代码来源:test_mlp_bn.py

示例4: accumulate_grad

# 需要导入模块: import chainer [as 别名]
# 或者: from chainer import functions [as 别名]
def accumulate_grad(self):
        if self.n_backward == 0:
            self.model.cleargrads()
        # Compute losses
        losses = []
        for r_seq, log_prob_seq, ent_seq in zip(self.reward_sequences,
                                                self.log_prob_sequences,
                                                self.entropy_sequences):
            assert len(r_seq) - 1 == len(log_prob_seq) == len(ent_seq)
            # Convert rewards into returns (=sum of future rewards)
            R_seq = np.cumsum(list(reversed(r_seq[1:])))[::-1]
            for R, log_prob, entropy in zip(R_seq, log_prob_seq, ent_seq):
                loss = -R * log_prob - self.beta * entropy
                losses.append(loss)
        total_loss = chainerrl.functions.sum_arrays(losses)
        # When self.batchsize is future.types.newint.newint, dividing a
        # Variable with it will raise an error, so it is manually converted to
        # float here.
        total_loss /= float(self.batchsize)
        F.squeeze(total_loss).backward()
        self.reward_sequences = [[]]
        self.log_prob_sequences = [[]]
        self.entropy_sequences = [[]]
        self.n_backward += 1 
开发者ID:chainer,项目名称:chainerrl,代码行数:26,代码来源:reinforce.py

示例5: block_embed

# 需要导入模块: import chainer [as 别名]
# 或者: from chainer import functions [as 别名]
def block_embed(embed, x, dropout=0.):
    """Embedding function followed by convolution

    Args:
        embed (callable): A :func:`~chainer.functions.embed_id` function
            or :class:`~chainer.links.EmbedID` link.
        x (:class:`~chainer.Variable` or :class:`numpy.ndarray` or \
        :class:`cupy.ndarray`): Input variable, which
            is a :math:`(B, L)`-shaped int array. Its first dimension
            :math:`(B)` is assumed to be the *minibatch dimension*.
            The second dimension :math:`(L)` is the length of padded
            sentences.
        dropout (float): Dropout ratio.

    Returns:
        ~chainer.Variable: Output variable. A float array with shape
        of :math:`(B, N, L, 1)`. :math:`(N)` is the number of dimensions
        of word embedding.

    """
    e = embed(x)
    e = F.dropout(e, ratio=dropout)
    e = F.transpose(e, (0, 2, 1))
    e = e[:, :, :, None]
    return e 
开发者ID:Pinafore,项目名称:qb,代码行数:27,代码来源:nets.py

示例6: vcall

# 需要导入模块: import chainer [as 别名]
# 或者: from chainer import functions [as 别名]
def vcall(self, module: 'values.Field', graph: 'graphs.Graph', inst: 'values.Object', args: 'functions.FunctionArgInput',
              option: 'vevaluator.VEvalContext' = None, line=-1):
        funcArgs = self.args.merge_inputs(inst, args)

        if inst.in_container:
            raise Exception('Invalid operation')

        keys = inst.get_value().internal_keys.values()

        vargs = []
        vargs_value = []
        for varg in keys:
            vargs.append(utils.try_get_obj(varg, 'dict_keys', utils.LineProperty()))
            vargs_value.append(utils.try_get_obj(varg, 'dict_keys', utils.LineProperty()).get_value())

        node = nodes.NodeGenerate('List', vargs_value , line)
        graph.add_node(node)

        value = values.ListValue(vargs)
        value.name = '@F.{}.{}'.format(line, self.name)
        node.set_outputs([value])
        return value 
开发者ID:pfnet-research,项目名称:chainer-compiler,代码行数:24,代码来源:functions_dict.py

示例7: veval_ast_bool_op

# 需要导入模块: import chainer [as 别名]
# 或者: from chainer import functions [as 别名]
def veval_ast_bool_op(astc : 'AstContext', local_field : 'values.Field', graph : 'graphs.Graph', context : 'functions.VEvalContext' = None):
    """
    eval bool operations.
    Ex. x and y
    """
    assert(isinstance(astc.nast, gast.gast.BoolOp))
    lineprop = utils.LineProperty(astc.lineno, astc.filename)

    multiaryop = nodes.MultiaryOpType.Unknown
    if isinstance(astc.nast.op, gast.And):
        multiaryop = nodes.MultiaryOpType.And
    if isinstance(astc.nast.op, gast.Or):
        multiaryop = nodes.MultiaryOpType.Or

    values_list = [veval_ast(astc.c(value_), local_field, graph, context) for value_ in astc.nast.values]
    values_list_value = [utils.try_get_value(value_, 'multiary', lineprop) for value_ in values_list]

    node = nodes.NodeMultiaryOp(values_list_value, multiaryop)

    ret_value = veval_multiary.veval(multiaryop, values_list_value)
    node.set_outputs([ret_value])
    graph.add_node(node)

    return values.Object(ret_value) 
开发者ID:pfnet-research,项目名称:chainer-compiler,代码行数:26,代码来源:vevaluator.py

示例8: veval_ast_name_constant

# 需要导入模块: import chainer [as 别名]
# 或者: from chainer import functions [as 别名]
def veval_ast_name_constant(astc : 'AstContext', local_field : 'values.Field', graph : 'Graph', context : 'functions.VEvalContext' = None):
    '''
    Ex. True
    '''
    assert(isinstance(astc.nast, gast.gast.Constant))
    lineprop = utils.LineProperty(astc.lineno, astc.filename)
    ret = None
    if astc.nast.value == True:
        ret = values.Object(values.BoolValue(True))
    elif astc.nast.value == False:
        ret = values.Object(values.BoolValue(False))
    elif astc.nast.value is None:
        ret = values.Object(values.NoneValue())
    else:
        print("Invalid name constant: {}".format(astc.nast.value))
        assert False

    name = values.create_ref_value_name_with_constant(ret)
    ret.name = name
    ret.get_value().name = name
    return ret 
开发者ID:pfnet-research,项目名称:chainer-compiler,代码行数:23,代码来源:vevaluator.py

示例9: veval_ast_list

# 需要导入模块: import chainer [as 别名]
# 或者: from chainer import functions [as 别名]
def veval_ast_list(astc : 'AstContext', local_field : 'values.Field', graph : 'Graph', context : 'functions.VEvalContext' = None):
    assert(isinstance(astc.nast, gast.gast.List))
    '''
    Ex. [],[x,y,z]
    TODO : Initializer
    '''
    lineprop = utils.LineProperty(astc.lineno, astc.filename)

    elts = []
    for elt in astc.nast.elts:
        elt_ = veval_ast(astc.c(elt), local_field, graph, context)
        elt_obj = utils.try_get_obj(elt_,'list', lineprop)
        elts.append(elt_obj)

    node = nodes.NodeGenerate('List', [elt.get_value() for elt in elts], lineprop)
    graph.add_node(node)
    value = values.ListValue(elts)
    node.set_outputs([value])

    return values.Object(value) 
开发者ID:pfnet-research,项目名称:chainer-compiler,代码行数:22,代码来源:vevaluator.py

示例10: veval_ast_dict

# 需要导入模块: import chainer [as 别名]
# 或者: from chainer import functions [as 别名]
def veval_ast_dict(astc : 'AstContext', local_field : 'values.Field', graph : 'Graph', context : 'functions.VEvalContext' = None):
    assert(isinstance(astc.nast, gast.gast.Dict))
    lineprop = utils.LineProperty(astc.lineno, astc.filename)

    keys = []
    elts = []

    for key, elt in zip(astc.nast.keys, astc.nast.values):
        key_ = veval_ast(astc.c(key), local_field, graph, context)
        elt_ = veval_ast(astc.c(elt), local_field, graph, context)
        key_obj = utils.try_get_obj(key_, 'dict', lineprop)
        elt_obj = utils.try_get_obj(elt_,'dict', lineprop)
        keys.append(key_obj)
        elts.append(return_value_or_obj(elt_obj))

    value = values.DictValue(keys, elts)

    return values.Object(value) 
开发者ID:pfnet-research,项目名称:chainer-compiler,代码行数:20,代码来源:vevaluator.py

示例11: vcall

# 需要导入模块: import chainer [as 别名]
# 或者: from chainer import functions [as 别名]
def vcall(self, module: 'values.Field', graph: 'graphs.Graph', inst: 'values.Object', args: 'functions.FunctionArgInput',
              context: 'functions.VEvalContext' = None, line=-1):
        funcArgs = self.args.merge_inputs(inst, args)

        node = nodes.NodeCall(self, funcArgs, line)
        graph.add_node(node)
        
        axis = funcArgs.keywords['axis']
        if isinstance(axis, values.NoneValue):
            value = values.NumberValue(None)
            value.dtype = np.int32
        else:
            value = values.TensorValue()
            value.dtype = np.int32

        value.name = '@F.{}.{}'.format(line, self.name)
        node.set_outputs([value])
        return values.Object(value) 
开发者ID:pfnet-research,项目名称:chainer-compiler,代码行数:20,代码来源:functions_builtin.py

示例12: forward

# 需要导入模块: import chainer [as 别名]
# 或者: from chainer import functions [as 别名]
def forward(self, x, layers=None, **kwargs):
        if layers is None:
            layers = ['prob']

        if kwargs:
            argument.check_unexpected_kwargs(
                kwargs, test='test argument is not supported anymore. '
                'Use chainer.using_config'
            )
            argument.assert_kwargs_empty(kwargs)

        h = x
        activations = {}
        target_layers = set(layers)
        for key, funcs in self.functions.items():
            if len(target_layers) == 0:
                break
            for func in funcs:
                h = func(h)
            if key in target_layers:
                activations[key] = h
                target_layers.remove(key)
        return activations 
开发者ID:pfnet-research,项目名称:chainer-compiler,代码行数:25,代码来源:chainer_chain.py

示例13: test_first_order

# 需要导入模块: import chainer [as 别名]
# 或者: from chainer import functions [as 别名]
def test_first_order(self):
        # First order, so its Hessian will contain None
        params, y = self._generate_params_and_first_order_output()

        old_style_funcs = trpo._find_old_style_function([y])
        if old_style_funcs:
            self.skipTest("\
Chainer v{} does not support double backprop of these functions: {}.".format(
                chainer.__version__, old_style_funcs))

        vec = np.random.rand(4).astype(np.float32)
        # Hessian-vector product computation should raise an error due to None
        with self.assertRaises(AssertionError):
            compute_hessian_vector_product(y, params, vec) 
开发者ID:chainer,项目名称:chainerrl,代码行数:16,代码来源:test_trpo.py

示例14: _test_call

# 需要导入模块: import chainer [as 别名]
# 或者: from chainer import functions [as 别名]
def _test_call(self, gpu):
        nonlinearity = getattr(F, self.nonlinearity)
        model = chainerrl.q_functions.FCSAQFunction(
            n_dim_obs=self.n_dim_obs,
            n_dim_action=self.n_dim_action,
            n_hidden_layers=self.n_hidden_layers,
            n_hidden_channels=self.n_hidden_channels,
            nonlinearity=nonlinearity,
            last_wscale=self.last_wscale,
        )
        self._test_call_given_model(model, gpu) 
开发者ID:chainer,项目名称:chainerrl,代码行数:13,代码来源:test_state_action_q_function.py

示例15: _test_call

# 需要导入模块: import chainer [as 别名]
# 或者: from chainer import functions [as 别名]
def _test_call(self, gpu):
        # This method only check if a given model can receive random input
        # data and return output data with the correct interface.
        nonlinearity = getattr(F, self.nonlinearity)
        min_action = np.full((self.action_size,), -0.01, dtype=np.float32)
        max_action = np.full((self.action_size,), 0.01, dtype=np.float32)
        model = self._make_model(
            n_input_channels=self.n_input_channels,
            action_size=self.action_size,
            bound_action=self.bound_action,
            min_action=min_action,
            max_action=max_action,
            nonlinearity=nonlinearity,
        )

        batch_size = 7
        x = np.random.rand(
            batch_size, self.n_input_channels).astype(np.float32)
        if gpu >= 0:
            model.to_gpu(gpu)
            x = chainer.cuda.to_gpu(x)
            min_action = chainer.cuda.to_gpu(min_action)
            max_action = chainer.cuda.to_gpu(max_action)
        y = model(x)
        self.assertTrue(isinstance(
            y, chainerrl.distribution.ContinuousDeterministicDistribution))
        a = y.sample()
        self.assertTrue(isinstance(a, chainer.Variable))
        self.assertEqual(a.shape, (batch_size, self.action_size))
        self.assertEqual(chainer.cuda.get_array_module(a),
                         chainer.cuda.get_array_module(x))
        if self.bound_action:
            self.assertTrue((a.array <= max_action).all())
            self.assertTrue((a.array >= min_action).all()) 
开发者ID:chainer,项目名称:chainerrl,代码行数:36,代码来源:test_deterministic_policy.py


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