-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrainai.c
396 lines (371 loc) · 11.1 KB
/
trainai.c
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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <stdbool.h>
float weights0[5][6][256][512];//yes im not messing with jagged arrays
float neuronw0[5][7][512];
float calcN[5][8][512]; //the first layer is a value saving for normalization
float biasneur[5][6];
float biasweights[5][6][512];
float storage[32];
FILE *fp0[6];
FILE *fp;
void loadweights(){//loading all the weights into memory
printf("loading neurons, please wait");
long int x = 0;
int layers[]={32,32,32,64,128,256,512};
for(int z = 0; z<7;z++){//repeat per layer
for(int i = 0; i<layers[z];i++){//load neuron layer for file
for(int m = 0; m<5;m++){ //load neuron layer in all files
fseek(fp0[m],x*8,SEEK_SET);
fscanf(fp0[m],"%f",&neuronw0[m][z][i]);
}
x++;//keep count of where you need to read in the weight files
}
}
printf("\nloading weights, please wait");
for(int z = 0; z<6;z++){//load weights all layers
for(int i = 0; i<layers[z];i++){//load weights on a singular layer
for(int ii = 0; ii<layers[z+1];ii++){//load weights on a singular value
for(int m = 0; m<5;m++){//load weights per file
fseek(fp0[m],x*8,SEEK_SET);
fscanf(fp0[m],"%f",&weights0[m][z][i][ii]);
}
x++;//same here
}
}
}
for (int i = 0; i < 6; i++) {
for(int m = 0; m<5;m++){//load weights per file
fseek(fp0[m],x*8,SEEK_SET);
fscanf(fp0[m],"%f",&biasneur[m][i]);
}
x++;
}
for (int i = 0; i < 6; i++) {
for (int p = 0; p < layers[i+1]; p++) {
for(int m = 0; m<5;m++){//load weights per file
fseek(fp0[m],x*8,SEEK_SET);
fscanf(fp0[m],"%f",&biasweights[m][i][p]);
biasweights[m][i][p] = biasweights[m][i][p]/10;
}
x++;
}
}
printf("\nweights have been loaded");
}
void saveweights(){
fclose(fp0[0]);
fclose(fp0[1]);
fclose(fp0[2]);
fclose(fp0[3]);
fclose(fp0[4]);
fp0[0]=fopen("Aiweights0","w+");//opening the weight files
fp0[1]=fopen("Aiweights1","w+");
fp0[2]=fopen("Aiweights2","w+");
fp0[3]=fopen("Aiweights3","w+");
fp0[4]=fopen("Aiweights4","w+");
long int x = 0;
int layers[]={32,32,32,64,128,256,512};
for(int z = 0; z<7;z++){//repeat per layer
for(int i = 0; i<layers[z];i++){//load neuron layer for file
for(int m = 0; m<5;m++){ //load neuron layer in all files
fprintf(fp0[m],"%f",neuronw0[m][z][i]);
}
x++;//keep count of where you need to read in the weight files
}
}
for(int z = 0; z<6;z++){//load weights all layers
for(int i = 0; i<layers[z];i++){//load weights on a singular layer
for(int ii = 0; ii<layers[z+1];ii++){//load weights on a singular value
for(int m = 0; m<5;m++){//load weights per file
fprintf(fp0[m],"%f",weights0[m][z][i][ii]);
}
x++;//same here
}
}
}
for (int i = 0; i < 6; i++) {
for(int m = 0; m<5;m++){//load weights per file
fprintf(fp0[m],"%f",biasneur[m][i]);
}
x++;
}
for (int i = 0; i < 6; i++) {
for (int p = 0; p < layers[i+1]; p++) {
for(int m = 0; m<5;m++){//load weights per file
fprintf(fp0[m],"%f",biasweights[m][i][p]/10);
}
x++;
}
}
fclose(fp0[0]);
fclose(fp0[1]);
fclose(fp0[2]);
fclose(fp0[3]);
fclose(fp0[4]);
fp0[0]=fopen("Aiweights0","r+");//opening the weight files
fp0[1]=fopen("Aiweights1","r+");
fp0[2]=fopen("Aiweights2","r+");
fp0[3]=fopen("Aiweights3","r+");
fp0[4]=fopen("Aiweights4","r+");
}
void printarray(){
int layers[]={32,32,32,32,64,128,256,512};
for(int i = 0; i<8;i++){
for(int z=0;z<layers[i];z++){
printf("%.2f ",calcN[0][i][z]);
}printf("\n\n");}
}
float convertinput(char input){
float storage;
switch(input){
case 'a':storage=0.015f;break;
case 'b':storage=0.045f;break;
case 'c':storage=0.075f;break;
case 'd':storage=0.105f;break;
case 'e':storage=0.135f;break;
case 'f':storage=0.165f;break;
case 'g':storage=0.195f;break;
case 'h':storage=0.225f;break;
case 'i':storage=0.255f;break;
case 'j':storage=0.285f;break;
case 'k':storage=0.315f;break;
case 'l':storage=0.345f;break;
case 'm':storage=0.375f;break;
case 'n':storage=0.405f;break;
case 'o':storage=0.435f;break;
case 'p':storage=0.465f;break;
case 'q':storage=0.495f;break;
case 'r':storage=0.525f;break;
case 's':storage=0.555f;break;
case 't':storage=0.585f;break;
case 'u':storage=0.615f;break;
case 'v':storage=0.645f;break;
case 'w':storage=0.675f;break;
case 'x':storage=0.705f;break;
case 'y':storage=0.735f;break;
case 'z':storage=0.765f;break;
case 'A':storage=0.005f;break;
case 'B':storage=0.030f;break;
case 'C':storage=0.060f;break;
case 'D':storage=0.090f;break;
case 'E':storage=0.120f;break;
case 'F':storage=0.150f;break;
case 'G':storage=0.180f;break;
case 'H':storage=0.210f;break;
case 'I':storage=0.240f;break;
case 'J':storage=0.270f;break;
case 'K':storage=0.300f;break;
case 'L':storage=0.330f;break;
case 'M':storage=0.360f;break;
case 'N':storage=0.390f;break;
case 'O':storage=0.420f;break;
case 'P':storage=0.450f;break;
case 'Q':storage=0.480f;break;
case 'R':storage=0.510f;break;
case 'S':storage=0.540f;break;
case 'T':storage=0.570f;break;
case 'U':storage=0.600f;break;
case 'V':storage=0.630f;break;
case 'W':storage=0.660f;break;
case 'X':storage=0.690f;break;
case 'Y':storage=0.720f;break;
case 'Z':storage=0.750f;break;
case ';':storage=0.78f;break;
case ':':storage=0.785f;break;
case '"':storage=0.79f;break;
case '0':storage=0.8f;break;
case '1':storage=0.82f;break;
case '2':storage=0.84f;break;
case '3':storage=0.86f;break;
case '4':storage=0.88f;break;
case '5':storage=0.9f;break;
case '6':storage=0.92f;break;
case '7':storage=0.94f;break;
case '8':storage=0.96f;break;
case '9':storage=0.98f;break;
case ',':storage=0.795f;break;
case '.':storage=0.985f;break;
case '/':storage=0.99f;break;
case '*':storage=0.995f;break;
default: storage=0.0f; break;
}
return storage;
}
void calcai(){
int layers[]={32,32,32,32,64,128,256,512};
for(int i = 0;i<32;i++){ // vectorizes input and calcs the values of the first neurons
for(int m = 0; m<5;m++){
calcN[m][0][i]=storage[i];
int y = calcN[m][0][i]*neuronw0[m][0][i];
calcN[m][1][i]=fabs(y/(1+fabs(y))); //sigmoid function f(x) = x / (1 + fabs(x))+1
}
}
for(int m = 0; m<5; m++){//for each file
for(int z = 2; z<8; z++){//for each layer
for(int i = 0; i<layers[z];i++){//for each neuron
float temp=0;
for(int w = 0; w<layers[z-1];w++){//for each weight and counts it up to for the value of the next neuron
temp += calcN[m][z-1][w]*weights0[m][z-2][w][i]; //saves the total value for
}
temp = temp/layers[z-1];//normalizes value each layer
temp += biasneur[m][z-2]*biasweights[m][z-2][i];//bias
calcN[m][z][i]=fabs(temp/(1+fabs(temp)));//sigmoid function
}
}
}
}
void printstory(){
char storage[5][512];
fp0[5]=fopen("aistories","w+");
for (int m = 0; m < 5; m++) {
int p = 0;
for (int i = 0; i < 512; i++) {
storage[m][i]=(char)(int)((calcN[m][7][i]*95)+32);
fprintf(fp0[5],"%c", storage[m][i]);
p++;
if(p==80){
fprintf(fp0[5], "\n");
p = 0;
}
}
fprintf(fp0[5],"\n\n");
}
fclose(fp0[5]);
}
float mutcha(float x){
int gh = (int)(rand()/100)%100;
switch(gh){
case 0 ... 12: x=x*0.75f; break;
case 13 ... 24: x=x*1.25f; break;
case 25 ... 30: x=x*2; break;
case 31 ... 35: x=x*0.5f; break;
case 36 ... 37: x=5; break;
case 38 ... 39: x=0; break;
case 40 ... 41: x=9.999f; break;
case 42 ... 50: x=fmodf((float)rand()/1000,10); break;
default: break;
}
if(x>9.9999f){
x=9.999f;
}
if(x<0){
x=0;
}
return x;
}
void mutweight(int num){ //mutates the weights
size_t layers[]={32,32,32,64,128,256,512};
int muse[4];
int extra = 0;
for(int i = 0; i<4;i++){
if((i+1)==num){
extra = 1;
}
muse[i]=i+extra;
}
num -= 1;
for (size_t m = 0; m < 4; m++) {
for (size_t z = 0; z < 7; z++) {
for (size_t i = 0; i < layers[z]; i++) {
neuronw0[muse[m]][z][i]=mutcha(neuronw0[num][z][i]);
}
}
for (size_t z = 0; z < 6; z++) {
biasneur[muse[m]][z]=mutcha(biasneur[num][z]);
for (size_t i = 0; i < layers[z+1]; i++) {
biasweights[muse[m]][z][i]=mutcha(biasweights[num][z][i]);
}
for (size_t i = 0; i < layers[z]; i++) {
for (size_t w = 0; w < layers[z+1]; w++) {
//printf("\n%d %d %d %d ", muse[m],z,i,w);
weights0[muse[m]][z][i][w]=mutcha(weights0[num][z][i][w]);
}
}
}
}
}
void autotrain(){
char input[33];
char check[512];
int best[6]={0,0,0,0,0,0};
fp=fopen("Autotrain.txt","r");
int zxy=0;
while(1){
printf("\nBusy with training on text%d, progress-",zxy);
fseek(fp,zxy*544,SEEK_SET);
fscanf(fp,"%s32", &input);
for(int i = 0;i<32;i++){ // converts input to a more legible value
storage[i] = convertinput(input[i]);
}
fseek(fp,(zxy*544)+32,SEEK_SET);
fscanf(fp,"%s512", &check);
for(int aii=0;aii<100;aii++){
int high=0;
for(int rept=0;rept<floor(log10(aii))+1;rept++){//dynamic progress printing
printf("\b");
}
printf("%d",aii);
calcai();
for(int m=0;m<5;m++){
for(int i=0;i<512;i++){
if((char)(int)((calcN[m][7][i]*95)+32)==check[i]){
best[m]++;
}
}
if(best[m]<best[m+1]){
high = m;
}
}
mutweight(high);
}
zxy++;
if(zxy>1){
zxy=0;
}
}
}
void main(){
char in[2];
char input[33];
char output[512];
fp0[0]=fopen("Aiweights0","r+");//opening the weight files
fp0[1]=fopen("Aiweights1","r+");
fp0[2]=fopen("Aiweights2","r+");
fp0[3]=fopen("Aiweights3","r+");
fp0[4]=fopen("Aiweights4","r+");
loadweights();
int automatic = 0;
printf("\ndo you want to train automaticly? (1=yes, 0=no)\n");
scanf("%d", &automatic);
if(automatic==0){
printf("\nplease input a maximum 32 chararacter prompt:");
scanf("%32[^\n]",input);
printf("\ngenerating 5 stories, please wait");
for(int i = 0;i<32;i++){ // converts input to a more legible value
storage[i] = convertinput(input[i]);
}
while(1){
calcai();
printstory();
printf("\nthe generated stories can be found in the story file");
printf("\ntell me which ai did the best job?");
printf("\nyou can use q to quit");
printf("\nplease give a number from 1 to 5 to indicate which ai did the best:\n");
scanf("%1s", &in);
if((int)in[0]==113){
return;
exit(0);
}
int num = (int)in[0]-48;//converts input to a integer
printf("\nmutating weights");
mutweight(num);
printf("\nsaving weights");
saveweights();
}
}else{
autotrain();
}
}