Example #1
0
def test_FlowNode_invertibility():
    flow = mdp.Flow([mdp.nodes.PolynomialExpansionNode(degree=2)])
    flownode = mh.FlowNode(flow)
    assert flownode.is_invertible() is False
    flow = mdp.Flow([
        mdp.nodes.PCANode(output_dim=15),
        mdp.nodes.SFANode(),
        mdp.nodes.PCANode(output_dim=3)
    ])
    flownode = mh.FlowNode(flow)
    assert flownode.is_invertible() is True
Example #2
0
def test_FlowNode_pretrained_flow():
    flow = mdp.Flow([
        mdp.nodes.PolynomialExpansionNode(degree=2),
        mdp.nodes.PCANode(output_dim=15, reduce=True),
        mdp.nodes.PolynomialExpansionNode(degree=2),
        mdp.nodes.PCANode(output_dim=3, reduce=True)
    ])
    flownode = mh.FlowNode(flow)
    x = numx_rand.random([300, 20])
    while flownode.get_remaining_train_phase() > 0:
        flownode.train(x)
        flownode.stop_training()
    # build new flownode with the trained nodes
    flownode = mh.FlowNode(flow)
    assert not flownode.is_training()
    flownode.execute(x)
Example #3
0
def test_FlowNode_fix_nodes_dimensions3():
    flow = mdp.Flow([mdp.nodes.IdentityNode()])
    flownode = mh.FlowNode(flow)
    # for a single node this should not raise an Exception
    flownode.set_output_dim(10)
    x = numx_rand.random([100, 10])
    flownode.execute(x)
Example #4
0
def test_FlowNode_fix_nodes_dimensions2():
    flow = mdp.Flow([mdp.nodes.IdentityNode(), mdp.nodes.IdentityNode()])
    flownode = mh.FlowNode(flow)
    # this should fail, since the internal nodes don't have fixed dims
    py.test.raises(mdp.InconsistentDimException,
                   lambda: flownode.set_output_dim(10))
    x = numx_rand.random([100, 10])
    flownode.execute(x)
    assert flownode.output_dim == 10
Example #5
0
def test_SFA_net(noisenode):
    sfa_node = mdp.nodes.SFANode(input_dim=20 * 20, output_dim=10, dtype='f')
    switchboard = mh.Rectangular2dSwitchboard(in_channels_xy=100,
                                              field_channels_xy=20,
                                              field_spacing_xy=10)
    flownode = mh.FlowNode(mdp.Flow([noisenode, sfa_node]))
    sfa_layer = mh.CloneLayer(flownode, switchboard.output_channels)
    flow = mdp.Flow([switchboard, sfa_layer])
    train_gen = numx.cast['f'](numx_rand.random((3, 10, 100 * 100)))
    flow.train([None, train_gen])
Example #6
0
def test_FlowNode_copy2():
    # Test that the FlowNode copy method delegates to internal nodes.
    class CopyFailException(Exception):
        pass

    class CopyFailNode(mdp.Node):
        def copy(self, protocol=None):
            raise CopyFailException()

    flow = mdp.Flow([mdp.Node(), CopyFailNode()])
    flownode = mh.FlowNode(flow)
    py.test.raises(CopyFailException, flownode.copy)
Example #7
0
def test_flownode_training():
    flow = mdp.Flow([
        mdp.nodes.PolynomialExpansionNode(degree=2),
        mdp.nodes.PCANode(output_dim=15, reduce=True),
        mdp.nodes.PolynomialExpansionNode(degree=2),
        mdp.nodes.PCANode(output_dim=3, reduce=True)
    ])
    flownode = mh.FlowNode(flow)
    x = numx_rand.random([300, 20])
    while flownode.get_remaining_train_phase() > 0:
        flownode.train(x)
        flownode.stop_training()
    flownode.execute(x)
Example #8
0
def test_FlowNode_fix_nodes_dimensions1():
    x = numx_rand.random([100, 10])
    last_node = mdp.nodes.IdentityNode()
    flow = mdp.Flow(
        [mdp.nodes.PCANode(output_dim=3),
         mdp.nodes.IdentityNode(), last_node])
    flownode = mh.FlowNode(flow)
    flownode.train(x)
    flownode.stop_training()
    # check that the dimensions of NoiseNode and FlowNode where all set
    # by calling _fix_nodes_dimensions
    assert flownode.output_dim == 3
    assert last_node.input_dim == 3
    assert last_node.output_dim == 3
Example #9
0
def testHiNetHTML(noisenode):
    # create some flow for testing
    sfa_node = mdp.nodes.SFANode(input_dim=20 * 20, output_dim=10)
    switchboard = mh.Rectangular2dSwitchboard(in_channels_xy=100,
                                              field_channels_xy=20,
                                              field_spacing_xy=10)
    flownode = mh.FlowNode(mdp.Flow([noisenode, sfa_node]))
    sfa_layer = mh.CloneLayer(flownode, switchboard.output_channels)
    flow = mdp.Flow([switchboard, sfa_layer])
    # create dummy file to write the HTML representation
    html_file = io.StringIO()
    hinet_html = mdp.hinet.HiNetHTMLVisitor(html_file)
    hinet_html.convert_flow(flow)
    html_file.close()
Example #10
0
def test_hinet_simple_net():
    switchboard = mh.Rectangular2dSwitchboard(in_channels_xy=(12, 8),
                                              field_channels_xy=4,
                                              field_spacing_xy=2,
                                              in_channel_dim=3)

    node = mdp.nodes.PCANode(input_dim=4 * 4 * 3, output_dim=5)
    flownode = mh.FlowNode(mdp.Flow([
        node,
    ]))
    layer = mh.CloneLayer(flownode, switchboard.output_channels)
    flow = mdp.Flow([switchboard, layer])
    x = numx_rand.random([5, switchboard.input_dim])
    flow.train(x)
Example #11
0
def test_FlowNode_pretrained_node():
    x = numx_rand.random([100, 10])
    pretrained_node = mdp.nodes.PCANode(output_dim=6)
    pretrained_node.train(x)
    pretrained_node.stop_training()
    flow = mdp.Flow([
        pretrained_node,
        mdp.nodes.PolynomialExpansionNode(degree=2),
        mdp.nodes.PCANode(output_dim=3)
    ])
    flownode = mh.FlowNode(flow)
    while flownode.get_remaining_train_phase() > 0:
        flownode.train(x)
        flownode.stop_training()
    flownode.execute(x)
Example #12
0
def test_FlowNode_copy1():
    flow = mdp.Flow([mdp.nodes.PCANode(), mdp.nodes.SFANode()])
    flownode = mh.FlowNode(flow)
    flownode.copy()