Beispiel #1
0
    def do_sum(self):
        if self.input is not None and self.weight is not None:
            self.set_property(
                "value",
                self.get_property("input") * self.get_property("weight"))

            self.emit("fire")

            if globals.runInDebug:
                print colored ("Synapse from Neuron %d at Layer %d fires to Neuron %d at Layer %d." % \
                        (self.source.Id, self.source.LayerId, self.target.Id, self.target.LayerId), "green")
Beispiel #2
0
 def do_sum (self):
     if self.input is not None and self.weight is not None:
         self.set_property (
             "value", 
             self.get_property ("input") * self.get_property ("weight")
         )
         
         self.emit ("fire")
         
         if globals.runInDebug:
             print colored ("Synapse from Neuron %d at Layer %d fires to Neuron %d at Layer %d." % \
                     (self.source.Id, self.source.LayerId, self.target.Id, self.target.LayerId), "green")
Beispiel #3
0
def _simple_dump_init (Object, Name=None, FullExposure=False):
    """
    
    """
    header = ""
    what_type = `type (Object)`[8:]
    what_type = what_type[:len(what_type)-2]
    introspect= dir (Object)
    
    properties = dict ()
    inherited_properties = dict ()
    methods    = dict ()
    
    for m in introspect:
        if not FullExposure:
            if m.startswith ("__") and m.endswith ("__"):
                continue
        
        target = getattr (Object, m)
        if callable (target):
            methods [m] = str (target).__doc__
        else:
            t = `type (target)`[7:].strip("'>< ") 
            #t = t[:len(t)-2]
            properties[m] = {
                "type" : t,
                "value": target
            }
    
    # Define the header
    if Name is not None:
        header = "Dumping instance %s of class %s" % (colored(Name, fg="bold"), colored(what_type, fg="underline"))
    else:
        header = "Dumping instance of class %s" % (colored(what_type, fg="underline"))
    
    print header
    
    data = {
        "method": methods,
        "properties": properties
    }
    
    return data
Beispiel #4
0
 def do_sum (self):
     if self.input is not None and self.weight is not None:
         self.set_property (
             "value", 
             self.get_property ("input") * self.get_property ("weight")
         )
         
         if globals.runInDebug:
             source = self.lockOwner
             target = None
             
             if source is self.startPoint:
                 target = self.endPoint
             else:
                 target = self.startPoint
             
             print colored ("BidiSynapse sums and fires\n\tFROM %s\n\tTO   %s", fg="green") % (source, target)
         
         self.emit ("bifire") 
Beispiel #5
0
    def do_sum(self):
        if self.input is not None and self.weight is not None:
            self.set_property(
                "value",
                self.get_property("input") * self.get_property("weight"))

            if globals.runInDebug:
                source = self.lockOwner
                target = None

                if source is self.startPoint:
                    target = self.endPoint
                else:
                    target = self.startPoint

                print colored(
                    "BidiSynapse sums and fires\n\tFROM %s\n\tTO   %s",
                    fg="green") % (source, target)

            self.emit("bifire")
Beispiel #6
0
def _simple_dump_init(Object, Name=None, FullExposure=False):
    """
    
    """
    header = ""
    what_type = ` type(Object) ` [8:]
    what_type = what_type[:len(what_type) - 2]
    introspect = dir(Object)

    properties = dict()
    inherited_properties = dict()
    methods = dict()

    for m in introspect:
        if not FullExposure:
            if m.startswith("__") and m.endswith("__"):
                continue

        target = getattr(Object, m)
        if callable(target):
            methods[m] = str(target).__doc__
        else:
            t = ` type(target) ` [7:].strip("'>< ")
            #t = t[:len(t)-2]
            properties[m] = {"type": t, "value": target}

    # Define the header
    if Name is not None:
        header = "Dumping instance %s of class %s" % (colored(
            Name, fg="bold"), colored(what_type, fg="underline"))
    else:
        header = "Dumping instance of class %s" % (colored(what_type,
                                                           fg="underline"))

    print header

    data = {"method": methods, "properties": properties}

    return data
Beispiel #7
0
 nn.createNet ()
 
 inputs = [ 1.0 ]
 weights= [-0.80,  1.0,  0.25, \
            0.13, -0.47, 0.64, 0.99, 0.20, \
            -1.0,  0.72 ]
 
 
 print "Neural Network Settings:"
 print "     Input: 1.0"
 print "   Weights: -0.80, 1.0, 0.25, 0.13, -0.47, 0.64, 0.99, 0.20, -1.0, 0.72\n"
 
 raw_input("\nPress any key for the first run...") 
 
 nn.update (inputs, weights)
 print "Neural network output:", colored ("%r", fg="yellow") % nn.output ()[0]
 print
 print
 
 print "We keep the the starting weights, but we input the value: 0.5"
 raw_input("\nPress any key for the second run...")
 nn.update ( inputs=[0.5] )
 print "Neural network output:", colored ("%r", fg="yellow") % nn.output ()[0]
 print
 print
 
 print "We keep the the starting weights, but we input the value: -0.25"
 raw_input("\nPress any key for the second run...")
 nn.update ( inputs=[-0.25] )
 print "Neural network output:", colored ("%r", fg="yellow") % nn.output ()[0]
 print
Beispiel #8
0
    second_hidden = NeuronLayer(Name="Hidden Layer #2",
                                Neurons=2,
                                AddBias=True)
    second_hidden.createLayer()
    output_layer = NeuronLayer(Name="Output Layer", Neurons=1, AddBias=True)
    output_layer.createLayer()

    print "*" * 100
    input_layer.connectTo(first_hidden)
    print "*" * 50
    first_hidden.connectTo(second_hidden)
    print "*" * 50
    second_hidden.connectTo(output_layer)
    print "*" * 50

    input_layer.putInputs((1.0, 2.0, 3.0))
    input_layer.putWeights((0.3, 0.4, -0.11, -0.8, -0.25, 0.25))

    first_hidden.putWeights((1.0, 0.8, 0.7, 0.1))
    first_hidden.putBiasWeights((0.5, -0.5))
    first_hidden.biasLayer.putBiasInputs()

    second_hidden.putWeights((0.1, 0.2))
    second_hidden.putBiasWeights((0.5, -0.5))
    second_hidden.biasLayer.putBiasInputs()

    output_layer.putBiasWeights((0.5, ))
    output_layer.biasLayer.putBiasInputs()

    print colored("Output: %r", fg="yellow") % output_layer[0].getValue()
Beispiel #9
0
def dump_properties(Object,
                    watchProps=None,
                    Name=None,
                    FullExposure=False,
                    showType=False,
                    showVal=True,
                    showTitle=False):
    """
    
    """
    data = _simple_dump_init(Object, Name, FullExposure)

    if watchProps is not None:
        title = "Watching Properties"
        props = dict()

        for p in watchProps:
            if p in data['properties']:
                props[p] = data['properties'][p]
    else:
        title = "Properties"
        props = data['properties']

    total = len(props)

    if total > 0:
        if showTitle: print "%s (%d):\n│" % (title, total)

        # Make the output pretty :-)
        spacing_name = 0
        spacing_type = 0
        for p in props.iterkeys():
            prop_name_len = 0

            if FullExposure:
                prop_name_len = len(p)
            else:
                if not p.startswith("__") and not p.endswith("__"):
                    prop_name_len = len(p)

            if prop_name_len > spacing_name: spacing_name = prop_name_len

            prop_type_len = len(props[p]['type'])
            if prop_type_len > spacing_type: spacing_type = prop_type_len

        # Print the properties order by name.
        counter = 0
        bgcolor = None
        s = sorted(props.iterkeys())
        for p in s:

            prop_name = p.ljust(spacing_name)
            prop_type = props[p]['type'].ljust(spacing_type)
            prop_val = props[p]['value']

            if counter < total - 1:
                symbol = "├─"
            else:
                symbol = "└─"
            """
            if p == "__gdoc__" or p == "__doc__":
                print "`--", colored(prop_name, fg="bold"), ":", prop_type, ":", prop_val                    
                continue                    
            """

            print symbol, colored(prop_name, fg="bold"),

            if showType: print ":", prop_type,
            if showVal:
                if type (prop_val) is types.DictionaryType or \
                    type (prop_val) is types.ListType:
                    print ":",
                    pprint.pprint(prop_val)
                else:
                    print ":", colored_value(prop_val)
            if not showType and not showVal: print

            counter += 1

    print
Beispiel #10
0
    
    This is the same as the "example1.py" in the "neurons" folder.
"""


# batteries
import sys

sys.path.append("../../src/")
# spiral framework
from spiral.helper import colored
from spiral.nn import globals
from spiral.nn.impl import Feedforward


if __name__ == "__main__":
    globals.runInDebug = True

    settings = {"Inputs": 3, "Outputs": 1, "Layers": 2, "NeuronsPerLayer": 2}

    nn = Feedforward(**settings)
    nn.createNet()
    nn.createDefaultSynapses()

    inputs = [1.0, 2.0, 3.0]
    weights = [0.3, 0.4, -0.1, -0.8, -0.25, 0.25, 1.0, 0.8, 0.7, 0.1, 0.1, 0.2]

    nn.update(inputs, weights)

    print colored("Neural Network output: %r", fg="yellow") % nn.output(0)
Beispiel #11
0
    nn = MyNetwork(**settings)
    nn.createNet()

    inputs = [1.0]
    weights= [-0.80,  1.0,  0.25, \
               0.13, -0.47, 0.64, 0.99, 0.20, \
               -1.0,  0.72 ]

    print "Neural Network Settings:"
    print "     Input: 1.0"
    print "   Weights: -0.80, 1.0, 0.25, 0.13, -0.47, 0.64, 0.99, 0.20, -1.0, 0.72\n"

    raw_input("\nPress any key for the first run...")

    nn.update(inputs, weights)
    print "Neural network output:", colored("%r", fg="yellow") % nn.output()[0]
    print
    print

    print "We keep the the starting weights, but we input the value: 0.5"
    raw_input("\nPress any key for the second run...")
    nn.update(inputs=[0.5])
    print "Neural network output:", colored("%r", fg="yellow") % nn.output()[0]
    print
    print

    print "We keep the the starting weights, but we input the value: -0.25"
    raw_input("\nPress any key for the second run...")
    nn.update(inputs=[-0.25])
    print "Neural network output:", colored("%r", fg="yellow") % nn.output()[0]
    print
Beispiel #12
0
def dump_properties (Object, watchProps=None, Name=None, FullExposure=False, showType=False, showVal=True, showTitle=False):
    """
    
    """
    data = _simple_dump_init (Object, Name, FullExposure)
    
    if watchProps is not None:
        title = "Watching Properties"
        props = dict ()
        
        for p in watchProps:
            if p in data['properties']:
                props [p] = data['properties'][p]
    else:
        title = "Properties"
        props = data['properties']
    
    total = len (props)
    
    if total > 0:
        if showTitle: print "%s (%d):\n│" % (title, total)
        
        # Make the output pretty :-)
        spacing_name = 0
        spacing_type = 0
        for p in props.iterkeys():
            prop_name_len = 0
            
            if FullExposure:
                prop_name_len = len (p)
            else:
                if not p.startswith ("__") and not p.endswith ("__"):
                    prop_name_len = len (p)
            
            if prop_name_len > spacing_name: spacing_name = prop_name_len
            
            prop_type_len = len (props[p]['type'])
            if prop_type_len > spacing_type: spacing_type = prop_type_len
        
        # Print the properties order by name.
        counter = 0
        bgcolor = None
        s = sorted(props.iterkeys())
        for p in s:
            
            prop_name = p.ljust (spacing_name)
            prop_type = props[p]['type'].ljust (spacing_type)
            prop_val  = props[p]['value']
            
            if counter < total-1:
                symbol = "├─"
            else:
                symbol = "└─"
            
            """
            if p == "__gdoc__" or p == "__doc__":
                print "`--", colored(prop_name, fg="bold"), ":", prop_type, ":", prop_val                    
                continue                    
            """
            
            print symbol, colored(prop_name, fg="bold"),
            
            if showType: print ":", prop_type,
            if showVal:
                if type (prop_val) is types.DictionaryType or \
                    type (prop_val) is types.ListType:
                    print ":",
                    pprint.pprint (prop_val)
                else:
                    print ":", colored_value (prop_val)
            if not showType and not showVal: print
            
            counter += 1
    
    print
Beispiel #13
0
if __name__ == "__main__":
    globals.runInDebug = True
       
    n1 = Neuron (Id=1, LayerId=0, Name="Input")
    n1.setAutoNotify (True)
    
    n2 = Neuron (Id=2, LayerId=1)
    n3 = Neuron (Id=3, LayerId=1)
    n4 = Neuron (Id=4, LayerId=2, Name="Output")
    
    # Create the synapses
    n1.createSynapse (1, n2)
    n1.createSynapse (1, n3)
    n2.createSynapse (2, n4)
    n3.createSynapse (2, n4)
    
    print "*" * 40
    
    # Put the weights
    n1.putWeights ((0.5, 0.25))
    n2.putWeights ((-.75,))
    n3.putWeights ((1.0,))

    # Insert the inputs
    n1.putInput (1.0)


    print colored ("Output value: %r", fg="yellow") % n4.getValue ()
    
Beispiel #14
0
     "NeuronsPerLayer": 2,
     "AddBias": True,
     "LearningRate": 0.9
 }
 
 bp = Backpropagation (**settings)
 bp.createNet ()
 bp.createDefaultSynapses ()
 
 random_weights = list ()
 for i in range (bp.NumberOfWeights):
     random_weights.append (SimpleMath.randomClamped())
 
 bp.putWeights (random_weights)
 
 print colored ("Training network for 1000 iterations ...", fg="yellow")
 print colored ("*" * 80, fg="yellow")
 for i in range (1):
     bp.train ( [0, 0], 0)
     bp.train ( [0, 1], 1)
     bp.train ( [1, 0], 1)
     bp.train ( [1, 1], 0)
 
 print
 print colored ("Testing ...", fg="yellow")
 print colored ("*" * 80, fg="yellow")
 print bp.run ([0, 0])
 print bp.run ([0, 1])
 print bp.run ([1, 0])
 print bp.run ([1, 1])
 
Beispiel #15
0
import sys
sys.path.append("../../src/")
# spiral framework
from spiral.helper import colored
from spiral.nn import globals
from spiral.nn.impl import Feedforward

if __name__ == "__main__":
    globals.runInDebug = False

    settings = {
        "Inputs": 3,
        "Outputs": 1,
        "Layers": 2,
        "NeuronsPerLayer": 2,
        "AddBias": True
    }

    nn = Feedforward(**settings)
    nn.createNet()
    nn.createDefaultSynapses()

    inputs = [1.0, 2.0, 3.0]
    weights= [ 0.3, 0.4, -0.1, -0.8, -0.25, 0.25, \
               1.0, 0.8,  0.7,  0.1, \
               0.1, 0.2, 1.0, 0.3, 0.24, 0.324, 0.12 ]

    nn.update(inputs, weights)
    print "Neural Network epoch completed in: %r seconds" % nn.timeUpdate()
    print colored("Neural Network output: %r", fg="yellow") % nn.output(0)
Beispiel #16
0
    n2.createSynapse(1, n4)
    n2.createSynapse(1, n5)
    n3.createSynapse(1, n4)
    n3.createSynapse(1, n5)
    # Create the synapses for the first hidden layer
    n4.createSynapse(2, n6)
    n4.createSynapse(2, n7)
    n5.createSynapse(2, n6)
    n5.createSynapse(2, n7)
    # Create the synapses for the second hidden layer
    n6.createSynapse(3, n8)
    n7.createSynapse(3, n8)

    # Put the weights
    n1.putWeights((0.3, 0.4))
    n2.putWeights((-0.1, -0.8))
    n3.putWeights((-0.25, 0.25))

    n4.putWeights((1.0, 0.8))
    n5.putWeights((0.7, 0.1))

    n6.putWeights((0.1, ))
    n7.putWeights((0.2, ))

    # Insert the inputs
    n1.putInput(1.0)
    n2.putInput(2.0)
    n3.putInput(3.0)

    print colored("Output value: %r", fg="yellow") % n8.getValue()
Beispiel #17
0
    first_hidden.createLayer ()
    second_hidden= NeuronLayer (Name="Hidden Layer #2", Neurons=2, AddBias=True)
    second_hidden.createLayer ()
    output_layer = NeuronLayer (Name="Output Layer"   , Neurons=1, AddBias=True)
    output_layer.createLayer ()
    
    print "*" * 100
    input_layer.connectTo (first_hidden)
    print "*" * 50
    first_hidden.connectTo (second_hidden)
    print "*" * 50
    second_hidden.connectTo (output_layer)
    print "*" * 50
    
    
    input_layer.putInputs   ( (1.0, 2.0, 3.0) )
    input_layer.putWeights  ( (0.3, 0.4, -0.11, -0.8, -0.25, 0.25) )
    
    first_hidden.putWeights ( (1.0, 0.8, 0.7, 0.1) )
    first_hidden.putBiasWeights ( (0.5, -0.5) )
    first_hidden.biasLayer.putBiasInputs ()
    
    second_hidden.putWeights( (0.1, 0.2) )
    second_hidden.putBiasWeights ( (0.5, -0.5) )
    second_hidden.biasLayer.putBiasInputs ()
    
    output_layer.putBiasWeights ( (0.5,) )
    output_layer.biasLayer.putBiasInputs ()

    print colored("Output: %r", fg="yellow") % output_layer [0].getValue ()