-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlcLdlTrain.m
193 lines (173 loc) · 6.31 KB
/
lcLdlTrain.m
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
function [weights,fval,exitFlag,output,grad] = lcLdlTrain(xInit,trainFeatures,trainLabels,optim)
fprintf('Begin training of BFGS-LC. \n');
% Read Optimalisation Parameters
options = optimoptions(@fminunc,'Display','iter','Algorithm','quasi-newton','SpecifyObjectiveGradient',true);
[weights,fval,exitFlag,output,grad] = fminunc(@lcProgress,xInit,options);
function [target,gradient] = lcProgress(weights)
c1=0.1;
c2=c1*0.1;
[row,cow]=size(weights);
modProb = exp(trainFeatures * weights);
sumProb = sum(modProb, 2);
modProb = modProb ./ (repmat(sumProb,[1 size(modProb,2)]));
%%损失函数第一项
costfir=-sum(sum(trainLabels.*log(modProb)));
%%损失函数第二项
costsec=norm(weights,'fro')*norm(weights,'fro');
%%损失函数第三项 theta中的不同列
weightssize=size(weights,2);
% weights
relevance=0;
for i=1:weightssize-1
for j=i+1 :weightssize
distance =euclideandist(weights(:,i), weights(:,j));
s=corrcoef([weights(:,i), weights(:,j)]);
relevance=relevance+s(1,2)*distance;
end
end
% Target function.
target =costfir+c1*costsec+c2*relevance;
% The gradient.第一项是原始模型,第二项是向量F范数求和的形式,第三项是theta相关性;
grad1=trainFeatures'*(modProb - trainLabels);
grad2=0;
for i=1:row
for j=1:cow
grad2(i,j)=2*sign(weights(i,j));
end
end
% euclideandist
for i=1:row
for j=1:cow
temp=0;
for k=1:cow
s=corrcoef([weights(:,j), weights(:,k)]);
temp1=abs(weights(i,j)-weights(i,k));
temp2=sqrt(sum((weights(:,j)-weights(:,k)).^2));
temp=temp + sign(s(1,2))* temp1./(temp2+0.00001);
end
grad3(i,j)=temp;
end
end
% sorensendist距离
% for i=1:row
% for j=1:cow
% temp=0;newtemp=0;
% for k=1:cow
% s=corrcoef([weights(:,j), weights(:,k)]);
% temp1=sign(weights(i,j)-weights(i,k));
% temp2=sign(weights(i,j)+weights(i,k));
% temp=temp + temp1;
% newtemp=newtemp+temp2;
% res=sign(s(1,2))*temp/(newtemp+0.00001);
% end
% grad3(i,j)=res;
% end
% end
% squaredchord距离
% for i=1:row
% for j=1:cow
% temp=0;
% for k=1:cow
% s=corrcoef([weights(:,j), weights(:,k)]);
% temp1=2*weights(i,j)^2-2*weights(i,k)^2-(weights(i,j)^2-weights(i,k)^2);
% temp2=(weights(i,j)+weights(i,k))^2;
% temp=temp +sign(s(1,2))* temp1./(temp2+0.00001);
% end
% grad3(i,j)=temp;
% end
% end
% kdlsist距离
% for i=1:row
% for j=1:cow
% temp=0;
% temp2=abs(weights(i,j));
% for k=1:cow
% s=corrcoef([weights(:,j), weights(:,k)]);
% temp1=-weights(i,k);
% temp=temp +sign(s(1,2))* temp1./(temp2+0.00001);
% end
% grad3(i,j)=temp;
% end
% end
% intersecion距离
% for i=1:row
% for j=1:cow
% temp=0;
% for k=1:cow
% s=corrcoef([weights(:,j), weights(:,k)]);
% if weights(i,j)<weights(i,k)
% temp=temp +sign(s(1,2))* 1;
% end
% end
% grad3(i,j)=temp;
% end
% end
% fldelity相似度偏导
% for i=1:row
% for j=1:cow
% temp=0;
% for k=1:cow
% %theat ik, ij可能乘机是负数,这里改为绝对值
% s=corrcoef([weights(:,j), weights(:,k)]);
% temp1=sqrt(abs(weights(i,k)));
% temp2=2*sqrt(abs(weights(i,j)));
% temp=temp + sign(s(1,2))* temp1./(temp2+0.00001);
% end
% grad3(i,j)=temp;
% end
% end
% clark距离偏导
% for i=1:row
% for j=1:cow
% temp=0;
% for k=1:cow
% s=corrcoef([weights(:,j), weights(:,k)]);
% temp1=weights(i,k)^2-weights(i,j)^2-(weights(i,j)-weights(i,k))^2;
% temp2=(weights(i,j)-weights(i,k))^2*abs(weights(i,k)-weights(i,j));
% temp=temp +sign(s(1,2))* temp1./(temp2+0.00001);
% end
% grad3(i,j)=temp;
% end
% end
% innerproduct距离偏导
% for i=1:row
% for j=1:cow
% temp=0;
% for k=1:cow
% %theat ik
% s=corrcoef([weights(:,j), weights(:,k)]);
% temp=temp + sign(s(1,2))* weights(i,k);
% end
% grad3(i,j)=temp;
% end
% end
% cosine距离
% for i=1:row
% for j=1:cow
% temp=0;
% for k=1:cow
% s=corrcoef([weights(:,j), weights(:,k)]);
% temp1=weights(i,k)*(sqrt(sum(weights(:,j).^2))+sqrt(sum(weights(:,k).^2)));
% temp2=weights(i,j)*sum(weights(:,j).*weights(:,k))/sqrt(sum(weights(:,j).^2));
% temp3=sqrt(sum(weights(:,j).^2))+sqrt(sum(weights(:,k).^2));
% temp=temp + sign(s(1,2))* (temp1-temp2)./(temp3^2+0.00001);
% end
% grad3(i,j)=temp;
% end
% end
% canberra距离
% for i=1:row
% for j=1:cow
% temp=0;
% for k=1:cow
% temp1=sign(weights(i,j))*(abs(weights(i,j))+abs(weights(i,k)));
% temp2=sign(weights(i,j))*(abs(weights(i,j)-weights(i,k)));
% temp3=(abs(weights(i,j))+abs(weights(i,k)))^2;
% temp=temp + (temp1-temp2)./(temp3+0.00001);
% end
% grad3(i,j)=temp;
% end
% end
gradient =grad1+c1.*grad2+c2.*grad3;
end
end