1
2
3
4 """ Contains the class _NetNode_ which is used to represent nodes in neural nets
5
6 **Network Architecture:**
7
8 A tacit assumption in all of this stuff is that we're dealing with
9 feedforward networks.
10
11 The network itself is stored as a list of _NetNode_ objects. The list
12 is ordered in the sense that nodes in earlier/later layers than a
13 given node are guaranteed to come before/after that node in the list.
14 This way we can easily generate the values of each node by moving
15 sequentially through the list, we're guaranteed that every input for a
16 node has already been filled in.
17
18 Each node stores a list (_inputNodes_) of indices of its inputs in the
19 main node list.
20
21 """
22 import numpy
23 import ActFuncs
24
25
26
27
28
30 """ a node in a neural network
31
32 """
33 - def Eval(self,valVect):
34 """Given a set of inputs (valVect), returns the output of this node
35
36 **Arguments**
37
38 - valVect: a list of inputs
39
40 **Returns**
41
42 the result of running the values in valVect through this node
43
44 """
45 if len(self.inputNodes) != 0:
46
47 inputs = numpy.take(valVect,self.inputNodes)
48
49 inputs = self.weights * inputs
50
51 val = self.actFunc(sum(inputs))
52 else:
53 val = 1
54
55 valVect[self.nodeIndex] = val
56 return val
57
80
82 """ Sets the weight list
83
84 **Arguments**
85
86 - weights: a list of values which are to be used as weights
87
88 **Note**
89
90 If this _NetNode_ already has _inputNodes_ and _weights_ is a different length,
91 this will bomb out with an assertion.
92
93 """
94 self.weights = numpy.array(weights)
95 if self.inputNodes:
96 assert (len(self.weights) == len(self.inputNodes)),\
97 'lengths of weights and nodes do not match'
98
100 """ returns the weight list
101
102 """
103 return self.weights
104
107 """ Constructor
108
109 **Arguments**
110
111 - nodeIndex: the integer index of this node in _nodeList_
112
113 - nodeList: the list of other _NetNodes_ already in the network
114
115 - inputNodes: a list of this node's inputs
116
117 - weights: a list of this node's weights
118
119 - actFunc: the activation function to be used here. Must support the API
120 of _ActFuncs.ActFunc_.
121
122 - actFuncParms: a tuple of extra arguments to be passed to the activation function
123 constructor.
124
125 **Note**
126 There should be only one copy of _inputNodes_, every _NetNode_ just has a pointer
127 to it so that changes made at one node propagate automatically to the others.
128
129 """
130 if inputNodes and weights:
131 assert (len(weights) == len(inputNodes))
132 if weights:
133 self.weights = numpy.array(weights)
134 else:
135 self.weights = None
136 if inputNodes:
137 self.inputNodes = inputNodes[:]
138 else:
139 self.inputNodes = None
140
141 self.nodeIndex = nodeIndex
142
143 self.nodeList = nodeList
144
145 self.actFunc = actFunc(*actFuncParms)
146