-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLinear_Network_Classfier_HW.m
More file actions
130 lines (87 loc) · 2.99 KB
/
Linear_Network_Classfier_HW.m
File metadata and controls
130 lines (87 loc) · 2.99 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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
%{
%% Script file: Online Training (Hebbian Learning)
% Example 1: T, G and F.
%% Load the Data and Graph the Results.
T1=[1 1 1 -1;-1 1 -1 -1;-1 1 -1 -1;-1 1 -1 -1];
T2=[-1 1 1 1;-1 -1 1 -1;-1 -1 1 -1;-1 -1 1 -1];
G1=[1 1 1 -1;1 -1 -1 -1; 1 1 1 -1 ; 1 1 1 -1];
G2=[-1 1 1 1;-1 1 -1 -1; -1 1 1 1; -1 1 1 1];
F1=[1 1 1 -1;1 1 -1 -1;1 -1 -1 -1;1 -1 -1 -1];
F2=[-1 1 1 1;-1 1 1 -1;-1 1 -1 -1;-1 1 -1 -1];
gg=colormap(gray); gg=gg(end:-1:1,:); %I'm reversing the usual grayscale values
X=[T1(:) T2(:) G1(:) G2(:) F1(:) F2(:)]; %X is 16 x 6
T=[1 1 0 0 0 0;0 0 1 1 0 0;0 0 0 0 1 1];
figure(1)
for j=1:6
subplot(2,3,j);
imagesc(reshape(X(:,j),4,4));
colormap(gg);
end
% Now that the data is ready, set things up and train:
%% Main code start
alpha=0.03;
NumPoints=6;
NumEpochs=60;
[W,b,EpochErr]=WidHoff(X,T,alpha,NumEpochs);
%% Output results
Z=W*X+b*ones(1,NumPoints);
figure(2)
plot(EpochErr);
[val1,idx1]=max(T,[],1); % Translate vectors into integer classes
[val2,idx2]=max(Z,[],1); % Same thing
[C,err1]=conf_matrix(idx1,idx2,3)
%Other one:
hatX=[X;ones(1,6)];
[U,S,V]=svd(hatX,'econ'); % Looks like its 6 dimensions.
P=V*diag(1./diag(S))*U';
hatW=T*P;
%% Output results
Z=hatW*hatX;
[val1,idx1]=max(T,[],1); % Translate vectors into integer classes
[val2,idx2]=max(Z,[],1); % Same thing
[C,err1]=conf_matrix(idx1,idx2,3)
%}
%Load Data
BreastData; % X is 106x9, T is 6x106
X=X'; %Function requires dim * points, X is 9x106
%1. Split the data into Training and Testing sets (70% for training, 30% for testing).
[Xtrain, Xtest, Ttrain, Ttest] = TrainTestSplit(X, T, 0.3);
%2. Do some data exploration. If you think you need to scale the data, you can use StandardScaler.m.
[Xtrain_scaled, Params] = StandardScaler(Xtrain, size(Xtrain, 1));
%X=(X-m)./s
Xtest_scaled = (Xtest - Params.m) ./ Params.s; %Scale test data too
%3. We’ll build two models- one using Widrow Hoff, and one using “batch” with the pseu- doinverse.
%Model 1: WidrowHoff
alpha=0.03;
NumPoints=106;
NumEpochs=60;
[W,b,EpochErr]=WidHoff(Xtrain_scaled,Ttrain,alpha,NumEpochs);
Z=W*Xtrain_scaled+b*ones(1,size(Xtrain_scaled, 2));
%figure(2)
%plot(EpochErr);
%Model 2: Batch
hatX=[Xtrain_scaled;ones(1,size(Xtrain_scaled, 2))];
[U,S,V]=svd(hatX,'econ'); %
P=V*diag(1./diag(S))*U';
hatW=Ttrain*P;
%% Output results
Z_B=hatW*hatX;
%4. At the end, please note the confusion matrix output and the overall error.
%Model 1:
Z_f = W*Xtest_scaled + b * ones(1,size(Xtest_scaled, 2));
[val1_1,idx1_1]=max(Ttest,[],1); % Translate vectors into integer classes
[val2_1,idx2_1]=max(Z_f,[],1); % Same thing
[C_1,err1]=conf_matrix(idx1_1,idx2_1, 6);
%Model 2
hatX_test = [Xtest_scaled;ones(1,size(Xtest_scaled, 2))];
z_bt = hatW*hatX_test;
[val1_2,idx1_2]=max(Ttest,[],1); % Translate vectors into integer classes
[val2_2,idx2_2]=max(z_bt,[],1); % Same thing
[C_2,err2]=conf_matrix(idx1_2,idx2_2,6);
disp('Model 1 Confusion Matrix & error:');
disp(C_1);
disp(err1);
disp('Model 2 Confusion Matrix & Error:');
disp(C_2);
disp(err2);