本文整理汇总了Python中theano.Variable方法的典型用法代码示例。如果您正苦于以下问题:Python theano.Variable方法的具体用法?Python theano.Variable怎么用?Python theano.Variable使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类theano
的用法示例。
在下文中一共展示了theano.Variable方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: _is_sparse_variable
# 需要导入模块: import theano [as 别名]
# 或者: from theano import Variable [as 别名]
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)
示例2: make_node
# 需要导入模块: import theano [as 别名]
# 或者: from theano import Variable [as 别名]
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])
示例3: get_flags
# 需要导入模块: import theano [as 别名]
# 或者: from theano import Variable [as 别名]
def get_flags(*types):
def get_dtype(t):
if isinstance(t, string_types):
return numpy.dtype(t)
elif isinstance(t, Type):
return t.dtype
elif isinstance(t, Variable):
return t.type.dtype
else:
raise TypeError("can't get a dtype from %s" % (type(t),))
dtypes = [get_dtype(t) for t in types]
flags = dict(cluda=True)
if any(d == numpy.float64 for d in dtypes):
flags['have_double'] = True
if any(d.itemsize < 4 for d in dtypes):
flags['have_small'] = True
if any(d.kind == 'c' for d in dtypes):
flags['have_complex'] = True
if any(d == numpy.float16 for d in dtypes):
flags['have_half'] = True
return flags
示例4: _is_dense_variable
# 需要导入模块: import theano [as 别名]
# 或者: from theano import Variable [as 别名]
def _is_dense_variable(x):
"""
Returns
-------
boolean
True if x is a L{tensor.TensorType} (and not a L{SparseVariable},
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, tensor.TensorType)
示例5: as_sparse_variable
# 需要导入模块: import theano [as 别名]
# 或者: from theano import Variable [as 别名]
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))
示例6: make_node
# 需要导入模块: import theano [as 别名]
# 或者: from theano import Variable [as 别名]
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)()])
示例7: make_node
# 需要导入模块: import theano [as 别名]
# 或者: from theano import Variable [as 别名]
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()])
示例8: make_node
# 需要导入模块: import theano [as 别名]
# 或者: from theano import Variable [as 别名]
def make_node(self):
return gof.Apply(self, [], [theano.Variable(Generic()),
tensor(self.dtype,
broadcastable=self.broadcastable)])
示例9: test0
# 需要导入模块: import theano [as 别名]
# 或者: from theano import Variable [as 别名]
def test0(self):
path = Variable(Generic())
# Not specifying mmap_mode defaults to None, and the data is
# copied into main memory
x = tensor.load(path, 'int32', (False,))
y = x * 2
fn = function([path], y)
assert (fn(self.filename) == (self.data * 2)).all()
示例10: test_invalid_modes
# 需要导入模块: import theano [as 别名]
# 或者: from theano import Variable [as 别名]
def test_invalid_modes(self):
# Modes 'r+', 'r', and 'w+' cannot work with Theano, becausei
# the output array may be modified inplace, and that should not
# modify the original file.
path = Variable(Generic())
for mmap_mode in ('r+', 'r', 'w+', 'toto'):
self.assertRaises(ValueError,
tensor.load, path, 'int32', (False,), mmap_mode)
示例11: test_memmap
# 需要导入模块: import theano [as 别名]
# 或者: from theano import Variable [as 别名]
def test_memmap(self):
path = Variable(Generic())
x = tensor.load(path, 'int32', (False,), mmap_mode='c')
fn = function([path], x)
assert type(fn(self.filename)) == numpy.core.memmap
示例12: make_node
# 需要导入模块: import theano [as 别名]
# 或者: from theano import Variable [as 别名]
def make_node(self, _x):
if not isinstance(_x, theano.Variable):
x = as_tensor_variable(_x)
else:
x = _x
if x.type.ndim != 2:
raise TypeError('ExtractDiag only works on matrices', _x)
return Apply(self, [x], [x.type.__class__(broadcastable=(False,),
dtype=x.type.dtype)()])
示例13: make_node
# 需要导入模块: import theano [as 别名]
# 或者: from theano import Variable [as 别名]
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()])
示例14: ensure_float
# 需要导入模块: import theano [as 别名]
# 或者: from theano import Variable [as 别名]
def ensure_float(val, default, name):
if val is None:
return default.clone()
if not isinstance(val, Variable):
val = constant(val)
if hasattr(val, 'ndim') and val.ndim == 0:
val = as_scalar(val)
if not isinstance(val.type, theano.scalar.Scalar):
raise TypeError("%s: expected a scalar value" % (name,))
if not val.type.dtype == 'float32':
raise TypeError("%s: type is not float32" % (name,))
return val
示例15: make_variable
# 需要导入模块: import theano [as 别名]
# 或者: from theano import Variable [as 别名]
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)