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

File LCA2.py, 2.4 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-.02) |

7 | elif x<-0.1: |

8 | return (x+.02) |

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('clips.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('dictionary.txt') |

61 | |

62 | num_dict = 128 |

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

74 | PHI_pre.append(dictpre) |

75 | f.close() |

76 | |

77 | |

78 | class MyInput(nef.SimpleNode): |

79 | def origin_value(self): |

80 | t=int(self.t_start/.2) |

81 | return clippre[t] |

82 | |

83 | |

84 | |

85 | dict = array(PHI_pre) |

86 | numNodes = len(dict) |

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

88 | |

89 | recur =multmin(dict) |

90 | |

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

92 | net.add_to(world) |

93 | |

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

95 | myinput=MyInput('input') |

96 | net.add(myinput) |

97 | neuron=net.make_array('neurons',100,numNodes,intercept=(0,1)) |

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

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

100 | |

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

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

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

104 | net.connect(neuron,outs,transform=dict.T,func=sthn) |

105 | net.connect(neuron,neuron_value,func=sthn) |

106 | |

107 | |

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