Neural Network - Plotting the results

3 views (last 30 days)
Benedikt Wessel
Benedikt Wessel on 9 Jun 2020
Commented: Benedikt Wessel on 9 Jun 2020
Hello everybody,
below you can find a screenshot from the tensorflow playground. My question is: how can I plot the results in matlab like in the picture?
Thanks in advance
  3 Comments
KSSV
KSSV on 9 Jun 2020
That can be plotted using meshgrid, inteprolations etc.
Benedikt Wessel
Benedikt Wessel on 9 Jun 2020
This is my plot and this my code:
clear
clc
%% Input
x1=0.1.*rand(20,2)+0.7;
x2=0.1.*rand(20,2);
x31=0.1.*rand(20,1);
x32=0.1.*rand(20,1)+0.7;
x3=[x31,x32];
x41=0.1.*rand(20,1)+0.7;
x42=0.1.*rand(20,1);
x4=[x41,x42];
x5=0.2*rand(20,2)+0.3;
x0=-ones(100,1); % negative bias for every Input
in=[x1;x2;x3;x4;x5];
in=[x0,in]'; % complete Inputmatrix
clear x1
clear x2
clear x0
o1=ones(20,1);
o2=zeros(20,1);
output=[o1;o1;o2;o2;o2]'; % correct Output
clear o2
clear o1
weights=2*rand(3,3)-1; % randomn values for the weight matrix
iterations=5000;
coeff=0.3;
err=zeros(iterations,1);
%% Training
for i=1:iterations
out=zeros(100,1);
numIn=length(in);
e=0;
for j=1:numIn
input=in(:,j);
desired_out=output(:,j);
% Forward-Propagation
% Hidden_Layer
H1=dot(input,weights(:,1)); % Hidden-Layer_Neuron1
H2=dot(input,weights(:,2)); % Hidden-Layer_Neuron2
HL(1)=sigmoid(H1); % Acitvation function (first Input for the Output_Layer)
HL(2)=sigmoid(H2); % Activation function (second Input for the Output_Layer)
% Output-Layer
input_OL=[input(1);HL(1);HL(2)]; % Input vector for the Ouput-Layer (Bias and the output from the Hidden-Layer)
O3=dot(input_OL,weights(:,3)); % Output-Layer
OL=sigmoid(O3); % Output value neuronal network
out(j)=OL;
% Backward-Propagation
% Output-Layer
Hout=[-1,HL]'; % Output Hiddenlayer
delta=desired_out-OL; % Deviation Errorfunction MSE 0.5*(delta)^2
dsigmoid_OL=OL*(1-OL); % Deviation Sigmoid-Function (Activationfunction)
% dO3/dw=H0, Deviation weighted sum regarding the weights
weights(:,3)=weights(:,3)+coeff*delta*dsigmoid_OL.*Hout;
% Hidden-Layer
dsigmoid_HL=Hout(2:3).*(1-Hout(2:3));
% weigths(input->hidden)+coeff*der(err.fct.)*der(act.fct_Outputlayer)*Input Outputlayer*weigths(hidden->outputlayer)*der(act.fct._Hiddenlayer)*Input
weights(:,1)=weights(:,1)+coeff*delta*dsigmoid_OL*HL(1)*weights(2,3)*dsigmoid_HL(1).*input; % Weights 1. Neuron in Hidden-Layer
weights(:,2)=weights(:,2)+coeff*delta*dsigmoid_OL*HL(2)*weights(3,3)*dsigmoid_HL(2).*input; % Weights 2. Neuron in Hidden-Layer
e=e+abs(delta);
end
err(i)=e;
end
%% Plot
plotpv(in(2:3,:),output)
hold on
x=(0:0.1:1);
a=weights(2,1)/weights(3,1);
b=weights(1,1)/weights(3,1);
y=-a*x+b; % aus den Gewichten eine Funktion ableiten
plot(x,y) % diese Funktion grafisch darstellen
a=weights(2,2)/weights(3,2);
b=weights(1,2)/weights(3,2);
y=-a*x+b; % aus den Gewichten eine Funktion ableiten
plot(x,y) % diese Funktion grafisch darstellen
%% Sigmoid
function s=sigmoid(x)
s=1/(1+exp(-x));
end
It´s my first attempt. I want to add a few layers and neurons later to get results for more complex data.

Sign in to comment.

Answers (0)

Products

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!