Ejemplo n.º 1
0
train_data = [np.random.random((10, switchboard.input_dim))
              for _ in range(3)]
flow.train([None, train_data])

sender_node = bimdp.nodes.SenderBiNode(node_id="sender", recipient_id="newton")
newton_node = NewtonNode(sender_id="sender", input_dim=sfa_layer.output_dim,
                         node_id="newton")
flow = sender_node + flow + newton_node

x_goal = np.random.random((2, switchboard.input_dim))
goal_y, msg = flow.execute(x_goal)

x_start = np.random.random((2, switchboard.input_dim))
y_start, _ = flow.execute(x_start)

msg = {"method": "newton", "n_iterations": n_iterations, "x_start": x_start}
if show_inspection:
    _, (x, msg) = bimdp.show_execution(flow, x=goal_y, msg=msg, target="newton")
else:
    x, msg = flow.execute(goal_y, msg, "newton")

y, _ = flow.execute(x)
print ("errors before optimization: %s" %
       np.sum((y_start - goal_y)**2, axis=1))
# Expected:
## errors before optimization: [ 196.49202899  241.31524993]
print ("errors after optimization  : %s" %
       np.sum((y - goal_y)**2, axis=1))
# Expected:
## errors after optimization  : [ 39.01372025  34.84160123]
Ejemplo n.º 2
0
                                     method_args=method_args,
                                     method_kwargs=method_kwargs)
        return section_id


## Create the flow.
noisenode = mdp.nodes.NormalNoiseNode(input_dim=20 * 20,
                                      noise_args=(0, 0.0001))
sfa_node = bimdp.nodes.SFABiNode(input_dim=20 * 20,
                                 output_dim=10,
                                 dtype='f',
                                 node_id="sfa")
switchboard = mdp.hinet.Rectangular2dSwitchboard(in_channels_xy=100,
                                                 field_channels_xy=20,
                                                 field_spacing_xy=10)
flownode = mdp.hinet.FlowNode(mdp.Flow([noisenode, sfa_node]))
sfa_layer = mdp.hinet.CloneLayer(flownode, switchboard.output_channels)
flow = mdp.Flow([switchboard, sfa_layer])
train_data = [
    numpy.cast['f'](numpy.random.random((3, 100 * 100))) for _ in range(5)
]
flow.train(data_iterables=[None, train_data])

## This is where the inspection happens.
html_converter = CustomTraceHTMLConverter()
# note that we could also specify a custom CSS file, via css_filename
tracer = bimdp.InspectionHTMLTracer(html_converter=html_converter)
filename, out = bimdp.show_execution(flow, x=train_data[0], tracer=tracer)

print "done."
Ejemplo n.º 3
0
"""

import numpy

import mdp
import bimdp

## Create the flow.
noisenode = mdp.nodes.NormalNoiseNode(input_dim=20*20,
                                      noise_args=(0, 0.0001))
sfa_node = mdp.nodes.SFANode(input_dim=20*20, output_dim=20, dtype='f')
sfa2_node = mdp.nodes.SFA2Node(input_dim=20, output_dim=10)
switchboard = mdp.hinet.Rectangular2dSwitchboard(
                                          in_channels_xy=100,
                                          field_channels_xy=20,
                                          field_spacing_xy=10)
flownode = mdp.hinet.FlowNode(noisenode + sfa_node + sfa2_node)
sfa_layer = mdp.hinet.CloneLayer(flownode, switchboard.output_channels)
flow = switchboard + sfa_layer

train_data = [numpy.cast['f'](numpy.random.random((10, 100*100)))
              for _ in range(5)]

## Do the inspections and open in browser.
# The debug=True is not needed here, unless one starts experimenting.
bimdp.show_training(flow=flow, data_iterables=[None, train_data],
                    debug=True)
filename, out = bimdp.show_execution(flow, x=train_data[0], debug=True)

print "done."
Ejemplo n.º 4
0
                               method_result=method_result,
                               method_args=method_args,
                               method_kwargs=method_kwargs)
        return section_id


## Create the flow.
noisenode = mdp.nodes.NormalNoiseNode(input_dim=20*20,
                                      noise_args=(0, 0.0001))
sfa_node = bimdp.nodes.SFABiNode(input_dim=20*20, output_dim=10, dtype='f',
                                 node_id="sfa")
switchboard = mdp.hinet.Rectangular2dSwitchboard(
                                          in_channels_xy=100,
                                          field_channels_xy=20,
                                          field_spacing_xy=10)
flownode = mdp.hinet.FlowNode(mdp.Flow([noisenode, sfa_node]))
sfa_layer = mdp.hinet.CloneLayer(flownode, switchboard.output_channels)
flow = mdp.Flow([switchboard, sfa_layer])
train_data = [numpy.cast['f'](numpy.random.random((3, 100*100)))
              for _ in range(5)]
flow.train(data_iterables=[None, train_data])

## This is where the inspection happens.
html_converter = CustomTraceHTMLConverter()
# note that we could also specify a custom CSS file, via css_filename
tracer = bimdp.InspectionHTMLTracer(html_converter=html_converter)
filename, out = bimdp.show_execution(flow, x=train_data[0],
                                     tracer=tracer)

print "done."
Ejemplo n.º 5
0
                                  n_nodes=6, use_copies=True, node_id="layer_1")
layer_flownode1 = bimdp.hinet.BiFlowNode(switchboard1 + layer1)
switchboard2 = bimdp.hinet.BiSwitchboard(input_dim=12, connections=range(12),
                                         node_id="switchboard_2")
layer2 = bimdp.hinet.CloneBiLayer(MPerceptronBiNode(input_dim=6),
                                  n_nodes=2, use_copies=True, node_id="layer_2")
layer_flownode2 = bimdp.hinet.BiFlowNode(switchboard2 + layer2)
# Note: bottom_node can't be set to container nodes, since the target value
#    gets overridden by the internal node's -1 default inverse target value.
backprop_node = BackpropBiNode(bottom_node="switchboard_1",
                               node_id="backprop_node")
perceptron = layer_flownode1 + layer_flownode2 + backprop_node

## Train with batch backpropagation.
n_patterns = 20
n_training_iterations = 5
data = np.random.random((n_patterns, 18))
# encapsulate reference data in dict to not confuse the switchboard
reference = (np.random.random((n_patterns, 2)),)
msg = {"reference_output": reference, "gamma": 0.2}
# show only the first training iteration
bimdp.show_execution(perceptron, data, msg, debug=True)
# remaining training iterations
perceptron.execute([data for _ in range(n_training_iterations-1)],
                   [msg for _ in range(n_training_iterations-1)])

## Test without backpropagation.
#_, result = bimdp.show_execution(perceptron, data, debug=True)
result = perceptron.execute(data)

print "done."
Ejemplo n.º 6
0
should not be included in the unittests.
"""

import numpy

import mdp
import bimdp

## Create the flow.
noisenode = mdp.nodes.NormalNoiseNode(input_dim=20 * 20,
                                      noise_args=(0, 0.0001))
sfa_node = mdp.nodes.SFANode(input_dim=20 * 20, output_dim=20, dtype='f')
sfa2_node = mdp.nodes.SFA2Node(input_dim=20, output_dim=10)
switchboard = mdp.hinet.Rectangular2dSwitchboard(in_channels_xy=100,
                                                 field_channels_xy=20,
                                                 field_spacing_xy=10)
flownode = mdp.hinet.FlowNode(noisenode + sfa_node + sfa2_node)
sfa_layer = mdp.hinet.CloneLayer(flownode, switchboard.output_channels)
flow = switchboard + sfa_layer

train_data = [
    numpy.cast['f'](numpy.random.random((10, 100 * 100))) for _ in range(5)
]

## Do the inspections and open in browser.
# The debug=True is not needed here, unless one starts experimenting.
bimdp.show_training(flow=flow, data_iterables=[None, train_data], debug=True)
filename, out = bimdp.show_execution(flow, x=train_data[0], debug=True)

print "done."
Ejemplo n.º 7
0
                                         connections=range(12),
                                         node_id="switchboard_2")
layer2 = bimdp.hinet.CloneBiLayer(MPerceptronBiNode(input_dim=6),
                                  n_nodes=2,
                                  use_copies=True,
                                  node_id="layer_2")
layer_flownode2 = bimdp.hinet.BiFlowNode(switchboard2 + layer2)
# Note: bottom_node can't be set to container nodes, since the target value
#    gets overridden by the internal node's -1 default inverse target value.
backprop_node = BackpropBiNode(bottom_node="switchboard_1",
                               node_id="backprop_node")
perceptron = layer_flownode1 + layer_flownode2 + backprop_node

## Train with batch backpropagation.
n_patterns = 20
n_training_iterations = 5
data = np.random.random((n_patterns, 18))
# encapsulate reference data in dict to not confuse the switchboard
reference = (np.random.random((n_patterns, 2)), )
msg = {"reference_output": reference, "gamma": 0.2}
# show only the first training iteration
bimdp.show_execution(perceptron, data, msg, debug=True)
# remaining training iterations
perceptron.execute([data for _ in range(n_training_iterations - 1)],
                   [msg for _ in range(n_training_iterations - 1)])

## Test without backpropagation.
#_, result = bimdp.show_execution(perceptron, data, debug=True)
result = perceptron.execute(data)

print "done."