In [131]:
class Node(object):
def __init__(self):
self.value = 0.0
self.connections = []
def connect(self, function, target, strength=0.1):
self.connections.append((function, target, strength))
def apply_connections(self):
for f,t,s in self.connections:
t += f(self.value) * s
def __iadd__(self, amount):
self.value += amount
if self.value > 1.0:
self.value = 1.0
if self.value < -1.0:
self.value = -1.0
if self.value < -0.1:
self.value = -0.1
return self
nouns = ['DOG', 'CAT']
verbs = ['CHASE']
def f_thresh(x):
if x > 0.5:
return 1.0
else:
return 0.0
def f_linear(x):
if x<0: return 0
if x>1: return 1
return x
def f_sigmoid(x):
v = np.arctan((x-0.5)*10)/np.pi+0.5
if v < 0.1: v = 0
return v
nodes = {}
for noun in nouns:
base = Node()
n = Node()
nodes[noun] = base
nodes['N_'+noun] = n
base.connect(f_sigmoid, n)
n.connect(f_sigmoid,base, strength=-0.2)
for verb in verbs:
base = Node()
v = Node()
nodes[verb] = base
nodes['V_'+verb] = v
base.connect(f_sigmoid, v)
v.connect(f_sigmoid, base, strength=-0.2)
for noun in nouns:
vp = Node()
nodes['VP_'+verb+'_'+noun] = vp
def vp_func(x, n=nodes[noun]):
return f_sigmoid(x * n.value)
v.connect(vp_func, vp)
vp.connect(f_sigmoid, vp)
vp.connect(f_sigmoid, v, -0.2)
vp.connect(f_sigmoid, n, -0.2)
vp.connect(f_sigmoid, nodes[noun], -0.2)
In [140]:
T = 100
stims = 'DOG CHASE CAT'.split()
keys = nodes.keys()
keys.sort()
for n in nodes.values():
n.value = 0
actv = []
for stim in stims:
for t in range(T):
nodes[stim] += 0.1
for n in nodes.values():
n.apply_connections()
a = [nodes[k].value for k in keys]
actv.append(a)
In [141]:
figure(figsize=(10,6))
lines=plot(actv, linewidth=4)
legend(lines, keys, loc='best')
ylim(-0.1, 1.1)
show()
In [116]:
print keys
In [15]:
print nodes
In [121]:
x = np.linspace(-1, 1, 100)
plot(x, np.arctan(x*10)*2/np.pi)
Out[121]:
In [ ]: