tlearn 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -47,6 +47,8 @@ extern long rms_report; /* report error every report sweeps */
47
47
 
48
48
  extern float criterion; /* exit program when rms error < criterion */
49
49
 
50
+ extern int start = 1;
51
+
50
52
 
51
53
  float rans(w)
52
54
  float w;
@@ -109,7 +111,6 @@ print_output(aold)
109
111
  print_error(e)
110
112
  float *e;
111
113
  {
112
- static int start = 1;
113
114
  static FILE *fp;
114
115
 
115
116
  FILE *fopen();
@@ -76,6 +76,8 @@ extern int *outputs; /* (no) indices of output nodes */
76
76
  extern int *selects; /* (nn+1) nodes selected for probe printout */
77
77
  extern int *linput; /* (ni) localist input array */
78
78
 
79
+ extern float *otarget;
80
+
79
81
  extern float *znew; /* (nt) inputs and activations at time t+1 */
80
82
  extern float *zold; /* (nt) inputs and activations at time t */
81
83
  extern float *zmem; /* (nt) inputs and activations at time t */
@@ -107,6 +109,7 @@ extern int localist; /* flag for speed-up with localist inputs */
107
109
  extern int randomly; /* flag for presenting inputs in random order */
108
110
  extern int limits; /* flag for limited weights */
109
111
  extern int ce; /* flag for cross_entropy */
112
+ extern int start;
110
113
 
111
114
  extern char root[128]; /* root filename for .cf, .data, .teach, etc.*/
112
115
  extern char loadfile[128]; /* filename for weightfile to be read in */
@@ -152,6 +155,70 @@ int run_fitness(argc,argv, nsweeps, file_path, current_weights_output)
152
155
  return(status);
153
156
  }
154
157
 
158
+ void post_cleanup(){
159
+ free(outputs);
160
+ free(selects);
161
+ free(linput);
162
+
163
+ free(ninfo);
164
+ free(cinfo);
165
+
166
+ free(znew);
167
+ free(zold);
168
+ free(zmem);
169
+ free(wt);
170
+ free(dwt);
171
+ free(winc);
172
+ free(target);
173
+ free(error);
174
+ free(pnew);
175
+ free(pold);
176
+
177
+ free(otarget);
178
+ free(data);
179
+ }
180
+
181
+ void cleanup_horrid_globals(){
182
+ optind = 1;
183
+ sweep = 0;
184
+ tsweeps = 0;
185
+ rate = .1;
186
+ momentum = 0.;
187
+ weight_limit = 1.;
188
+ criterion = 0.;
189
+ init_bias = 0.;
190
+ rms_report = 0;
191
+ ngroups = 0;
192
+ teacher = 0;
193
+ localist = 0;
194
+ randomly = 0;
195
+ limits = 0;
196
+ ce = 0;
197
+ outputs = 0;
198
+ selects = 0;
199
+ linput = 0;
200
+ cinfo = 0;
201
+ ninfo = 0;
202
+ znew = 0;
203
+ zold = 0;
204
+ zmem = 0;
205
+ pnew = 0;
206
+ pold = 0;
207
+ wt = 0;
208
+ dwt = 0;
209
+ winc = 0;
210
+ target = 0;
211
+ error = 0;
212
+ cfp = 0;
213
+ data = 0;
214
+ ngroups = 0;
215
+ root[0] = 0;
216
+ loadfile[0] = 0;
217
+
218
+ otarget = 0;
219
+ start = 1;
220
+ }
221
+
155
222
  int run(argc,argv, nsweeps, file_path, backprop, current_weights_output)
156
223
  int argc;
157
224
  char **argv;
@@ -160,42 +227,8 @@ int run(argc,argv, nsweeps, file_path, backprop, current_weights_output)
160
227
  int backprop;
161
228
  float *current_weights_output;
162
229
  {
163
- //Reset EVERYTHING. Globals, such a great idea...
164
- optind = 1;
165
- sweep = 0;
166
- tsweeps = 0;
167
- rate = .1;
168
- momentum = 0.;
169
- weight_limit = 1.;
170
- criterion = 0.;
171
- init_bias = 0.;
172
- rms_report = 0;
173
- ngroups = 0;
174
- teacher = 0;
175
- localist = 0;
176
- randomly = 0;
177
- limits = 0;
178
- ce = 0;
179
- outputs = 0;
180
- selects = 0;
181
- linput = 0;
182
- cinfo = 0;
183
- ninfo = 0;
184
- znew = 0;
185
- zold = 0;
186
- zmem = 0;
187
- pnew = 0;
188
- pold = 0;
189
- wt = 0;
190
- dwt = 0;
191
- winc = 0;
192
- target = 0;
193
- error = 0;
194
- cfp = 0;
195
- data = 0;
196
- ngroups = 0;
197
- root[0] = 0;
198
- loadfile[0] = 0;
230
+
231
+ cleanup_horrid_globals();
199
232
 
200
233
  FILE *fopen();
201
234
  FILE *fpid;
@@ -479,8 +512,6 @@ int run(argc,argv, nsweeps, file_path, backprop, current_weights_output)
479
512
  for (i = 0; i < no; i++){
480
513
  current_weights_output[i] = zold[ni+outputs[i]];
481
514
  }
482
-
483
- //print_output(zold);
484
515
  }
485
516
  if (rms_report && (++rtime >= rms_report)){
486
517
  rtime = 0;
@@ -513,15 +544,6 @@ int run(argc,argv, nsweeps, file_path, backprop, current_weights_output)
513
544
 
514
545
  /* -- Ruby interface -- */
515
546
 
516
- int do_print(VALUE key, VALUE val, VALUE in) {
517
- fprintf(stderr, "Input data is %s\n", StringValueCStr(in));
518
-
519
- fprintf(stderr, "Key %s=>Value %s\n", StringValueCStr(key),
520
- StringValueCStr(val));
521
-
522
- return ST_CONTINUE;
523
- }
524
-
525
547
  static VALUE tlearn_train(VALUE self, VALUE config) {
526
548
  VALUE sweeps_value = rb_hash_aref(config, ID2SYM(rb_intern("sweeps")));
527
549
  long nsweeps = NUM2DBL(sweeps_value);
@@ -532,6 +554,9 @@ static VALUE tlearn_train(VALUE self, VALUE config) {
532
554
  float current_weights_output[6];
533
555
 
534
556
  int result = run_training(nsweeps, file_root, current_weights_output);
557
+
558
+ post_cleanup();
559
+
535
560
  return rb_int_new(result);
536
561
  }
537
562
 
@@ -561,6 +586,8 @@ static VALUE tlearn_fitness(VALUE self, VALUE config) {
561
586
 
562
587
  int failure = run_fitness(tlearn_args_count, tlearn_args, nsweeps, file_root, current_weights_output);
563
588
 
589
+ post_cleanup();
590
+
564
591
  if(failure == 0){
565
592
  float weight;
566
593
  int result_index;
@@ -50,6 +50,9 @@ extern int randomly; /* flag for presenting inputs in random order */
50
50
  extern int localist; /* flag for localist inputs */
51
51
  extern int limits; /* flag for limited weights */
52
52
 
53
+ extern float *otarget = 0; /* (no) back-up copy of target values */
54
+
55
+
53
56
  long dc = 0;
54
57
  int *ldata = 0;
55
58
 
@@ -246,8 +249,6 @@ update_targets(atarget,time,tick,flag,maxtime)
246
249
 
247
250
  static long next; /* next time tag in .teach file */
248
251
 
249
- static float *otarget = 0; /* (no) back-up copy of target values */
250
-
251
252
  static FILE *fp;
252
253
 
253
254
  char buf[128];
@@ -5,10 +5,14 @@ module TLearn
5
5
  NUMBER_OF_RESET_TIMEPOINTS = 3497
6
6
  DEFAULT_NUMBER_OF_SWEEPS = 1333000
7
7
 
8
- def initialize(config)
9
- @connections_config = config[:connections] || {}
10
- @special_config = config[:special] || {}
11
- @nodes_config = config[:nodes] || {}
8
+ def initialize(config, working_dir = nil)
9
+ @connections_config = config.delete(:connections) || {}
10
+ @config = config || {}
11
+ @working_dir = working_dir || WORKING_DIR
12
+ end
13
+
14
+ def working_dir
15
+ @working_dir
12
16
  end
13
17
 
14
18
  def setup_config(training_data)
@@ -30,21 +34,35 @@ module TLearn
30
34
  end
31
35
 
32
36
  def file_root
33
- "#{WORKING_DIR}/#{TLEARN_NAMESPACE}"
37
+ "#{working_dir}/#{TLEARN_NAMESPACE}"
34
38
  end
35
39
 
36
40
  private
37
41
 
38
42
  def connections_ranges_to_strings(connections_config)
39
- connections_config.map{|hash| {hash.keys[0].to_s.gsub('..','-') => hash.values[0]}}
43
+ connections_config.map{|hash| {input_to_config_string(hash.keys[0]) => input_to_config_string(hash.values[0])}}
44
+ end
45
+
46
+ def input_to_config_string(input)
47
+ if input.is_a?(Hash)
48
+ "#{input[:min]} & #{input[:max]}"
49
+ elsif input.is_a?(Range)
50
+ input.to_s.gsub('..','-')
51
+ elsif input.is_a?(Array)
52
+ values = input.map{|value| input_to_config_string(value)}
53
+ values[0] = values[0] + " ="
54
+ values.join(" ").gsub('one_to_one', 'one-to-one')
55
+ else
56
+ input
57
+ end
40
58
  end
41
59
 
42
60
  def evaulator_config(training_data)
43
61
  nodes_config = {
44
- :nodes => @nodes_config[:number_of_nodes],
62
+ :nodes => @config[:number_of_nodes],
45
63
  :inputs => training_data.no_of_inputs,
46
64
  :outputs => training_data.no_of_outputs,
47
- :output_nodes => @nodes_config[:output_nodes]
65
+ :output_nodes => input_to_config_string(@config[:output_nodes])
48
66
  }
49
67
 
50
68
  @connections_config = connections_ranges_to_strings(@connections_config)
@@ -53,9 +71,13 @@ module TLearn
53
71
  node_config_strings = nodes_config.map{|key,value| "#{key.to_s.gsub('_',' ')} = #{value}" }
54
72
  node_config_strings << "output nodes are #{output_nodes}"
55
73
 
56
- connection_config_strings = @connections_config.map{|mapping| "#{mapping.keys[0]} from #{mapping.values[0]}" }
74
+ connection_config_strings = @connections_config.map{|mapping| "#{mapping.keys[0]} from #{input_to_config_string(mapping.values[0])}" }
57
75
  connection_config_strings = ["groups = #{0}"] + connection_config_strings
58
-
76
+
77
+ special_config = {}
78
+ special_config[:linear] = @config[:linear]
79
+ special_config[:weight_limit] = @config[:weight_limit]
80
+ special_config[:selected] = @config[:selected]
59
81
 
60
82
  config = <<EOS
61
83
  NODES:
@@ -63,7 +85,7 @@ NODES:
63
85
  CONNECTIONS:
64
86
  #{connection_config_strings.join("\n")}
65
87
  SPECIAL:
66
- #{@special_config.map{|key,value| "#{key} = #{value}" }.join("\n")}
88
+ #{special_config.map{|key,value| "#{key} = #{input_to_config_string(value)}" }.join("\n")}
67
89
  EOS
68
90
  end
69
91
 
@@ -6,8 +6,8 @@ module TLearn
6
6
  @out = out
7
7
  end
8
8
 
9
- def train(data, number_of_sweeps = nil)
10
- run_tlearn = RunTLearn.new(@config)
9
+ def train(data, number_of_sweeps = nil, working_dir = nil)
10
+ run_tlearn = RunTLearn.new(@config, working_dir)
11
11
 
12
12
  results = run_tlearn.train(TrainingData.new(data), number_of_sweeps)
13
13
 
@@ -19,8 +19,8 @@ module TLearn
19
19
  end
20
20
  end
21
21
 
22
- def fitness(data, number_of_sweeps = nil)
23
- run_tlearn = RunTLearn.new(@config)
22
+ def fitness(data, number_of_sweeps = nil, working_dir = nil)
23
+ run_tlearn = RunTLearn.new(@config, working_dir)
24
24
 
25
25
  run_tlearn.fitness(FitnessData.new(data), number_of_sweeps)
26
26
  end
@@ -2,8 +2,8 @@ module TLearn
2
2
  class RunTLearn
3
3
  class UntrainedError < Exception; end;
4
4
 
5
- def initialize(config = {})
6
- @config = Config.new(config)
5
+ def initialize(config = {}, working_dir = nil)
6
+ @config = Config.new(config, working_dir)
7
7
  end
8
8
 
9
9
  def fitness(data, number_of_sweeps = @config.number_of_sweeps)
@@ -25,8 +25,8 @@ module TLearn
25
25
  execute_tlearn_train(number_of_sweeps)
26
26
 
27
27
  if training_successful?(number_of_sweeps)
28
- weights = File.read("#{Config::WORKING_DIR}/#{Config::TLEARN_NAMESPACE}.#{number_of_sweeps}.wts").split("\n")
29
- `cp #{Config::WORKING_DIR}/#{Config::TLEARN_NAMESPACE}.#{number_of_sweeps}.wts #{Config::WORKING_DIR}/#{Config::TLEARN_NAMESPACE}.wts`
28
+ weights = File.read("#{@config.working_dir}/#{Config::TLEARN_NAMESPACE}.#{number_of_sweeps}.wts").split("\n")
29
+ `cp #{@config.working_dir}/#{Config::TLEARN_NAMESPACE}.#{number_of_sweeps}.wts #{@config.working_dir}/#{Config::TLEARN_NAMESPACE}.wts`
30
30
  weights.map{|line| line.split("\t").map{|number| number.strip}}
31
31
  else
32
32
  false
@@ -36,7 +36,7 @@ module TLearn
36
36
  private
37
37
 
38
38
  def file_root
39
- "#{File.expand_path(Config::WORKING_DIR)}/#{Config::TLEARN_NAMESPACE}"
39
+ "#{File.expand_path(@config.working_dir)}/#{Config::TLEARN_NAMESPACE}"
40
40
  end
41
41
 
42
42
  def clear_previous_fitness_session
@@ -45,7 +45,7 @@ module TLearn
45
45
  end
46
46
 
47
47
  def clear_entire_training_data
48
- FileUtils.rm_f(Dir.glob("#{Config::WORKING_DIR}/*"))
48
+ FileUtils.rm_f(Dir.glob("#{@config.working_dir}/*"))
49
49
  end
50
50
 
51
51
  def training_successful?(number_of_sweeps)
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: tlearn
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-10-19 00:00:00.000000000 Z
12
+ date: 2012-10-26 00:00:00.000000000Z
13
13
  dependencies: []
14
14
  description:
15
15
  email:
@@ -57,8 +57,9 @@ required_rubygems_version: !ruby/object:Gem::Requirement
57
57
  version: '0'
58
58
  requirements: []
59
59
  rubyforge_project:
60
- rubygems_version: 1.8.24
60
+ rubygems_version: 1.8.6
61
61
  signing_key:
62
62
  specification_version: 3
63
- summary: ruby bindings for tlearn
63
+ summary: Ruby bindings for tlearn
64
64
  test_files: []
65
+ has_rdoc: