本文整理汇总了Python中theano.compile.Mode方法的典型用法代码示例。如果您正苦于以下问题:Python compile.Mode方法的具体用法?Python compile.Mode怎么用?Python compile.Mode使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类theano.compile
的用法示例。
在下文中一共展示了compile.Mode方法的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_c_thunks
# 需要导入模块: from theano import compile [as 别名]
# 或者: from theano.compile import Mode [as 别名]
def test_c_thunks():
a = tensor.scalars('a')
b, c = tensor.vectors('bc')
cases = [False]
if theano.config.cxx:
cases.append(True)
for c_thunks in cases:
f = function([a, b, c], ifelse(a, a * b, b * c),
mode=Mode(
optimizer=None,
linker=vm.VM_Linker(c_thunks=c_thunks,
use_cloop=False)))
f(1, [2], [3, 2])
from nose.tools import assert_raises
assert_raises(ValueError, f, 0, [2], [3, 4])
assert any([hasattr(t, 'cthunk') for t in f.fn.thunks]) == c_thunks
示例2: test_vm_gc
# 需要导入模块: from theano import compile [as 别名]
# 或者: from theano.compile import Mode [as 别名]
def test_vm_gc():
"""This already caused a bug in the trunk of Theano.
The bug was introduced in the trunk on July 5th, 2012 and fixed on
July 30th.
"""
x = theano.tensor.vector()
p = RunOnce()(x)
mode = theano.Mode(linker=theano.gof.vm.VM_Linker(lazy=True))
f = theano.function([theano.In(x, mutable=True)], [p + 1, p + 2],
mode=mode)
f([1, 2, 3])
p = RunOnce()(x)
pp = p + p
f = theano.function([x], [pp + pp],
mode=mode)
f([1, 2, 3])
示例3: test1
# 需要导入模块: from theano import compile [as 别名]
# 或者: from theano.compile import Mode [as 别名]
def test1(self):
# this is a quick test after the LazyLinker branch merge
# to check that all the current modes can still be used.
linker_classes_involved = []
predef_modes = ['FAST_COMPILE', 'FAST_RUN', 'DEBUG_MODE']
# Use a new instance of ProfileMode instead of 'ProfileMode' to
# avoid printing a profile mode summary in nose output
predef_modes.append(ProfileMode())
# Linkers to use with regular Mode
if theano.config.cxx:
linkers = ['py', 'c|py', 'c|py_nogc', 'vm', 'vm_nogc',
'cvm', 'cvm_nogc']
else:
linkers = ['py', 'c|py', 'c|py_nogc', 'vm', 'vm_nogc']
modes = predef_modes + [Mode(linker, 'fast_run') for linker in linkers]
for mode in modes:
x = T.matrix()
y = T.vector()
f = theano.function([x, y], x + y, mode=mode)
# test that it runs something
f([[1, 2], [3, 4]], [5, 6])
linker_classes_involved.append(f.maker.mode.linker.__class__)
# print 'MODE:', mode, f.maker.mode.linker, 'stop'
# regression check:
# there should be
# - VM_Linker
# - OpWiseCLinker (FAST_RUN)
# - WrapLinker ("ProfileMode")
# - PerformLinker (FAST_COMPILE)
# - DebugMode's Linker (DEBUG_MODE)
assert 5 == len(set(linker_classes_involved))
示例4: test_callback
# 需要导入模块: from theano import compile [as 别名]
# 或者: from theano.compile import Mode [as 别名]
def test_callback(self):
a, b, c = tensor.scalars('abc')
f = function([a, b, c], (a + b) + c,
mode=Mode(
optimizer=None,
linker=vm.VM_Linker(callback=self.callback)))
f(1, 2, 3)
assert sum(self.n_callbacks.values()) == len(f.maker.fgraph.toposort())
f(1, 2, 3)
assert (sum(self.n_callbacks.values()) ==
len(f.maker.fgraph.toposort()) * 2)
示例5: test_callback_with_ifelse
# 需要导入模块: from theano import compile [as 别名]
# 或者: from theano.compile import Mode [as 别名]
def test_callback_with_ifelse(self):
a, b, c = tensor.scalars('abc')
f = function([a, b, c], ifelse(a, 2 * b, 2 * c),
mode=Mode(
optimizer=None,
linker=vm.VM_Linker(callback=self.callback)))
f(1, 2, 3)
assert self.n_callbacks['IfElse'] == 2
示例6: test_no_leak_many_call_lazy
# 需要导入模块: from theano import compile [as 别名]
# 或者: from theano.compile import Mode [as 别名]
def test_no_leak_many_call_lazy():
# Verify no memory leaks when calling a function a lot of times
# This isn't really a unit test, you have to run it and look at top to
# see if there's a leak
def build_graph(x, depth=5):
z = x
for d in range(depth):
z = ifelse(z.mean() > 0.5, -z, z)
return z
def time_linker(name, linker):
steps_a = 10
x = tensor.dvector()
a = build_graph(x, steps_a)
f_a = function([x], a,
mode=Mode(optimizer=None,
linker=linker()))
inp = numpy.random.rand(1000000)
for i in xrange(100):
f_a(inp)
if 0: # this doesn't seem to work, prints 0 for everything
import resource
pre = resource.getrusage(resource.RUSAGE_SELF)
post = resource.getrusage(resource.RUSAGE_SELF)
print(pre.ru_ixrss, post.ru_ixrss)
print(pre.ru_idrss, post.ru_idrss)
print(pre.ru_maxrss, post.ru_maxrss)
print(1)
time_linker('vmLinker_C',
lambda: vm.VM_Linker(allow_gc=False, use_cloop=True))
print(2)
time_linker('vmLinker',
lambda: vm.VM_Linker(allow_gc=False, use_cloop=False))
示例7: test_no_leak_many_call_nonlazy
# 需要导入模块: from theano import compile [as 别名]
# 或者: from theano.compile import Mode [as 别名]
def test_no_leak_many_call_nonlazy():
# Verify no memory leaks when calling a function a lot of times
# This isn't really a unit test, you have to run it and look at top to
# see if there's a leak.
def build_graph(x, depth=5):
z = x
for d in range(depth):
z = tensor.sin(-z + 1)
return z
def time_linker(name, linker):
steps_a = 10
x = tensor.dvector()
a = build_graph(x, steps_a)
f_a = function([x], a,
mode=Mode(optimizer=None,
linker=linker()))
inp = numpy.random.rand(1000000)
for i in xrange(500):
f_a(inp)
print(1)
time_linker('vmLinker_C',
lambda: vm.VM_Linker(allow_gc=False, use_cloop=True))
print(2)
time_linker('vmLinker',
lambda: vm.VM_Linker(allow_gc=False, use_cloop=False))
示例8: test_reallocation
# 需要导入模块: from theano import compile [as 别名]
# 或者: from theano.compile import Mode [as 别名]
def test_reallocation():
x = tensor.scalar('x')
y = tensor.scalar('y')
z = tensor.tanh(3 * x + y) + tensor.cosh(x + 5 * y)
# The functinality is currently implement for non lazy and non c VM only.
for l in [vm.VM_Linker(allow_gc=False, lazy=False, use_cloop=False),
vm.VM_Linker(allow_gc=True, lazy=False, use_cloop=False)]:
m = theano.compile.get_mode(theano.Mode(linker=l))
m = m.excluding('fusion', 'inplace')
f = theano.function([x, y], z, name="test_reduce_memory",
mode=m)
output = f(1, 2)
assert output
storage_map = f.fn.storage_map
def check_storage(storage_map):
from theano.tensor.var import TensorConstant
for i in storage_map:
if not isinstance(i, TensorConstant):
keys_copy = list(storage_map.keys())[:]
keys_copy.remove(i)
for o in keys_copy:
if (storage_map[i][0] and
storage_map[i][0] is storage_map[o][0]):
return [True, storage_map[o][0]]
return [False, None]
assert check_storage(storage_map)[0]
assert len(set(id(v) for v in
itervalues(storage_map))) < len(storage_map)
示例9: get_neg_detection_mode
# 需要导入模块: from theano import compile [as 别名]
# 或者: from theano.compile import Mode [as 别名]
def get_neg_detection_mode():
"""
Returns a theano Mode that detects if any negative value occurs in the
evaluation of a theano function.
"""
class NegDetectionMode(Mode):
def __init__(self):
def flatten(l):
if isinstance(l, (list, tuple)):
rval = []
for elem in l:
if isinstance(elem, (list, tuple)):
rval.extend(flatten(elem))
else:
rval.append(elem)
else:
return rval
def do_check_on(var, nd, f):
if var.min() < 0:
raise NegativeVariableError()
def neg_check(i, node, fn):
inputs = fn.inputs
for x in flatten(inputs):
do_check_on(x, node, fn)
fn()
outputs = fn.outputs
for j, x in enumerate(flatten(outputs)):
do_check_on(x, node, fn)
wrap_linker = theano.gof.WrapLinkerMany(
[theano.gof.OpWiseCLinker()],
[neg_check])
super(NegDetectionMode, self).__init__(wrap_linker,
optimizer='fast_run')
return NegDetectionMode()
示例10: test_speed_lazy
# 需要导入模块: from theano import compile [as 别名]
# 或者: from theano.compile import Mode [as 别名]
def test_speed_lazy():
def build_graph(x, depth=5):
z = x
for d in range(depth):
z = ifelse(z[0] > 0, -z, z)
return z
def time_linker(name, linker):
steps_a = 10
steps_b = 100
x = tensor.vector()
a = build_graph(x, steps_a)
b = build_graph(x, steps_b)
f_a = function([x], a,
mode=Mode(optimizer=None,
linker=linker()))
f_b = function([x], b,
mode=Mode(optimizer=None,
linker=linker()))
f_a([2.0])
t0 = time.time()
f_a([2.0])
t1 = time.time()
f_b([2.0])
t2 = time.time()
f_b([2.0])
t3 = time.time()
t_a = t1 - t0
t_b = t3 - t2
print("%s takes %f s/Kop" % (
name,
(1000 * (t_b - t_a) / (steps_b - steps_a))))
time_linker('vmLinker', vm.VM_Linker)
time_linker('vmLinker_nogc', lambda: vm.VM_Linker(allow_gc=False))
if theano.config.cxx:
time_linker('vmLinker_C', lambda: vm.VM_Linker(allow_gc=False,
use_cloop=True))