def make_node(self, state, time):
"""Creates an Apply node representing the application of the op on
the inputs provided.
Parameters
----------
state : array_like
The state to transform into feature space
time : int
The current time being processed
Returns
-------
theano.Apply
[description]
"""
state = T.as_tensor_variable(state)
time = T.as_tensor_variable(time)
return theano.Apply(self, [state, time], [T.bmatrix()])
python类as_tensor_variable()的实例源码
def compile_maxpool(output_shape, pool_size):
X = T.tensor4()
# compute output with both methods
out1 = T.signal.pool.pool_2d(X, pool_size,
ignore_border=True, st=None,
padding=(0, 0), mode='max')
out2 = my_pool_2d(X, pool_size,
ignore_border=True, st=None,
padding=(0, 0), mode='max')
# compute gradient with random incoming gradient for both cases
incoming_grad = T.as_tensor_variable(np.random.random(size=output_shape)
.astype(np.float32))
grad1 = T.grad(None, wrt=X, known_grads={out1: incoming_grad})
grad2 = T.grad(None, wrt=X, known_grads={out2: incoming_grad})
return theano.function([X], [out1, out2, grad1, grad2])
def _HamiltonianShootingCarrying(self, q, p, i0) :
"""
Given initial control points/momentums q0 and p0 given as n-by-d matrices,
and a "template" image i0, outputs the trajectories q_t, p_t, I_t = I0 \circ phi_{t->0}.
"""
# Here, we use the "scan" theano routine, which can be understood as a "for" loop
identity = T.as_tensor_variable(0. * self.dense_grid()) # We encode the identity as a null displacement field.
# Here, we use the "scan" theano routine, which can be understood as a "for" loop
result, updates = theano.scan(fn = lambda x,y,z : self._hamiltonian_step_carrying2(x,y,z),
outputs_info = [q,p, identity],
n_steps = int(np.round(1/self.dt) ))
phi_inv_1 = result[2][-1] # We do not store the intermediate results
I1 = self._image_circ_diffeo(i0, self.dense_grid() + phi_inv_1) # instead of interpolating the images It at all timesteps, we only do it in the end.
return [result[0][-1], result[1][-1], I1] # and only return the final state + momentum + image
def __init__(self, ratio=2, kernlen=5, **kwargs):
# I imagine the idea could be aplied to a bigger ratio
assert ratio == 2
# the kernel is stripped from opencv, need a function that output such a sharp gauss kern
assert kernlen == 5
self.ratio = ratio
#kernel = T.as_tensor_variable(gkern(kernlen))
#self.kernel = kernel.dimshuffle('x','x',0,1)
kernel = np.asarray([[1,4,6,4,1],
[4,16,26,16,4],
[6,26,64,26,6],
[1,4,6,4,1],
[4,16,26,16,4]])
kernel = kernel[None,None,:,:] / 64.
self.kernel = T.as_tensor_variable(kernel.astype(np.float32))
super(GaussianKernelUpsampling, self).__init__(**kwargs)
def shared_like(variable, name=None):
"""Construct a shared variable to hold the value of a tensor variable.
Parameters
----------
variable : :class:`~tensor.TensorVariable`
The variable whose dtype and ndim will be used to construct
the new shared variable.
name : :obj:`str` or :obj:`None`
The name of the shared variable. If None, the name is determined
based on variable's name.
"""
variable = tensor.as_tensor_variable(variable)
if name is None:
name = "shared_{}".format(variable.name)
return theano.shared(numpy.zeros((0,) * variable.ndim,
dtype=variable.dtype),
name=name)
def test_dnn_conv_desc_merge():
if not dnn.dnn_available(test_ctx_name):
raise SkipTest(dnn.dnn_available.msg)
kern_shp = T.as_tensor_variable(
numpy.asarray([3, 1, 2, 2]).astype('int64'))
desc1 = dnn.GpuDnnConvDesc(border_mode='valid', subsample=(2, 2),
conv_mode='conv')(kern_shp)
desc2 = dnn.GpuDnnConvDesc(border_mode='full', subsample=(1, 1),
conv_mode='cross')(kern_shp)
# CDataType is not DeepCopyable so this will crash if we don't use
# borrow=True
f = theano.function([], [theano.Out(desc1, borrow=True),
theano.Out(desc2, borrow=True)])
d1, d2 = f()
# This will be the case if they are merged, which would be bad.
assert d1 != d2
def make_node(self, x, ilist):
ctx_name = infer_context_name(x, ilist)
x_ = as_gpuarray_variable(x, ctx_name)
ilist__ = tensor.as_tensor_variable(ilist)
if ilist__.type.dtype[:3] not in ('int', 'uin'):
raise TypeError('index must be integers')
if ilist__.type.dtype != 'int64':
ilist__ = tensor.cast(ilist__, 'int64')
ilist_ = as_gpuarray_variable(ilist__, ctx_name)
if ilist_.type.dtype != 'int64':
raise TypeError('index must be int64')
if ilist_.type.ndim != 1:
raise TypeError('index must be a vector')
if x_.type.ndim == 0:
raise TypeError('cannot index into a scalar')
bcast = ilist_.broadcastable + x_.broadcastable[1:]
return gof.Apply(self, [x_, ilist_],
[GpuArrayType(dtype=x.dtype,
context_name=ctx_name,
broadcastable=bcast)()])
def make_node(self, x, y, ilist):
ctx_name = infer_context_name(x, y)
x_ = as_gpuarray_variable(x, ctx_name)
y_ = as_gpuarray_variable(y, ctx_name)
ilist_ = tensor.as_tensor_variable(ilist)
assert x_.type.ndim >= y_.type.ndim
if ilist_.type.dtype[:3] not in ('int', 'uin'):
raise TypeError('index must be integers')
if ilist_.type.ndim != 1:
raise TypeError('index must be vector')
if x_.type.ndim == 0:
raise TypeError('cannot index into a scalar')
if y_.type.ndim > x_.type.ndim:
if self.set_instead_of_inc:
opname = 'set'
else:
opname = 'increment'
raise TypeError(
'cannot %s x subtensor with ndim=%s'
' by y with ndim=%s to x subtensor with ndim=%s ' % (
opname, x_.type.ndim, y_.type.ndim))
return gof.Apply(self, [x_, y_, ilist_], [x_.type()])
def make_node(self, o, W, h, inputIdx, outputIdx):
ctx = infer_context_name(o, W, h)
o = as_gpuarray_variable(o, ctx)
W = as_gpuarray_variable(W, ctx)
h = as_gpuarray_variable(h, ctx)
inputIdx = as_tensor_variable(inputIdx)
outputIdx = as_tensor_variable(outputIdx)
assert o.ndim == 3
assert W.ndim == 4
assert h.ndim == 3
assert inputIdx.ndim == 2
assert outputIdx.ndim == 2
assert inputIdx.type.dtype in discrete_dtypes
assert outputIdx.type.dtype in discrete_dtypes
return Apply(self, [o, W, h, inputIdx, outputIdx],
[o.type()])
def as_sparse_or_tensor_variable(x, name=None):
"""
Same as `as_sparse_variable` but if we can't make a
sparse variable, we try to make a tensor variable.
Parameters
----------
x
A sparse matrix.
Returns
-------
SparseVariable or TensorVariable version of `x`
"""
try:
return as_sparse_variable(x, name)
except (ValueError, TypeError):
return theano.tensor.as_tensor_variable(x, name)
def make_node(self, x):
x = tensor.as_tensor_variable(x)
if x.ndim > 2:
raise TypeError(
"Theano does not have sparse tensor types with more "
"than 2 dimensions, but %s.ndim = %i" % (x, x.ndim))
elif x.ndim == 1:
x = x.dimshuffle('x', 0)
elif x.ndim == 0:
x = x.dimshuffle('x', 'x')
else:
assert x.ndim == 2
return gof.Apply(self,
[x],
[SparseType(dtype=x.type.dtype,
format=self.format)()])
def make_node(self, x, index, gz):
x = as_sparse_variable(x)
gz = as_sparse_variable(gz)
assert x.format in ["csr", "csc"]
assert gz.format in ["csr", "csc"]
ind = tensor.as_tensor_variable(index)
assert ind.ndim == 1
assert "int" in ind.dtype
scipy_ver = [int(n) for n in scipy.__version__.split('.')[:2]]
if not scipy_ver >= [0, 13]:
raise NotImplementedError("Scipy version is to old")
return gof.Apply(self, [x, ind, gz], [x.type()])
def make_node(self, x, y):
x, y = as_sparse_variable(x), tensor.as_tensor_variable(y)
assert x.format in ["csr", "csc"]
# upcast the tensor. Is the cast of sparse done implemented?
dtype = scalar.upcast(x.type.dtype, y.type.dtype)
# The magic number two here arises because L{scipy.sparse}
# objects must be matrices (have dimension 2)
# Broadcasting of the sparse matrix is not supported.
# We support nd == 0 used by grad of SpSum()
assert y.type.ndim in [0, 2]
out = SparseType(dtype=dtype,
format=x.type.format)()
return gof.Apply(self, [x, y], [out])
def make_node(self, alpha, x, y, z):
if not _is_sparse_variable(x) and not _is_sparse_variable(y):
# If x and y are tensor, we don't want to use this class
# We should use Dot22 and Gemm in that case.
raise TypeError(x)
dtype_out = scalar.upcast(alpha.type.dtype, x.type.dtype,
y.type.dtype, z.type.dtype)
alpha = tensor.as_tensor_variable(alpha)
z = tensor.as_tensor_variable(z)
assert z.ndim == 2
assert alpha.type.broadcastable == (True,) * alpha.ndim
if not _is_sparse_variable(x):
x = tensor.as_tensor_variable(x)
assert y.format in ["csr", "csc"]
assert x.ndim == 2
if not _is_sparse_variable(y):
y = tensor.as_tensor_variable(y)
assert x.format in ["csr", "csc"]
assert y.ndim == 2
return gof.Apply(self, [alpha, x, y, z],
[tensor.tensor(dtype=dtype_out,
broadcastable=(False, False))])
def make_node(self, x, y):
x, y = sparse.as_sparse_variable(x), tensor.as_tensor_variable(y)
out_dtype = scalar.upcast(x.type.dtype, y.type.dtype)
if self.inplace:
assert out_dtype == y.dtype
indices, indptr, data = csm_indices(x), csm_indptr(x), csm_data(x)
# We either use CSC or CSR depending on the format of input
assert self.format == x.type.format
# The magic number two here arises because L{scipy.sparse}
# objects must be matrices (have dimension 2)
assert y.type.ndim == 2
out = tensor.TensorType(dtype=out_dtype,
broadcastable=y.type.broadcastable)()
return gof.Apply(self,
[data, indices, indptr, y],
[out])
def make_node(self, x, y, p_data, p_ind, p_ptr, p_ncols):
x = tensor.as_tensor_variable(x)
y = tensor.as_tensor_variable(y)
p_data = tensor.as_tensor_variable(p_data)
p_ind = tensor.as_tensor_variable(p_ind)
p_ptr = tensor.as_tensor_variable(p_ptr)
p_ncols = tensor.as_tensor_variable(p_ncols)
assert p_ncols.dtype == 'int32'
dtype_out = scalar.upcast(x.type.dtype, y.type.dtype,
p_data.type.dtype)
dot_out = scalar.upcast(x.type.dtype, y.type.dtype)
# We call blas ?dot function that take only param of the same type
x = tensor.cast(x, dot_out)
y = tensor.cast(y, dot_out)
return gof.Apply(self, [x, y, p_data, p_ind, p_ptr, p_ncols], [
tensor.tensor(dtype=dtype_out, broadcastable=(False,)),
tensor.tensor(dtype=p_ind.type.dtype, broadcastable=(False,)),
tensor.tensor(dtype=p_ptr.type.dtype, broadcastable=(False,))
])
def make_node(self, a, s=None):
a = T.as_tensor_variable(a)
if a.ndim < 2:
raise TypeError('%s: input must have dimension > 2, with first dimension batches' %
self.__class__.__name__)
if s is None:
s = a.shape[1:]
s = T.as_tensor_variable(s)
else:
s = T.as_tensor_variable(s)
if (not s.dtype.startswith('int')) and \
(not s.dtype.startswith('uint')):
raise TypeError('%s: length of the transformed axis must be'
' of type integer' % self.__class__.__name__)
return gof.Apply(self, [a, s], [self.output_type(a)()])
def make_node(self, a, s=None):
a = T.as_tensor_variable(a)
if a.ndim < 3:
raise TypeError('%s: input must have dimension >= 3, with ' %
self.__class__.__name__ +
'first dimension batches and last real/imag parts')
if s is None:
s = a.shape[1:-1]
s = T.set_subtensor(s[-1], (s[-1] - 1) * 2)
s = T.as_tensor_variable(s)
else:
s = T.as_tensor_variable(s)
if (not s.dtype.startswith('int')) and \
(not s.dtype.startswith('uint')):
raise TypeError('%s: length of the transformed axis must be'
' of type integer' % self.__class__.__name__)
return gof.Apply(self, [a, s], [self.output_type(a)()])
def make_node(self, a, b):
assert imported_scipy, (
"Scipy not available. Scipy is needed for the Eigvalsh op")
if b == theano.tensor.NoneConst:
a = as_tensor_variable(a)
assert a.ndim == 2
out_dtype = theano.scalar.upcast(a.dtype)
w = theano.tensor.vector(dtype=out_dtype)
return Apply(self, [a], [w])
else:
a = as_tensor_variable(a)
b = as_tensor_variable(b)
assert a.ndim == 2
assert b.ndim == 2
out_dtype = theano.scalar.upcast(a.dtype, b.dtype)
w = theano.tensor.vector(dtype=out_dtype)
return Apply(self, [a, b], [w])
def test_slice_canonical_form_0(self):
start = tensor.iscalar('b')
stop = tensor.iscalar('e')
step = tensor.iscalar('s')
length = tensor.iscalar('l')
cnf = get_canonical_form_slice(slice(start, stop, step), length)
f = self.function([start, stop, step, length], [
tensor.as_tensor_variable(cnf[0].start),
tensor.as_tensor_variable(cnf[0].stop),
tensor.as_tensor_variable(cnf[0].step),
tensor.as_tensor_variable(cnf[1])], N=0, op=self.ops)
length = 5
a = numpy.arange(length)
for start in [-8, -5, -4, -1, 0, 1, 4, 5, 8]:
for stop in [-8, -5, -4, -1, 0, 1, 4, 5, 8]:
for step in [-6, -3, -1, 2, 5]:
out = f(start, stop, step, length)
t_out = a[out[0]:out[1]:out[2]][::out[3]]
v_out = a[start:stop:step]
assert numpy.all(t_out == v_out)
assert numpy.all(t_out.shape == v_out.shape)
def test_slice_canonical_form_1(self):
stop = tensor.iscalar('e')
step = tensor.iscalar('s')
length = tensor.iscalar('l')
cnf = get_canonical_form_slice(slice(None, stop, step), length)
f = self.function([stop, step, length], [
tensor.as_tensor_variable(cnf[0].start),
tensor.as_tensor_variable(cnf[0].stop),
tensor.as_tensor_variable(cnf[0].step),
tensor.as_tensor_variable(cnf[1])], N=0, op=self.ops)
length = 5
a = numpy.arange(length)
for stop in [-8, -5, -4, -1, 0, 1, 4, 5, 8]:
for step in [-6, -3, -1, 2, 5]:
out = f(stop, step, length)
t_out = a[out[0]:out[1]:out[2]][::out[3]]
v_out = a[:stop:step]
assert numpy.all(t_out == v_out)
assert numpy.all(t_out.shape == v_out.shape)
def test_slice_canonical_form_2(self):
start = tensor.iscalar('b')
step = tensor.iscalar('s')
length = tensor.iscalar('l')
cnf = get_canonical_form_slice(slice(start, None, step), length)
f = self.function([start, step, length], [
tensor.as_tensor_variable(cnf[0].start),
tensor.as_tensor_variable(cnf[0].stop),
tensor.as_tensor_variable(cnf[0].step),
tensor.as_tensor_variable(cnf[1])], N=0, op=self.ops)
length = 5
a = numpy.arange(length)
for start in [-8, -5, -4, -1, 0, 1, 4, 5, 8]:
for step in [-6, -3, -1, 2, 5]:
out = f(start, step, length)
t_out = a[out[0]:out[1]:out[2]][::out[3]]
v_out = a[start:None:step]
assert numpy.all(t_out == v_out)
assert numpy.all(t_out.shape == v_out.shape)
def test_slice_canonical_form_3(self):
start = tensor.iscalar('b')
stop = tensor.iscalar('e')
length = tensor.iscalar('l')
cnf = get_canonical_form_slice(slice(start, stop, None), length)
f = self.function([start, stop, length], [
tensor.as_tensor_variable(cnf[0].start),
tensor.as_tensor_variable(cnf[0].stop),
tensor.as_tensor_variable(cnf[0].step),
tensor.as_tensor_variable(cnf[1])], N=0, op=self.ops)
length = 5
a = numpy.arange(length)
for start in [-8, -5, -4, -1, 0, 1, 4, 5, 8]:
for stop in [-8, -5, -4, -1, 0, 1, 4, 5, 8]:
out = f(start, stop, length)
t_out = a[out[0]:out[1]:out[2]][::out[3]]
v_out = a[start:stop:None]
assert numpy.all(t_out == v_out)
assert numpy.all(t_out.shape == v_out.shape)
def test_slice_canonical_form_5(self):
start = tensor.iscalar('b')
length = tensor.iscalar('l')
cnf = get_canonical_form_slice(slice(start, None, None), length)
f = self.function([start, length], [
tensor.as_tensor_variable(cnf[0].start),
tensor.as_tensor_variable(cnf[0].stop),
tensor.as_tensor_variable(cnf[0].step),
tensor.as_tensor_variable(cnf[1])], N=0, op=self.ops)
length = 5
a = numpy.arange(length)
for start in [-8, -5, -4, -1, 0, 1, 4, 5, 8]:
out = f(start, length)
t_out = a[out[0]:out[1]:out[2]][::out[3]]
v_out = a[start:None:None]
assert numpy.all(t_out == v_out)
assert numpy.all(t_out.shape == v_out.shape)
def test_slice_canonical_form_6(self):
stop = tensor.iscalar('e')
length = tensor.iscalar('l')
cnf = get_canonical_form_slice(slice(None, stop, None), length)
f = self.function([stop, length], [
tensor.as_tensor_variable(cnf[0].start),
tensor.as_tensor_variable(cnf[0].stop),
tensor.as_tensor_variable(cnf[0].step),
tensor.as_tensor_variable(cnf[1])], N=0, op=self.ops)
length = 5
a = numpy.arange(length)
for stop in [-8, -5, -4, -1, 0, 1, 4, 5, 8]:
out = f(stop, length)
t_out = a[out[0]:out[1]:out[2]][::out[3]]
v_out = a[None:stop:None]
assert numpy.all(t_out == v_out)
assert numpy.all(t_out.shape == v_out.shape)
def test2_valid_neg(self):
n = as_tensor_variable(rand(2, 3))
v, i = eval_outputs(max_and_argmax(n, -1))
assert i.dtype == 'int64'
self.assertTrue(v.shape == (2,))
self.assertTrue(i.shape == (2,))
self.assertTrue(numpy.all(v == numpy.max(n.value, -1)))
self.assertTrue(numpy.all(i == numpy.argmax(n.value, -1)))
v, i = eval_outputs(max_and_argmax(n, -2))
assert i.dtype == 'int64'
self.assertTrue(v.shape == (3,))
self.assertTrue(i.shape == (3,))
self.assertTrue(numpy.all(v == numpy.max(n.value, -2)))
self.assertTrue(numpy.all(i == numpy.argmax(n.value, -2)))
v = eval_outputs(max_and_argmax(n, -1)[0].shape)
assert v == (2)
v = eval_outputs(max_and_argmax(n, -2)[0].shape)
assert v == (3)
def test_grad_argmin(self):
data = rand(2, 3)
n = as_tensor_variable(data)
n.name = 'n'
# test grad of argmin
utt.verify_grad(lambda v: argmin(v, axis=-1), [data])
utt.verify_grad(lambda v: argmin(v, axis=[0]), [data])
utt.verify_grad(lambda v: argmin(v, axis=[1]), [data])
utt.verify_grad(lambda v: argmin(v.flatten()), [data])
try:
cost = argmin(n, axis=-1)
cost.name = None
g = grad(cost, n)
raise Exception('Expected an error')
except TypeError:
pass
def test_grad_argmax(self):
data = rand(2, 3)
n = as_tensor_variable(data)
# test grad of argmax
utt.verify_grad(lambda v: argmax(v, axis=-1), [data])
utt.verify_grad(lambda v: argmax(v, axis=[0]), [data])
utt.verify_grad(lambda v: argmax(v, axis=[1]), [data])
utt.verify_grad(lambda v: argmax(v.flatten()), [data])
try:
grad(argmax(n, axis=-1), n)
raise Exception('Expected an error')
except TypeError:
pass
def test_join_matrix_dtypes(self):
if "float32" in self.shared.__name__:
raise SkipTest(
"The shared variable constructor"
" need to support other dtype then float32")
# Test mixed dtype. There was a bug that caused crash in the past.
av = numpy.array([[1, 2, 3], [4, 5, 6]], dtype='int8')
bv = numpy.array([[7], [8]], dtype='float32')
a = self.shared(av)
b = as_tensor_variable(bv)
s = join(1, a, b)
want = numpy.array([[1, 2, 3, 7], [4, 5, 6, 8]], dtype='float32')
out = self.eval_outputs_and_check_join([s])
self.assertTrue((out == want).all())
grad(s.sum(), b)
grad(s.sum(), a)
utt.verify_grad(lambda b: join(1, a, b), [bv],
eps=1.0e-2, mode=self.mode)
def test_join_matrix_ints(self):
if "float32" in self.shared.__name__:
raise SkipTest(
"The shared variable constructor"
" need to support other dtype then float32")
# Test mixed dtype. There was a bug that caused crash in the past.
av = numpy.array([[1, 2, 3], [4, 5, 6]], dtype='int8')
bv = numpy.array([[7], [8]], dtype='int32')
a = self.shared(av)
b = as_tensor_variable(bv)
s = join(1, a, b)
want = numpy.array([[1, 2, 3, 7], [4, 5, 6, 8]], dtype='float32')
out = self.eval_outputs_and_check_join([s])
self.assertTrue((out == want).all())
assert (numpy.asarray(grad(s.sum(), b).eval()) == 0).all()
assert (numpy.asarray(grad(s.sum(), a).eval()) == 0).all()