forked from nbatfai/amminadab
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
544 lines (466 loc) · 14.7 KB
/
main.cpp
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
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
/**
* @brief JUDAH - Jacob is equipped with a text-based user interface
*
* @file main.hpp
* @author Norbert Bátfai <[email protected]>
* @version 0.0.1
*
* @section LICENSE
*
* Copyright (C) 2015 Norbert Bátfai, [email protected]
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* @section DESCRIPTION
*
* JACOB, https://github.com/nbatfai/jacob
*
* "The son of Isaac is Jacob." The project called Jacob is an experiment
* to replace Isaac's (GUI based) visual imagination with a character console.
*
* ISAAC, https://github.com/nbatfai/isaac
*
* "The son of Samu is Isaac." The project called Isaac is a case study
* of using deep Q learning with neural networks for predicting the next
* sentence of a conversation.
*
* SAMU, https://github.com/nbatfai/samu
*
* The main purpose of this project is to allow the evaluation and
* verification of the results of the paper entitled "A disembodied
* developmental robotic agent called Samu Bátfai". It is our hope
* that Samu will be the ancestor of developmental robotics chatter
* bots that will be able to chat in natural language like humans do.
*
* [1] Norbert Bátfai, "A disembodied developmental robotic agent
* called Samu Bátfai" (submitted manuscript)
*/
#include <iostream>
#include <string>
#include <sstream>
#include <signal.h>
#include "samu.hpp"
Samu samu {"Amminadab", "samu.soul.txt"};
bool halted {false};
void save_samu ( int sig )
{
if ( halted )
return;
halted = true;
#ifndef Q_LOOKUP_TABLE
//std::string samuImage {"samu.soul.txt"};
//samu.save ( samuImage );
samu.save ();
#endif
samu.halt();
exit ( 0 );
}
double to_samu ( int channel, SPOTriplets &tv )
{
double r {0.0};
try
{
samu.triplet ( channel, tv );
r = samu.reward();
}
catch ( const char* err )
{
std::cerr << err << std::endl;
}
return r;
}
double to_samu ( int channel, std::string &msg )
{
double r {0.0};
try
{
samu.sentence ( channel, msg );
r = samu.reward();
}
catch ( const char* err )
{
std::cerr << err << std::endl;
}
return r;
}
double to_samu ( int channel, std::string &msg, std::string &key )
{
double r {0.0};
try
{
samu.sentence ( channel, msg, key );
r = samu.reward();
}
catch ( const char* err )
{
std::cerr << err << std::endl;
}
return r;
}
std::map<std::string, SPOTriplets> cache;
int samuHasAlreadyLearned {7};
int reinforcement {0};
double read_cache ( std::string & key, int &cnt, int &brel )
{
double sum {0.0};
int count {0};
for ( auto const & t: cache[key] )
{
if ( !samu.sleep() )
break;
if ( count++ >= samuHasAlreadyLearned )
break;
SPOTriplets tv;
tv.push_back ( t );
sum += to_samu ( 12, tv );
++cnt;
brel += samu.get_brel();
}
return sum;
}
int main ( int argc, char **argv )
{
/*
#ifndef Q_LOOKUP_TABLE
std::string samuImage {"samu.soul.txt"};
std::fstream samuFile ( samuImage, std::ios_base::in );
if ( samuFile )
samu.load ( samuFile );
#endif
*/
struct sigaction sa;
sa.sa_handler = save_samu;
sigemptyset ( &sa.sa_mask );
sa.sa_flags = SA_RESTART;
sigaction ( SIGINT, &sa, NULL );
sigaction ( SIGTERM, &sa, NULL );
sigaction ( SIGKILL, &sa, NULL );
sigaction ( SIGHUP, &sa, NULL );
// Do not remove this copyright notice!
std::cerr << "This program is "
<< samu.get_name()
<< ", the son of Samu Bátfai."
<< std::endl
<< "Copyright (C) 2015 Norbert Bátfai"
<< std::endl
<< "License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>"
<< std::endl
<< "This is free software: you are free to change and redistribute it."
<< std::endl
<< "There is NO WARRANTY, to the extent permitted by law."
<< std::endl
<< std::endl;
std::map<std::string, std::vector<std::string>> tests
{
{
"sentences of the paper [1]",
{
"A rare black squirrel has become a regular visitor to a suburban garden",
"This is a car",
"This car is mine",
"I have a little car",
"The sky is blue",
"The little brown bear has eaten all of the honey",
"I love Samu"
}
},
{
"introduce myself",
{
"Who are you",
"I am a robot",
"What is your name",
"My name is Judah",
"Where do you live",
"I live in Debrecen",
"How old are you",
"I am one year old",
"Where were you born"
"I was born is Debrecen"
"What is your favourite colour"
"My favourite colours are red, white and green"
}
}
};
std::map<std::string, SPOTriplets> test_triplets
{
{
"introduce myself",
{
{ "who", "are", "you" },
{ "i", "am", "robot" },
{"what", "is", "name"},
{"name", "is", "Judah"},
{"you", "live", "where"},
{"I", "live", "Debrecen"},
{"you", "are", "how"},
{"i", "am", "one"},
{"you", "born", "where"},
{"I", "born", "Debrecen"}
}
}
};
int j {0};
int N_e {20};
std::string training_file = samu.get_training_file();
std::string t_f;
std::cerr << "Please Enter training file" << std::endl;
std::cin >> t_f;
samu.set_training_file ( t_f );
double prev_mbrel {0};
int mbrelc {0};
int mbrelc2 {0};
int prev_bad {0};
#ifdef SUPER_OR_REMOTE_COMP
//for ( int ii {0}; samu.run() && ii < 1000 + 4000 + 5000 + 4000 + 1000; ++ii )
for ( int ii {0}; samu.run() /*&& ii < 50000*/; ++ii )
#else
for ( ; samu.run(); )
//for ( int ii {0}; samu.run() && ii < 50; ++ii )
#endif
{
auto start = std::chrono::high_resolution_clock::now();
double sum {0.0};
int cnt {0};
int brel {0};
if ( samu.sleep() )
{
#ifdef SUPER_OR_REMOTE_COMP
/*
if ( ii == 1 )
{
std::cerr << " iter, training file changed " << std::endl;
samu.set_training_file ( "bbe" );
}
else if ( ii == 3 )
{
std::cerr << " iter, training file changed " << std::endl;
training_file = "bbe";
}
*/
/*
if ( ii == 1000 )
{
std::cerr << " iter, training file changed " << std::endl;
samu.set_training_file ( "bbe" );
}
else if ( ii == 1000 + 4000 )
{
std::cerr << " iter, training file changed " << std::endl;
training_file = "none";
samu.set_training_file ( training_file );
}
else if ( ii == 1000 + 4000 + 5000 )
{
std::cerr << " iter, training file changed " << std::endl;
samu.set_training_file ( "bbe" );
}
else if ( ii == 1000 + 4000 + 5000 + 4000 )
{
std::cerr << " iter, training file changed " << std::endl;
training_file = "none";
samu.set_training_file ( training_file );
}
*/
#endif
samu.clear_vi();
if ( samu.get_training_file() == training_file )
{
samu.set_N_e ( N_e );
for ( int i {0}; i<test_triplets["introduce myself"].size() && samu.sleep(); ++i )
{
SPOTriplets tv;
tv.push_back ( test_triplets["introduce myself"][i] );
sum += to_samu ( 11, tv );
++cnt;
brel += samu.get_brel();
}
}
else
{
samu.set_N_e ( N_e );
std::string key = samu.get_training_file();
if ( cache.find ( key ) == cache.end() )
{
std::fstream triplet_train ( key+".triplets", std::ios_base::in );
if ( triplet_train )
{
do
{
SPOTriplet t;
triplet_train >> t;
if ( !t.empty() )
cache[key].push_back ( t );
}
while ( !triplet_train.eof() && samu.sleep() );
triplet_train.close();
sum = read_cache ( key, cnt, brel );
}
else
{
std::fstream train ( samu.get_training_file(), std::ios_base::in );
if ( train )
{
std::string file = key+".triplets";
for ( std::string line; std::getline ( train, line ) && samu.sleep(); )
{
std::cerr <<"Loading line: " << line << std::endl;
#ifndef TRIPLET_CACHE
sum += to_samu ( 12, line );
#else
sum += to_samu ( 12, line, file );
#endif
++cnt;
brel += samu.get_brel();
}
train.close();
}
}
}
else
{
sum = read_cache ( key, cnt, brel );
}
}
//std::cerr << "###### " << ++j << "-th iter " << sum << std::endl;
double mbrel = ( double ) brel/ ( double ) cnt;
int bad = ( sum - samu.get_max_reward() * cnt ) / ( samu.get_min_reward() - samu.get_max_reward() );
std::cerr << ++j
<< "-th iter, err: "
<< cnt*samu.get_max_reward() - sum
<< " ("
<< sum
<< ", good: "
<< ( cnt-bad )
<< ", bad: "
<< bad
<< "), brel%: "
<< mbrel
<< ", "
<< std::chrono::duration_cast<std::chrono::milliseconds> ( std::chrono::high_resolution_clock::now() - start ).count()
<< " ms "
<< N_e
<< std::endl;
/*
//if ( fabs ( prev_mbrel - mbrel ) < 1.2 )
if ( mbrel < 0.0 )
++mbrelc;
else
mbrelc = 0;
if ( fabs ( prev_mbrel - mbrel ) < 2.0 )
++mbrelc2;
else
mbrelc2 = 0;
*/
if ( fabs ( prev_mbrel - mbrel ) < 1.0 && prev_bad == bad )
++mbrelc;
else
mbrelc = 0;
prev_bad = bad;
/*
if ( fabs ( prev_mbrel - mbrel ) < .2 )
++mbrelc2;
else
mbrelc2 = 0;
*/
/*
if ( bad <= 3 )
{
if ( ++reinforcement == 20 )
{
samuHasAlreadyLearned += 7;
reinforcement = 0;
}
}
else if ( mbrelc2 >= 2*samuHasAlreadyLearned )
{
if ( N_e < samuHasAlreadyLearned )
N_e +=1;
samu.scale_N_e ( .75 );
mbrelc2 = 0;
std::cerr << "(mbrelc2) iter, N structure rescaled " << std::endl;
}
else if ( mbrelc > 2*samuHasAlreadyLearned && bad > 3 )
{
if ( N_e >samuHasAlreadyLearned )
N_e -=1;
samu.scale_N_e ( .82 );
//N_e += 5;
mbrelc = 0;
std::cerr << " iter, N structure rescaled " << std::endl;
//} else if(!bad)
//} else if(bad <cnt/10)
}
*/
if ( /*mbrel > 35.0 &&*/ mbrelc > 20 && bad >=2 )
{
samu.scale_N_e();
//N_e += 5;
mbrelc = 0;
std::cerr << " iter, N structure rescaled " << std::endl;
//} else if(!bad)
}
else if ( bad <2 )
{
if ( ++reinforcement == 10 )
{
samuHasAlreadyLearned += 7;
reinforcement = 0;
samu.debug_tree();
}
}
//if ( /*mbrel > 35.0 &&*/ mbrelc > 50 && cnt-bad <= cnt- ( cnt/10 ) )
/*if ( mbrelc > samuHasAlreadyLearned && bad >= 3 )
{
//samu.scale_N_e(.92);
//N_e += 1;
//samu.scale_N_e(.6);
//mbrelc = 0;
//std::cerr << " iter, N structure rescaled " << std::endl;
//} else if(!bad)
//} else if(bad <cnt/10)
}
else */
/*if ( mbrelc2 >= samuHasAlreadyLearned )
{
if(N_e < samuHasAlreadyLearned)
N_e +=1;
samu.scale_N_e(.67);
mbrelc2 = 0;
std::cerr << "(mbrelc2) iter, N structure rescaled " << std::endl;
}
else if ( bad < 3 )
{
if ( ++reinforcement == 10 )
{
samuHasAlreadyLearned += 7;
reinforcement = 0;
}
}
*/
prev_mbrel = mbrel;
}
else
sleep ( 1 );
}
/*
#ifndef Q_LOOKUP_TABLE
{
std::string samuImage {"samu.soul.txt"};
samu.save ( samuImage );
}
#endif
*/
return 0;
}