You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

fann_train.pl 5.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. #!/usr/bin/env perl
  2. # This script is a very simple prototype to learn fann from rspamd logs
  3. # For now, it is intended for internal use only
  4. use strict;
  5. use warnings FATAL => 'all';
  6. use AI::FANN qw(:all);
  7. use Getopt::Std;
  8. my %sym_idx; # Symbols by index
  9. my %sym_names; # Symbols by name
  10. my $num = 1; # Number of symbols
  11. my @spam;
  12. my @ham;
  13. my $max_samples = -1;
  14. my $split = 1;
  15. my $preprocessed = 0; # ouptut is in format <score>:<0|1>:<SYM1,...SYMN>
  16. my $score_spam = 12;
  17. my $score_ham = -6;
  18. sub process {
  19. my ($input, $spam, $ham) = @_;
  20. my $samples = 0;
  21. while(<$input>) {
  22. if (!$preprocessed) {
  23. if (/^.*rspamd_task_write_log.*: \[(-?\d+\.?\d*)\/(\d+\.?\d*)\]\s*\[(.+)\].*$/) {
  24. if ($1 > $score_spam) {
  25. $_ = "$1:1: $3";
  26. }
  27. elsif ($1 < $score_ham) {
  28. $_ = "$1:0: $3\n";
  29. }
  30. else {
  31. # Out of boundary
  32. next;
  33. }
  34. }
  35. else {
  36. # Not our log message
  37. next;
  38. }
  39. }
  40. $_ =~ /^(-?\d+\.?\d*):([01]):\s*(\S.*)$/;
  41. my $is_spam = 0;
  42. if ($2 == 1) {
  43. $is_spam = 1;
  44. }
  45. my @ar = split /,/, $3;
  46. my %sample;
  47. foreach my $sym (@ar) {
  48. chomp $sym;
  49. if (!$sym_idx{$sym}) {
  50. $sym_idx{$sym} = $num;
  51. $sym_names{$num} = $sym;
  52. $num++;
  53. }
  54. $sample{$sym_idx{$sym}} = 1;
  55. }
  56. if ($is_spam) {
  57. push @{$spam}, \%sample;
  58. }
  59. else {
  60. push @{$ham}, \%sample;
  61. }
  62. $samples++;
  63. if ($max_samples > 0 && $samples > $max_samples) {
  64. return;
  65. }
  66. }
  67. }
  68. # Shuffle array
  69. sub fisher_yates_shuffle
  70. {
  71. my $array = shift;
  72. my $i = @$array;
  73. while ( --$i ) {
  74. my $j = int rand( $i + 1 );
  75. @$array[$i, $j] = @$array[$j, $i];
  76. }
  77. }
  78. # Train network
  79. sub train {
  80. my ($ann, $sample, $result) = @_;
  81. my @row;
  82. for (my $i = 1; $i < $num; $i++) {
  83. if ($sample->{$i}) {
  84. push @row, 1;
  85. }
  86. else {
  87. push @row, 0;
  88. }
  89. }
  90. #print "@row -> @{$result}\n";
  91. $ann->train(\@row, \@{$result});
  92. }
  93. sub test {
  94. my ($ann, $sample) = @_;
  95. my @row;
  96. for (my $i = 1; $i < $num; $i++) {
  97. if ($sample->{$i}) {
  98. push @row, 1;
  99. }
  100. else {
  101. push @row, 0;
  102. }
  103. }
  104. my $ret = $ann->run(\@row);
  105. return $ret;
  106. }
  107. my %opts;
  108. getopts('o:i:s:n:t:hpS:H:', \%opts);
  109. if ($opts{'h'}) {
  110. print "$0 [-i input] [-o output] [-s scores] [-n max_samples] [-S spam_score] [-H ham_score] [-ph]\n";
  111. exit;
  112. }
  113. my $input = *STDIN;
  114. if ($opts{'i'}) {
  115. open($input, '<', $opts{'i'}) or die "cannot open $opts{i}";
  116. }
  117. if ($opts{'n'}) {
  118. $max_samples = $opts{'n'};
  119. }
  120. if ($opts{'t'}) {
  121. # Test split
  122. $split = $opts{'t'};
  123. }
  124. if ($opts{'p'}) {
  125. $preprocessed = 1;
  126. }
  127. if ($opts{'H'}) {
  128. $score_ham = $opts{'H'};
  129. }
  130. if ($opts{'S'}) {
  131. $score_spam = $opts{'S'};
  132. }
  133. # ham_prob, spam_prob
  134. my @spam_out = (1);
  135. my @ham_out = (0);
  136. process($input, \@spam, \@ham);
  137. fisher_yates_shuffle(\@spam);
  138. fisher_yates_shuffle(\@ham);
  139. my $nspam = int(scalar(@spam) / $split);
  140. my $nham = int(scalar(@ham) / $split);
  141. my $ann = AI::FANN->new_standard($num - 1, ($num + 2) / 2, 1);
  142. my @train_data;
  143. # Train ANN
  144. for (my $i = 0; $i < $nham; $i++) {
  145. push @train_data, [ $ham[$i], \@ham_out ];
  146. }
  147. for (my $i = 0; $i < $nspam; $i++) {
  148. push @train_data, [ $spam[$i], \@spam_out ];
  149. }
  150. fisher_yates_shuffle(\@train_data);
  151. foreach my $train_row (@train_data) {
  152. train($ann, @{$train_row}[0], @{$train_row}[1]);
  153. }
  154. print "Trained $nspam SPAM and $nham HAM samples\n";
  155. # Now run fann
  156. if ($split > 1) {
  157. my $sample = 0.0;
  158. my $correct = 0.0;
  159. for (my $i = $nham; $i < $nham * $split; $i++) {
  160. my $ret = test($ann, $ham[$i]);
  161. #print "@{$ret}\n";
  162. if (@{$ret}[0] < 0.5) {
  163. $correct++;
  164. }
  165. $sample++;
  166. }
  167. print "Tested $sample HAM samples, correct matched: $correct, rate: ".($correct / $sample)."\n";
  168. $sample = 0.0;
  169. $correct = 0.0;
  170. for (my $i = $nspam; $i < $nspam * $split; $i++) {
  171. my $ret = test($ann, $spam[$i]);
  172. #print "@{$ret}\n";
  173. if (@{$ret}[0] > 0.5) {
  174. $correct++;
  175. }
  176. $sample++;
  177. }
  178. print "Tested $sample SPAM samples, correct matched: $correct, rate: ".($correct / $sample)."\n";
  179. }
  180. if ($opts{'o'}) {
  181. $ann->save($opts{'o'}) or die "cannot save ann into $opts{o}";
  182. }
  183. if ($opts{'s'}) {
  184. open(my $scores, '>',
  185. $opts{'s'}) or die "cannot open score file $opts{'s'}";
  186. print $scores "{";
  187. for (my $i = 1; $i < $num; $i++) {
  188. my $n = $i - 1;
  189. if ($i != $num - 1) {
  190. print $scores "\"$sym_names{$i}\":$n,";
  191. }
  192. else {
  193. print $scores "\"$sym_names{$i}\":$n}\n";
  194. }
  195. }
  196. }