rdkit.ML.Neural.NetNode

Package rdkit :: Package ML :: Package Neural :: Module NetNode |
[hide private]
[frames] | no frames]

# Source Code for Module rdkit.ML.Neural.NetNode

      1  #
      2  #  Copyright (C) 2000-2008  greg Landrum
      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  # FIX: this class has not been updated to new-style classes
     26  # (RD Issue380) because that would break all of our legacy pickled
     27  # data. Until a solution is found for this breakage, an update is
     28  # impossible.
     29 -class NetNode:
     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        # grab our list of weighted inputs
     47        inputs = numpy.take(valVect,self.inputNodes)
     48        # weight them
     49        inputs = self.weights * inputs
     50        # run that through the activation function
     51        val = self.actFunc(sum(inputs))
     52      else:
     53        val = 1
     54      # put our value in the list and return it (just in case)
     55      valVect[self.nodeIndex] = val
     56      return val
     57
     58 -  def SetInputs(self,inputNodes):
     59      """ Sets the input list
     60
     61        **Arguments**
     62
     63          - inputNodes: a list of _NetNode_s which are to be used as inputs
     64
     65        **Note**
     66
     67          If this _NetNode_ already has weights set and _inputNodes_ is a different length,
     68          this will bomb out with an assertion.
     69
     70      """
     71      self.inputNodes = inputNodes[:]
     72      if self.weights:
     73        assert (len(self.weights) == len(self.inputNodes)),\
     74               'lengths of weights and nodes do not match'
     75 -  def GetInputs(self):
     76      """ returns the input list
     77
     78      """
     79      return self.inputNodes
     80
     81 -  def SetWeights(self,weights):
     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
     99 -  def GetWeights(self):
    100      """ returns the weight list
    101
    102      """
    103      return self.weights
    104
    105 -  def __init__(self,nodeIndex,nodeList,inputNodes=None,weights=None,
    106                 actFunc=ActFuncs.Sigmoid,actFuncParms=()):
    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      # there's only one of these, everybody has a pointer to it.
    143      self.nodeList = nodeList
    144
    145      self.actFunc = apply(actFunc,actFuncParms)
    146