0% found this document useful (0 votes)
162 views6 pages

Neural Networks Lab #1: 1.1 Perceptron Learning Algorithm

This document describes a MATLAB code for implementing a perceptron neural network to classify data from a halfmoon pattern. The code generates halfmoon training and testing data, initializes the perceptron with random weights, and runs a main loop to train the perceptron over multiple epochs. It updates the weights using a learning rate after each training example to minimize error. After training, it tests the perceptron on unlabeled examples and calculates the testing error rate. The document also describes functions to generate the halfmoon data pattern and implement the threshold activation function for the perceptron. The assignment is to create a C# program that implements the perceptron learning algorithm.

Uploaded by

naroqueen
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
162 views6 pages

Neural Networks Lab #1: 1.1 Perceptron Learning Algorithm

This document describes a MATLAB code for implementing a perceptron neural network to classify data from a halfmoon pattern. The code generates halfmoon training and testing data, initializes the perceptron with random weights, and runs a main loop to train the perceptron over multiple epochs. It updates the weights using a learning rate after each training example to minimize error. After training, it tests the perceptron on unlabeled examples and calculates the testing error rate. The document also describes functions to generate the halfmoon data pattern and implement the threshold activation function for the perceptron. The assignment is to create a C# program that implements the perceptron learning algorithm.

Uploaded by

naroqueen
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 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

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy