-
Notifications
You must be signed in to change notification settings - Fork 3
/
ann.pl
332 lines (298 loc) · 7.51 KB
/
ann.pl
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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
#!/usr/bin/perl
#parameters, parameters n, m, η α, an limit on E and maximum number of epochs
#the window size
$n=shift @ARGV;
#hidden node size
$m=shift @ARGV;
#output
$o=3;
#slide of the window
$slide=1;
#number of iteration, maximum number of epochs
$iter=shift @ARGV;
#threshold, an limit on E
$threshold=shift @ARGV;
#learning rate, η
$eta=shift @ARGV;
#momentum,α
$alpha=shift @ARGV;
#hidden layer construct
$hlayers=1;
$hidnodez=$m;
$pstatus=1;
for($c1=0;$c1<$hlayers;$c1++){
@HL[$c1]=$hidnodez;
}
#Making hashmap for normalized Hydrophobicity
$hydro=shift @ARGV;
open(H,$hyrdo);
while($h1=<H>){
chomp $h1;
$lhy++;
if($lhy==1){@aa=split(/\s+/,$tr1)};
if($lhy==2){@hval=split(/\s+/,$h1)};
}
#Making training Data hashmap
$train=shift @ARGV;
open(T,$train);
while($tr1=<T>){
chomp $tr1;
$try++;
if($try%3==1){$name=$tr1};
if($try%3==2){$TRAINING_DATA{$name}=$tr1};
if($try%3==0){$TRAINING_DATA_P{$name}=$tr1};
}
#Making Validation Data hashmap
$vali=shift @ARGV;
open(V,$vali);
while($vr1=<V>){
chomp $vr1;
$vry++;
if($vry%3==1){$name=$vr1};
if($vry%3==2){$VALIDATION_DATA{$name}=$vr1};
if($vry%3==0){$VALIDATION_DATA_P{$name}=$vr1};
}
#write training to file with output
open(FTR,">training");
foreach $test (keys %TRAINING_DATA) {
@temp1=split(//,$TRAINING_DATA{$test});
@temp2=split(//,$TRAINING_DATA_P{$test});
for($c=0;$c<=($#temp1-$n);$c+=$slide){
for($c2=$c;$c2<($n+$c);$c2++){
print FTR"$hvalaa{@temp1[$c2]}\t";
}
if(lc(@temp2[($c+$n-1)/2]) eq "e" or lc(@temp2[($c+$n-1)/2]) eq "s"){
print FTR"0.1\t0.9\t0.1\n";
}
if(lc(@temp2[($c+$n-1)/2]) eq "c" or lc(@temp2[($c+$n-1)/2]) eq "u"){
print FTR"0.1\t0.1\t0.9\n";
}
if(lc(@temp2[($c+$n-1)/2]) eq "h"){
print FTR"0.9\t0.1\t0.1\n";
}
else{
print FTR"0.1\t0.1\t0.9\n";
}
}
print "$test=>$TRAINING_DATA{$test}\n";
}
#write validation data to file with output
open(FTV,">validation");
foreach $test (keys %VALIDATION_DATA) {
@temp1=split(//,$VALIDATION_DATA{$test});
@temp2=split(//,$VALIDATION_DATA_P{$test});
for($c=0;$c<=($#temp1-$n);$c+=$slide){
for($c2=$c;$c2<($n+$c);$c2++){
print FTV"$hvalaa{@temp1[$c2]}\t";
}
if(lc(@temp2[($c+$n-1)/2]) eq "e" or lc(@temp2[($c+$n-1)/2]) eq "s"){
print FTV"0.1\t0.9\t0.1\n";
}
if(lc(@temp2[($c+$n-1)/2]) eq "c" or lc(@temp2[($c+$n-1)/2]) eq "u"){
print FTV"0.1\t0.1\t0.9\n";
}
if(lc(@temp2[($c+$n-1)/2]) eq "h"){
print FTV"0.9\t0.1\t0.1\n";
}
else{
print FTV"0.1\t0.1\t0.9\n";
}
}
print "$test=>$VALIDATION_DATA{$test}\n";
}
$f="training";
$ftr=$n;
$features=$ftr;
$c3=READDATA();
$samples=$row;
$class=$o;
INITIALIZE();
LEARNING();
print "SSE after iteration-$iterno:$squareerror [$invsqerr]\t";
MISCLASSIFICATION();
#read the input data
sub READDATA {
if($ftr != "") {
open(F,$f);
$foo=$f.".out";
open(FO,">$foo");
$c1=0;$rowno=0;
while($l1=<F>){
chomp $l1;
@t1=split(/\t/,$l1);
for($c2=0;$c2<$ftr;$c2++){
$data[$c1][$c2]=@t1[$c2]+0;
}
for($c3=$ftr;$c3<=$#t1;$c3++){
$c5=$c3-$ftr;
$target[$c1][$c5]=@t1[$c3]+0;
if(@t1[$c3]==0.9){
$label=$c3-$ftr+1;
}
print FO"$target[$c1][$c5]\t";
}
print FO"\n";
$row=$c1+1;
$labhash{$row}=$label;
$clsno{$label}++;
push(@lab,$label);
$c1++;$label=0;
}
close F;
close FO;
}
else{die "Number of Feature ($ftr) is vague";}
return $c3;
}
#initialise the network
sub INITIALIZE{
for($c1=0;$c1<$hlayers;$c1++){
$hidnodez=@HL[$c1];
if($c1==0){
for($c2=0;$c2<$features;$c2++){
for($c3=0;$c3<$hidnodez;$c3++){
$weight12[$c2][$c3] = (rand(1)-0.5);#$t++;
#print "$t\t$weight12[$c2][$c3]\t";
}
}
}
if(($hlayers-$c1)==1){
for($c2=0;$c2<$class;$c2++){
for($c3=0;$c3<$hidnodez;$c3++){
$weight23[$c2][$c3] = (rand(1)-0.5);#$t++;
#print "$t\t$weight12[$c2][$c3]\t";
}
}
}
}
}
#perceptron
sub LEARNING{$iterno=0;$invsqerr=$class*$samples;
while(($iterno < $iter) and ( $invsqerr > $threshold )){
$squareerror =0;
for( $datarow = 0; $datarow < $samples; $datarow++){
FORWARD($datarow);
for($c1 = 0; $c1 < $class; $c1++){
$squareerror+=(@output3[$c1]-$target[$datarow][$c1])**2;
}
}
if($iterno%($pstatus)==0){
$iterationo=$iterno+1;
print "SSE after iteration - $iterationo:\t$squareerror [$invsqerr]\t";
MISCLASSIFICATION();
}
for( $datarow = 0; $datarow < $samples; $datarow++){
FORWARD($datarow);
for( $k = 0; $k < $hidnodez; $k++){
@delta2[$k] = 0;
}
for( $i = 0; $i < $class; $i++){
@error[$i]=@output3[$i]-$target[$datarow][$i];
#@error[$i]=(@output3[$i]-$target[$datarow][$i])/$target[$datarow][$i];
}
for( $i = 0; $i < $class; $i++){
for( $k = 0; $k < $hidnodez; $k++){
$weight23[$i][$k]-=($eta*@error[$i]*@output2[$k]*@output3[$i]*(1-@output3[$i]));
}
}
for($k = 0; $k < $hidnodez; $k++){
for( $i = 0; $i < $class; $i++){
@delta2[$k]+=(@error[$i]*$weight23[$i][$k]*@output3[$i]*(1-@output3[$i]));
}
}
for($i=0; $i<$features; $i++){
for($j=0;$j<$hidnodez;$j++){
$weight12[$i][$j]-=($alpha*$eta*@delta2[$j]*$data[$datarow][$i]*(1-@output2[$j])*@output2[$j]);
}
}
}
$iterno++;
$invsqerr=$squareerror/($samples*$class);
}
}
#feed forward
sub FORWARD{
$dataNo=shift;
for ($kf = 0; $kf < $hidnodez; $kf++){
@output2[$kf] = CALOP1($kf,$dataNo);
}
for( $if = 0; $if < $class; $if++){
@output3[$if] = CALOP2($if);
}
}
#weight from input to hidden
sub CALOP1{
$hiddenUnitID=shift;
$datarow=shift;
$temp1=0;
for($ic1=0;$ic1<$features;$ic1++){
$temp1+=$weight12[$ic1][$hiddenUnitID]*$data[$datarow][$ic1];#/*atten[n];*/
}
$return1 = 1/(1 + exp(-($temp1)));
return($return1);
}
#weight from hidden to output
sub CALOP2{
$classID=shift;$temp2=0;
for( $kc2 = 0; $kc2 < $hidnodez ; $kc2++){
$temp2+=$weight23[$classID][$kc2]*@output2[$kc2];
}
$return2 = 1/(1 + exp(-$temp2));
return($return2);
}
#calculate misclassification
sub MISCLASSIFICATION{
$file=shift;
if($file != ""){$f=$file;}
$filemisclass=$f.".mis.out";
open(FM,">$filemisclass");
$misclas=0;
for($r1=0; $r1 < $samples; $r1++){
FORWARD($r1);
for($ims=0; $ims< $class; $ims++){
print FM"$output3[$ims]\t";
if($output3[$ims]>$max){
$max = $output3[$ims];
$label = $ims;
}
}
print FM"\n";
if($target[$r1][$label]!=0.9){$misclas++;}
$max =0; $label =0;
}
printf "MISCLASSIFICATION-$misclas\n";
close FM;
}
#test the validation set
TEST("validation");
sub TEST
{
print "Testing validation data...";
$f=shift;
$choice=$f;
$fileout=$f.".out";
open(FO,">$fileout");
open(FT,$choice);
while($l1=<FT>){
@t1=split(/\t/,$l1);
for($c2=0;$c2<$ftr;$c2++){
$data[$c1][$c2]=@t1[$c2]+0;
}
for($c3=$ftr;$c3<=$#t1;$c3++){
$c5=$c3-$ftr;
$target[$c1][$c5]=@t1[$c3]+0;
if(@t1[$c3]==0.9){$label=$c3-$ftr+1;}
print FO"$target[$c1][$c5]\t";
}
print FO"\n";
$row=$c1+1;
$labhash{$row}=$label;
$clsno{$label}++;
push(@lab,$label);
$c1++;$label=0;
}
close FT;
$samples=$row;
print "Misclassification on test data $f- \n";
MISCLASSIFICATION($f);
}