2011/results/att11/spikevision: LCA2.py

File LCA2.py, 2.4 KB (added by sshapero, 7 years ago)

Code for sparse representation of 8x8 inputs

Line 
1import nef
2from numeric import *
3
4def sth(x):
5        if x>0.1:
6                return (x-.02)
7        elif x<-0.1:
8                return (x+.02)
9        else:
10                return 0
11
12def sthn(x):
13        return [sth(y) for y in x]
14
15def zero(m,n):
16    # Create zero matrix
17    new_matrix = [[0 for row in range(n)] for col in range(m)]
18    return new_matrix
19
20
21def multmin(matrix1):
22        # Matrix multiplication
23        new_matrix = zero(len(matrix1),len(matrix1))
24        for i in range(len(matrix1)):
25                for j in range(len(matrix1)):
26                        if(i != j):
27                                for k in range(len(matrix1[0])):
28                                        new_matrix[i][j] -= matrix1[i][k]*matrix1[j][k]
29
30        return new_matrix
31
32
33def transpose(matrix1):
34        new_matrix = zero(len(matrix1[0]),len(matrix1))
35        for i in range(len(matrix1)):
36                for j in range(len(matrix1[0])):
37                        new_matrix[j][i] = matrix1[i][j]
38
39        return new_matrix
40
41f = open('clips.txt')
42
43num_images = 200
44clippre = []
45for img in range(num_images):
46        line = f.readline()
47        imgpre=[]
48        first = 0
49        for char in range(len(line)):
50                if (line[char] == ' '):
51                        last = char
52                        imgpre.append(round(float(line[first:last]),4))
53                        first = char+1
54               
55        clippre.append(imgpre)
56               
57f.close()
58
59
60f = open('dictionary.txt')
61
62num_dict = 128
63PHI_pre = []
64for img in range(num_dict):
65        line = f.readline()
66        dictpre=[]
67        first = 0
68        for char in range(len(line)):
69                if (line[char] == ' '):
70                        last = char
71                        dictpre.append(round(float(line[first:last]),4))
72                        first = char+1
73               
74        PHI_pre.append(dictpre)
75f.close()
76
77
78class MyInput(nef.SimpleNode):
79        def origin_value(self):
80                t=int(self.t_start/.2)
81                return clippre[t]
82
83
84
85dict = array(PHI_pre)
86numNodes = len(dict)
87numInputs = len(dict[0])
88
89recur =multmin(dict)
90
91net=nef.Network('LCA',quick=True)
92net.add_to(world)
93
94#input=net.make_input('input',clippre[15])
95myinput=MyInput('input')
96net.add(myinput)
97neuron=net.make_array('neurons',100,numNodes,intercept=(0,1))
98outs=net.make('decoders',1,numInputs,mode='direct')
99neuron_value=net.make('neuron_value',1,numNodes,mode='direct')
100
101#net.connect(input,neuron,transform=dict)
102net.connect(myinput.getOrigin('value'),neuron,transform=dict)
103net.connect(neuron,neuron,transform=recur,func=sthn)
104net.connect(neuron,outs,transform=dict.T,func=sthn)
105net.connect(neuron,neuron_value,func=sthn)
106
107
108net.view(play=1)
109
110
111#sim=net.network.simulator
112#sim.run(0,1,0.001)
113
114#print neuron_value.getOrigin('X').getValues().getValues()
115#print outs.getOrigin('X').getValues().getValues()