<jupyter_start><jupyter_text>Disambiguation in detailThe goal of this notebook is to study at detail what happens in a desambiguation juncture. To do this, we need to change the input that both nodes get and how the activity in C3 evolves as a function of time.<jupyter_code>import sys
sys.path.append('../')
import matplotlib.pyplot as plt
import numpy as np
import seaborn as sns

%matplotlib inline
sns.set(font_scale=3.0)

np.set_printoptions(suppress=True, precision=4)
from network import MinaNetwork<jupyter_output><empty_output><jupyter_text>Train in two networks<jupyter_code>n_input = 200  # Inputs size
n_recurrent = 200  # C3 size
v = 50.0   # Input - C3 connection
b = 50.0   # Input - C1 connection
Kr = 0.90  # Recurrent self-inhibition gain
Ki = 0.0 # Input - C3 inhibition
Ci = 0.0  # Inhibition from the input to C1
Cr = Kr  # Inhibition from C3 to C1
p =  1.0  # Sparness parameter
p2 = 1.0
w0 = 0.36
uniform_w = False

# Dynamical parameters
theta = 0.0
phi = 0


# Training parameters
training_time = 100
epsilon = 0.1

# Instantiate the network
nn = MinaNetwork(n_input=n_input, n_recurrent=n_recurrent, p=p, p2=p2, v=v, b=b, Ki=Ki, Kr=Kr, Ci=Ci, Cr=Cr,
                 theta=theta, phi=phi, uniform_w=uniform_w, w0=w0)

# Build the patterns
number_of_patterns = 20
sparsity = 5.0
nn.build_patterns_dictionary(number_of_patterns=number_of_patterns, sparsity=sparsity)

# Training
sequence1 = [ 0,  1,  2, 9,  3,  4,  5]
sequence2 = [11, 12, 13, 9, 14, 15, 16]
sequences = [sequence1, sequence2]
epsilon = 0.02
training_time = 200
pre_synaptic_rule = True

nn.train_network(epsilon=epsilon, training_time=training_time, sequence=sequence1, 
                 pre_synaptic_rule=pre_synaptic_rule)
nn.train_network(epsilon=epsilon, training_time=training_time, sequence=sequence2, 
                 pre_synaptic_rule=pre_synaptic_rule);
nn.plot_weight_matrices()
success, z_out, z_c3 = nn.test_recall(sequence1)
print('success', success)
fig = plt.figure(figsize=(16, 12))
ax1 = fig.add_subplot(121)
im1 = ax1.imshow(z_out, aspect='auto')

ax1.grid()
ax1.set_title('C1 Activity (output)')

ax2 = fig.add_subplot(122)
im2 = ax2.imshow(z_c3, aspect='auto')

ax2.grid()
ax2.set_title('C3 activity')

fig.colorbar(im1, ax=ax1)
fig.colorbar(im2, ax=ax2);<jupyter_output><empty_output><jupyter_text>I realized that the problem is that **I am training first a fully complete sequence and then the other** I need to stand in 9 and see what this projects to the output of 3 and 14<jupyter_code>time = 3
threshold = 0.001
np.where(z_c3[time] > threshold)<jupyter_output><empty_output><jupyter_text>That is, the C3 activity sits at the intersection, where it should be. Now we want to see what C3 activity will be in the next step **where it should have disambiguated**<jupyter_code>vote_c3 = np.dot(nn.GC3 * nn.c1 * nn.w, z_c3[time])
indexes = np.where(vote_c3 > threshold)[0]
print(indexes)
z_c3[time + 1, indexes]<jupyter_output><empty_output><jupyter_text>We see that the update rule (excitation contribution) predicts that the both the pattern 3 and 14 should be activated in C3.<jupyter_code>np.where(z_c3[time + 1] > threshold)<jupyter_output><empty_output><jupyter_text>Now what happens with the output at time 4<jupyter_code>np.where(z_out[time + 1] > threshold)<jupyter_output><empty_output><jupyter_text>We see that only pattern 4 survived. To get this pattern we need to see how the activity from C3 at time=4 propagated.<jupyter_code>vote_c1 = np.dot(nn.GC1 * nn.c2 * nn.a, z_c3[time + 1])
print(vote_c1)
indexes = np.where(vote_c1 > threshold)[0]
print(indexes)
z_out[time + 1, indexes]<jupyter_output><empty_output><jupyter_text>Train two networks one by one<jupyter_code>n_input = 200  # Inputs size
n_recurrent = 200  # C3 size
v = 50.0   # Input - C3 connection
b = 50.0   # Input - C1 connection
Kr = 0.65 # Recurrent self-inhibition gain
Ki = 0.0 # Input - C3 inhibition
Ci = 0.0  # Inhibition from the input to C1
Cr = Kr  # Inhibition from C3 to C1
p =  1.0  # Sparness parameter
p2 = 1.0
w0 = 0.36
uniform_w = False
GC3 = 1.0

# Dynamical parameters
theta = 0.0
phi = 0

# Instantiate the network
nn = MinaNetwork(n_input=n_input, n_recurrent=n_recurrent, p=p, p2=p2, v=v, b=b, Ki=Ki, Kr=Kr, Ci=Ci, Cr=Cr,
                 theta=theta, phi=phi, uniform_w=uniform_w, w0=w0)

# Build the patterns
number_of_patterns = 20
sparsity = 5.0
nn.build_patterns_dictionary(number_of_patterns=number_of_patterns, sparsity=sparsity)

# Training
sequence1 = [ 0,  1,  2, 9,  3,  4,  5]
sequence2 = [11, 12, 13, 9, 14, 15, 16]
sequences = [sequence1, sequence2]
epsilon = 0.02
training_time = 200
pre_synaptic_rule = True

nn.train_network_sequences(epsilon=epsilon, training_time=training_time, sequences=sequences, 
                           pre_synaptic_rule=pre_synaptic_rule)
nn.plot_weight_matrices()<jupyter_output><empty_output><jupyter_text>Let's extract the connections that point to 3 and 14 besides 90<jupyter_code>aux = 0.6
nn.w[30:40, 90:100] = aux
nn.w[140:150, 90:100] = aux
plt.plot(nn.w[:, 93])
plt.ylim([0.60, 0.70])

nn.plot_weight_matrices()
support1 = np.where(nn.a[30, :] > 0)[0]
print(support1)
support2 = np.where(nn.a[140, :] > 0)[0]
print(support2)
nn.GC3 = 0.2
nn.GC1 = 0.2
success1, z_out, z_c3 = nn.test_recall(sequence1)
success2, z_out2, z_c32 = nn.test_recall(sequence2)
print('success 1', success1)
print('success 2', success2)
fig = plt.figure(figsize=(16, 12))
ax1 = fig.add_subplot(121)
im1 = ax1.imshow(z_c3, aspect='auto')

ax1.grid()
ax1.set_title('C3 Activity sequence 1')

ax2 = fig.add_subplot(122)
im2 = ax2.imshow(z_c32, aspect='auto')

ax2.grid()
ax2.set_title('C3 activity sequence 2')

fig.colorbar(im1, ax=ax1)
fig.colorbar(im2, ax=ax2);
fig = plt.figure(figsize=(16, 12))
ax1 = fig.add_subplot(121)
im1 = ax1.imshow(z_out, aspect='auto')

ax1.grid()
ax1.set_title('C1 Activity sequence 1')

ax2 = fig.add_subplot(122)
im2 = ax2.imshow(z_out2, aspect='auto')

ax2.grid()
ax2.set_title('C1 activity sequence 2')

fig.colorbar(im1, ax=ax1)
fig.colorbar(im2, ax=ax2);
success, z_out, z_c3 = nn.test_recall(sequence2)
fig = plt.figure(figsize=(16, 12))
ax1 = fig.add_subplot(121)
im1 = ax1.imshow(z_out, aspect='auto')

ax1.grid()
ax1.set_title('C1 Activity')

ax2 = fig.add_subplot(122)
im2 = ax2.imshow(z_c3, aspect='auto')

ax2.grid()
ax2.set_title('C3 activity')

fig.colorbar(im1, ax=ax1)
fig.colorbar(im2, ax=ax2);
time = 3
threshold = nn.Kr
z_c3[time] 
np.where(z_c3[time] > threshold)<jupyter_output><empty_output><jupyter_text>That is, the C3 activity sits at the intersection, where it should be. Now we want to see what C3 activity will be in the next step **where it should have disambiguated**<jupyter_code>vote_c3 = np.dot(nn.GC3 * nn.c1 * nn.w, z_c3[time])
print(vote_c3)
z_c3[time].sum() * nn.Kr
nn.GC3
indexes = np.where(vote_c3 > threshold)[0]
print(indexes)
z_c3[time + 1, indexes]<jupyter_output><empty_output><jupyter_text>We see that the update rule (excitation contribution) predicts that the both the pattern 3 and 14 should be activated in C3.<jupyter_code>np.where(z_c3[time + 1] > threshold)<jupyter_output><empty_output><jupyter_text>Now what happens with the output at time 4<jupyter_code>np.where(z_out[time + 1] > threshold)<jupyter_output><empty_output><jupyter_text>We see that only pattern 4 survived. To get this pattern we need to see how the activity from C3 at time=4 propagated.<jupyter_code>vote_c1 = np.dot(nn.GC1 * nn.c2 * nn.a, z_c3[time + 1])
print(vote_c1)
indexes = np.where(vote_c1 > threshold)[0]
print(indexes)
z_out[time + 1, indexes]<jupyter_output><empty_output><jupyter_text>Let's do more systematic study of the problem<jupyter_code>### Structure paramters
n_input = 200  # Inputs size
n_recurrent = 200 # C3 size
v = 21.0   # Input - C3 connection
b = 21.0   # Input - C1 connection
Kr = 0.85   # Recurrent self-inhibition gain
Ki = 0.0  # Input - C3 inhibition
Ci = 0.0  # Inhibition from the input to C1
Cr = 0.85  # Inhibition from C3 to C1
p = 1.0  # Sparness parameter
uniform_w = False

# Dynamical parameters
theta = 0.0
phi = 0

# Build the patterns
number_of_patterns = 20
sparsity = 5.0

# Training
sequence1 = [ 0,  1, 2,  9,  3,  4,  5]
sequence2 = [11, 12, 13, 9, 14, 15, 16]
sequences = [sequence1, sequence2]
epsilon = 0.02
training_time = 200
pre_synaptic_rule = True

dt = 0.05
Kr_vector = np.arange(0.3, 1 + dt, dt)
right_scores_history = []
wrong_scores_history = []

for Kr in Kr_vector:
    Cr = Kr
    # Instantiate the network
    nn = MinaNetwork(n_input=n_input, n_recurrent=n_recurrent, p=p, v=v, b=b, Ki=Ki, Kr=Kr, Ci=Ci, Cr=Cr,
                     theta=theta, phi=phi, uniform_w=uniform_w)
    
    nn.build_patterns_dictionary(number_of_patterns=number_of_patterns, sparsity=sparsity)

    # Training
    nn.train_network_sequences(epsilon=epsilon, training_time=training_time, sequences=sequences, 
                               pre_synaptic_rule=pre_synaptic_rule)   
    
    right_scores = []
    wrong_scores = []

    for sequence in sequences:
        success, z, z_c3 = nn.test_recall(sequence)
        goal_pattern = nn.patterns_dictionary[sequence[-1]]

        # Take the indexes where the pattern has to be active and where not
        wrong_indexes = np.where(goal_pattern == 0)[0]
        right_indexes = np.where(goal_pattern == 1)[0]

        # See if the right indexes are activated and if the wrong indexes are activated
        right_score =  z[-1][right_indexes].sum() 
        wrong_score = z[-1][wrong_indexes].sum()

        right_scores.append(right_score)
        wrong_scores.append(wrong_score)
    
    right_scores_history.append(right_scores)
    wrong_scores_history.append(wrong_scores)
right_scores_history
x = np.array(right_scores_history) 
y = np.array(wrong_scores_history)

right_results = np.mean(x, axis=1)
wrong_results = np.mean(y, axis=1)

fig = plt.figure(figsize=(16, 12))
ax = fig.add_subplot(111)

ax.plot(Kr_vector, right_results, '*-', markersize=15, label='right')
ax.plot(Kr_vector, wrong_results, '*-', markersize=15, label='wrong')

ax.axhline(0, color='black', linestyle='--')

# ax.set_ylim([-0.1, 1.1])
ax.legend();<jupyter_output><empty_output><jupyter_text>Systematic with sparse parameters<jupyter_code>n_input = 200  # Inputs size
n_recurrent = 200  # C3 size
v = 35.0   # Input - C3 connection
b = 35.0   # Input - C1 connection
Kr = 0.20  # Recurrent self-inhibition gain
Ki = 0.0 # Input - C3 inhibition
Ci = 0.0  # Inhibition from the input to C1
Cr = Kr  # Inhibition from C3 to C1
p =  0.4  # Sparness parameter
p2 = 0.4
w0 = 0.36
uniform_w = True

# Dynamical parameters
theta = 0.0
phi = 0

# Training
sequence1 = [ 0,  1, 9,  2,  3,  4,  5,  6]
sequence2 = [12, 13, 9, 14, 15, 16, 17, 18]
sequences = [sequence1, sequence2]
epsilon = 0.02
training_time = 200
pre_synaptic_rule = True

dt = 0.01
Kr_vector = np.arange(0.1, 1 + dt, dt)
right_scores_history = []
wrong_scores_history = []

for Kr in Kr_vector:
    Cr = Kr
    # Instantiate the network
    nn = MinaNetwork(n_input=n_input, n_recurrent=n_recurrent, p=p, p2=p2, v=v, b=b, Ki=Ki, Kr=Kr, Ci=Ci, Cr=Cr,
                     theta=theta, phi=phi, uniform_w=uniform_w)
    
    nn.build_patterns_dictionary(number_of_patterns=number_of_patterns, sparsity=sparsity)

    # Training
    nn.train_network_sequences(epsilon=epsilon, training_time=training_time, sequences=sequences, 
                               pre_synaptic_rule=pre_synaptic_rule)   
    
    right_scores = []
    wrong_scores = []

    for sequence in sequences:
        success, z, z_c3 = nn.test_recall(sequence)
        goal_pattern = nn.patterns_dictionary[sequence[-1]]

        # Take the indexes where the pattern has to be active and where not
        wrong_indexes = np.where(goal_pattern == 0)[0]
        right_indexes = np.where(goal_pattern == 1)[0]

        # See if the right indexes are activated and if the wrong indexes are activated
        right_score =  z[-1][right_indexes].sum() 
        wrong_score = z[-1][wrong_indexes].sum()

        right_scores.append(right_score)
        wrong_scores.append(wrong_score)
    
    right_scores_history.append(right_scores)
    wrong_scores_history.append(wrong_scores)
x = np.array(right_scores_history) 
y = np.array(wrong_scores_history)

right_results = np.mean(x, axis=1)
wrong_results = np.mean(y, axis=1)

fig = plt.figure(figsize=(16, 12))
ax = fig.add_subplot(111)

ax.plot(Kr_vector, right_results, '*-', markersize=15, label='right')
ax.plot(Kr_vector, wrong_results, '*-', markersize=15, label='wrong')

ax.axhline(0, color='black', linestyle='--')

# ax.set_ylim([-0.1, 1.1])
ax.legend();
x.shape
Kr_vector.shape<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.11988627389038066, 'fraction_numerical': 0.04983414942347181, 'mean_word_length': 4.3793542905692435, 'pattern_counts': {'":': 0, '<': 61, '<?xml version=': 0, '>': 73, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '15', 'dir': 'jupyter-structured-clean-dedup', 'id': '289251', 'license': 'MIT', 'n_tokens_mistral': 5240, 'n_tokens_neox': 4765, 'n_words': 1180, 'path': 'notebooks/2017-10-31(Disambiguation in detail).ipynb', 'repo_name': 'h-mayorquin/minai_levy_hippocampus', 'size': '12662'}