def get_DBN_flow(n_layers, hidden_dims): """Factory function for DBNs.""" dbn_ids = [] nodes = [bimdp.nodes.SenderBiNode(node_id="sender")] for i_layer in range(n_layers): dbn_ids.append("dbn_%d" % (i_layer+1)) nodes.append(DBNLayerBiNode(node_id=dbn_ids[i_layer], hidden_dim=hidden_dims[i_layer])) nodes.append(DBNMasterBiNode(dbn_ids=dbn_ids, sender_id="sender", node_id="dbn_master")) return bimdp.BiFlow(nodes)
def __add__(self, other): """Adding binodes returns a BiFlow. If a normal Node or Flow is added to a BiNode then a BiFlow is returned. Note that if a flow is added then a deep copy is used (deep copies of the nodes are used). """ # unfortunately the inline imports are required to avoid # a cyclic import (unless one adds a helper function somewhere else) if isinstance(other, mdp.Node): import bimdp return bimdp.BiFlow([self, other]) elif isinstance(other, mdp.Flow): flow_copy = other.copy() import bimdp biflow = bimdp.BiFlow([self.copy()] + flow_copy.flow) return biflow else: # can delegate old cases return super(BiNode, self).__add__(other)
def test_network_gradient(self): """Test gradient for a small SFA network.""" sfa_node = bimdp.nodes.SFABiNode(input_dim=4 * 4, output_dim=5) switchboard = bimdp.hinet.Rectangular2dBiSwitchboard( in_channels_xy=8, field_channels_xy=4, field_spacing_xy=2) flownode = bimdp.hinet.BiFlowNode(bimdp.BiFlow([sfa_node])) sfa_layer = bimdp.hinet.CloneBiLayer(flownode, switchboard.output_channels) flow = bimdp.BiFlow([switchboard, sfa_layer]) train_gen = [ numx_rand.random((10, switchboard.input_dim)) for _ in range(3) ] flow.train([None, train_gen]) # now can test the gradient mdp.activate_extension("gradient") try: x = numx_rand.random((3, switchboard.input_dim)) result = flow(x, {"method": "gradient"}) grad = result[1]["grad"] assert grad.shape == (3, sfa_layer.output_dim, switchboard.input_dim) finally: mdp.deactivate_extension("gradient")
err_grad_norm = np.sqrt(np.sum(error_grad**2, axis=1)) unit_error_grad = error_grad / err_grad_norm[:,np.newaxis] # x_{n+1} = x_n - f(x_n) / f'(x_n) x = x - (error / err_grad_norm)[:,np.newaxis] * unit_error_grad y, grad, x, _ = yield x, msg.copy(), self.sender_id raise StopIteration(x, None, "exit") n_iterations = 3 show_inspection = True sfa_node = bimdp.nodes.SFA2BiNode(input_dim=4*4, output_dim=5) switchboard = bimdp.hinet.Rectangular2dBiSwitchboard( in_channels_xy=8, field_channels_xy=4, field_spacing_xy=2) flownode = bimdp.hinet.BiFlowNode(bimdp.BiFlow([sfa_node])) sfa_layer = bimdp.hinet.CloneBiLayer(flownode, switchboard.output_channels) flow = bimdp.BiFlow([switchboard, sfa_layer]) 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)
# -*- coding: utf-8 -*- # Generated by codesnippet sphinx extension on 2020-11-22 import mdp import numpy as np np.random.seed(0) import bimdp pca_node = bimdp.nodes.PCABiNode(node_id="pca") biflow = bimdp.BiFlow([pca_node]) biflow["pca"] # Expected: ## PCABiNode(input_dim=None, output_dim=None, dtype=None, node_id="pca") samples = np.random.random((100, 10)) labels = np.arange(100) biflow = bimdp.BiFlow([mdp.nodes.PCANode(), bimdp.nodes.FDABiNode()]) biflow.train([[samples], [samples]], msg_iterables=[None, [{ "labels": labels }]]) biflow = bimdp.nodes.PCABiNode(output_dim=10) + bimdp.nodes.SFABiNode() x = np.random.random((100, 20)) biflow.train(x) y, msg = biflow.execute(x) msg # Expected: ## {} y, msg = biflow.execute(x, msg_iterable={"test": 1}) msg
import mdp import bimdp import mnistdigits chunk_size = 2000 verbose = True pca_dim = 35 fda1_dim = 9 layer1_switchboard = mdp.hinet.Rectangular2dSwitchboard(in_channels_xy=28, field_channels_xy=14, field_spacing_xy=14) layer1_node = bimdp.hinet.BiFlowNode( bimdp.BiFlow([ mdp.nodes.PCANode(input_dim=14**2, output_dim=pca_dim), mdp.nodes.QuadraticExpansionNode(), bimdp.nodes.FDABiNode(output_dim=fda1_dim) ])) biflow = bimdp.parallel.ParallelBiFlow( [ layer1_switchboard, bimdp.hinet.CloneBiLayer(layer1_node, n_nodes=4), # mdp.nodes.PCANode(output_dim=pca_dim), mdp.nodes.QuadraticExpansionNode(), bimdp.nodes.FDABiNode(output_dim=(mnistdigits.N_IDS)), bimdp.nodes.GaussianBiClassifier() ], verbose=verbose) ## training and execution train_data, train_ids = mnistdigits.get_data("train",