Python theano.gof.Variable() Examples
The following are 30
code examples of theano.gof.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.gof
, or try the search function
.
Example #1
Source File: basic.py From D-VAE with MIT License | 6 votes |
def _is_sparse_variable(x): """ Returns ------- boolean True iff x is a L{SparseVariable} (and not a L{tensor.TensorType}, for instance). """ if not isinstance(x, gof.Variable): raise NotImplementedError("this function should only be called on " "*variables* (of type sparse.SparseType " "or tensor.TensorType, for instance), not ", x) return isinstance(x.type, SparseType)
Example #2
Source File: basic.py From attention-lvcsr with MIT License | 6 votes |
def _is_sparse_variable(x): """ Returns ------- boolean True iff x is a L{SparseVariable} (and not a L{tensor.TensorType}, for instance). """ if not isinstance(x, gof.Variable): raise NotImplementedError("this function should only be called on " "*variables* (of type sparse.SparseType " "or tensor.TensorType, for instance), not ", x) return isinstance(x.type, SparseType)
Example #3
Source File: basic.py From D-VAE with MIT License | 6 votes |
def make_node(self, x, index): assert isinstance(x.type, TypedListType) if not isinstance(index, Variable): if isinstance(index, slice): index = Constant(SliceType(), index) return Apply(self, [x, index], [x.type()]) else: index = T.constant(index, ndim=0, dtype='int64') return Apply(self, [x, index], [x.ttype()]) if isinstance(index.type, SliceType): return Apply(self, [x, index], [x.type()]) elif isinstance(index, T.TensorVariable) and index.ndim == 0: assert index.dtype == 'int64' return Apply(self, [x, index], [x.ttype()]) else: raise TypeError('Expected scalar or slice as index.')
Example #4
Source File: basic.py From attention-lvcsr with MIT License | 6 votes |
def make_node(self, x, index): assert isinstance(x.type, TypedListType) if not isinstance(index, Variable): if isinstance(index, slice): index = Constant(SliceType(), index) return Apply(self, [x, index], [x.type()]) else: index = T.constant(index, ndim=0, dtype='int64') return Apply(self, [x, index], [x.ttype()]) if isinstance(index.type, SliceType): return Apply(self, [x, index], [x.type()]) elif isinstance(index, T.TensorVariable) and index.ndim == 0: assert index.dtype == 'int64' return Apply(self, [x, index], [x.ttype()]) else: raise TypeError('Expected scalar or slice as index.')
Example #5
Source File: rng_curand.py From attention-lvcsr with MIT License | 6 votes |
def uniform(self, size, low=0.0, high=1.0, ndim=None, dtype=config.floatX): """ Return symbolic tensor of uniform numbers. """ if isinstance(size, tuple): msg = "size must be a tuple of int or a Theano variable" assert all([isinstance(i, int) or isinstance(i, Variable) for i in size]), msg else: msg = "size must be a tuple of int or a Theano variable" assert isinstance(size, Variable) and size.ndim == 1, msg generator = theano.shared(False) # makes a generic s_size = theano.tensor.as_tensor_variable(size) u = CURAND_Uniform.new_auto_update(generator, ndim, dtype, s_size, self.next_seed()) self.state_updates.append(u.update) rval = u * (high - low) + low if u.type.broadcastable != rval.type.broadcastable: raise NotImplementedError( 'Increase the size to match the broadcasting pattern of ' 'low and `high` arguments' ) return rval
Example #6
Source File: rng_curand.py From D-VAE with MIT License | 6 votes |
def uniform(self, size, low=0.0, high=1.0, ndim=None, dtype=config.floatX): """ Return symbolic tensor of uniform numbers. """ if isinstance(size, tuple): msg = "size must be a tuple of int or a Theano variable" assert all([isinstance(i, int) or isinstance(i, Variable) for i in size]), msg else: msg = "size must be a tuple of int or a Theano variable" assert isinstance(size, Variable) and size.ndim == 1, msg generator = theano.shared(False) # makes a generic s_size = theano.tensor.as_tensor_variable(size) u = CURAND_Uniform.new_auto_update(generator, ndim, dtype, s_size, self.next_seed()) self.state_updates.append(u.update) rval = u * (high - low) + low if u.type.broadcastable != rval.type.broadcastable: raise NotImplementedError( 'Increase the size to match the broadcasting pattern of ' 'low and `high` arguments' ) return rval
Example #7
Source File: function_module.py From D-VAE with MIT License | 6 votes |
def wrap_in(input): if isinstance(input, (SymbolicInput, SymbolicInputKit)): return input elif isinstance(input, gof.Variable): # r -> SymbolicInput(variable=r) return SymbolicInput(input) elif isinstance(input, (list, tuple)): # (r, u) -> SymbolicInput(variable=r, update=u) if len(input) == 2: return SymbolicInput(input[0], update=input[1]) else: raise TypeError("Expected two elements in the list or tuple.", input) else: raise TypeError("Unknown input type: %s (%s), expected Variable " "instance", type(input), input)
Example #8
Source File: function_module.py From attention-lvcsr with MIT License | 6 votes |
def wrap_in(input): if isinstance(input, (SymbolicInput, SymbolicInputKit)): return input elif isinstance(input, gof.Variable): # r -> SymbolicInput(variable=r) return SymbolicInput(input) elif isinstance(input, (list, tuple)): # (r, u) -> SymbolicInput(variable=r, update=u) if len(input) == 2: return SymbolicInput(input[0], update=input[1]) else: raise TypeError("Expected two elements in the list or tuple.", input) else: raise TypeError("Unknown input type: %s (%s), expected Variable " "instance", type(input), input)
Example #9
Source File: ops.py From attention-lvcsr with MIT License | 5 votes |
def make_node(self, x): # x could be one of a number of types # the only thing we require is that the variable have a .ndim, # and that the value have a .shape if not isinstance(x, theano.Variable): raise TypeError('x must be Variable with ndim attribute', x) if x.ndim <= self.i: raise TypeError('x has too few dimensions for Shape_i', (x, self.i)) return theano.Apply(self, [x], [theano.tensor.lscalar()])
Example #10
Source File: subtensor.py From attention-lvcsr with MIT License | 5 votes |
def my_as_scalar(a): # Since scal.as_scalar does not know about tensor types (it would # create a circular import) , this method converts either a # TensorVariable or a ScalarVariable to a scalar. if isinstance(a, gof.Variable) and isinstance(a.type, TensorType): return theano.tensor.scalar_from_tensor(a) else: return scal.as_scalar(a)
Example #11
Source File: subtensor.py From attention-lvcsr with MIT License | 5 votes |
def as_index_variable(idx): if idx is None: return NoneConst.clone() if isinstance(idx, slice): return make_slice(idx) if isinstance(idx, gof.Variable) and isinstance(idx.type, SliceType): return idx idx = theano.tensor.as_tensor_variable(idx) if idx.type.dtype[:3] not in ('int', 'uin'): raise TypeError('index must be integers') return idx
Example #12
Source File: type.py From attention-lvcsr with MIT License | 5 votes |
def filter_variable(self, other, allow_convert=True): """ Convert a symbolic Variable into a TensorType, if compatible. For the moment, only a TensorType or CudaNdarrayType will be converted, provided they have the same number of dimensions, broadcastable pattern, and dtype. """ if hasattr(other, '_as_TensorVariable'): other = other._as_TensorVariable() if not isinstance(other, Variable): # The value is not a Variable: we cast it into # a Constant of the appropriate Type. other = self.Constant(type=self, data=other) if other.type == self: return other if allow_convert: # Attempt safe broadcast conversion. other2 = self.convert_variable(other) if other2 is not None and other2.type == self: return other2 raise TypeError( 'Cannot convert Type %(othertype)s ' '(of Variable %(other)s) into Type %(self)s. ' 'You can try to manually convert %(other)s into a %(self)s.' % dict(othertype=other.type, other=other, self=self))
Example #13
Source File: type.py From attention-lvcsr with MIT License | 5 votes |
def make_variable(self, name=None): """ Return a `TensorVariable` of this type. Parameters ---------- name : str A pretty name to identify this `Variable` when printing and debugging """ return self.Variable(self, name=name)
Example #14
Source File: var.py From attention-lvcsr with MIT License | 5 votes |
def all(self, axis=None, keepdims=False): return theano.tensor.basic.all(self, axis=axis, keepdims=keepdims) # Otherwise TensorVariable[:-1] does not work as Python 2.5.1 calls # __len__ before calling __getitem__. It also does not catch the raised # Exception! # def __len__(self): # # We can't implement __len__ as Python requests that this # # function returns an integer >=0 # raise Exception("Theano Variables can't work with len(Theano " # "Variable) due to Python restriction. You can use " # "TheanoVariable.shape[0] instead.")
Example #15
Source File: function_module.py From attention-lvcsr with MIT License | 5 votes |
def validate(self, fgraph): if not hasattr(fgraph, 'destroyers'): return True for r in self.protected + list(fgraph.outputs): if fgraph.destroyers(r): raise gof.InconsistencyError("Trying to destroy a protected" "Variable.", r)
Example #16
Source File: function_module.py From attention-lvcsr with MIT License | 5 votes |
def expand_in(sinput, rinputs): # For SymbolicInputKits, this extracts a list of SymbolicInput # instances and corresponding indices such that these # SymbolicInputs are representative of some of the Variable # instances in inputs. For SymbolicInput, this returns None # as the list of indices and a list with just the # SymbolicInput. if isinstance(sinput, SymbolicInputKit): return sinput.complete(rinputs) elif isinstance(sinput, SymbolicInput): return [None, [sinput]]
Example #17
Source File: function_module.py From attention-lvcsr with MIT License | 5 votes |
def wrap_out(output): if isinstance(output, SymbolicOutput): return output elif isinstance(output, gof.Variable): return SymbolicOutput(output) else: raise TypeError("Unknown output type: %s (%s)", type(output), output)
Example #18
Source File: ops.py From attention-lvcsr with MIT License | 5 votes |
def make_node(self, x): # Must work for all type that have a shape attribute. # This will fail at execution time. if not isinstance(x, theano.Variable): x = theano.tensor.as_tensor_variable(x) return gof.Apply(self, [x], [theano.tensor.lvector()])
Example #19
Source File: basic.py From attention-lvcsr with MIT License | 5 votes |
def as_sparse_variable(x, name=None): """ Wrapper around SparseVariable constructor to construct a Variable with a sparse matrix with the same dtype and format. Parameters ---------- x A sparse matrix. Returns ------- object SparseVariable version of `x`. """ # TODO # Verify that sp is sufficiently sparse, and raise a # warning if it is not if isinstance(x, gof.Apply): if len(x.outputs) != 1: raise ValueError("It is ambiguous which output of a " "multi-output Op has to be fetched.", x) else: x = x.outputs[0] if isinstance(x, gof.Variable): if not isinstance(x.type, SparseType): raise TypeError("Variable type field must be a SparseType.", x, x.type) return x try: return constant(x, name=name) except TypeError: raise TypeError("Cannot convert %s to SparseType" % x, type(x))
Example #20
Source File: ops.py From attention-lvcsr with MIT License | 5 votes |
def make_node(self, x, shape): if not isinstance(x, gof.Variable): x = theano.tensor.as_tensor_variable(x) shape = theano.tensor.as_tensor_variable(shape) assert shape.ndim == 1 assert "int" in shape.dtype if isinstance(shape, theano.tensor.TensorConstant): assert shape.data.size == x.ndim return gof.Apply(self, [x, shape], [x.type()])
Example #21
Source File: debugmode.py From attention-lvcsr with MIT License | 5 votes |
def str_diagnostic(self): """ Return a pretty multiline string representing the cause of the exception. """ sio = StringIO() print("BadThunkOutput", file=sio) print(" Apply :", self.r.owner, file=sio) print(" op :", self.offending_op(), file=sio) print(" Outputs Type:", self.r.type, file=sio) print(" Outputs Shape:", getattr(self.val1, 'shape', None), file=sio) print(" Outputs Strides:", getattr(self.val1, 'strides', None), file=sio) print(" Inputs Type :", [i.type for i in self.r.owner.inputs], file=sio) print(" Inputs Shape:", [getattr(val, 'shape', None) for val in self.inputs_val], file=sio) print(" Inputs Strides:", [getattr(val, 'strides', None) for val in self.inputs_val], file=sio) scalar_values = [] for ipt in self.inputs_val: if getattr(ipt, "size", -1) <= 10: scalar_values.append(ipt) else: scalar_values.append("not shown") print(" Inputs values: %s" % scalar_values, file=sio) print(" Bad Variable:", self.r, file=sio) print(" thunk1 :", self.thunk1, file=sio) print(" thunk2 :", self.thunk2, file=sio) # Don't import it at the top of the file to prevent circular import. utt = theano.tests.unittest_tools print(utt.str_diagnostic(self.val1, self.val2, None, None), file=sio) ret = sio.getvalue() return ret
Example #22
Source File: test_debugmode.py From attention-lvcsr with MIT License | 5 votes |
def make_node(self, v): if not isinstance(v, gof.Variable): v = theano.tensor.as_tensor_variable(v) assert v.type.ndim == 1 type_class = type(v.type) out_r_type = type_class(dtype=v.dtype, broadcastable=(True, False)) out_c_type = type_class(dtype=v.dtype, broadcastable=(False, True)) return gof.Apply(self, [v], [out_r_type(), out_c_type()])
Example #23
Source File: scan_utils.py From attention-lvcsr with MIT License | 5 votes |
def forced_replace(out, x, y): """ Check all internal values of the graph that compute the variable ``out`` for occurrences of values identical with ``x``. If such occurrences are encountered then they are replaced with variable ``y``. Parameters ---------- out : Theano Variable x : Theano Variable y : Theano Variable Examples -------- out := sigmoid(wu)*(1-sigmoid(wu)) x := sigmoid(wu) forced_replace(out, x, y) := y*(1-y) """ if out is None: return None # ``visited`` is a set of nodes that are already known and don't need to be # checked again, speeding up the traversal of multiply-connected graphs. visited = set() def local_traverse(graph, x): if graph in visited: return [] visited.add(graph) if equal_computations([graph], [x]): return [graph] elif not graph.owner: return [] else: rval = [] for inp in graph.owner.inputs: rval += local_traverse(inp, x) return rval to_replace = local_traverse(out, x) return clone(out, replace=OrderedDict((v, y) for v in to_replace))
Example #24
Source File: rng_curand.py From attention-lvcsr with MIT License | 5 votes |
def normal(self, size=None, avg=0.0, std=1.0, ndim=None, dtype=config.floatX): """ Return symbolic tensor of normally-distributed numbers. Parameters ---------- size Can be a list of integer or Theano variable (ex: the shape of other Theano Variable) """ if isinstance(size, tuple): msg = "size must be a tuple of int or a Theano variable" assert all([isinstance(i, int) or isinstance(i, Variable) for i in size]), msg else: msg = "size must be a tuple of int or a Theano variable" assert isinstance(size, Variable) and size.ndim == 1, msg generator = theano.shared(False) # makes a generic s_size = theano.tensor.as_tensor_variable(size) u = CURAND_Normal.new_auto_update(generator, ndim, dtype, s_size, self.next_seed()) self.state_updates.append(u.update) rval = u * std + avg if u.type.broadcastable != rval.type.broadcastable: raise NotImplementedError( 'Increase the size to match the broadcasting pattern of `low`' 'and `high` arguments' ) return rval
Example #25
Source File: basic.py From attention-lvcsr with MIT License | 5 votes |
def make_node(self, a): assert isinstance(a, (tuple, list)) a2 = [] for elem in a: if not isinstance(elem, theano.gof.Variable): elem = theano.tensor.as_tensor_variable(elem) a2.append(elem) if not all(a2[0].type == elem.type for elem in a2): raise TypeError( "MakeList need all input variable to be of the same type.") tl = theano.typed_list.TypedListType(a2[0].type)() return Apply(self, a2, [tl])
Example #26
Source File: base.py From attention-lvcsr with MIT License | 5 votes |
def _setitem(self, key, value): if isinstance(value, Variable): add_role(value, PARAMETER) add_annotation(value, self.brick)
Example #27
Source File: 02_traverse_soln.py From theano_exercises with BSD 3-Clause "New" or "Revised" License | 5 votes |
def arg_to_softmax(prob): """ Oh no! Someone has passed you the probability output, "prob", of a softmax function, and you want the unnormalized log probability--the argument to the softmax. Verify that prob really is the output of a softmax. Raise a TypeError if it is not. If it is, return the argument to the softmax. """ if not isinstance(prob, Variable): raise TypeError() if prob.owner is None: raise TypeError() owner = prob.owner if not isinstance(owner.op, T.nnet.Softmax): raise TypeError() rval, = owner.inputs return rval
Example #28
Source File: utils_.py From kusanagi with MIT License | 5 votes |
def fast_jacobian(expr, wrt, chunk_size=16, func=None): ''' Computes the jacobian by tiling the inputs Copied from https://gist.github.com/aam-at/2b2bc5c35850b553d4ec ''' assert isinstance(expr, Variable), \ "tensor.jacobian expects a Variable as `expr`" assert expr.ndim < 2, \ ("tensor.jacobian expects a 1 dimensional variable as " "`expr`. If not use flatten to make it a vector") num_chunks = tt.ceil(1.0 * expr.shape[0] / chunk_size) num_chunks = tt.cast(num_chunks, 'int32') steps = tt.arange(num_chunks) remainder = expr.shape[0] % chunk_size def chunk_grad(i): ''' operates on a subset of the gradient variables ''' wrt_rep = tt.tile(wrt, (chunk_size, 1)) if func is not None: expr_rep = func(wrt_rep) else: expr_rep, _ = theano.scan( fn=lambda wrt_: theano.clone(expr, {wrt: wrt_}), sequences=wrt_rep) chunk_expr_grad = tt.roll( tt.identity_like(expr_rep), i * chunk_size, axis=1) return tt.grad(cost=None, wrt=wrt_rep, known_grads={ expr_rep: chunk_expr_grad }) grads, _ = theano.scan(chunk_grad, sequences=steps) grads = grads.reshape((chunk_size * grads.shape[0], wrt.shape[0])) jac = ifelse.ifelse(tt.eq(remainder, 0), grads, grads[:expr.shape[0], :]) return jac
Example #29
Source File: 02_traverse_soln.py From ccw_tutorial_theano with BSD 2-Clause "Simplified" License | 5 votes |
def arg_to_softmax(prob): """ Oh no! Someone has passed you the probability output, "prob", of a softmax function, and you want the unnormalized log probability--the argument to the softmax. Verify that prob really is the output of a softmax. Raise a TypeError if it is not. If it is, return the argument to the softmax. """ if not isinstance(prob, Variable): raise TypeError() if prob.owner is None: raise TypeError() owner = prob.owner if not isinstance(owner.op, T.nnet.Softmax): raise TypeError() rval, = owner.inputs return rval
Example #30
Source File: basic.py From D-VAE with MIT License | 5 votes |
def make_node(self, data, indices, indptr, shape): data = tensor.as_tensor_variable(data) if not isinstance(indices, gof.Variable): indices_ = numpy.asarray(indices) indices_32 = theano._asarray(indices, dtype='int32') assert (indices_ == indices_32).all() indices = indices_32 if not isinstance(indptr, gof.Variable): indptr_ = numpy.asarray(indptr) indptr_32 = theano._asarray(indptr, dtype='int32') assert (indptr_ == indptr_32).all() indptr = indptr_32 if not isinstance(shape, gof.Variable): shape_ = numpy.asarray(shape) shape_32 = theano._asarray(shape, dtype='int32') assert (shape_ == shape_32).all() shape = shape_32 indices = tensor.as_tensor_variable(indices) indptr = tensor.as_tensor_variable(indptr) shape = tensor.as_tensor_variable(shape) if data.type.ndim != 1: raise TypeError('data argument must be a vector', data.type, data.type.ndim) if indices.type.ndim != 1 or indices.type.dtype not in discrete_dtypes: raise TypeError('indices must be vector of integers', indices, indices.type) if indptr.type.ndim != 1 or indptr.type.dtype not in discrete_dtypes: raise TypeError('indices must be vector of integers', indptr, indptr.type) if shape.type.ndim != 1 or shape.type.dtype not in discrete_dtypes: raise TypeError('n_rows must be integer type', shape, shape.type) return gof.Apply(self, [data, indices, indptr, shape], [SparseType(dtype=data.type.dtype, format=self.format)()])