You are on page 1of 6

Neural Networks

Lab #1
1. Introduction
1.1 Perceptron learning algorithm
How does a Perception (Artificial Neuron) learn?

1.2 Halfmoon pattern classification


2. MATLAB Code
%%================== Step 0: Generating halfmoon data =====================
rad = 10; % central radius of the half moon
width = 6; % width of the half moon
dist = 1; %-1 % distance between two half moons
num_tr = 1000; % number of training sets
num_te = 2000; % number of testing sets
num_samp = num_tr+num_te;% number of samples
epochs = 50;
[data, data_shuffled] = halfmoon(rad,width,dist,num_samp);
%%============= Step 1: Initialization of Perceptron network ==============
num_in = 2; % number of input neuron
b = dist/2; % bias
err = 0; % a counter to denote the number of error outputs
eta = 0.95; % learning rate parameter
w = [b;zeros(num_in,1)];% initial weights
%%=========================== Main Loop ===================================
%% Step 2,3: activation and actual response
for epoch = 1:epochs,
for i = 1:num_tr,
x = [1 ; data_shuffled(1:2,i)]; % fetching data from database
d = data_shuffled(3,i); % fetching desired response from
database
y = mysign(w'*x);
ee(i) = d-y;
%% Step 4: update of weight
w_new = w + eta * (d-y)*x;
w = w_new;
end
mse(epoch) = mean(ee.^2);
end
%%============================== Testing ==================================
for i = 1 : num_te,
x = [1 ; data_shuffled(1:2,i+num_tr)]; % fetching data for testing
y(i) = mysign(w'*x);
if y(i) == 1 ,
plot(x(2),x(3),'rx');
end
if y(i) == -1,
plot(x(2),x(3),'k+');
end
end
xlabel('x');ylabel('y');
% Calculate testing error rate
for i = 1:num_te,
if abs(y(i) - data_shuffled(3,i+num_tr)) > 1E-6,
err = err + 1;
end
end

3. Important Functions
3.1 halfmoon(rad,width,dist,num_samp)
A function to generate the halfmoon data,
where Input:
 rad - central radius of the half moon
 width - width of the half moon
 dist - distance between two half moon
 num_samp - total number of the samples
Output:
 data - output data (data belonging to first region then data belonging to second region)
 data_shuffled - shuffled data (i.e. data belonging to both regions not in order)
For example
halfmoon(10,2,0,1000) will generate 1000 data of two half moons with radius [9-11] and
space 0.

3.2 mysign(d)
Represents the activation function. It is a threshold function that outputs 0 or 1.
4. Assignment
Implement the Perceptron learning algorithm using C#.

HINTS: You will need to create a “Perceptron class” whose members might include:

1. Perceptron weights,
2. Perceptron inputs,
3. “StartTraining” function,
4. “StartTesting” function,
5. And the helper functions “Adder” and “ActivationFunction”.

You might also like