Code snippets for page FlowsΒΆ

Download flows.py. Browse the code snippet index.

# -*- coding: utf-8 -*-
# Generated by codesnippet sphinx extension on 2020-12-16

import mdp
import numpy as np
np.random.seed(0)
inp = np.random.random((1000, 20))

inp = (inp - np.mean(inp, 0))/np.std(inp, axis=0, ddof=0)

inp[:,5:] /= 10.0

x = mdp.utils.mult(inp,np.random.random((20, 20)))

inp_test = np.random.random((1000, 20))
inp_test = (inp_test - np.mean(inp_test, 0))/np.std(inp_test, 0)
inp_test[:,5:] /= 10.0
x_test = mdp.utils.mult(inp_test, np.random.random((20, 20)))

pca = mdp.nodes.PCANode(output_dim=5)
pca.train(x)
out1 = pca(x)

ica = mdp.nodes.CuBICANode()
ica.train(out1)
out2 = ica(out1)

out1_test = pca(x_test)
out2_test = ica(out1_test)
hitnode = mdp.nodes.HitParadeNode(3)
hitnode.train(out2_test)
maxima, indices = hitnode.get_maxima()

flow = mdp.Flow([mdp.nodes.PCANode(output_dim=5), mdp.nodes.CuBICANode()])

flow = mdp.nodes.PCANode(output_dim=5) + mdp.nodes.CuBICANode()

flow.train(x)

flow.append(mdp.nodes.HitParadeNode(3))

flow += mdp.nodes.HitParadeNode(3)

flow.train(x_test)
maxima, indices = flow[2].get_maxima()

out = flow.execute(x)
cov = np.amax(abs(mdp.utils.cov2(inp[:,:5], out)), axis=1)
print cov
# Expected:
## [ 0.9957042   0.98482351  0.99557617  0.99680391  0.99232424]

out = flow(x)

rec = flow.inverse(out)

cov = np.amax(abs(mdp.utils.cov2(x/np.std(x,axis=0),
                                 rec/np.std(rec,axis=0))))
print cov
# Expected:
## 0.999622205447

for node in flow:
    print repr(node)
# Expected:
## PCANode(input_dim=20, output_dim=5, dtype='float64')
## CuBICANode(input_dim=5, output_dim=5, dtype='float64')
## HitParadeNode(input_dim=5, output_dim=5, dtype='float64')
## HitParadeNode(input_dim=5, output_dim=5, dtype='float64')

len(flow)
# Expected:
## 4
print flow[::2]
# Expected:
## [PCANode, HitParadeNode]
nodetoberemoved = flow.pop(-1)
nodetoberemoved
# Expected:
## HitParadeNode(input_dim=5, output_dim=5, dtype='float64')
len(flow)
# Expected:
## 3

dummyflow = flow[1:].copy()
longflow = flow + dummyflow
len(longflow)
# Expected:
## 5

class BogusExceptNode(mdp.Node):
   def train(self,x):
       self.bogus_attr = 1
       raise Exception, "Bogus Exception"
   def execute(self,x):
       raise Exception, "Bogus Exception"
flow = mdp.Flow([BogusExceptNode()])

flow.set_crash_recovery(1)

flow.train(x)
# Expected:
## Traceback (most recent call last):
##   File "<stdin>", line 1, in ?
##   [...]
## mdp.linear_flows.FlowExceptionCR:
## ----------------------------------------
## ! Exception in node #0 (BogusExceptNode):
## Node Traceback:
## Traceback (most recent call last):
##   [...]
## Exception: Bogus Exception
## ----------------------------------------
## A crash dump is available on: "/tmp/MDPcrash_LmISO_.pic"