Python theano.tensor.as_tensor_variable() Examples
The following are 30
code examples of theano.tensor.as_tensor_variable().
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
theano.tensor
, or try the search function
.
Example #1
Source File: basic.py From D-VAE with MIT License | 6 votes |
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))])
Example #2
Source File: abstract_conv.py From D-VAE with MIT License | 6 votes |
def make_node(self, img, kern): # Make sure both inputs are Variables with the same Type if not isinstance(img, theano.Variable): img = as_tensor_variable(img) if not isinstance(kern, theano.Variable): kern = as_tensor_variable(kern) ktype = img.type.clone(dtype=kern.dtype, broadcastable=kern.broadcastable) kern = ktype.filter_variable(kern) if img.type.ndim != 4: raise TypeError('img must be 4D tensor') if kern.type.ndim != 4: raise TypeError('kern must be 4D tensor') broadcastable = [img.broadcastable[0], kern.broadcastable[0], False, False] output = img.type.clone(broadcastable=broadcastable)() return Apply(self, [img, kern], [output])
Example #3
Source File: abstract_conv.py From D-VAE with MIT License | 6 votes |
def make_node(self, img, topgrad, shape): # Make sure both inputs are Variables with the same Type if not isinstance(img, theano.Variable): img = as_tensor_variable(img) if not isinstance(topgrad, theano.Variable): topgrad = as_tensor_variable(topgrad) gtype = img.type.clone(dtype=topgrad.dtype, broadcastable=topgrad.broadcastable) topgrad = gtype.filter_variable(topgrad) if img.type.ndim != 4: raise TypeError('img must be 4D tensor') if topgrad.type.ndim != 4: raise TypeError('topgrad must be 4D tensor') shape = as_tensor_variable(shape) broadcastable = [topgrad.broadcastable[1], img.broadcastable[1], False, False] output = img.type.clone(broadcastable=broadcastable)() return Apply(self, [img, topgrad, shape], [output])
Example #4
Source File: opt.py From D-VAE with MIT License | 6 votes |
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])
Example #5
Source File: cuda.py From spinn with MIT License | 6 votes |
def make_node(self, x, y, ilist): x_ = T.as_tensor_variable(x) y_ = T.as_tensor_variable(y) ilist_ = T.as_tensor_variable(ilist) #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' % ( opname, x_.type.ndim, y_.type.ndim)) return theano.gof.Apply(self, [x_, y_, ilist_], [x_.type()])
Example #6
Source File: cuda.py From spinn with MIT License | 6 votes |
def make_node(self, x, ilist): x_ = as_cuda_ndarray_variable(x) ilist_ = gpu_contiguous(T.cast(ilist, dtype=config.floatX)) # T.as_tensor_variable(ilist) #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') # # c code suppose it is int64 # if x.ndim in [1, 2, 3] and ilist_.dtype in [ # 'int8', 'int16', 'int32', 'uint8', 'uint16', 'uint32']: # ilist_ = tensor.cast(ilist_, 'int64') bcast = (ilist_.broadcastable[0],) + x_.broadcastable[1:] return theano.gof.Apply(self, [x_, ilist_], [CudaNdarrayType(dtype=x.dtype, broadcastable=bcast)()])
Example #7
Source File: test_corr.py From D-VAE with MIT License | 6 votes |
def test_shape_Constant_tensor(self): """ Tests correlation where the {image,filter}_shape is a Constant tensor. """ as_t = T.as_tensor_variable border_modes = ['valid', 'full', 'half', (1, 1), (2, 1), (1, 2), (3, 3), 1] for border_mode in border_modes: self.validate((as_t(3), as_t(2), as_t(7), as_t(5)), (5, 2, 2, 3), border_mode) self.validate(as_t([3, 2, 7, 5]), (5, 2, 2, 3), border_mode) self.validate(as_t((3, 2, 7, 5)), (5, 2, 2, 3), border_mode) self.validate((3, 2, 7, 5), (as_t(5), as_t(2), as_t(2), as_t(3)), 'valid') self.validate((3, 2, 7, 5), as_t([5, 2, 2, 3]), border_mode) self.validate(as_t([3, 2, 7, 5]), as_t([5, 2, 2, 3]), border_mode)
Example #8
Source File: opt.py From D-VAE with MIT License | 6 votes |
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,)) ])
Example #9
Source File: test_subtensor.py From D-VAE with MIT License | 6 votes |
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)
Example #10
Source File: test_subtensor.py From D-VAE with MIT License | 6 votes |
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)
Example #11
Source File: test_subtensor.py From D-VAE with MIT License | 6 votes |
def test_slice_canonical_form_4(self): step = tensor.iscalar('s') length = tensor.iscalar('l') cnf = get_canonical_form_slice(slice(None, None, step), length) f = self.function([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 step in [-6, -3, -1, 2, 5]: out = f(step, length) t_out = a[out[0]:out[1]:out[2]][::out[3]] v_out = a[None:None:step] assert numpy.all(t_out == v_out) assert numpy.all(t_out.shape == v_out.shape)
Example #12
Source File: basic.py From D-VAE with MIT License | 6 votes |
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)
Example #13
Source File: test_neighbours.py From D-VAE with MIT License | 6 votes |
def test_neibs_bad_shape(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=self.mode) self.assertRaises(TypeError, f) # Test that ignore border work in that case. f = function([], images2neibs(images, neib_shape, mode='ignore_borders'), mode=self.mode) assert self.op in [type(node.op) for node in f.maker.fgraph.toposort()] f()
Example #14
Source File: basic.py From D-VAE with MIT License | 6 votes |
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])
Example #15
Source File: corr.py From D-VAE with MIT License | 6 votes |
def make_node(self, img, topgrad, shape=None): img = as_tensor_variable(img) topgrad = as_tensor_variable(topgrad) if img.type.ndim != 4: raise TypeError('img must be 4D tensor') if topgrad.type.ndim != 4: raise TypeError('topgrad must be 4D tensor') if self.subsample != (1, 1) or self.border_mode == "half": if shape is None: raise ValueError('shape must be given if subsample != (1, 1)' ' or border_mode == "half"') height_width = [as_tensor_variable(shape[0]).astype('int64'), as_tensor_variable(shape[1]).astype('int64')] else: height_width = [] broadcastable = [topgrad.type.broadcastable[1], img.type.broadcastable[1], False, False] dtype = img.type.dtype return Apply(self, [img, topgrad] + height_width, [TensorType(dtype, broadcastable)()])
Example #16
Source File: corr.py From D-VAE with MIT License | 6 votes |
def make_node(self, kern, topgrad, shape=None): kern = as_tensor_variable(kern) topgrad = as_tensor_variable(topgrad) if kern.type.ndim != 4: raise TypeError('kern must be 4D tensor') if topgrad.type.ndim != 4: raise TypeError('topgrad must be 4D tensor') if self.subsample != (1, 1) and shape is None: raise ValueError('shape must be given if subsample != (1, 1)') height_width = [as_tensor_variable(shape[0]).astype('int64'), as_tensor_variable(shape[1]).astype('int64')] if self.subsample != (1, 1) else [] broadcastable = [topgrad.type.broadcastable[0], kern.type.broadcastable[1], False, False] dtype = kern.type.dtype return Apply(self, [kern, topgrad] + height_width, [TensorType(dtype, broadcastable)()])
Example #17
Source File: abstract_conv.py From D-VAE with MIT License | 6 votes |
def conv2d(input, filters, input_shape=None, filter_shape=None, border_mode='valid', subsample=(1, 1), filter_flip=True): """This function will build the symbolic graph for convolving a mini-batch of a stack of 2D inputs with a set of 2D filters. The implementation is modelled after Convolutional Neural Networks (CNN). Refer to :func:`nnet.conv2d <theano.tensor.nnet.conv2d>` for a more detailed documentation. """ input = as_tensor_variable(input) filters = as_tensor_variable(filters) conv_op = AbstractConv2d(imshp=input_shape, kshp=filter_shape, border_mode=border_mode, subsample=subsample, filter_flip=filter_flip) return conv_op(input, filters)
Example #18
Source File: test_subtensor.py From D-VAE with MIT License | 6 votes |
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)
Example #19
Source File: basic.py From D-VAE with MIT License | 6 votes |
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()])
Example #20
Source File: test_subtensor.py From D-VAE with MIT License | 6 votes |
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)
Example #21
Source File: basic.py From D-VAE with MIT License | 6 votes |
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)()])
Example #22
Source File: test_subtensor.py From D-VAE with MIT License | 6 votes |
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)
Example #23
Source File: test_blas.py From D-VAE with MIT License | 5 votes |
def test_destroy_map0(self): """test that only first input can be overwritten""" Z = as_tensor_variable(self.rand(2, 2)) try: gemm_inplace(Z, 1.0, Z, Z, 1.0) except InconsistencyError as e: if exc_message(e) == Gemm.E_z_uniq: return self.fail()
Example #24
Source File: test_blas_scipy.py From D-VAE with MIT License | 5 votes |
def b(self, bval): return tensor.as_tensor_variable(numpy.asarray(bval, dtype=self.dtype))
Example #25
Source File: test_neighbours.py From D-VAE with MIT License | 5 votes |
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()
Example #26
Source File: test_blas.py From D-VAE with MIT License | 5 votes |
def cmp(self, z_, a_, x_, y_, b_): for dtype in ['float32', 'float64', 'complex64', 'complex128']: z = numpy.asarray(z_, dtype=dtype) a = numpy.asarray(a_, dtype=dtype) x = numpy.asarray(x_, dtype=dtype) y = numpy.asarray(y_, dtype=dtype) b = numpy.asarray(b_, dtype=dtype) def cmp_linker(z, a, x, y, b, l): z, a, x, y, b = [numpy.asarray(p) for p in (z, a, x, y, b)] z_orig = z.copy() tz, ta, tx, ty, tb = [as_tensor_variable(p).type() for p in (z, a, x, y, b)] f = inplace_func([tz, ta, tx, ty, tb], gemm_inplace(tz, ta, tx, ty, tb), mode=compile.Mode(optimizer=None, linker=l)) new_z = f(z, a, x, y, b) z_after = self._gemm(z_orig, a, x, y, b) # print z_orig, z_after, z, type(z_orig), type(z_after), type(z) unittest_tools.assert_allclose(z_after, z) if a == 0.0 and b == 1.0: return elif z_orig.size == 0: self.assertTrue(z.size == 0) else: self.assertFalse(numpy.all(z_orig == z)) cmp_linker(copy(z), a, x, y, b, 'c|py') cmp_linker(copy(z), a, x, y, b, 'py') if (config.blas.ldflags and not dtype.startswith("complex") and theano.config.cxx): # If blas.ldflags is equal to '', the C code will not # be generated cmp_linker(copy(z), a, x, y, b, 'c')
Example #27
Source File: pool.py From D-VAE with MIT License | 5 votes |
def make_node(self, x, gz, dummy=None): # make_node should only be called by the grad function of # Pool, so these asserts should not fail. assert isinstance(x, Variable) and x.ndim == 4 assert isinstance(gz, Variable) and gz.ndim == 4 x = tensor.as_tensor_variable(x) gz = tensor.as_tensor_variable(gz) return Apply(self, [x, gz], [x.type()])
Example #28
Source File: pool.py From D-VAE with MIT License | 5 votes |
def make_node(self, x, maxout, gz): # make_node should only be called by the grad function of # Pool, so these asserts should not fail. assert isinstance(x, Variable) and x.ndim == 4 assert isinstance(maxout, Variable) and maxout.ndim == 4 assert isinstance(gz, Variable) and gz.ndim == 4 x = tensor.as_tensor_variable(x) maxout = tensor.as_tensor_variable(maxout) gz = tensor.as_tensor_variable(gz) return Apply(self, [x, maxout, gz], [x.type()])
Example #29
Source File: pool.py From D-VAE with MIT License | 5 votes |
def make_node(self, x): if x.type.ndim != 4: raise TypeError() # TODO: consider restricting the dtype? x = tensor.as_tensor_variable(x) # If the input shape are broadcastable we can have 0 in the output shape broad = x.broadcastable[:2] + (False, False) out = tensor.TensorType(x.dtype, broad) return gof.Apply(self, [x], [out()])
Example #30
Source File: test_neighbours.py From D-VAE with MIT License | 5 votes |
def speed_neibs_wrap_centered(self): shape = (100, 40, 18, 18) images = shared(numpy.arange(numpy.prod(shape), dtype='float32').reshape(shape)) neib_shape = T.as_tensor_variable((3, 3)) f = function([], images2neibs(images, neib_shape, mode="wrap_centered"), mode=self.mode) for i in range(1000): f()