<jupyter_start><jupyter_text>TrainingLetter Recognition with Threshold Perceptron 1. Import the Dataset<jupyter_code>pattern = np.loadtxt("pattern1.txt", dtype=int)<jupyter_output><empty_output><jupyter_text>2. Set Variables<jupyter_code># number of input and output channels
nIn = 12*13
nOut = 26
# random initial weight matrix
wOut = np.random.rand(nOut,nIn) - 0.5<jupyter_output><empty_output><jupyter_text>3. Train Vectors<jupyter_code># matrix of input pattern
rIn = np.reshape(np.transpose(pattern), (nIn, 26))
# matrix of desired outputs
rDes = np.identity(26)<jupyter_output><empty_output><jupyter_text>4. Update and Train Vectors<jupyter_code># error array
error = []
for training_step in range(20):
  # test all pattern
  # threshold activation function
  rOut = np.matmul(wOut,rIn)>0
  dist = sum(sum(abs(rDes - rOut)))/26
  error.append(dist)
  # training with delta rule
  wOut = wOut + 0.1*np.matmul((rDes-rOut),np.transpose(rIn))<jupyter_output><empty_output><jupyter_text>5. Plot the Results<jupyter_code>plt.plot(range(1,21),error)
plt.xlabel('Training step')
plt.ylabel('Average number of wrong bits')
plt.title("Letter recognition with threshold perceptron")
plt.xticks(range(1,21))
plt.show()<jupyter_output><empty_output><jupyter_text>TestingTesting Generalization of Performance of Trained Perceptron 1. Import Dataset<jupyter_code>letterMatrix = rIn<jupyter_output><empty_output><jupyter_text>2. Define randomFlipMatrix Function<jupyter_code>def randomFlipMatrix(n):
  r = np.zeros((156,26))
  for i in range(26):
    x = np.random.permutation(156)
    r[x[1:n],i] = 1
  return r<jupyter_output><empty_output><jupyter_text>3. Flip random bits and test the perceptron<jupyter_code>meanDist1=[]
meanDist2=[]
stdDist1=[]
stdDist2=[]
for nflip in range(80):
  dist1 = []
  dist2 = []
  for trial in range(10):
    rIn = abs(letterMatrix - randomFlipMatrix(nflip))
    # Threshold output Function
    rOut1 = np.matmul(wOut,rIn)>0
    nerror = 0
    for j in range(26):
      nerror = nerror + (sum(rDes[:,j]!=rOut1[:,j])>0)
    dist1.append(int(nerror))
    # max output function
    A = np.matmul(wOut,rIn)
    [v,i] = A.max(axis=1),A.argmax(axis=1)
    rOut2 = np.zeros((26,26))
    for j in range(26):
      rOut2[i[j],j]=1
    dist2.append(0.5*sum(sum(rDes!=rOut2)))
  meanDist1.append(mean(dist1))
  meanDist2.append(mean(dist2))
  stdDist1.append(stdev(dist1))
  stdDist2.append(stdev(dist2))<jupyter_output><empty_output><jupyter_text>4. Plot the results<jupyter_code>plt.errorbar(range(80),meanDist1,yerr=stdDist1,capsize=4,label="Threshold Activation Function")
plt.errorbar(range(80),meanDist2,yerr=stdDist2,capsize=4,label="Max Output Function")

plt.title("Generalization Ability")
plt.xlabel("Number of Flipped Bits")
plt.ylabel("Average Number of Wrong Letters")
plt.legend()
plt.show()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.14004914004914004, 'fraction_numerical': 0.0343980343980344, 'mean_word_length': 6.251908396946565, 'pattern_counts': {'":': 0, '<': 37, '<?xml version=': 0, '>': 40, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '9', 'dir': 'jupyter-structured-clean-dedup', 'id': '313926', 'license': 'MIT', 'n_tokens_mistral': 1157, 'n_tokens_neox': 1094, 'n_words': 197, 'path': 'Chapter06/Single_Layer_Perceptron.ipynb', 'repo_name': 'Project-Encephalon/Computational-neuroscience', 'size': '2849'}