-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtrain.m
More file actions
75 lines (58 loc) · 1.62 KB
/
train.m
File metadata and controls
75 lines (58 loc) · 1.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
%Create training set
p1 = [1, 4];
t1 = 0;
p2 = [1, 5];
t2 = 0;
p3 = [2, 4];
t3 = 0;
p4 = [2, 5];
t4 = 0;
p5 = [3, 1];
t5 = 1;
p6 = [3, 2];
t6 = 1;
p7 = [4, 1];
t7 = 1;
p8 = [4, 2];
t8 = 1;
patterns = [p1]';
targets = [t1];
%Initialize a PerceptronLayer neural network
perceptron = PerceptronLayer(2, 1, "hardlim");
%print perceptron weight and bias
%print patterns and training set
disp("Patterns")
disp(patterns)
disp("Targets")
disp(targets)
perceptron = trainPerceptron(perceptron, patterns, targets);
testPerceptron(perceptron, patterns, targets);
%this function initializes and trains a PerceptronLayer neural network to classify two-dimensional vectors according to the training set provided
function perceptron = trainPerceptron(perceptron, patterns, targets)
%initialize the neural network
arguments
perceptron PerceptronLayer
patterns double
targets double
end
%train the neural network
perceptron = perceptron.input_setter(patterns);
perceptron = perceptron.output_setter(targets);
perceptron = perceptron.learn();
end
function testPerceptron(perceptron, patterns, targets)
%initialize the neural network
arguments
perceptron PerceptronLayer
patterns double
targets double
end
%test the neural network by calling forward and comparing the output (a) to the target
for i = 1:size(patterns, 2)
a = perceptron.forward(patterns(:, i));
%assert that the output is equal to the target
disp("a " + a + "targets " + targets(1));
assert(a == targets(i), "Test failed!");
end
fprintf("Successful!");
end