2011/results/att11/spikevision: LCAbig.py

File LCAbig.py, 2.8 KB (added by sshapero, 7 years ago)

Network for sparse representation of a 24x24 input.

Line 
1import nef
2from numeric import *
3
4def sth(x):
5        if x>0.1:
6                return (x-.05)
7        elif x<-0.1:
8                return (x+.05)
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('smclips.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('smdictionary.txt')
61
62num_dict = 128*8
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        #print img     
74        PHI_pre.append(dictpre)
75f.close()
76
77f = open('smrecur.txt')
78
79recur_pre = []
80for img in range(num_dict):
81        line = f.readline()
82        dictpre=[]
83        first = 0
84        for char in range(len(line)):
85                if (line[char] == ' '):
86                        last = char
87                        dictpre.append(round(float(line[first:last]),4))
88                        first = char+1
89               
90        recur_pre.append(dictpre)       
91f.close()
92
93
94class MyInput(nef.SimpleNode):
95        def origin_value(self):
96                t=int(self.t_start/.1)
97                return clippre[t]
98
99
100
101dict = array(PHI_pre)
102numNodes = len(dict)
103numInputs = len(dict[0])
104
105recur = array(recur_pre)
106
107net=nef.Network('LCA',quick=True)
108#net.add_to(world)
109
110#input=net.make_input('input',clippre[15])
111myinput=MyInput('input')
112net.add(myinput)
113neuron=net.make_array('neurons',30,numNodes,intercept=(0,1))
114outs=net.make('decoders',1,numInputs,mode='direct')
115neuron_value=net.make('neuron_value',1,numNodes,mode='direct')
116
117#net.connect(input,neuron,transform=dict)
118net.connect(myinput.getOrigin('value'),neuron,transform=dict)
119net.connect(neuron,neuron,transform=recur,func=sthn)
120net.connect(neuron,outs,transform=dict.T,func=sthn,pstc=.001)
121net.connect(neuron,neuron_value,func=sthn,pstc=.001)
122
123
124net.view(play=.2)
125
126
127#sim=net.network.simulator
128#sim.run(0,1,0.001)
129
130#print neuron_value.getOrigin('X').getValues().getValues()
131#print outs.getOrigin('X').getValues().getValues()