本文整理汇总了Python中mdp.activate_extension函数的典型用法代码示例。如果您正苦于以下问题:Python activate_extension函数的具体用法?Python activate_extension怎么用?Python activate_extension使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了activate_extension函数的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Python代码示例。
示例1: testContextManager1
def testContextManager1():
"""Test that the context manager activates extensions."""
class Test1ExtensionNode(mdp.ExtensionNode):
extension_name = "__test1"
def _testtest(self):
pass
class Test2ExtensionNode(mdp.ExtensionNode):
extension_name = "__test2"
def _testtest(self):
pass
assert mdp.get_active_extensions() == []
with mdp.extension('__test1'):
assert mdp.get_active_extensions() == ['__test1']
assert mdp.get_active_extensions() == []
# with multiple extensions
with mdp.extension(['__test1', '__test2']):
active = mdp.get_active_extensions()
assert '__test1' in active
assert '__test2' in active
assert mdp.get_active_extensions() == []
mdp.activate_extension("__test1")
# Test that only activated extensions are deactiveted.
with mdp.extension(['__test1', '__test2']):
active = mdp.get_active_extensions()
assert '__test1' in active
assert '__test2' in active
assert mdp.get_active_extensions() == ["__test1"]
开发者ID:Debilski,项目名称:mdp-toolkit,代码行数:29,代码来源:test_extension.py
示例2: testExtensionInheritanceInjection
def testExtensionInheritanceInjection():
"""Test the injection of inherited methods"""
class TestNode(object):
def _test1(self):
return 0
class TestExtensionNode(mdp.ExtensionNode):
extension_name = "__test"
def _test1(self):
return 1
def _test2(self):
return 2
def _test3(self):
return 3
class TestNodeExt(TestExtensionNode, TestNode):
def _test2(self):
return "2b"
@mdp.extension_method("__test", TestNode)
def _test4(self):
return 4
test_node = TestNode()
mdp.activate_extension("__test")
assert test_node._test1() == 1
assert test_node._test2() == "2b"
assert test_node._test3() == 3
assert test_node._test4() == 4
mdp.deactivate_extension("__test")
assert test_node._test1() == 0
assert not hasattr(test_node, "_test2")
assert not hasattr(test_node, "_test3")
assert not hasattr(test_node, "_test4")
开发者ID:Debilski,项目名称:mdp-toolkit,代码行数:30,代码来源:test_extension.py
示例3: test_sfa2_gradient2
def test_sfa2_gradient2(self):
def _alt_sfa2_grad(self, x):
"""Reference grad method based on quadratic forms."""
# note that the H and f arrays are cached in the node and remain even
# after the extension has been deactivated
if not hasattr(self, "__gradient_Hs"):
quad_forms = [self.get_quadratic_form(i)
for i in range(self.output_dim)]
self.__gradient_Hs = numx.vstack((quad_form.H[numx.newaxis]
for quad_form in quad_forms))
self.__gradient_fs = numx.vstack((quad_form.f[numx.newaxis]
for quad_form in quad_forms))
grad = (numx.dot(x, self.__gradient_Hs) +
numx.repeat(self.__gradient_fs[numx.newaxis,:,:],
len(x), axis=0))
return grad
sfa2_node = bimdp.nodes.SFA2BiNode(output_dim=3)
x = numx_rand.random((300, 6))
sfa2_node.train(x)
sfa2_node.stop_training()
x = numx_rand.random((2, 6))
mdp.activate_extension("gradient")
try:
result1 = sfa2_node.execute(x, {"method": "gradient"})
grad1 = result1[1]["grad"]
grad2 = _alt_sfa2_grad(sfa2_node, x)
assert numx.amax(abs(grad1 - grad2)) < 1E-9
finally:
mdp.deactivate_extension("gradient")
开发者ID:Debilski,项目名称:mdp-toolkit,代码行数:29,代码来源:test_gradient.py
示例4: test_quadexpan_gradient2
def test_quadexpan_gradient2(self):
"""Test gradient with multiple data points."""
node = mdp.nodes.QuadraticExpansionNode()
x = numx_rand.random((3,5))
node.execute(x)
mdp.activate_extension("gradient")
try:
result = node._gradient(x)
gradient = result[1]["grad"]
assert gradient.shape == (3,20,5)
finally:
mdp.deactivate_extension("gradient")
开发者ID:Debilski,项目名称:mdp-toolkit,代码行数:12,代码来源:test_gradient.py
示例5: test_sfa2_gradient
def test_sfa2_gradient(self):
sfa2_node1 = bimdp.nodes.SFA2BiNode(output_dim=5)
sfa2_node2 = bimdp.nodes.SFA2BiNode(output_dim=3)
flow = sfa2_node1 + sfa2_node2
x = numx_rand.random((300, 6))
flow.train(x)
x = numx_rand.random((2, 6))
mdp.activate_extension("gradient")
try:
flow.execute(x, {"method": "gradient"})
finally:
mdp.deactivate_extension("gradient")
开发者ID:Debilski,项目名称:mdp-toolkit,代码行数:12,代码来源:test_gradient.py
示例6: testExtensionInheritanceTwoExtensions
def testExtensionInheritanceTwoExtensions():
"""Test non_extension injection for multiple extensions."""
class Test1ExtensionNode(mdp.ExtensionNode):
extension_name = "__test1"
def _execute(self):
return 1
class Test2ExtensionNode(mdp.ExtensionNode):
extension_name = "__test2"
class Test3ExtensionNode(mdp.ExtensionNode):
extension_name = "__test3"
def _execute(self):
return "3a"
class TestNode1(mdp.Node):
pass
class TestNode2(TestNode1):
pass
class ExtendedTest1Node2(Test1ExtensionNode, TestNode2):
pass
class ExtendedTest2Node1(Test2ExtensionNode, TestNode1):
def _execute(self):
return 2
class ExtendedTest3Node1(Test3ExtensionNode, TestNode1):
def _execute(self):
return "3b"
test_node = TestNode2()
mdp.activate_extension('__test2')
assert test_node._execute() == 2
mdp.deactivate_extension('__test2')
# in this order TestNode2 should get execute from __test1,
# the later addition by __test1 to TestNode1 doesn't matter
mdp.activate_extensions(['__test1', '__test2'])
assert test_node._execute() == 1
mdp.deactivate_extensions(['__test2', '__test1'])
# now activate in inverse order
# TestNode2 already gets _execute from __test2, but that is still
# overriden by __test1, thats how its registered in _extensions
mdp.activate_extensions(['__test2', '__test1'])
assert test_node._execute() == 1
mdp.deactivate_extensions(['__test2', '__test1'])
## now the same with extension 3
mdp.activate_extension('__test3')
assert test_node._execute() == "3b"
mdp.deactivate_extension('__test3')
# __test3 does not override, since the _execute slot for Node2
# was first filled by __test1
mdp.activate_extensions(['__test3', '__test1'])
assert test_node._execute() == 1
mdp.deactivate_extensions(['__test3', '__test1'])
# inverse order
mdp.activate_extensions(['__test1', '__test3'])
assert test_node._execute() == 1
mdp.deactivate_extensions(['__test2', '__test1'])
开发者ID:Debilski,项目名称:mdp-toolkit,代码行数:52,代码来源:test_extension.py
示例7: test_switchboard_gradient1
def test_switchboard_gradient1(self):
"""Test that gradient is correct for a tiny switchboard."""
sboard = mdp.hinet.Switchboard(input_dim=4, connections=[2,0])
x = numx_rand.random((2,4))
mdp.activate_extension("gradient")
try:
result = sboard._gradient(x)
grad = result[1]["grad"]
ref_grad = numx.array([[[0,0,1,0], [1,0,0,0]],
[[0,0,1,0], [1,0,0,0]]], dtype=grad.dtype)
assert numx.all(grad == ref_grad)
finally:
mdp.deactivate_extension("gradient")
开发者ID:Debilski,项目名称:mdp-toolkit,代码行数:13,代码来源:test_gradient.py
示例8: test_sfa_gradient
def test_sfa_gradient(self):
"""Test gradient for combination of SFA nodes."""
sfa_node1 = bimdp.nodes.SFABiNode(output_dim=8)
sfa_node2 = bimdp.nodes.SFABiNode(output_dim=7)
sfa_node3 = bimdp.nodes.SFABiNode(output_dim=5)
flow = sfa_node1 + sfa_node2 + sfa_node3
x = numx_rand.random((300, 10))
flow.train(x)
x = numx_rand.random((2, 10))
mdp.activate_extension("gradient")
try:
flow.execute(x, {"method": "gradient"})
finally:
mdp.deactivate_extension("gradient")
开发者ID:Debilski,项目名称:mdp-toolkit,代码行数:14,代码来源:test_gradient.py
示例9: testExtensionSetupTeardown
def testExtensionSetupTeardown():
"""Test defining setup and teardown functions."""
setup_calls = []
teardown_calls = []
@mdp.extension_setup("__test")
def dummy_setup():
setup_calls.append(True)
@mdp.extension_teardown("__test")
def dummy_setup():
teardown_calls.append(True)
mdp.activate_extension("__test")
assert len(setup_calls) == 1
mdp.deactivate_extension("__test")
assert len(teardown_calls) == 1
开发者ID:AlbertoEsc,项目名称:mdp-toolkit,代码行数:14,代码来源:test_extension.py
示例10: testExtensionInheritance2
def testExtensionInheritance2():
"""Test inheritance of extension nodes, using super."""
class TestExtensionNode(mdp.ExtensionNode):
extension_name = "__test"
def _testtest(self):
pass
class TestSFANode(TestExtensionNode, mdp.nodes.SFANode):
def _testtest(self):
return 42
class TestSFA2Node(mdp.nodes.SFA2Node, TestSFANode):
def _testtest(self):
return super(mdp.nodes.SFA2Node, self)._testtest()
sfa2_node = mdp.nodes.SFA2Node()
mdp.activate_extension("__test")
assert sfa2_node._testtest() == 42
开发者ID:Debilski,项目名称:mdp-toolkit,代码行数:15,代码来源:test_extension.py
示例11: test_clonebilayer_gradient
def test_clonebilayer_gradient(self):
"""Test gradient for a simple layer."""
layer = bimdp.hinet.CloneBiLayer(
bimdp.nodes.SFA2BiNode(input_dim=5, output_dim=2),
n_nodes=3)
x = numx_rand.random((100,15))
layer.train(x)
layer.stop_training()
mdp.activate_extension("gradient")
try:
x = numx_rand.random((7,15))
result = layer._gradient(x)
grad = result[1]["grad"]
assert grad.shape == (7,6,15)
finally:
mdp.deactivate_extension("gradient")
开发者ID:Debilski,项目名称:mdp-toolkit,代码行数:16,代码来源:test_gradient.py
示例12: testDecoratorExtension
def testDecoratorExtension():
"""Test extension decorator with a single new extension."""
@mdp.extension_method("__test", mdp.nodes.SFANode, "_testtest")
def _sfa_testtest(self):
return 42
@mdp.extension_method("__test", mdp.nodes.SFA2Node)
def _testtest(self):
return 42 + _sfa_testtest(self)
sfa_node = mdp.nodes.SFANode()
sfa2_node = mdp.nodes.SFA2Node()
mdp.activate_extension("__test")
assert sfa_node._testtest() == 42
assert sfa2_node._testtest() == 84
mdp.deactivate_extension("__test")
assert not hasattr(mdp.nodes.SFANode, "_testtest")
assert not hasattr(mdp.nodes.SFA2Node, "_testtest")
开发者ID:AlbertoEsc,项目名称:mdp-toolkit,代码行数:16,代码来源:test_extension.py
示例13: testDecoratorInheritance
def testDecoratorInheritance():
"""Test inhertiance with decorators for a single new extension."""
class TestExtensionNode(mdp.ExtensionNode):
extension_name = "__test"
def _testtest(self):
pass
@mdp.extension_method("__test", mdp.nodes.SFANode, "_testtest")
def _sfa_testtest(self):
return 42
@mdp.extension_method("__test", mdp.nodes.SFA2Node)
def _testtest(self):
return 42 + super(mdp.nodes.SFA2Node, self)._testtest()
sfa_node = mdp.nodes.SFANode()
sfa2_node = mdp.nodes.SFA2Node()
mdp.activate_extension("__test")
assert sfa_node._testtest() == 42
assert sfa2_node._testtest() == 84
开发者ID:Debilski,项目名称:mdp-toolkit,代码行数:17,代码来源:test_extension.py
示例14: testSimpleExtension
def testSimpleExtension():
"""Test for a single new extension."""
class TestExtensionNode(mdp.ExtensionNode):
extension_name = "__test"
def _testtest(self):
pass
_testtest_attr = 1337
class TestSFANode(TestExtensionNode, mdp.nodes.SFANode):
def _testtest(self):
return 42
_testtest_attr = 1338
sfa_node = mdp.nodes.SFANode()
mdp.activate_extension("__test")
assert sfa_node._testtest() == 42
assert sfa_node._testtest_attr == 1338
mdp.deactivate_extension("__test")
assert not hasattr(mdp.nodes.SFANode, "_testtest")
开发者ID:Debilski,项目名称:mdp-toolkit,代码行数:17,代码来源:test_extension.py
示例15: testExtensionInheritanceInjectionNonExtension
def testExtensionInheritanceInjectionNonExtension():
"""Test non_extension method injection."""
class TestExtensionNode(mdp.ExtensionNode):
extension_name = "__test"
def _execute(self):
return 0
class TestNode(mdp.Node):
# no _execute method
pass
class ExtendedTestNode(TestExtensionNode, TestNode):
pass
test_node = TestNode()
mdp.activate_extension('__test')
assert hasattr(test_node, "_non_extension__execute")
mdp.deactivate_extension('__test')
assert not hasattr(test_node, "_non_extension__execute")
assert not hasattr(test_node, "_extension_for__execute")
# test that the non-native _execute has been completely removed
assert "_execute" not in test_node.__class__.__dict__
开发者ID:Debilski,项目名称:mdp-toolkit,代码行数:19,代码来源:test_extension.py
示例16: testExtensionInheritanceInjectionNonExtension2
def testExtensionInheritanceInjectionNonExtension2():
"""Test non_extension method injection."""
class TestExtensionNode(mdp.ExtensionNode):
extension_name = "__test"
def _execute(self):
return 0
class TestNode(mdp.Node):
def _execute(self):
return 1
class ExtendedTestNode(TestExtensionNode, TestNode):
pass
test_node = TestNode()
mdp.activate_extension('__test')
# test that non-extended attribute has been added as well
assert hasattr(test_node, "_non_extension__execute")
mdp.deactivate_extension('__test')
assert not hasattr(test_node, "_non_extension__execute")
assert not hasattr(test_node, "_extension_for__execute")
# test that the native _execute has been preserved
assert "_execute" in test_node.__class__.__dict__
开发者ID:Debilski,项目名称:mdp-toolkit,代码行数:20,代码来源:test_extension.py
示例17: testExtensionInheritance
def testExtensionInheritance():
"""Test inheritance of extension nodes."""
class TestExtensionNode(mdp.ExtensionNode):
extension_name = "__test"
def _testtest(self):
pass
class TestSFANode(TestExtensionNode, mdp.nodes.SFANode):
def _testtest(self):
return 42
_testtest_attr = 1337
class TestSFA2Node(TestSFANode, mdp.nodes.SFA2Node):
def _testtest(self):
if sys.version_info[0] < 3:
return TestSFANode._testtest.im_func(self)
else:
return TestSFANode._testtest(self)
sfa2_node = mdp.nodes.SFA2Node()
mdp.activate_extension("__test")
assert sfa2_node._testtest() == 42
assert sfa2_node._testtest_attr == 1337
开发者ID:Debilski,项目名称:mdp-toolkit,代码行数:20,代码来源:test_extension.py
示例18: test_stop_message_attribute
def test_stop_message_attribute(self):
"""Test that the stop_result attribute is present in forked node."""
stop_result = ({"test": "blabla"}, "node123")
x = n.random.random([100,10])
node = SFABiNode(stop_result=stop_result)
try:
mdp.activate_extension("parallel")
node2 = node.fork()
node2.train(x)
forked_result = node2.stop_training()
assert forked_result == (None,) + stop_result
# same with derived sfa2 node
node = SFA2BiNode(stop_result=stop_result)
mdp.activate_extension("parallel")
node2 = node.fork()
node2.train(x)
forked_result = node2.stop_training()
assert forked_result == (None,) + stop_result
finally:
mdp.deactivate_extension("parallel")
开发者ID:AlbertoEsc,项目名称:mdp-toolkit,代码行数:20,代码来源:test_parallelbiflow.py
示例19: test_layer_gradient
def test_layer_gradient(self):
"""Test gradient for a simple layer."""
node1 = mdp.nodes.SFA2Node(input_dim=4, output_dim=3)
node2 = mdp.nodes.SFANode(input_dim=6, output_dim=2)
layer = mdp.hinet.Layer([node1, node2])
x = numx_rand.random((100,10))
layer.train(x)
layer.stop_training()
mdp.activate_extension("gradient")
try:
x = numx_rand.random((7,10))
result = layer._gradient(x)
grad = result[1]["grad"]
# get reference result
grad1 = node1._gradient(x[:, :node1.input_dim])[1]["grad"]
grad2 = node2._gradient(x[:, node1.input_dim:])[1]["grad"]
ref_grad = numx.zeros(((7,5,10)))
ref_grad[:, :node1.output_dim, :node1.input_dim] = grad1
ref_grad[:, node1.output_dim:, node1.input_dim:] = grad2
assert numx.all(grad == ref_grad)
finally:
mdp.deactivate_extension("gradient")
开发者ID:Debilski,项目名称:mdp-toolkit,代码行数:22,代码来源:test_gradient.py
示例20: test_quadexpan_gradient1
def test_quadexpan_gradient1(self):
"""Test validity of gradient for QuadraticExpansionBiNode."""
node = mdp.nodes.QuadraticExpansionNode()
x = numx.array([[1, 3, 4]])
node.execute(x)
mdp.activate_extension("gradient")
try:
result = node._gradient(x)
grad = result[1]["grad"]
reference = numx.array(
[[[ 1, 0, 0], # x1
[ 0, 1, 0], # x2
[ 0, 0, 1], # x3
[ 2, 0, 0], # x1x1
[ 3, 1, 0], # x1x2
[ 4, 0, 1], # x1x3
[ 0, 6, 0], # x2x2
[ 0, 4, 3], # x2x3
[ 0, 0, 8]]]) # x3x3
assert numx.all(grad == reference)
finally:
mdp.deactivate_extension("gradient")
开发者ID:Debilski,项目名称:mdp-toolkit,代码行数:22,代码来源:test_gradient.py
注:本文中的mdp.activate_extension函数示例由纯净天空整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。 |
请发表评论