<jupyter_start><jupyter_text>Implementation of RBF Network with more than one set of neuronsThe following is how to implement a RBF network with Gradien Descent with two subsets of neurons. Each set of neurons will be trained on a different class of MNIST.<jupyter_code>import RBFGrad
from RBFGrad import *
import numpy as np<jupyter_output><empty_output><jupyter_text>Instantiate<jupyter_code>layer= RBFLayer()<jupyter_output><empty_output><jupyter_text>1. Add First Set of Neurons---<jupyter_code>layer.add(10)
print ("number of neurons: " + str(layer.num_neurons))<jupyter_output>number of neurons: 10<jupyter_text>2. Load Desired DataThe data must be normalized. In the example we will train 10 neurons on MNIST class 0, create another set and train those 10 neurons on MNIST class 4<jupyter_code>#for datasets only 
import tensorflow as tf 
(train_data,train_labels),(test_data,test_labels)= tf.keras.datasets.mnist.load_data()
zeros = train_data[np.where(train_labels==0)] #MNIST 0
fours=train_data[np.where(train_labels==4)]  #MNIST 4
input_sample=np.zeros((39,28,28))
input_sample[0:19]=zeros[0:19] #The first 0:19 inputs will be MNIST 0
input_sample[20:39]=fours[0:19] #The second 20:39 inputs will be MNIST 4
input_sample=input_sample.reshape(39,784) 
input_mean = np.mean(input_sample)
input_stdev=np.std(input_sample)
input_sample =input_sample-input_mean
input_sample=input_sample/input_stdev<jupyter_output><empty_output><jupyter_text>3. Load 'Prototype'Load the prototype of MNIST 0 on 10 neurons.<jupyter_code>layer.initialize(input_sample[0])<jupyter_output><empty_output><jupyter_text>4.Train the Neurons Using Similar Samples.<jupyter_code>ctr=0
for i in range(100):
  layer.set_input(input_sample[ctr])
  if ctr<=19:
    layer.trainNeurons(range(0,9))
  ctr=ctr+1
  if ctr>19: 
    ctr=0<jupyter_output><empty_output><jupyter_text>5. Now add 10 additional neurons, and assign them the prototype of MNIST 4, and train on MNIST data 4<jupyter_code>layer.add (10) 
layer.initialize(input_sample[20])
ctr=20    #you want to start at MNIST 4 data only
for i in range(100):
  layer.set_input(input_sample[ctr])
  if ctr<=38:
    layer.trainNeurons(range(10,19))
  ctr=ctr+1
  if ctr>38: 
    ctr=20<jupyter_output><empty_output><jupyter_text>5. Test NeuronGather test data, in this case it is MNIST 4 and MNIST 0. You will expect that the first set of neurons will have low activation for MNIST 4 as it has been trained on MNIST 0, while the second set will have low activation for MNIST 0, and high activation for for MNIST 4 Both neurons will have low activation for MNIST 4<jupyter_code>twos=train_data[np.where(train_labels==2)]
fours = train_data[np.where(train_labels==4)]
testData = np.zeros((41,28,28))
testData[0:20]=fours[41:61] #these are MNIST data that was not present during training
testData[21:41]=zeros[41:61]
testData= testData-input_mean 
testData=testData/input_stdev
testData=testData.reshape(41,784)<jupyter_output><empty_output><jupyter_text>Reults<jupyter_code>import matplotlib.pyplot as py
neuronSet1=0
neuronSet2=0
py.title('Sample Number versus Layer Summed Activation - Layer 1')
py.xlabel('sample number') 
py.ylabel('layer summed activation')

for i in range(40):
  neuronSet1=(sum(layer.layerOutput(testData[i])[0:9] ))
  neuronSet2=(sum(layer.layerOutput(testData[i])[10:19] ))
  if i<=19: #displaying MNIST 4
    py.scatter(i,neuronSet1,color='red') #Trained on zeros - Expect Low
    py.scatter(i,neuronSet2,color='blue') #Trained on fours - Expect High
  elif i>=20 and i<=40: #displaying MNIST 0.
    py.scatter(i,neuronSet1,color='red') #Trained on zeros -Expect High
    py.scatter(i,neuronSet2,color='blue') #Trained on Fours.  - Expect Low<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.10633727175080558, 'fraction_numerical': 0.04189044038668099, 'mean_word_length': 6.586558044806518, 'pattern_counts': {'":': 0, '<': 40, '<?xml version=': 0, '>': 39, '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': '410605', 'license': 'MIT', 'n_tokens_mistral': 1459, 'n_tokens_neox': 1338, 'n_words': 349, 'path': 'RBF_Multiple_Neuron_Sets.ipynb', 'repo_name': 'pluu2/RBF-Network-Gradient', 'size': '3724'}