0% found this document useful (0 votes)
60 views

Artificial Neural Network

This document demonstrates how to simulate an XOR problem using a neural network in MATLAB. It creates a dataset with inputs and outputs for XOR, divides it into training and testing sets. It then creates a neural network, simulates it before and after training on the training set, and tests it on the testing set to measure performance. The goal is for the neural network to learn the logic of the XOR gate from examples.

Uploaded by

Kai Gustav
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
60 views

Artificial Neural Network

This document demonstrates how to simulate an XOR problem using a neural network in MATLAB. It creates a dataset with inputs and outputs for XOR, divides it into training and testing sets. It then creates a neural network, simulates it before and after training on the training set, and tests it on the testing set to measure performance. The goal is for the neural network to learn the logic of the XOR gate from examples.

Uploaded by

Kai Gustav
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 15

Neural Networks using MATLAB

Ihsan M. Yassin Faculty of Electrical Engineering, UiTM

Neural Networks

Neural networks are a branch of Artificial Intelligence capable of learning similar to the way a human does. The goal of Neural Network is to create machines/computers that could learn a particular logic or function from examples, rather than being hard-coded its programs.

NN Example: XOR Problem

In this example, we will try to simulate the XOR problem. We want the NN to learn how to simulate an output of the XOR gate based on examples that we give to it.

Step 1: Create a XOR dataset


% demonstration of neural network toolbox % tested on MATLAB Version 7.12.0.635 (R2011a) clear, close all clc

% create dataset % sample XOR problem u = rand(2,5000); u = u > 0.5; u = double(u);

y = zeros(1,size(u,2)); for i = 1:1:size(u,2) if u(1,i) == u(2,i) y(i) = 0; else y(i) = 1; end end

Step 2: Divide the Dataset


% divide into training and testing set u_train = u(:,1:2500); u_test = u(:,2501:5000); y_train = y(:,1:2500); y_test = y(:,2501:5000);

Step 3: Create a NN, and try to simulate

% create a new neural network net = newff(u_train, y_train, 3); % simulate prior to training [y_train_sim, pf] = sim(net, u_train); figure, plot(y_train); hold on; plot(y_train_sim,'r:'); hold off; title('Simulation results (before training)'); xlabel('Samples'); ylabel('Value');

Step 4: Train the NN, and simulate again

% train and simulate again [net,tr] = train(net,u_train,y_train); [y_train_sim, pf] = sim(net, u_train); figure, plot(y_train); hold on; plot(y_train_sim,'r:'); hold off; title('Simulation results (after training)'); xlabel('Samples'); ylabel('Value');

Step 5: Measure performance on test set

% simulate on testing set [y_test_sim, pf] = sim(net, u_test); figure, plot(y_test); hold on; plot(y_test,'r:'); hold off; title('Simulation results (testing set, after training)'); xlabel('Samples'); ylabel('Value');

The End

You might also like