-
Notifications
You must be signed in to change notification settings - Fork 0
/
minerva.py
57 lines (36 loc) · 1.35 KB
/
minerva.py
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
# -*- coding: utf-8 -*-
"""
Created on Sat Jan 30 18:05:17 2021
@author: Lu, Yihe
Original Minverva2 from Hintzman (1984)
"""
import numpy as np
def proba0(x):
_x = np.asarray(x)
_x[_x > 1], _x[_x < -1] = 1, -1
p = 1 - np.abs(_x)
return p
class Minerva2: # Hintzman (1984)
def __init__(self, trace_size):
self.trace_size = trace_size
self.reset()
def reset(self):
self.memory = np.empty((0, self.trace_size))
def get_memory_matrix(self):
return self.memory
def learn(self, learning_data):
_data = np.reshape(learning_data, (-1, self.trace_size))
self.memory = np.concatenate((self.memory, _data), axis = 0)
def respond(self, probes, recurrence = 1):
echo = probes[:]
for epoch in range(recurrence):
echo = self._echo(echo)[1]
return echo
def _echo(self, probes):
_probe = np.reshape(probes, (-1, self.trace_size))
similarity = np.dot(self.memory, _probe.T) # Eq. 1
activation = similarity ** 3 # Eq. 2
intensity = np.sum(activation, axis = 0) # Eq. 3
content = np.dot(activation.T, self.memory) # Eq. 4
normalised_echo = content / np.amax(np.abs(content), axis = 1).reshape((-1, 1))
return intensity, normalised_echo