MBDyn-1.7.3
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups
ann_in.c
Go to the documentation of this file.
1 /* $Header: /var/cvs/mbdyn/mbdyn/mbdyn-1.0/utils/ann_in.c,v 1.12 2017/01/12 15:10:27 masarati Exp $ */
2 /*
3  * MBDyn (C) is a multibody analysis code.
4  * http://www.mbdyn.org
5  *
6  * Copyright (C) 1996-2017
7  *
8  * Pierangelo Masarati <masarati@aero.polimi.it>
9  * Paolo Mantegazza <mantegazza@aero.polimi.it>
10  *
11  * Dipartimento di Ingegneria Aerospaziale - Politecnico di Milano
12  * via La Masa, 34 - 20156 Milano, Italy
13  * http://www.aero.polimi.it
14  *
15  * Changing this copyright notice is forbidden.
16  *
17  * This program is free software; you can redistribute it and/or modify
18  * it under the terms of the GNU General Public License as published by
19  * the Free Software Foundation (version 2 of the License).
20  *
21  *
22  * This program is distributed in the hope that it will be useful,
23  * but WITHOUT ANY WARRANTY; without even the implied warranty of
24  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25  * GNU General Public License for more details.
26  *
27  * You should have received a copy of the GNU General Public License
28  * along with this program; if not, write to the Free Software
29  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
30  */
31 /*
32  * Copyright (C) 2008
33  *
34  * Mattia Mattaboni <mattaboni@aero.polimi.it>
35  */
36 
37 #include <stdio.h>
38 #include <stdlib.h>
39 #include <math.h>
40 #include <getopt.h>
41 #include <string.h>
42 #include <time.h>
43 
44 #include "ann.h"
45 
46 struct option options[] = {
47  { "usage", 0, 0, 'u' },
48  { "mode", 1, 0, 'm' },
49  { "mean", 1, 0, 'M' },
50  { "variance", 1, 0, 'V' },
51  { "min", 1, 0, 'N' },
52  { "max", 1, 0, 'X' },
53  { "minW", 1, 0, 'w' },
54  { "maxW", 1, 0, 'W' },
55  { "file", 1, 0, 'f' },
56  { "fileINPUT", 1, 0, 'i' },
57  { "fileOUTPUT", 1, 0, 'o' }
58 };
59 void print_usage( void ){
60 
61  fprintf( stdout, "\nUSAGE OPTIONS:\n"
62  " -u, --usage\n"
63  " print usage\n"
64  " -f, --file\n"
65  " ann initialized file name (default data/ann.dat)\n"
66  " -i, --fileINPUT\n"
67  " input file name (default data/Input.dat)\n"
68  " -o, --fileOUTPUT\n"
69  " desired output file name (defalut/DOutput.dat)\n"
70  " -m, --mode\n"
71  " scaling input/output mode: 1 mean-variance (default)\n"
72  " 2 min-max\n"
73  " 0 no scaling\n"
74  " -M, --mean\n"
75  " mean value (defaul 0.)\n"
76  " -V, --variance"
77  " variance value (defaul 1.)\n"
78  " -N, --min\n"
79  " minimum value (defaul -1.)\n"
80  " -X, --max\n"
81  " maximum value (defaul 1.)\n"
82  " -w, --minW\n"
83  " random initialization minimum weight value (defaul -0.001)\n"
84  " -W, --maxW\n"
85  " random initialization maximum weight value (defaul 0.001)\n"
86  );
87  exit(0);
88 
89 }
90 
91 
92 int MODE = 1;
93 float MEAN = 0.;
94 float VARIANCE = 1.;
95 float MIN = -1.;
96 float MAX = 1.;
97 float minW = -0.001;
98 float maxW = 0.001;
99 static char *fileINPUT = "data/Input.dat";
100 static char *fileOUTPUT = "data/DOutput.dat";
101 static char *file = "data/ann.dat";
102 
103 int main( int argc, char **argv ){
104 
105  int opt;
106  extern char *optarg;
107  matrix INPUT, OUTPUT, SF_INPUT, SF_OUTPUT , MAT;
108  double mean, var, min, max;
109  int N_input, N_output, N_layer, r, N_sample;
110  int *N_neuron = NULL;
111  double doub;
112  int in;
113  unsigned i;
114  FILE * fh;
115 
116  /* 0. Training options */
117  do {
118  opt = getopt_long( argc, argv, "uf:i:o:m:M:V:N:X:w:W:", options, NULL );
119  switch( opt ) {
120  case 'u': print_usage();
121  break;
122  case 'f': file = strdup( optarg );
123  break;
124  case 'i': fileINPUT = strdup( optarg );
125  break;
126  case 'o': fileOUTPUT = strdup( optarg );
127  break;
128  case 'm': MODE = atoi( optarg );
129  break;
130  case 'M': MEAN = atof( optarg );
131  break;
132  case 'V': VARIANCE = atof( optarg );
133  break;
134  case 'N': MIN = atof( optarg );
135  break;
136  case 'X': MAX = atof( optarg );
137  break;
138  case 'w': minW = atof( optarg );
139  break;
140  case 'W': maxW = atof( optarg );
141  break;
142  default: break;
143  }
144  } while (opt >= 0);
145 
146  /* initialize random seed: */
147  srand ( time(NULL) );
148 
149  /* inizailizzazione del file contenente le informazioni sulla rete neurale */
150  fprintf( stdout, "ARTIFICIAL NEURAL NETWORK INITIALIZATION....\n" );
151  fprintf( stdout, "Output save in %s\n\n", file );
152  fh = fopen( file, "w" );
153 
154  fprintf( stdout, "*** Network architecture ***\n" );
155  fprintf( stdout, "\tNetwork inputs' number?\n" );
156  fscanf( stdin, "%d", &N_input );
157  if( N_input <= 0 ){
158  fprintf( stderr, "ERROR: Input number must be greater than zero.\n");
159  goto exit_error;
160  }
161  fprintf( fh, "%d\n", N_input );
162 
163  fprintf( stdout, "\tNetwork outputs' number?\n" );
164  fscanf( stdin, "%d", &N_output );
165  if( N_output <= 0 ){
166  fprintf( stderr, "ERROR: Output number must be greater than zero.\n");
167  goto exit_error;
168  }
169  fprintf( fh, "%d\n", N_output );
170 
171  fprintf( stdout, "\tHidden layers' number?\n" );
172  fscanf( stdin, "%d", &N_layer );
173  if( N_layer < 0 ){
174  fprintf( stderr, "ERROR: Hidden layer number must be not negative.\n");
175  goto exit_error;
176  }
177  fprintf( fh, "%d\n", N_layer );
178 
179  fprintf( stdout, "\tTime step delay's number?\n" );
180  fscanf( stdin, "%d", &r );
181  if( r < 0 ){
182  fprintf( stderr, "ERROR Timestep delay number must be not negative.\n");
183  goto exit_error;
184  }
185 
186  fprintf( fh, "%d\n", r );
187  N_neuron = (int *)malloc( (N_layer+2) * sizeof(int) );
188  if (N_neuron==NULL) {
189  return 1;
190  }
191  N_neuron[0] = 0;
192 
193  for( i=0; i<N_layer; i++ ){
194  fprintf( stdout, "\tNeuron number at %d hidden layer number?\n", i+1 );
195  fscanf( stdin, "%d", &in );
196  N_neuron[i+1] = in;
197  if( in <= 0 ){
198  fprintf( stderr, "ERROR: Neuron number at %d hidden layer must be greater than zero.\n",i+1);
199  goto exit_error;
200  }
201  fprintf( fh, "%d ", in );
202  }
203  fprintf( stdout, "\tNeuron number at the visible layer number?\n" );
204  fscanf( stdin, "%d", &in );
205  N_neuron[N_layer+1] = in;
206  if( in < N_output ){
207  fprintf( stderr, "ERROR: Neuron number at the visible layer must be not lesser than output number.\n");
208  goto exit_error;
209  }
210  fprintf( fh, "%d ", in );
211  N_neuron[0] = N_input + ( r * N_neuron[N_layer+1] );
212 
213  fprintf( fh, " \n\n\n" );
214 
215  fprintf( stdout, "*** Activation function definition***\n" );
216  fprintf( stdout, " 1: Hyperbolic tangent [ y = a*tanh(b*x) ]\n" );
217  fprintf( stdout, " 2: Linear function [ y = mx +q ]\n" );
218  fprintf( stdout, "\tChoose activation function [ 1 2 ]\n" );
219  fscanf( stdin, "%d", &in );
220  fprintf( fh, "%d\n", in );
221  fprintf( stdout, "\tActivation function parameters?\n" );
222  fscanf( stdin, "%lf", &doub );
223  fprintf( fh, "\n%e ", doub );
224  fscanf( stdin, "%lf", &doub );
225  fprintf( fh, " %e\n", doub );
226 
227  fprintf( fh, " \n\n\n" );
228 
229  fprintf( stdout, "*** Training parameters***\n" );
230  fprintf( stdout, "\tLearning rate?\n" );
231  fscanf( stdin, "%lf", &doub );
232  if( doub < 0 ){
233  fprintf( stderr, "ERROR: Learning rate must be not negative.\n");
234  goto exit_error;
235  }
236  fprintf( fh, "%e\n", doub );
237  fprintf( stdout, "\nMomentum term?\n" );
238  fscanf( stdin, "%lf", &doub );
239  fprintf( fh, "%e\n", doub );
240 
241  fprintf( fh, " \n\n\n" );
242 
243  fprintf( stdout, "*** Random weight matrices initialization ( Wmin = %f; Wmax = %f )***\n", minW, maxW );
244  for( i=0; i<N_layer+1; i++){
245  matrix_init( &MAT, N_neuron[i], N_neuron[i+1] );
246  matrix_random( &MAT, minW, maxW );
247  matrix_write( &MAT, fh, W_M_BIN );
248  matrix_destroy( &MAT );
249  }
250 
251  fprintf( stdout, "*** Input/Output Scale factor computing ***\n" );
252  N_sample = 0;
253  if( ANN_DataRead( &INPUT, &N_sample, fileINPUT ) ){
254  fprintf( stderr, "ERRRO: Error in Input data acquisition\n");
255  goto exit_error;
256  }
257  if( ANN_DataRead( &OUTPUT, &N_sample, fileOUTPUT ) ){
258  fprintf( stderr, "ERROR: Error in Input data acquisition\n");
259  goto exit_error;
260  }
261  matrix_init( &SF_INPUT, INPUT.Ncolumn, 2 );
262  matrix_init( &SF_OUTPUT, OUTPUT.Ncolumn, 2 );
263  for( i=0; i<N_input; i++ ){
264  SF_INPUT.mat[i][0] = 1.0;
265  }
266  for( i=0; i<N_output; i++ ){
267  SF_OUTPUT.mat[i][0] = 1.0;
268  }
269  if( MODE == 1 ){
270  for( i=0; i<INPUT.Ncolumn; i++ ){
271  mean = mean_value( &INPUT, i );
272  var = variance( &INPUT, i );
273  fprintf( stdout, "Input number %d (mean value = %e, variance = %e), do you want ot scale it? [1 = yes, 0 = no]\n", i+1, mean, var );
274  fscanf( stdin, "%d", &in);
275  if( in == 1){
276  SF_INPUT.mat[i][0] = sqrt( VARIANCE/var );
277  SF_INPUT.mat[i][1] = MEAN - SF_INPUT.mat[i][0]*mean;
278  }
279  }
280  for( i=0; i<OUTPUT.Ncolumn; i++ ){
281  mean = mean_value( &OUTPUT, i );
282  var = variance( &OUTPUT, i );
283  SF_OUTPUT.mat[i][0] = sqrt( VARIANCE/var );
284  SF_OUTPUT.mat[i][1] = MEAN - SF_OUTPUT.mat[i][0]*mean;
285  }
286  }
287  if( MODE == 2 ){
288  for( i=0; i<INPUT.Ncolumn; i++ ){
289  min = minimum( &INPUT, i );
290  max = maximum( &INPUT, i );
291  fprintf( stdout, "Input number %d (min value = %e, max = %e), do you want ot scale it? [1 = yes, 0 = no]\n", i+1, min, max );
292  fscanf( stdin, "%d", &in);
293  if( in == 1){
294  SF_INPUT.mat[i][0] = (MAX-MIN)/(max-min);
295  SF_INPUT.mat[i][1] = (max*MIN-MAX*min)/(max-min);
296  }
297  }
298  for( i=0; i<OUTPUT.Ncolumn; i++ ){
299  min = minimum( &OUTPUT, i );
300  max = maximum( &OUTPUT, i );
301  SF_OUTPUT.mat[i][0] = (MAX-MIN)/(max-min);
302  SF_OUTPUT.mat[i][1] = (max*MIN-MAX*min)/(max-min);
303  }
304  }
305 
306  matrix_write( &SF_INPUT, fh, W_M_BIN );
307  matrix_write( &SF_OUTPUT, fh, W_M_BIN );
308 
309  exit_error:
310  matrix_destroy(&INPUT);
311  matrix_destroy(&OUTPUT);
312  matrix_destroy(&SF_INPUT);
313  matrix_destroy(&SF_OUTPUT);
314  free(N_neuron);
315 
316 
317  fclose(fh);
318  return 0;
319 
320 }
mat_res_t matrix_random(matrix *MAT, double min, double max)
Definition: matrix.c:552
mat_res_t matrix_write(matrix *MAT, FILE *fh, unsigned flags)
Definition: matrix.c:467
double variance(matrix *MAT, int column)
Definition: matrix.c:598
static char * fileOUTPUT
Definition: ann_in.c:100
static char * fileINPUT
Definition: ann_in.c:99
unsigned Ncolumn
Definition: matrix.h:64
void print_usage(void)
Definition: ann_in.c:59
ann_res_t ANN_DataRead(matrix *MAT, int *N_sample, char *FileName)
Definition: ann.c:475
#define W_M_BIN
Definition: matrix.h:48
Definition: matrix.h:61
float MIN
Definition: ann_in.c:95
double mean_value(matrix *MAT, int column)
Definition: matrix.c:586
int MODE
Definition: ann_in.c:92
mat_res_t matrix_destroy(matrix *MAT)
Definition: matrix.c:84
static char * file
Definition: ann_in.c:101
float minW
Definition: ann_in.c:97
GradientExpression< UnaryExpr< FuncSqrt, Expr > > sqrt(const GradientExpression< Expr > &u)
Definition: gradient.h:2974
double maximum(matrix *MAT, int column)
Definition: matrix.c:612
float MAX
Definition: ann_in.c:96
struct option options[]
Definition: ann_in.c:46
double ** mat
Definition: matrix.h:62
char * optarg
Definition: getopt.c:74
float maxW
Definition: ann_in.c:98
float MEAN
Definition: ann_in.c:93
int main(int argc, char **argv)
Definition: ann_in.c:103
float VARIANCE
Definition: ann_in.c:94
mat_res_t matrix_init(matrix *MAT, unsigned Nrow, unsigned Ncolumn)
Definition: matrix.c:43
double minimum(matrix *MAT, int column)
Definition: matrix.c:628