本文整理汇总了Python中nengo.utils.compat.is_iterable函数的典型用法代码示例。如果您正苦于以下问题:Python is_iterable函数的具体用法?Python is_iterable怎么用?Python is_iterable使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了is_iterable函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: extend
def extend(self, keys, unitary=False):
"""Extends the vocabulary with additional keys.
Creates and adds the semantic pointers listed in keys to the
vocabulary.
Parameters
----------
keys : list
List of semantic pointer names to be added to the vocabulary.
unitary : bool or list, optional (Default: False)
If True, all generated pointers will be unitary. If a list of
strings, any pointer whose name is on the list will be forced to
be unitary when created.
"""
if is_iterable(unitary):
if is_iterable(self.unitary):
self.unitary.extend(unitary)
else:
self.unitary = list(unitary)
elif unitary:
if is_iterable(self.unitary):
self.unitary.extend(keys)
else:
self.unitary = list(keys)
for key in keys:
if key not in self.keys:
self[key]
示例2: __init__
def __init__(self, shape_in, filters, biases=None, stride=1, padding=0, activation='linear'): # noqa: C901
from nengo.utils.compat import is_iterable, is_integer
self.shape_in = tuple(shape_in)
self.filters = filters
self.stride = stride if is_iterable(stride) else [stride] * 2
self.padding = padding if is_iterable(padding) else [padding] * 2
self.activation=activation
nf = self.filters.shape[0]
nxi, nxj = self.shape_in[1:]
si, sj = self.filters.shape[-2:]
pi, pj = self.padding
sti, stj = self.stride
nyi = 1 + max(int(np.ceil((2*pi + nxi - si) / float(sti))), 0)
nyj = 1 + max(int(np.ceil((2*pj + nxj - sj) / float(stj))), 0)
self.shape_out = (nf, nyi, nyj)
self.biases = biases if biases is not None else None
if self.biases is not None:
if self.biases.size == 1:
self.biases.shape = (1, 1, 1)
elif self.biases.size == np.prod(self.shape_out):
self.biases.shape = self.shape_out
elif self.biases.size == self.shape_out[0]:
self.biases.shape = (self.shape_out[0], 1, 1)
elif self.biases.size == np.prod(self.shape_out[1:]):
self.biases.shape = (1,) + self.shape_out[1:]
super(Conv2d, self).__init__(
default_size_in=np.prod(self.shape_in),
default_size_out=np.prod(self.shape_out))
示例3: __init__
def __init__(self, shape_in, pool_size, strides=None,
kind='avg', mode='full'):
self.shape_in = shape_in
self.pool_size = (pool_size if is_iterable(pool_size) else
[pool_size] * 2)
self.strides = (strides if is_iterable(strides) else
[strides] * 2 if strides is not None else
self.pool_size)
self.kind = kind
self.mode = mode
if not all(st <= p for st, p in zip(self.strides, self.pool_size)):
raise ValueError("Strides %s must be <= pool_size %s" %
(self.strides, self.pool_size))
nc, nxi, nxj = self.shape_in
nyi_float = float(nxi - self.pool_size[0]) / self.strides[0]
nyj_float = float(nxj - self.pool_size[1]) / self.strides[1]
if self.mode == 'full':
nyi = 1 + int(np.ceil(nyi_float))
nyj = 1 + int(np.ceil(nyj_float))
elif self.mode == 'valid':
nyi = 1 + int(np.floor(nyi_float))
nyj = 1 + int(np.floor(nyj_float))
self.shape_out = (nc, nyi, nyj)
super(Pool2d, self).__init__(
default_size_in=np.prod(self.shape_in),
default_size_out=np.prod(self.shape_out))
示例4: add_input
def add_input(self, name, input_vectors, input_scales=1.0):
# Handle different vocabulary types
if is_iterable(input_vectors):
input_vectors = np.matrix(input_vectors)
# Handle possible different input_scale values for each
# element in the associative memory
if not is_iterable(input_scales):
input_scales = np.matrix([input_scales] * input_vectors.shape[0])
else:
input_scales = np.matrix(input_scales)
if input_scales.shape[1] != input_vectors.shape[0]:
raise ValueError(
'Number of input_scale values do not match number of input '
'vectors. Got: %d, expected %d.' %
(input_scales.shape[1], input_vectors.shape[0]))
input = nengo.Node(size_in=input_vectors.shape[1], label=name)
if hasattr(self, name):
raise NameError('Name "%s" already exists as a node in the'
'associative memory.')
else:
setattr(self, name, input)
nengo.Connection(input, self.elem_input,
synapse=None,
transform=np.multiply(input_vectors, input_scales.T))
示例5: simplify
def simplify(self):
is_num = lambda x: isinstance(x, NumExp)
if isinstance(self.fn, str):
return self # cannot simplify
elif all(map(is_num, self.args)):
# simplify scalar function
return NumExp(self.fn(*[a.value for a in self.args]))
elif all(is_num(a) or is_iterable(a) and all(map(is_num, a))
for a in self.args):
# simplify vector function
return NumExp(self.fn(
[[aa.value for aa in a] if is_iterable(a) else a.value
for a in self.args]))
else:
return self # cannot simplify
示例6: add_output
def add_output(self, name, function, synapse=None, **conn_kwargs):
dims_per_ens = self.dimensions_per_ensemble
# get output size for each ensemble
sizes = np.zeros(self.n_ensembles, dtype=int)
if is_iterable(function) and all(callable(f) for f in function):
if len(list(function)) != self.n_ensembles:
raise ValidationError(
"Must have one function per ensemble", attr='function')
for i, func in enumerate(function):
sizes[i] = np.asarray(func(np.zeros(dims_per_ens))).size
elif callable(function):
sizes[:] = np.asarray(function(np.zeros(dims_per_ens))).size
function = [function] * self.n_ensembles
elif function is None:
sizes[:] = dims_per_ens
function = [None] * self.n_ensembles
else:
raise ValidationError("'function' must be a callable, list of "
"callables, or None", attr='function')
output = nengo.Node(output=None, size_in=sizes.sum(), label=name)
setattr(self, name, output)
indices = np.zeros(len(sizes) + 1, dtype=int)
indices[1:] = np.cumsum(sizes)
for i, e in enumerate(self.ea_ensembles):
nengo.Connection(
e, output[indices[i]:indices[i+1]], function=function[i],
synapse=synapse, **conn_kwargs)
return output
示例7: validate
def validate(self, instance, rule):
if is_iterable(rule):
for r in (itervalues(rule) if isinstance(rule, dict) else rule):
self.validate_rule(instance, r)
elif rule is not None:
self.validate_rule(instance, rule)
super(LearningRuleTypeParam, self).validate(instance, rule)
示例8: validate
def validate(self, instance, rule):
if is_iterable(rule):
for lr in rule:
self.validate_rule(instance, lr)
elif rule is not None:
self.validate_rule(instance, rule)
super(LearningRuleParam, self).validate(instance, rule)
示例9: __getitem__
def __getitem__(self, item):
if isinstance(item, slice):
item = np.arange(len(self))[item]
if is_iterable(item):
rval = self.__class__.__new__(self.__class__)
rval.starts = [self.starts[i] for i in item]
rval.shape0s = [self.shape0s[i] for i in item]
rval.shape1s = [self.shape1s[i] for i in item]
rval.stride0s = [self.stride0s[i] for i in item]
rval.stride1s = [self.stride1s[i] for i in item]
rval.buf = self.buf
rval.names = [self.names[i] for i in item]
return rval
else:
if isinstance(item, np.ndarray):
item.shape = () # avoid numpy DeprecationWarning
itemsize = self.dtype.itemsize
shape = (self.shape0s[item], self.shape1s[item])
byteoffset = itemsize * self.starts[item]
bytestrides = (itemsize * self.stride0s[item],
itemsize * self.stride1s[item])
return np.ndarray(
shape=shape, dtype=self.dtype, buffer=self.buf.data,
offset=byteoffset, strides=bytestrides)
示例10: add_output_mapping
def add_output_mapping(self, name, output_vectors):
"""Adds another output to the associative memory network.
Creates a transform with the given output vectors between the
associative memory element output and a named output node to enable the
selection of output vectors by the associative memory.
Parameters
----------
name: str
Name to use for the output node. This name will be used as
the name of the attribute for the associative memory network.
output_vectors: array_like
The list of vectors to be produced for each match.
"""
# --- Put arguments in canonical form
if is_iterable(output_vectors):
output_vectors = np.array(output_vectors, ndmin=2)
# --- Check preconditions
if hasattr(self, name):
raise ValidationError("Name '%s' already exists as a node in the "
"associative memory." % name, attr='name')
# --- Make the output node and connect it
output = nengo.Node(size_in=output_vectors.shape[1], label=name)
setattr(self, name, output)
if self.thresh_ens is not None:
c = nengo.Connection(self.thresh_ens.output, output,
synapse=None, transform=output_vectors.T)
else:
c = nengo.Connection(self.elem_output, output,
synapse=None, transform=output_vectors.T)
self.out_conns.append(c)
示例11: __init__
def __init__(self, fn, in_dims=None, out_dim=None):
if in_dims is not None and not is_iterable(in_dims):
in_dims = [in_dims]
self.fn = fn
self.in_dims = in_dims
self.out_dim = out_dim
self._translator = None
示例12: add_input_mapping
def add_input_mapping(self, name, input_vectors, input_scales=1.0):
"""Adds a set of input vectors to the associative memory network.
Creates a transform with the given input vectors between the
a named input node and associative memory element input to enable the
inputs to be mapped onto ensembles of the Associative Memory.
Parameters
----------
name: string
Name to use for the input node. This name will be used as the name
of the attribute for the associative memory network.
input_vectors: array_like
The list of vectors to be compared against.
input_scales: array_list, optional
Scaling factor to apply on each of the input vectors. Note that it
is possible to scale each vector independently.
"""
# --- Put arguments in canonical form
n_vectors, d_vectors = input_vectors.shape
if is_iterable(input_vectors):
input_vectors = np.array(input_vectors, ndmin=2)
if not is_iterable(input_scales):
input_scales = input_scales * np.ones((1, n_vectors))
else:
input_scales = np.array(input_scales, ndmin=2)
# --- Check some preconditions
if input_scales.shape[1] != n_vectors:
raise ValidationError("Number of input_scale values (%d) does not "
"match number of input vectors (%d)."
% (input_scales.shape[1], n_vectors),
attr='input_scales')
if hasattr(self, name):
raise ValidationError("Name '%s' already exists as a node in the "
"associative memory." % name, attr='name')
# --- Finally, make the input node and connect it
in_node = nengo.Node(size_in=d_vectors, label=name)
setattr(self, name, in_node)
nengo.Connection(in_node, self.elem_input,
synapse=None,
transform=input_vectors * input_scales.T)
示例13: function
def function(self, _function):
if _function is not None:
self._check_pre_ensemble('function')
x = (self.eval_points[0] if is_iterable(self.eval_points) else
np.zeros(self._pre.dimensions))
size = np.asarray(_function(x)).size
else:
size = 0
self._function = (_function, size)
self._check_shapes()
示例14: make_step
def make_step(self, shape_in, shape_out, dt, rng):
size_out = shape_out[0] if is_iterable(shape_out) else shape_out
if self.base_output is None:
f = self.passthrough
elif isinstance(self.base_output, Process):
f = self.base_output.make_step(shape_in, shape_out, dt, rng)
else:
f = self.base_output
return self.Step(size_out, f,
to_client=self.to_client,
from_client=self.from_client)
示例15: _broadcast_args
def _broadcast_args(self, func, args):
"""Apply 'func' element-wise to lists of args"""
as_list = lambda x: list(x) if is_iterable(x) else [x]
args = list(map(as_list, args))
arg_lens = list(map(len, args))
max_len = max(arg_lens)
assert all(n in [0, 1, max_len] for n in arg_lens), (
"Could not broadcast arguments with lengths %s" % arg_lens)
result = [func(*[a[i] if len(a) > 1 else a[0] for a in args])
for i in range(max_len)]
result = [r.simplify() for r in result]
return result[0] if len(result) == 1 else result