本文整理汇总了Python中theano.tensor.Constant方法的典型用法代码示例。如果您正苦于以下问题:Python tensor.Constant方法的具体用法?Python tensor.Constant怎么用?Python tensor.Constant使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类theano.tensor
的用法示例。
在下文中一共展示了tensor.Constant方法的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: isNaN_or_Inf_or_None
# 需要导入模块: from theano import tensor [as 别名]
# 或者: from theano.tensor import Constant [as 别名]
def isNaN_or_Inf_or_None(x):
isNone = x is None
try:
isNaN = numpy.isnan(x)
isInf = numpy.isinf(x)
isStr = isinstance(x, string_types)
except Exception:
isNaN = False
isInf = False
isStr = False
if not isNaN and not isInf:
try:
val = get_scalar_constant_value(x)
isInf = numpy.isinf(val)
isNaN = numpy.isnan(val)
except Exception:
isNaN = False
isInf = False
if isinstance(x, gof.Constant) and isinstance(x.data, string_types):
isStr = True
else:
isStr = False
return isNone or isNaN or isInf or isStr
示例2: reconstruct_graph
# 需要导入模块: from theano import tensor [as 别名]
# 或者: from theano.tensor import Constant [as 别名]
def reconstruct_graph(inputs, outputs, tag=None):
"""
Different interface to clone, that allows you to pass inputs.
Compared to clone, this method always replaces the inputs with
new variables of the same type, and returns those (in the same
order as the original inputs).
"""
if tag is None:
tag = ''
nw_inputs = [safe_new(x, tag) for x in inputs]
givens = OrderedDict()
for nw_x, x in izip(nw_inputs, inputs):
givens[x] = nw_x
allinputs = theano.gof.graph.inputs(outputs)
for inp in allinputs:
if isinstance(inp, theano.Constant):
givens[inp] = inp.clone()
nw_outputs = clone(outputs, replace=givens)
return (nw_inputs, nw_outputs)
示例3: make_node
# 需要导入模块: from theano import tensor [as 别名]
# 或者: from theano.tensor import Constant [as 别名]
def make_node(self, a, q):
# cast q to theano variable
if isinstance(q, (int, float)):
scalar_type = T.scalar().type
q = T.Constant(scalar_type, q)
# set to all axes if none specified
if self.axis is None:
axis = range(a.ndim)
elif isinstance(self.axis, int):
axis = [self.axis]
else:
axis = self.axis
# calculate broadcastable
if self.keepdims:
broadcastable = [b or (ax in axis)
for ax, b in enumerate(a.broadcastable)]
else:
broadcastable = [b
for ax, b in enumerate(a.broadcastable)
if ax not in axis]
out = T.TensorType(a.dtype, broadcastable)()
return theano.gof.Apply(self, [a, q], [out])
示例4: tensor_from_layer
# 需要导入模块: from theano import tensor [as 别名]
# 或者: from theano.tensor import Constant [as 别名]
def tensor_from_layer(self, arg, collect_params=True):
"""
Grab the theano tensor representing the computation of this
layer/operator iff `arg` is a layer.
:type collect_params: bool
:param collect_params: Flag. If true, also collect the parameters
and inputs of the layer `arg` and make them parameters and inputs
needed to compute the current layer/operator
"""
if not collect_params:
if isinstance(arg, Container): return arg.out
else: return arg
if isinstance(arg, Container):
self.merge_params(arg)
return arg.out
elif isinstance(arg, theano.gof.Variable):
inps = [x for x in theano.gof.graph.inputs([arg])
if not isinstance(x, (TT.Constant, theano.compile.SharedVariable))]
self.add_inputs(inps)
return arg
else:
return arg
示例5: get_outer_ndim
# 需要导入模块: from theano import tensor [as 别名]
# 或者: from theano.tensor import Constant [as 别名]
def get_outer_ndim(self, var, scan_args):
# Given a variable, determine the number of dimension it would have if
# it was pushed out of scan
if (var in scan_args.inner_in_non_seqs or
isinstance(var, theano.Constant)):
outer_ndim = var.ndim
else:
outer_ndim = var.ndim + 1
return outer_ndim
示例6: tensor_from_layer
# 需要导入模块: from theano import tensor [as 别名]
# 或者: from theano.tensor import Constant [as 别名]
def tensor_from_layer(self, arg, collect_params=True):
"""
Grab the theano tensor representing the computation of this
layer/operator iff `arg` is a layer.
:type collect_params: bool
:param collect_params: Flag. If true, also collect the parameters
and inputs of the layer `arg` and make them parameters and inputs
needed to compute the current layer/operator
"""
if not collect_params:
if isinstance(arg, Container):
return arg.out
else:
return arg
if isinstance(arg, Container):
self.merge_params(arg)
return arg.out
elif isinstance(arg, theano.gof.Variable):
inps = [x for x in theano.gof.graph.inputs([arg])
if not isinstance(x, (TT.Constant, theano.compile.SharedVariable))]
self.add_inputs(inps)
return arg
else:
return arg
示例7: find_inputs_and_params
# 需要导入模块: from theano import tensor [as 别名]
# 或者: from theano.tensor import Constant [as 别名]
def find_inputs_and_params(node):
'''Walk a computation graph and extract root variables.
Parameters
----------
node : Theano expression
A symbolic Theano expression to walk.
Returns
-------
inputs : list Theano variables
A list of candidate inputs for this graph. Inputs are nodes in the graph
with no parents that are not shared and are not constants.
params : list of Theano shared variables
A list of candidate parameters for this graph. Parameters are nodes in
the graph that are shared variables.
'''
queue, seen, inputs, params = [node], set(), set(), set()
while queue:
node = queue.pop()
seen.add(node)
queue.extend(p for p in node.get_parents() if p not in seen)
if not node.get_parents():
if isinstance(node, theano.compile.SharedVariable):
params.add(node)
elif not isinstance(node, TT.Constant):
inputs.add(node)
return list(inputs), list(params)
示例8: push_out_inner_vars
# 需要导入模块: from theano import tensor [as 别名]
# 或者: from theano.tensor import Constant [as 别名]
def push_out_inner_vars(self, fgraph, inner_vars, old_scan_node,
old_scan_args):
outer_vars = [None] * len(inner_vars)
new_scan_node = old_scan_node
new_scan_args = old_scan_args
# For the inner_vars that already exist in the outer graph,
# simply obtain a reference to them
for idx in range(len(inner_vars)):
var = inner_vars[idx]
if var in old_scan_args.inner_in_seqs:
idx_seq = old_scan_args.inner_in_seqs.index(var)
outer_vars[idx] = old_scan_args.outer_in_seqs[idx_seq]
elif var in old_scan_args.inner_in_non_seqs:
idx_non_seq = old_scan_args.inner_in_non_seqs.index(var)
outer_vars[idx] = old_scan_args.outer_in_non_seqs[idx_non_seq]
elif isinstance(var, theano.Constant):
outer_vars[idx] = var.clone()
elif var in old_scan_args.inner_out_nit_sot:
idx_nitsot = old_scan_args.inner_out_nit_sot.index(var)
outer_vars[idx] = old_scan_args.outer_out_nit_sot[idx_nitsot]
# For the inner_vars that don't already exist in the outer graph, add
# them as new nitsot outputs to the scan node.
idx_add_as_nitsots = [i for i in range(len(outer_vars))
if outer_vars[i] is None]
add_as_nitsots = [inner_vars[idx] for idx in idx_add_as_nitsots]
if len(add_as_nitsots) > 0:
new_scan_node = self.add_nitsot_outputs(fgraph, old_scan_node,
old_scan_args,
add_as_nitsots)
new_scan_args = scan_args(new_scan_node.inputs,
new_scan_node.outputs,
new_scan_node.op.inputs,
new_scan_node.op.outputs,
new_scan_node.op.info)
new_outs = new_scan_args.outer_out_nit_sot[-len(add_as_nitsots):]
for i in range(len(new_outs)):
outer_vars[idx_add_as_nitsots[i]] = new_outs[i]
return outer_vars, new_scan_node, new_scan_args