Exemplo n.º 1
0
 def getUOG(self, chunkList):
     numOfItems = len(chunkList)
     chunk = HRR(data=numpy.zeros(self.N))
     sum = HRR(data=numpy.zeros(self.N))
     p = numOfItems  # initially, this will be set to index of ? when ? is found
     for i in range(0, numOfItems):
         # get the vector for the value i
         value = chunkList[i]
         # set p as the location of the placeholder ?
         if value == '?':
             p = i
         # if value starts with ! then negate the environment vector
         if value.startswith('!'):
             valVec = -1 * self.env[value[1:]]
         # otherwise use the environment vector as is
         else:
             valVec = self.env[value]
         # compute the chunk vector
         if i == 0:
             sum = valVec
         elif (i > 0) and (i < p):
             leftOperand = chunk + sum
             leftOperand = leftOperand.permute(self.left)
             chunk = chunk + leftOperand.convolve(valVec)
             sum = sum + valVec
         elif i == p:  # force all skip grams to include item p
             leftOperand = chunk + sum
             leftOperand = leftOperand.permute(self.left)
             chunk = leftOperand.convolve(valVec)
             sum = valVec
         else:  # i > p, i > 0
             leftOperand = chunk + sum
             leftOperand = leftOperand.permute(self.left)
             chunk = chunk + leftOperand.convolve(valVec)
     return chunk
Exemplo n.º 2
0
 def set(self, value, vector):
     try:  # assume vector is an HRR object
         newVec = vector.copy()
         newVec.normalize()
         self.env[value] = newVec
     except:  # assume vector is a list of numbers
         vector = [float(i) for i in vector]
         self.env[value] = HRR(data=vector)
         self.env[value].normalize()
     # check to see if it's in memory already, if not, define its memory as a vector of zeros
     if value not in self.mem:
         self.mem[value] = HRR(data=numpy.zeros(self.N))
Exemplo n.º 3
0
 def __init__(self,
              buffer,
              latency=0.05,
              threshold=0.1,
              maximum_time=10.0,
              finst_size=4,
              finst_time=3.0,
              N=512,
              verbose=False,
              max_gram_size=7):
     Memory.__init__(self, buffer)
     self._buffer = buffer
     self.N = N
     self.verbose = verbose
     self.environment = {'?': HRR(N=self.N)}
     self.placeholder = self.environment['?']
     self.cLambda = max_gram_size
     self.memory = {}
     self.memStr = {}
     self.slots = {')': numpy.random.permutation(self.N)}
     self.left = self.slots[')']
     self.error = False
     self.busy = False
     self.adaptors = []
     self.latency = latency
     self.threshold = threshold
     self.maximum_time = maximum_time
     self.partials = []
     self.finst = Finst(self, size=finst_size, time=finst_time)
     self.record_all_chunks = False
     self._request_count = 0
Exemplo n.º 4
0
 def __init__(self,
              buffer,
              latency=0.05,
              threshold=-4.6,
              maximum_time=100.0,
              finst_size=4,
              finst_time=3.0,
              N=512,
              verbose=False,
              forgetting=1.0,
              noise=0.0):
     Memory.__init__(self, buffer)
     self._buffer = buffer
     self.N = N
     self.verbose = verbose
     self.env = {'?': HRR(N=self.N)}
     self.placeholder = self.env['?']
     self.mem = {}
     self.slots = {')': numpy.random.permutation(self.N)}
     self.left = self.slots[')']
     self.error = False
     self.busy = False
     self.adaptors = []
     self.latency = latency
     self.threshold = self.logodds_to_cosine(threshold)
     self.maximum_time = maximum_time
     self.partials = []
     self.finst = Finst(self, size=finst_size, time=finst_time)
     self._request_count = 0
     self.inhibited = []  # list of inhibited values
     self.forgetting = forgetting
     self.noise = noise
     self.lastUpdate = 0.0
Exemplo n.º 5
0
 def addNoise(self):
     # weight by time difference
     diff = self.now() - self.lastUpdate
     for value in self.mem.keys():
         noiseVector = HRR(N=self.N)
         self.mem[value] = self.mem[value] + (self.noise * diff *
                                              noiseVector)
     self.lastUpdate = self.now()
Exemplo n.º 6
0
 def getUOGwithSlots(self, chunkList):
     numOfItems = len(chunkList)
     chunk = HRR(data=numpy.zeros(self.N))
     sum = HRR(data=numpy.zeros(self.N))
     #sumStr = ''
     #chunkStr = ''
     p = numOfItems  # initially, this will be set to index of ? when ? is found
     for i in range(0, numOfItems):
         # get the vector for the slot value pair at i
         slotvalue = chunkList[i]
         slot = slotvalue[0]
         value = slotvalue[1]
         # set p as the location of the placeholder ?
         if value == '?':
             p = i
         # if value starts with ! then negate the environment vector
         if value.startswith('!'):
             valVec = -1 * self.env[value[1:]]
         # otherwise use the environment vector as is
         else:
             valVec = self.env[value]
         # permute the environment vector by the slot
         valVec = valVec.permute(self.slots[slot])
         #slotvalueStr = slot+':'+value
         # compute the chunk vector
         if i == 0:
             sum = valVec
             #sumStr = slotvalueStr
         elif (i > 0) and (i < p):
             leftOperand = chunk + sum
             chunk = chunk + leftOperand.convolve(valVec)
             #chunkStr    = chunkStr + ' + ' + slotvalueStr + ' * (' +  chunkStr + ' + ' + sumStr + ')'
             sum = sum + valVec
             #sumStr      = sumStr + ' + ' + slotvalueStr
         elif i == p:  # force all skip grams to include item p
             leftOperand = chunk + sum
             chunk = leftOperand.convolve(valVec)
             #chunkStr    = slotvalueStr + ' * (' +  chunkStr + ' + ' + sumStr + ')'
             sum = valVec
             #sumStr      = slotvalueStr
         else:  # i > p, i > 0
             leftOperand = chunk + sum
             chunk = chunk + leftOperand.convolve(valVec)
             #chunkStr    = chunkStr + ' + ' + slotvalueStr + ' * (' +  chunkStr + ' + ' + sumStr + ')'
     return chunk  #, chunkStr
Exemplo n.º 7
0
 def defineVectors(self, chunkList):
     for attribute in chunkList:
         # check to see if there is a slot, or if it's just a value without a slot
         if isinstance(attribute, list):
             slot, value = attribute
             # if it's a new slot, create a new random permutation
             if slot not in self.slots.keys():
                 self.slots[slot] = numpy.random.permutation(self.N)
         else:
             value = attribute
         # if it starts with ! (i.e., not) just ignore that for now
         if value.startswith('!'):
             value = value[1:]
         # if it's a new value, create a new random vector
         if value not in self.environment.keys():
             self.environment[value] = HRR(N=self.N)
             self.memory[value] = HRR(data=numpy.zeros(
                 self.N))  #self.environment[value]
Exemplo n.º 8
0
 def array_to_value(self, array):
     return HRR(data=array)
Exemplo n.º 9
0
 def get(self, value):
     if value not in self.env:
         self.env[value] = HRR(N=self.N)
         self.mem[value] = HRR(data=numpy.zeros(self.N))
     return self.env[value].copy()