Python chainer.functions() Examples
The following are 30
code examples of chainer.functions().
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
, or try the search function
.
Example #1
Source File: nets.py From contextual_augmentation with MIT License | 6 votes |
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
Example #2
Source File: test_mlp_bn.py From chainerrl with MIT License | 6 votes |
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))
Example #3
Source File: test_trpo.py From chainerrl with MIT License | 6 votes |
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))
Example #4
Source File: reinforce.py From chainerrl with MIT License | 6 votes |
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
Example #5
Source File: test_trpo.py From chainerrl with MIT License | 6 votes |
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))
Example #6
Source File: nets.py From qb with MIT License | 6 votes |
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
Example #7
Source File: functions_dict.py From chainer-compiler with MIT License | 6 votes |
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
Example #8
Source File: vevaluator.py From chainer-compiler with MIT License | 6 votes |
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)
Example #9
Source File: vevaluator.py From chainer-compiler with MIT License | 6 votes |
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
Example #10
Source File: vevaluator.py From chainer-compiler with MIT License | 6 votes |
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)
Example #11
Source File: vevaluator.py From chainer-compiler with MIT License | 6 votes |
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)
Example #12
Source File: functions_builtin.py From chainer-compiler with MIT License | 6 votes |
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)
Example #13
Source File: functions_builtin.py From chainer-compiler with MIT License | 6 votes |
def vcall(self, module: 'values.Field', graph: 'graphs.Graph', inst: 'values.Object', args: 'functions.FunctionArgInput', context: 'functions.VEvalContext' = None, line=-1): if context._for_unroll: for ref in args.inputs: if not ref.get_value().has_constant_value(): assert False, 'Loop unrolling was requested for non-constant sequence at %s' % line refs = [] for num in range(*(ref.get_value().internal_value for ref in args.inputs)): refs.append(values.Object(values.NumberValue(num))) value = values.ListValue(refs) return values.Object(value) else: node = nodes.NodeGenerate( 'range', [v.get_value() for v in args.inputs], line) graph.add_node(node) value = values.RangeValue() value.name = '@F.{}.{}'.format(line, self.name) node.set_outputs([value]) return values.Object(value)
Example #14
Source File: functions_builtin.py From chainer-compiler with MIT License | 6 votes |
def vcall(self, module: 'values.Field', graph: 'graphs.Graph', inst: 'values.Object', args: 'functions.FunctionArgInput', context: 'functions.VEvalContext' = None, line=-1): assert(inst is None) funcArgs = self.args.merge_inputs(inst, args) vargs = funcArgs.get_value().inputs value = values.ListValue() if isinstance(vargs[0], values.NoneValue): node = nodes.NodeGenerate('List', [], line) graph.add_node(node) else: node = nodes.NodeConvert('List', vargs[0], line) graph.add_node(node) if vargs[0].has_constant_value(): refs = [] for attr_or_ref in vargs[0].internal_value: refs.append(utils.try_get_obj(attr_or_ref, 'list', utils.LineProperty())) value.internal_value = refs value.name = '@F.{}.{}'.format(line, self.name) node.set_outputs([value]) return values.Object(value)
Example #15
Source File: functions_builtin.py From chainer-compiler with MIT License | 6 votes |
def vcall(self, module: 'values.Field', graph: 'graphs.Graph', inst: 'values.Object', args: 'functions.FunctionArgInput', context: 'functions.VEvalContext' = None, line=-1): func_args = self.args.merge_inputs(inst, args) name = func_args.get_value().get_value(key='name') obj = func_args.keywords['object'] attr = obj.get_field().get_attribute(name.internal_value, graph.root_graph, False) # property(getter) if attr.has_obj() and isinstance(attr.get_obj().get_value(), values.FuncValue) and attr.get_obj().get_value().func.is_property: func_value = attr.get_obj().get_value() ret = func_value.func.vcall(func_value.module, graph, func_value.obj, functions.FunctionArgInput(), context, utils.LineProperty(line)) return ret if attr.has_obj(): return attr # if attr is not found gotten_obj = obj.try_get_and_store_obj(name.internal_value, graph.root_graph) if gotten_obj is not None: return obj.get_field().get_attribute(name.internal_value, graph.root_graph, False) return None
Example #16
Source File: functions_builtin.py From chainer-compiler with MIT License | 6 votes |
def vcall(self, module: 'values.Field', graph: 'graphs.Graph', inst: 'values.Object', args: 'functions.FunctionArgInput', context: 'functions.VEvalContext' = None, line=-1): func_args = self.args.merge_inputs(inst, args) name = func_args.get_value().get_value(key='name') obj = func_args.keywords['obj'] attr = obj.get_field().get_attribute(name.internal_value, graph.root_graph, False) if attr.has_obj(): return values.Object(values.BoolValue(True)) # if attr is not found gotten_obj = obj.try_get_and_store_obj(name.internal_value, graph.root_graph) if gotten_obj is not None: return values.Object(values.BoolValue(True)) return values.Object(values.BoolValue(False))
Example #17
Source File: chainer_chain.py From chainer-compiler with MIT License | 6 votes |
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
Example #18
Source File: nets.py From contextual_augmentation with MIT License | 6 votes |
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
Example #19
Source File: functions_builtin.py From chainer-compiler with MIT License | 5 votes |
def vcall(self, module: 'values.Field', graph: 'graphs.Graph', inst: 'values.Object', args: 'functions.FunctionArgInput', context: 'functions.VEvalContext' = None, line=-1): node = nodes.NodeCall(self, args, line) graph.add_node(node) item = args.get_value().inputs[0] default_value = None # constant propagation whenever possible if item.has_constant_value(): default_value = len(item.internal_value) value = values.NumberValue(default_value) value.name = '@F.{}.{}'.format(line, self.name) node.set_outputs([value]) return values.Object(value)
Example #20
Source File: functions_ndarray.py From chainer-compiler with MIT License | 5 votes |
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) #value = functions.generate_value_with_same_type(vargs[0]) value = self.ret_value_func() value.name = '@F.{}.{}'.format(line, self.name) node.set_outputs([value]) return values.Object(value)
Example #21
Source File: functions_ndarray.py From chainer-compiler with MIT License | 5 votes |
def vcall(self, module: 'values.Field', graph: 'graphs.Graph', inst: 'values.Object', args: 'functions.FunctionArgInput', context: 'functions.VEvalContext' = None, line=-1): args = functions.FunctionArgInput() args.inputs.append(inst) args.keywords['self'] = inst node = nodes.NodeCall(self, args, line) value = values.TupleValue() value.name = '@F.{}.{}'.format(line, self.name) node.set_outputs([value]) graph.add_node(node) return values.Object(value)
Example #22
Source File: functions_ndarray.py From chainer-compiler with MIT License | 5 votes |
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) vargs = funcArgs.get_value().inputs node = nodes.NodeCall(self, funcArgs, line) graph.add_node(node) value = functions.generate_value_with_same_type(vargs[0]) value.name = '@F.{}.{}'.format(line, self.name) node.set_outputs([value]) return values.Object(value)
Example #23
Source File: functions_ndarray.py From chainer-compiler with MIT License | 5 votes |
def vcall(self, module: 'values.Field', graph: 'graphs.Graph', inst: 'values.Object', args: 'functions.FunctionArgInput', context: 'functions.VEvalContext' = None, line=-1): assert(inst is None) funcArgs = self.args.merge_inputs(inst ,args) vargs = funcArgs.get_value().inputs node = nodes.NodeCall(self, funcArgs, line) graph.add_node(node) value = functions.generate_value_with_same_type(vargs[0]) value.name = '@F.{}.{}'.format(line, self.name) node.set_outputs([value]) return values.Object(value)
Example #24
Source File: functions_ndarray.py From chainer-compiler with MIT License | 5 votes |
def vcall(self, module: 'values.Field', graph: 'graphs.Graph', inst: 'values.Object', args: 'functions.FunctionArgInput', context: 'functions.VEvalContext' = None, line=-1): args = functions.FunctionArgInput() args.inputs.append(inst) args.keywords['self'] = inst node = nodes.NodeCall(self, args, line) value = values.NumberValue(None) value.dtype = np.array(0).dtype value.name = '@F.{}.{}'.format(line, self.name) node.set_outputs([value]) graph.add_node(node) return values.Object(value)
Example #25
Source File: functions_ndarray.py From chainer-compiler with MIT License | 5 votes |
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) value = values.TensorValue() value.name = '@F.{}.{}'.format(line, self.name) node.set_outputs([value]) return values.Object(value)
Example #26
Source File: functions_ndarray.py From chainer-compiler with MIT License | 5 votes |
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) value = values.TensorValue() value.name = '@F.{}.{}'.format(line, self.name) node.set_outputs([value]) return values.Object(value)
Example #27
Source File: functions_ndarray.py From chainer-compiler with MIT License | 5 votes |
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) value = values.TensorValue() value.name = '@F.{}.{}'.format(line, self.name) node.set_outputs([value]) return values.Object(value)
Example #28
Source File: functions_builtin.py From chainer-compiler with MIT License | 5 votes |
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) value = self.ret_value_func(funcArgs) value.name = '@F.{}.{}'.format(line, self.name) node.set_outputs([value]) return values.Object(value)
Example #29
Source File: functions_builtin.py From chainer-compiler with MIT License | 5 votes |
def vcall(self, module: 'values.Field', graph: 'graphs.Graph', inst: 'values.Object', args: 'functions.FunctionArgInput', context: 'functions.VEvalContext' = None, line=-1): node = nodes.NodeCopy(args.inputs[0].get_value()) graph.add_node(node) ret = functions.generate_copied_value(args.inputs[0].get_value()) node.set_outputs([ret]) return values.Object(ret)
Example #30
Source File: functions_ndarray.py From chainer-compiler with MIT License | 5 votes |
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) value = values.TensorValue() value.name = '@F.{}.{}'.format(line, self.name) node.set_outputs([value]) return values.Object(value)