本文整理汇总了Python中theano.tensor.as_tensor_variable函数的典型用法代码示例。如果您正苦于以下问题:Python as_tensor_variable函数的具体用法?Python as_tensor_variable怎么用?Python as_tensor_variable使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了as_tensor_variable函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_downsample
def test_downsample():
shps = [
(1, 1, 1, 12),
(1, 1, 2, 2),
(1, 1, 1, 1),
(1, 1, 4, 4),
(1, 1, 10, 11),
(1, 2, 2, 2),
(3, 5, 4, 4),
(25, 1, 7, 7),
(1, 1, 12, 12),
(1, 1, 2, 14),
(1, 1, 12, 14),
(1, 1, 14, 14),
(1, 1, 16, 16),
(1, 1, 18, 18),
(1, 1, 24, 24),
(1, 6, 24, 24),
(10, 1, 24, 24),
(10, 6, 24, 24),
(30, 6, 12, 12),
(30, 2, 24, 24),
(30, 6, 24, 24),
(10, 10, 10, 11),
(1, 1, 10, 1025),
(1, 1, 10, 1023),
(1, 1, 1025, 10),
(1, 1, 1023, 10),
]
numpy.random.RandomState(unittest_tools.fetch_seed()).shuffle(shps)
for shp in shps:
for ds in (2, 2), (3, 2), (1, 1):
if ds[0] > shp[2]:
continue
if ds[1] > shp[3]:
continue
# GpuDownsampleFactorMax doesn't like having more than 512 columns
# in the output tensor.
if float(shp[3]) / ds[1] > 512:
continue
for ignore_border in (True, False):
print "test_downsample", shp, ds, ignore_border
ds_op = DownsampleFactorMax(ds, ignore_border=ignore_border)
a = tcn.shared_constructor(my_rand(*shp), "a")
f = pfunc([], ds_op(tensor.as_tensor_variable(a)), mode=mode_with_gpu)
f2 = pfunc([], ds_op(tensor.as_tensor_variable(a)), mode=mode_without_gpu)
assert any([isinstance(node.op, tcn.blas.GpuDownsampleFactorMax) for node in f.maker.env.toposort()])
assert any([isinstance(node.op, DownsampleFactorMax) for node in f2.maker.env.toposort()])
assert numpy.allclose(f(), f2())
g = pfunc([], tensor.grad(ds_op(tensor.as_tensor_variable(a)).sum(), a), mode=mode_with_gpu)
g2 = pfunc([], tensor.grad(ds_op(tensor.as_tensor_variable(a)).sum(), a), mode=mode_without_gpu)
assert any(
[isinstance(node.op, tcn.blas.GpuDownsampleFactorMaxGrad) for node in g.maker.env.toposort()]
)
assert any([isinstance(node.op, DownsampleFactorMaxGrad) for node in g2.maker.env.toposort()])
assert numpy.allclose(g(), g2())
示例2: make_node
def make_node(self, a, n, axis):
a = tensor.as_tensor_variable(a)
if a.ndim < 1:
raise TypeError('%s: input must be an array, not a scalar' %
self.__class__.__name__)
if axis is None:
axis = a.ndim - 1
axis = tensor.as_tensor_variable(axis)
else:
axis = tensor.as_tensor_variable(axis)
if (not axis.dtype.startswith('int')) and \
(not axis.dtype.startswith('uint')):
raise TypeError('%s: index of the transformed axis must be'
' of type integer' % self.__class__.__name__)
elif axis.ndim != 0 or (isinstance(axis, tensor.TensorConstant) and
(axis.data < 0 or axis.data > a.ndim - 1)):
raise TypeError('%s: index of the transformed axis must be'
' a scalar not smaller than 0 and smaller than'
' dimension of array' % self.__class__.__name__)
if n is None:
n = a.shape[axis]
n = tensor.as_tensor_variable(n)
else:
n = tensor.as_tensor_variable(n)
if (not n.dtype.startswith('int')) and \
(not n.dtype.startswith('uint')):
raise TypeError('%s: length of the transformed axis must be'
' of type integer' % self.__class__.__name__)
elif n.ndim != 0 or (isinstance(n, tensor.TensorConstant) and
n.data < 1):
raise TypeError('%s: length of the transformed axis must be a'
' strictly positive scalar'
% self.__class__.__name__)
return gof.Apply(self, [a, n, axis], [tensor.TensorType('complex128',
a.type.broadcastable)()])
示例3: __init__
def __init__(self, distribution, lower, upper, transform="infer", *args, **kwargs):
dtype = kwargs.get("dtype", theano.config.floatX)
if lower is not None:
lower = tt.as_tensor_variable(lower).astype(dtype)
if upper is not None:
upper = tt.as_tensor_variable(upper).astype(dtype)
if transform == "infer":
if lower is None and upper is None:
transform = None
default = None
elif lower is not None and upper is not None:
transform = transforms.interval(lower, upper)
default = 0.5 * (lower + upper)
elif upper is not None:
transform = transforms.upperbound(upper)
default = upper - 1
else:
transform = transforms.lowerbound(lower)
default = lower + 1
else:
default = None
super().__init__(
distribution, lower, upper, default, *args, transform=transform, **kwargs
)
示例4: quantized_lognormal_sampler
def quantized_lognormal_sampler(
rstream, mu=0.0, sigma=1.0, step=1, draw_shape=None, ndim=None, dtype=theano.config.floatX
):
"""
Sample from a quantized log-normal distribution centered on avg with
the specified standard deviation (std).
If the size argument is ambiguous on the number of dimensions, ndim
may be a plain integer to supplement the missing information.
If size is None, the output shape will be determined by the shapes
of avg and std.
If dtype is not specified, it will be inferred from the dtype of
avg and std, but will be at least as precise as floatX.
"""
mu = tensor.as_tensor_variable(mu)
sigma = tensor.as_tensor_variable(sigma)
step = tensor.as_tensor_variable(step)
if dtype == None:
dtype = tensor.scal.upcast(theano.config.floatX, mu.dtype, sigma.dtype, step.dtype)
rstate = rstream.new_shared_rstate()
ndim, draw_shape, bcast = tensor.raw_random._infer_ndim_bcast(ndim, draw_shape, mu, sigma)
op = QuantizedLognormal(otype=tensor.TensorType(dtype=dtype, broadcastable=bcast))
new_rstate, out = op(rstate, draw_shape, mu, sigma, step)
rstream.add_default_update(out, rstate, new_rstate)
return out
示例5: make_node
def make_node(self, activations, labels, input_lengths):
t_activations = T.as_tensor_variable(activations)
# Ensure activations array is C-contiguous
t_activations = cpu_contiguous(t_activations)
t_labels = T.as_tensor_variable(labels)
t_input_lengths = T.as_tensor_variable(input_lengths)
if t_activations.type.dtype != 'float32':
raise TypeError('activations must use the float32 type!')
if t_activations.ndim != 3:
raise ValueError('activations must have 3 dimensions.')
if t_labels.type.dtype != 'int32':
raise TypeError('labels must use the int32 type!')
if t_labels.ndim != 2:
raise ValueError('labels must have 2 dimensions.')
if t_input_lengths.type.dtype != 'int32':
raise TypeError('input_lengths must use the int32 type!')
if t_input_lengths.ndim != 1:
raise ValueError('input_lengths must have 1 dimension.')
costs = T.fvector(name="ctc_cost")
outputs = [costs]
if self.compute_grad:
gradients = T.ftensor3(name="ctc_grad")
outputs += [gradients]
return gof.Apply(self, inputs=[t_activations, t_labels, t_input_lengths],
outputs=outputs)
示例6: make_node
def make_node(self,x,y):
if x.type.ndim != y.type.ndim:
raise TypeError()
# TODO: consider restricting the dtype?
x = tensor.as_tensor_variable(x)
y = tensor.as_tensor_variable(y)
return gof.Apply(self, [x,y], [])
示例7: test_neibs_bad_shape_wrap_centered
def test_neibs_bad_shape_wrap_centered(self):
shape = (2, 3, 10, 10)
for dtype in self.dtypes:
images = shared(numpy.arange(
numpy.prod(shape), dtype=dtype
).reshape(shape))
for neib_shape in [(3, 2), (2, 3)]:
neib_shape = T.as_tensor_variable(neib_shape)
f = function([], images2neibs(images, neib_shape,
mode="wrap_centered"),
mode=self.mode)
self.assertRaises(TypeError, f)
for shape in [(2, 3, 2, 3), (2, 3, 3, 2)]:
images = shared(numpy.arange(numpy.prod(shape)).reshape(shape))
neib_shape = T.as_tensor_variable((3, 3))
f = function([], images2neibs(images, neib_shape,
mode="wrap_centered"),
mode=self.mode)
self.assertRaises(TypeError, f)
# Test a valid shapes
shape = (2, 3, 3, 3)
images = shared(numpy.arange(numpy.prod(shape)).reshape(shape))
neib_shape = T.as_tensor_variable((3, 3))
f = function([],
images2neibs(images, neib_shape, mode="wrap_centered"),
mode=self.mode)
f()
示例8: make_node
def make_node(self, x, scale, shift):
if x.type.ndim != 4:
raise TypeError()
x = tensor.as_tensor_variable(x)
scale = tensor.as_tensor_variable(scale)
shift = tensor.as_tensor_variable(shift)
return gof.Apply(self, [x, scale, shift], [x.type()])
示例9: __init__
def __init__(self, distribution, lower, upper,
transform='infer', *args, **kwargs):
dtype = kwargs.get('dtype', theano.config.floatX)
if lower is not None:
lower = tt.as_tensor_variable(lower).astype(dtype)
if upper is not None:
upper = tt.as_tensor_variable(upper).astype(dtype)
if transform == 'infer':
if lower is None and upper is None:
transform = None
default = None
elif lower is not None and upper is not None:
transform = transforms.interval(lower, upper)
default = 0.5 * (lower + upper)
elif upper is not None:
transform = transforms.upperbound(upper)
default = upper - 1
else:
transform = transforms.lowerbound(lower)
default = lower + 1
else:
default = None
super(_ContinuousBounded, self).__init__(
distribution=distribution, lower=lower, upper=upper,
transform=transform, default=default, *args, **kwargs)
示例10: _traverse
def _traverse(node):
""" TODO: writeme """
if node is None:
return None
else:
op = node.op
inputs = node.inputs
# Compute the evaluation points corresponding to each of the
# inputs of the node
local_eval_points = []
for inp in inputs:
if inp in wrt:
local_eval_points.append(eval_points[wrt.index(inp)])
elif inp.owner is None:
local_eval_points.append(inp.zeros_like())
elif inp.owner in seen_nodes:
local_eval_points.append(seen_nodes[inp.owner][inp.owner.outputs.index(inp)])
else:
# We actually need to compute the R_op for this node
_traverse(inp.owner)
local_eval_points.append(seen_nodes[inp.owner][inp.owner.outputs.index(inp)])
for x, y in zip(inputs, local_eval_points):
if y is not None:
assert as_tensor_variable(x).type == as_tensor_variable(y).type
seen_nodes[node] = op.R_op(node.inputs, local_eval_points)
return None
示例11: make_node
def make_node(self, kern, topgrad, shape=None):
kern = as_tensor_variable(kern)
topgrad = as_tensor_variable(topgrad)
kern, topgrad = self.as_common_dtype(kern, topgrad)
if kern.type.ndim != 5:
raise TypeError('kern must be 5D tensor')
if topgrad.type.ndim != 5:
raise TypeError('topgrad must be 5D tensor')
if shape is None:
if self.subsample != (1, 1, 1):
raise ValueError('shape must be given if subsample != (1, 1, 1)')
height_width_depth = []
else:
height_width_depth = [as_tensor_variable(shape[0]).astype('int64'),
as_tensor_variable(shape[1]).astype('int64'),
as_tensor_variable(shape[2]).astype('int64')]
if self.num_groups > 1:
broadcastable = [topgrad.type.broadcastable[0], False,
False, False, False]
else:
broadcastable = [topgrad.type.broadcastable[0], kern.type.broadcastable[1],
False, False, False]
dtype = kern.type.dtype
return Apply(self, [kern, topgrad] + height_width_depth,
[TensorType(dtype, broadcastable)()])
示例12: choice
def choice(random_state, size=None, a=2, replace=True, p=None, ndim=None,
dtype='int64'):
"""
Choose values from `a` with or without replacement. `a` can be a 1-D array
or a positive scalar. If `a` is a scalar, the samples are drawn from the
range 0,...,a-1.
If the size argument is ambiguous on the number of dimensions, ndim
may be a plain integer to supplement the missing information.
If size is None, a scalar will be returned.
"""
# numpy.random.choice is only available for numpy versions >= 1.7
major, minor, _ = numpy.version.short_version.split('.')
if (int(major), int(minor)) < (1, 7):
raise ImportError('choice requires at NumPy version >= 1.7 '
'(%s)' % numpy.__version__)
a = tensor.as_tensor_variable(a)
if isinstance(replace, bool):
replace = tensor.constant(replace, dtype='int8')
else:
replace = tensor.as_tensor_variable(replace)
# encode p=None as an empty vector
p = tensor.as_tensor_variable(p or [])
ndim, size, bcast = _infer_ndim_bcast(ndim, size)
op = RandomFunction(choice_helper, tensor.TensorType(dtype=dtype,
broadcastable=bcast))
return op(random_state, size, a, replace, p)
示例13: make_node
def make_node(self, A, b):
A = as_tensor_variable(A)
b = as_tensor_variable(b)
otype = tensor.tensor(
broadcastable=b.broadcastable,
dtype = (A*b).dtype)
return Apply(self, [A,b], [otype])
示例14: __init__
def __init__(self, q, beta, *args, **kwargs):
super().__init__(*args, defaults=('median',), **kwargs)
self.q = q = tt.as_tensor_variable(q)
self.beta = beta = tt.as_tensor_variable(beta)
self.median = self._ppf(0.5)
示例15: __init__
def __init__(self, w, mu, *args, **kwargs):
_, sd = get_tau_sd(tau=kwargs.pop('tau', None),
sd=kwargs.pop('sd', None))
self.mu = mu = tt.as_tensor_variable(mu)
self.sd = sd = tt.as_tensor_variable(sd)
super(NormalMixture, self).__init__(w, Normal.dist(mu, sd=sd),
*args, **kwargs)