UserPreferences

PyroRAAMExample


  1 
  2 
  3 
  4 
  5 
  6 
  7 
  8 
  9 
 10 
 11 
 12 
 13 
 14 
 15 
 16 
 17 
 18 
 19 
 20 
 21 
 22 
 23 
 24 
 25 
 26 
 27 
 28 
 29 
 30 
 31 
 32 
 33 
 34 
 35 
 36 
 37 
 38 
 39 
 40 
 41 
 42 
 43 
 44 
 45 
 46 
 47 
 48 
 49 
 50 
 51 
 52 
 53 
 54 
 55 
 56 
 57 
 58 
 59 
 60 
 61 
 62 
 63 
 64 
 65 
 66 
 67 
 68 

# % python -i raam.py
# ... runs for a bit
# ... look at results, get hidden layer acts at end of sentence
# >>> raam.setLayerVerification(0)
# >>> retval = raam.propagateFrom("hidden", hidden=[0.89, 0.91, 0.03, 0.76, 0.97, 0.99, 0.11, 0.93])
# >>> retval["output"]
# [0.00019695890003820294, 0.99139862359118414, 0.00035552614835753892, 0.021935466446129315]          MARY
# >>> hid = retval["outcontext"]
# >>> retval = raam.propagateFrom("hidden", hidden=hid)
# >>> retval["output"]
# [0.038566659128093157, 0.013443895767298429, 0.90200538449592227, 0.023225075823005009]              LIKES
# >>> hid = retval["outcontext"]
# >>> retval = raam.propagateFrom("hidden", hidden=hid)
# >>> retval["output"]
# [0.015620707998835377, 0.0014200232163398988, 0.027158337904633942, 0.96938726876497006]             JOHN
# >>> raam.setLayerVerification(1)

# An example showing memory in a sRAAM

from pyrobot.brain.conx import *

# Create network:

raam = SRN()
raam.setSequenceType("random-segmented")
raam.setPatterns({"john"  : [0, 0, 0, 1],
                  "likes" : [0, 0, 1, 0],
                  "mary"  : [0, 1, 0, 0],
                  "is" : [1, 0, 0, 0],
                  })

size = len(raam.getPattern("john"))
raam.addSRNLayers(size, size * 2, size)
raam.add( Layer("outcontext", size * 2) )
raam.connect("hidden", "outcontext")

raam.associate('input', 'output')
raam.associate('context', 'outcontext')

raam.setInputs([ [ "john", "likes", "mary" ],
                 [ "mary", "likes", "john" ],
                 [ "john", "is", "john" ],
                 [ "mary", "is", "mary" ],
               ])

# Network learning parameters:

raam.setLearnDuringSequence(1)
raam.setReportRate(10)
raam.setEpsilon(0.1)
raam.setMomentum(0.0)
raam.setBatch(0)

# Ending criteria:
raam.setTolerance(0.4)
raam.setStopPercent(1.0)
raam.setResetEpoch(5000)
raam.setResetLimit(0)

# Train:
raam.train()

# Test:
raam.setLearning(0)
raam.setInteractive(1)
raam.sweep()
raam.saveWeightsToFile("raam.wts")