예제 #1
0
    def __init__(self,
                 name,
                 node,
                 hrr=None,
                 enter_f=None,
                 exit_f=None,
                 timer_f=None,
                 timer_t=None,
                 input=None):
        """Initializes a finite state machine state. Requires a name
        (which must be unique to the node) and the FSMNode
        which it will be a part of.
        
        Keyword arguments:
        hrr -- An HRR to be bound to this state. If it is not assigned, it will be
          randomly generated.
        enter_f -- A function to run when you enter this state.
        exit_f -- A function to run when you exit this state.
        timer_f -- A function to run after a certain amount of time has elapsed.
        timer_t -- The amount of time after which the timer function will run.
        input -- A function to run in response to some input. (Does this make sense to include?)
        """
        self.name = name
        self.node = node
        self.enter_f = enter_f
        self.exit_f = exit_f
        self.timer_f = timer_f
        self.timer_t = timer_t
        self.input = input

        if not hrr:
            self.hrr = _hrr.HRR(N=node.state_dims)
        else:
            self.hrr = hrr
예제 #2
0
 def termination_input(self,value):
     if self.normalizing:
         h=hrr.HRR(data=value)
         length=h.length()
         if length>1.2: h=h*(1.2/length)
         value=h.v
     self.X=value
예제 #3
0
def output(trial_length, main, main_vector, alternate, noise_func=default_noise):
    tick = 0
    vector = main_vector
    main_hrr = hrr.HRR(data=main_vector)

    while True:
        if tick == trial_length :
            tick = 0
            if main:
                vector = main_vector
            else:
                vector = noise_func(main_vector)
                u = hrr.HRR(data=vector)
                similarity = u.compare(main_hrr)
                print "Sim:", similarity

            if alternate:
                main = not main

        tick += 1

        yield vector
예제 #4
0
def ortho_vector(input_vec, normalize=False):
    """
    Returns a random vector orthogonal to the input vector.

    Args:
    input_vec -- Returned vector will be orthogonal to input_vec
    normalize -- whether to normalize the vector before returning
    """

    normed_input = input_vec / np.linalg.norm(input_vec)
    dim = len(input_vec)
    ortho = hrr.HRR(dim).v
    proj = np.dot(normed_input, ortho) * normed_input
    ortho -= proj
    assert np.allclose([np.dot(ortho, input_vec)], [0])
    if normalize:
        ortho = ortho / np.linalg.norm(ortho)
    return ortho
예제 #5
0
    def f(input_vec):

        input_vec = hrr.HRR(data=input_vec)
        partner_key = random.choice(keys)

        pair_keys = filter(lambda x: x != partner_key, keys)

        pairs = random.sample(pair_keys, 2 * num)
        p0 = (pairs[x] for x in range(0, len(pairs), 2))
        p1 = (pairs[x] for x in range(1, len(pairs), 2))
        S = map(lambda x, y: noise_vocab[x].convolve(noise_vocab[y]), p0, p1)

        S = reduce(lambda x, y: x + y, S,
                   noise_vocab[partner_key].convolve(input_vec))
        S.normalize()

        reconstruction = S.convolve(~noise_vocab[partner_key])
        reconstruction.normalize()

        return reconstruction.v
예제 #6
0
    def hrr_noise(input_vec):
        noise_vocab = hrr.Vocabulary(D)
        keys = [noise_vocab.parse(str(x)) for x in range(2*num+1)]

        input_vec = hrr.HRR(data=input_vec)
        partner_key = random.choice(keys)

        pair_keys = filter(lambda x: x != partner_key, keys)

        pairs = random.sample(pair_keys, 2 * num)
        p0 = (pairs[x] for x in range(0,len(pairs),2))
        p1 = (pairs[x] for x in range(1,len(pairs),2))
        S = map(lambda x, y: noise_vocab[x].convolve(noise_vocab[y]), p0, p1)

        S = reduce(lambda x, y: x + y, S, noise_vocab[partner_key].convolve(input_vec))
        S.normalize()

        vec_hrr = S.convolve(~noise_vocab[partner_key])
        similarity = vec_hrr.compare(input_vec)
        similarities.append(similarity)
        return vec_hrr.v
예제 #7
0
    def hrr_noise_from_string(input_vec):
        """
        Uses expression, names, query_vectors from wrapper function

        Args:

        input_vec -- the vector to add noise to. Can be an HRR vector or a
                     numpy ndarry.  Returns a noisy vector of the same type
                     as input_vec.

        """

        use_ndarray = type(input_vec) == np.ndarray
        if use_ndarray:
            input_vec = hrr.HRR(data=input_vec)

        vocab = hrr.Vocabulary(D, unitary=unitary_names)

        for n, v in names.iteritems():
            vocab.add(n, v)

        vocab.add(placeholder, input_vec)

        try:
            h = eval(expression, {}, vocab)
        except Exception as e:
            print 'Error evaluating HRR string ' + original
            raise e

        if normalize:
            h.normalize()

        vocab.add('h', h)

        noisy = eval('h * ~(' + ' * '.join(query_vectors) + ')', {}, vocab)

        if use_ndarray:
            noisy = noisy.v

        return noisy
예제 #8
0
 def termination_symbol(self, x, dimensions=dimensions):
     self.sym = hrr.HRR(data=x)
예제 #9
0
 def termination_C(self, x, dimensions=dimensions):
     self.col = hrr.HRR(data=x)
예제 #10
0
 def termination_R(self, x, dimensions=dimensions):
     self.row = hrr.HRR(data=x)
예제 #11
0
 def termination_R(self, x, dimensions=dimensions):
     self.content_R = hrr.HRR(data=x)
예제 #12
0
 def termination_vision(self, x, dimensions=dimensions):
     self.content = hrr.HRR(data=x)
예제 #13
0
 def termination_identifier(self, x, dimensions=dimensions):
     self.x = hrr.HRR(data=x)
예제 #14
0
 def termination_salience(self, x, dimensions=dimensions):
     self.pairs = hrr.HRR(data=x)
예제 #15
0
 def termination_6(self, x, dimensions=dimensions):
     self.v6 = hrr.HRR(data=x)
예제 #16
0
order = list(range(len(m_inputs)))
random.shuffle(order)

# compute the semantic pointers for each digit by averaging the encoded values for each category
vocab = hrr.Vocabulary(50)
for i, label in enumerate([
        'ZERO', 'ONE', 'TWO', 'THREE', 'FOUR', 'FIVE', 'SIX', 'SEVEN', 'EIGHT',
        'NINE'
]):
    v = numeric.array([0] * 50, typecode='f')
    count = 0
    for j, yy in enumerate(m_inputsy):
        if yy[0] == i:
            v += m_encode[j]
            count += 1
    sp = hrr.HRR(data=v / count)
    sp.normalize()
    vocab.add(label, sp)
    print label, sp.v


class Input(nef.SimpleNode):
    def origin_image(self):  # present the visual input to the model
        index = int(self.t / presentation_time) % len(order)
        return m_inputs[order[index]]

    def origin_exact(self):  # the exact correct value (as computed by
        index = int(self.t / presentation_time) % len(
            order)  #  the non-spiking RBM model).  Used for
        return m_encode[order[index]]  #  comparison purposes only
예제 #17
0
 def f(input_vec):
     v = input_vec + noise * hrr.HRR(D).v
     v = v / np.sqrt(sum(v**2))
     return v
예제 #18
0
    """
    num_unitary_wildcards = expression.count('?u')
    expression = expression.replace('?u', '%s')
    unitary_names = ['u'+str(i) for i in range(num_unitary_wildcards)]
    expression = expression % tuple(unitary_names)

    num_wildcards = expression.count('?')
    expression = expression.replace('?', '%s')
    temp_names = ['h'+str(i) for i in range(num_wildcards)]
    expression = expression % tuple(temp_names)

    return expression, unitary_names, temp_names

if __name__ == "__main__":
    D = 512
    i = hrr.HRR(D)
    f = make_hrr_noise_from_string(D, '?u*?u*?*!*?u')
    x = f(i)
    print i.compare(x)

    i = hrr.HRR(D)
    noise_string = '?*? *? + ? * ? * ? + ? * ? * ! + ?*?'
    f = make_hrr_noise_from_string(D, noise_string, normalize=False)
    x = f(i)
    print i.compare(x)

    i = hrr.HRR(D)
    noise_string = '? * ! + ? * ?'
    f = make_hrr_noise_from_string(D, noise_string, normalize=False)
    x = f(i)
    print i.compare(x)