def _active_locations(self, address): """ Return a list containing the indices of the activated locations. :param address: array of bits in {0, 1} :type address: numpy.array :rtype: numpy.array """ return (numpy.inner(self.hard_addresses, helper.convert(address)) >= self.activation_threshold)
def store(self, words): """ Store a stack of words in the memory. Importat: Everytime this method is called it clears the content of the memory and add the new content based on the words parameter. :param words: a matrix in which each line is a word in {0, 1} :type words: numpy.array """ data = helper.convert(words) size, word_length = words.shape self.content = ((1 / word_length) * numpy.dot(data.T, data) - (size / word_length) * numpy.identity(word_length))
def store(self, address, word): """ Store a word in a given address in the memory. :param address: array of bits in {0, 1} :param word: array of bits in {0, 1} :type address: numpy.array :type word: numpy.array """ active = self._active_locations(address) self.content[active] = numpy.clip( self.content[active] + helper.convert(word), -self.counter_range, self.counter_range)
def retrieve(self, probe): """ Retrieve a word stored in the memory given a probe vector. :param probe: array of bits in {0, 1} :type probe: numpy.array :rtype: numpy.array """ assert len(probe) == len(self.content) word = numpy.array(helper.convert(probe)) word_length = len(word) for i in numpy.random.permutation(xrange(word_length)): temp = 0 while temp - word[i] != 0: temp = word[i] word[i] = numpy.sign(numpy.dot(self.content[i], word)) return helper.bitify(word)
def __init__(self, memory_size, address_length, word_length, activation_radius): """ :param memory_size: number of hard locations :param address_length: size of each address :param word_length: size of the bit word :param activation_radius: radius of activatio :type memory_size: int :type address_length: int :type word_length: int :type activation_radius: int """ self.hard_addresses = helper.convert( numpy.random.randint(0, 2, (memory_size, address_length))) self.activation_threshold = address_length - 2 * activation_radius self.counter_range = numpy.ones(word_length) * 15 self.content = numpy.zeros((memory_size, word_length), dtype=numpy.int8)