神经网络的简单感知器和Adaline感知器实现。

2022-05-28 15:14:30 浏览数 (1)

run.m

代码语言:javascript复制
clear;
load x.mat;
load t.mat;

net = newp([-1 1; -1 1], 1, 'hardlims');
net.trainParam.epochs = 10;
net.inputweights{1,1}.initFcn = 'rands';
net.biases{1}.initFcn = 'rands';

net = init(net);

net = train(net,x',t');

y = sim(net,x');

fprintf('Error %gn', numel(find(y' ~= t)) / size(t,1));

hold on;
t = t == 1;
plotpv(x',t');
plotpc(net.IW{1,1}, net.b{1});
hold off;

f(x)=2x_function.m

代码语言:javascript复制
clear;
x = -1:0.1:1;
n = length(x);
f = 2 * x   (0.25-(-0.25)) .* rand(1,n)   (-0.25);

rate = 0:0.01:0.1;
epoci = 0:10:100;
epoci(1) = 1;
MSE = zeros(length(rate), length(epoci));

for i = 1:length(rate)
    for j = 1:length(epoci)
        net = newlin(x,f,0,rate(i));
        net.trainParam.epochs = epoci(j);
        net = train(net, x, f);
        y = sim(net, x);
        
        MSE(i,j) = 1.0/length(f) * sum((f - y).^2);
    end
end

[xx, yy] = meshgrid(rate, epoci);
surf(xx, yy, MSE);

MIN = min(min(MSE))
[i, j] = find(MSE == MIN);
fprintf('Optimal pairs of learning rates and epochs:n');
[rate(i); epoci(j)]

net = newlin(x,f,0,0.01);
net.trainParam.epochs = 60;
net = train(net, x, f);
y = sim(net, x);

figure(2);
hold on;
plot(x, f, 'b');
plot(x, y, 'r', 'LineWidth', 2);
plot(x, 2 * x, 'g', 'LineWidth', 2);
hold off;

sinus_function.m

代码语言:javascript复制
time = 0:0.025:5;
n = length(time);
%f = sin(time * 2 * pi);
f = sin(time * 2 * pi)   rand(1,n) * 0.1; % with noise
P = num2cell(f(1:n-1)); %until last example: the last one is the example, rest of them the history
T = num2cell(f(2:n));
Pi = {0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0};�lay units
net = newlind(P, T, Pi);
Y = sim(net, P);
a = cell2mat(Y);

hold on;
plot(time, f, 'b');
plot(time(2:n), a, 'r', 'LineWidth', 2);
hold off;

0 人点赞