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

File LCAbig.py, 2.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 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 | |

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

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

41 | f = open('smclips.txt') |

42 | |

43 | num_images = 200 |

44 | clippre = [] |

45 | for 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 | |

57 | f.close() |

58 | |

59 | |

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

61 | |

62 | num_dict = 128*8 |

63 | PHI_pre = [] |

64 | for 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) |

75 | f.close() |

76 | |

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

78 | |

79 | recur_pre = [] |

80 | for 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) |

91 | f.close() |

92 | |

93 | |

94 | class MyInput(nef.SimpleNode): |

95 | def origin_value(self): |

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

97 | return clippre[t] |

98 | |

99 | |

100 | |

101 | dict = array(PHI_pre) |

102 | numNodes = len(dict) |

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

104 | |

105 | recur = array(recur_pre) |

106 | |

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

108 | #net.add_to(world) |

109 | |

110 | #input=net.make_input('input',clippre[15]) |

111 | myinput=MyInput('input') |

112 | net.add(myinput) |

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

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

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

116 | |

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

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

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

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

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

122 | |

123 | |

124 | net.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() |