# 2011/results/att11/spikevision: LCAsal.py

File LCAsal.py, 3.8 KB (added by sshapero, 7 years ago) |
---|

Line | |
---|---|

1 | import nef |

2 | from numeric import * |

3 | |

4 | def 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 | |

12 | def sthn(x): |

13 | return [sth(y) for y in x] |

14 | |

15 | def absn(x): |

16 | return [abs(sth(y)) for y in x] |

17 | |

18 | def rect(x): |

19 | if x>0: |

20 | return x |

21 | else: |

22 | return 0 |

23 | |

24 | def rectn(x): |

25 | return [rect(y) for y in x] |

26 | |

27 | def zero(m,n): |

28 | # Create zero matrix |

29 | new_matrix = [[0 for row in range(n)] for col in range(m)] |

30 | return new_matrix |

31 | |

32 | |

33 | def multmin(matrix1): |

34 | # Matrix multiplication |

35 | new_matrix = zero(len(matrix1),len(matrix1)) |

36 | for i in range(len(matrix1)): |

37 | for j in range(len(matrix1)): |

38 | if(i != j): |

39 | for k in range(len(matrix1[0])): |

40 | new_matrix[i][j] -= matrix1[i][k]*matrix1[j][k] |

41 | |

42 | return new_matrix |

43 | |

44 | |

45 | def transpose(matrix1): |

46 | new_matrix = zero(len(matrix1[0]),len(matrix1)) |

47 | for i in range(len(matrix1)): |

48 | for j in range(len(matrix1[0])): |

49 | new_matrix[j][i] = matrix1[i][j] |

50 | |

51 | return new_matrix |

52 | |

53 | f = open('smoneclip.txt') |

54 | |

55 | num_images = 1 |

56 | clippre = [] |

57 | for img in range(num_images): |

58 | line = f.readline() |

59 | imgpre=[] |

60 | first = 0 |

61 | for char in range(len(line)): |

62 | if (line[char] == ' '): |

63 | last = char |

64 | imgpre.append(round(float(line[first:last]),4)) |

65 | first = char+1 |

66 | |

67 | clippre.append(imgpre) |

68 | |

69 | f.close() |

70 | |

71 | |

72 | f = open('smdictionary.txt') |

73 | |

74 | num_dict = 128*8 |

75 | PHI_pre = [] |

76 | for img in range(num_dict): |

77 | line = f.readline() |

78 | dictpre=[] |

79 | first = 0 |

80 | for char in range(len(line)): |

81 | if (line[char] == ' '): |

82 | last = char |

83 | dictpre.append(round(float(line[first:last]),4)) |

84 | first = char+1 |

85 | #print img |

86 | PHI_pre.append(dictpre) |

87 | f.close() |

88 | |

89 | f = open('smrecur.txt') |

90 | |

91 | recur_pre = [] |

92 | for img in range(num_dict): |

93 | line = f.readline() |

94 | dictpre=[] |

95 | first = 0 |

96 | for char in range(len(line)): |

97 | if (line[char] == ' '): |

98 | last = char |

99 | dictpre.append(round(float(line[first:last]),4)) |

100 | first = char+1 |

101 | |

102 | recur_pre.append(dictpre) |

103 | f.close() |

104 | |

105 | f = open('salrecur.txt') |

106 | |

107 | salrec_pre = [] |

108 | for img in range(num_dict): |

109 | line = f.readline() |

110 | dictpre=[] |

111 | first = 0 |

112 | for char in range(len(line)): |

113 | if (line[char] == ' '): |

114 | last = char |

115 | dictpre.append(round(float(line[first:last]),4)) |

116 | first = char+1 |

117 | |

118 | salrec_pre.append(dictpre) |

119 | |

120 | f.close() |

121 | |

122 | |

123 | class MyInput(nef.SimpleNode): |

124 | def origin_value(self): |

125 | t=int(self.t_start/.1) |

126 | return clippre[t] |

127 | |

128 | |

129 | |

130 | dict = array(PHI_pre) |

131 | absdict_pre = [[abs(x) for x in y] for y in PHI_pre] |

132 | absdict = array(absdict_pre) |

133 | numNodes = len(dict) |

134 | numInputs = len(dict[0]) |

135 | |

136 | recur = array(recur_pre) |

137 | salrec = array(salrec_pre) |

138 | |

139 | net=nef.Network('LCA',quick=True) |

140 | #net.add_to(world) |

141 | |

142 | input=net.make_input('input',clippre[0]) |

143 | #myinput=MyInput('input') |

144 | #net.add(myinput) |

145 | neuron=net.make_array('neurons',30,numNodes,intercept=(0,1)) |

146 | outs=net.make('decoders',1,numInputs,mode='direct') |

147 | neuron_value=net.make('neuron_value',1,numNodes,mode='direct') |

148 | salience=net.make_array('salience',20,numNodes,intercept=(0,1)) |

149 | sal_decode=net.make('sal_decoder',1,numInputs,mode='direct') |

150 | |

151 | net.connect(input,neuron,transform=dict) |

152 | #net.connect(myinput.getOrigin('value'),neuron,transform=dict) |

153 | net.connect(neuron,neuron,transform=recur,func=sthn) |

154 | net.connect(neuron,outs,transform=dict.T,func=sthn,pstc=.001) |

155 | net.connect(neuron,neuron_value,func=sthn,pstc=.001) |

156 | net.connect(neuron,salience,func=absn,pstc=.005) |

157 | net.connect(salience,salience,transform=salrec,func=rectn,pstc=.001) |

158 | net.connect(salience,sal_decode,transform=absdict.T,func=rectn,pstc=.001) |

159 | |

160 | |

161 | net.view(play=.2) |

162 | |

163 | |

164 | #sim=net.network.simulator |

165 | #sim.run(0,1,0.001) |

166 | |

167 | #print neuron_value.getOrigin('X').getValues().getValues() |

168 | #print outs.getOrigin('X').getValues().getValues() |