Artificial Neural Network
Artificial Neural Network
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.
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.
% demonstration of neural network toolbox % tested on MATLAB Version 7.12.0.635 (R2011a) clear, close all clc
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
% 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);
% 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');
% 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');
% 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