Originally Posted by
yann
Hi, i just made 2 more (now i have 3) perceptrons,
I made XOR all by myself!!! I have 2 inputs and 3 perceptrons, i will post the code later, its on my lap
I'm now on to making some bigger connections!
DD so happy i finally got the whole thing, i have a presentation in biology about this, and in informatics
No i don't have 3 inputs, i have only two, but i have 3 PERCEPTRONS;
1, for teaching percept. 1
Code:
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
float weight_x3 = 0.0f, weight_y3 = 0.0f, weight_z3 = 0.0f;
float weight_x = 0.0f, weight_y = 0.0f, weight_z = 0.0f;
float weight_x2 = 0.0f, weight_y2 = 0.0f, weight_z2 = 0.0f;
float threshold = 0.5f;
int successive_right = 0;
int total_go_round = 1;
const float learning_rate = 0.1f;
int input_x;
int input_y;
int input_z;
bool percept;
bool target(int x, int y, int z) { //što ucimo perceptron
//x je bias, pa se ne ubraja
return (y||z); //funkcija koju ucimo perceptron
}
int educate() {
input_x = 1; //bias, uvijek 1
input_y = rand() % 2;
input_z = rand() % 2;
bool goal = target(input_x, input_y, input_z);
percept = (weight_x*input_x+weight_y*input_y+weight_z*input_z > threshold);
if (percept == goal) {
successive_right++;
} else {
successive_right = 0;
int sign = goal ? 1 : -1; //sign of (y-f(x))
weight_x += learning_rate*sign*input_x;
weight_y += learning_rate*sign*input_y;
weight_z += learning_rate*sign*input_z;
}
printf("%d", percept);
return;
}
int main(){
FILE *g;
g = fopen("per.dat","rb");
fread(&weight_x,sizeof(weight_x),1,g);
fread(&weight_y,sizeof(weight_y),1,g);
fread(&weight_z,sizeof(weight_z),1,g);
fread(&weight_x2,sizeof(weight_x2),1,g);
fread(&weight_y2,sizeof(weight_y2),1,g);
fread(&weight_z2,sizeof(weight_z2),1,g);
fread(&weight_x3,sizeof(weight_x3),1,g);
fread(&weight_y3,sizeof(weight_y3),1,g);
fread(&weight_z3,sizeof(weight_z3),1,g);
fclose(g);
printf("1\n");
srand(time(NULL));
total_go_round=1;
while(total_go_round <= 575){
educate();
total_go_round++;FILE *g;
}
printf("Perceptron: (%.2f, %.2f, %.2f, %.2f)\n", weight_x, weight_y, weight_z, threshold);
input_x = 1;
printf("input_y: ");
scanf("%d", &input_y);
printf("input_z: ");
scanf("%d", &input_z);
percept = (weight_x*input_x+weight_y*input_y+weight_z*input_z > threshold);
printf("perceptron: ");
printf("%d\n", percept);
FILE *f;
f = fopen("per.dat","wb");
fwrite(&weight_x,sizeof(weight_x),1,f);
fwrite(&weight_y,sizeof(weight_y),1,f);
fwrite(&weight_z,sizeof(weight_z),1,f);
fwrite(&weight_x2,sizeof(weight_x2),1,f);
fwrite(&weight_y2,sizeof(weight_y2),1,f);
fwrite(&weight_z2,sizeof(weight_z2),1,f);
fwrite(&weight_x3,sizeof(weight_x3),1,f);
fwrite(&weight_y3,sizeof(weight_y3),1,f);
fwrite(&weight_z3,sizeof(weight_z3),1,f);
fclose(g);
return 0;
}
2, for teaching percept. 1
Code:
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
float weight_x3 = 0.0f, weight_y3 = 0.0f, weight_z3 = 0.0f;
float weight_x = 0.0f, weight_y = 0.0f, weight_z = 0.0f;
float weight_x2 = 0.0f, weight_y2 = 0.0f, weight_z2 = 0.0f;
float threshold = 0.5f;
int successive_right = 0;
int total_go_round = 1;
const float learning_rate = 0.1f;
int input_x;
int input_y;
int input_z;
bool percept;
bool target(int x, int y, int z) { //što ucimo perceptron
//x je bias, pa se ne ubraja
return (!(y&&z)); //funkcija koju ucimo perceptron
}
int educate() {
input_x = 1; //bias, uvijek 1
input_y = rand() % 2;
input_z = rand() % 2;
bool goal = target(input_x, input_y, input_z);
percept = (weight_x2*input_x+weight_y2*input_y+weight_z2*input_z > threshold);
if (percept == goal) {
successive_right++;
} else {
successive_right = 0;
int sign = goal ? 1 : -1; //sign of (y-f(x))
weight_x2 += learning_rate*sign*input_x;
weight_y2 += learning_rate*sign*input_y;
weight_z2 += learning_rate*sign*input_z;
}
printf("%d", percept);
return;
}
int main(){
FILE *g;
g = fopen("per.dat","rb");
fread(&weight_x,sizeof(weight_x),1,g);
fread(&weight_y,sizeof(weight_y),1,g);
fread(&weight_z,sizeof(weight_z),1,g);
fread(&weight_x2,sizeof(weight_x2),1,g);
fread(&weight_y2,sizeof(weight_y2),1,g);
fread(&weight_z2,sizeof(weight_z2),1,g);
fread(&weight_x3,sizeof(weight_x3),1,g);
fread(&weight_y3,sizeof(weight_y3),1,g);
fread(&weight_z3,sizeof(weight_z3),1,g);
fclose(g);
printf("1\n");
srand(time(NULL));
total_go_round=1;
while(total_go_round <= 575){
educate();
total_go_round++;
}
printf("Perceptron: (%.2f, %.2f, %.2f, %.2f)\n", weight_x2, weight_y2, weight_z2, threshold);
input_x = 1;
printf("input_y: ");
scanf("%d", &input_y);
printf("input_z: ");
scanf("%d", &input_z);
percept = (weight_x2*input_x+weight_y2*input_y+weight_z2*input_z > threshold);
printf("perceptron: ");
printf("%d\n", percept);
FILE *f;
f = fopen("per.dat","wb");
fwrite(&weight_x2,sizeof(weight_x2),1,f);
fwrite(&weight_y2,sizeof(weight_y2),1,f);
fwrite(&weight_z2,sizeof(weight_z2),1,f);
fwrite(&weight_x,sizeof(weight_x),1,f);
fwrite(&weight_y,sizeof(weight_y),1,f);
fwrite(&weight_z,sizeof(weight_z),1,f);
fwrite(&weight_x3,sizeof(weight_x3),1,f);
fwrite(&weight_y3,sizeof(weight_y3),1,f);
fwrite(&weight_z3,sizeof(weight_z3),1,f);
fclose(g);
return 0;
}
3, for guess what, teaching perceptron 3
Code:
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
float weight_x3 = 0.0f, weight_y3 = 0.0f, weight_z3 = 0.0f;
float weight_x = 0.0f, weight_y = 0.0f, weight_z = 0.0f;
float weight_x2 = 0.0f, weight_y2 = 0.0f, weight_z2 = 0.0f;
float threshold = 0.5f;
int successive_right = 0;
int total_go_round = 1;
const float learning_rate = 0.1f;
int input_x;
int input_y;
int input_z;
bool percept;
bool target(int x, int y, int z) { //što ucimo perceptron
//x je bias, pa se ne ubraja
return (y&&z); //funkcija koju ucimo perceptron
}
int educate() {
input_x = 1; //bias, uvijek 1
input_y = rand() % 2;
input_z = rand() % 2;
bool goal = target(input_x, input_y, input_z);
percept = (weight_x3*input_x+weight_y3*input_y+weight_z3*input_z > threshold);
if (percept == goal) {
successive_right++;
} else {
successive_right = 0;
int sign = goal ? 1 : -1; //sign of (y-f(x))
weight_x3 += learning_rate*sign*input_x;
weight_y3 += learning_rate*sign*input_y;
weight_z3 += learning_rate*sign*input_z;
}
printf("%d", percept);
return;
}
int main(){
FILE *g;
g = fopen("per.dat","rb");
fread(&weight_x,sizeof(weight_x),1,g);
fread(&weight_y,sizeof(weight_y),1,g);
fread(&weight_z,sizeof(weight_z),1,g);
fread(&weight_x2,sizeof(weight_x2),1,g);
fread(&weight_y2,sizeof(weight_y2),1,g);
fread(&weight_z2,sizeof(weight_z2),1,g);
fread(&weight_x3,sizeof(weight_x3),1,g);
fread(&weight_y3,sizeof(weight_y3),1,g);
fread(&weight_z3,sizeof(weight_z3),1,g);
fclose(g);
printf("1\n");
srand(time(NULL));
total_go_round=1;
while(total_go_round <= 575){
educate();
total_go_round++;FILE *g;
}
printf("Perceptron: (%.2f, %.2f, %.2f, %.2f)\n", weight_x, weight_y, weight_z, threshold);
input_x = 1;
printf("input_y: ");
scanf("%d", &input_y);
printf("input_z: ");
scanf("%d", &input_z);
percept = (weight_x3*input_x+weight_y3*input_y+weight_z3*input_z > threshold);
printf("perceptron: ");
printf("%d\n", percept);
FILE *f;
f = fopen("per.dat","wb");
fwrite(&weight_x,sizeof(weight_x),1,f);
fwrite(&weight_y,sizeof(weight_y),1,f);
fwrite(&weight_z,sizeof(weight_z),1,f);
fwrite(&weight_x2,sizeof(weight_x2),1,f);
fwrite(&weight_y2,sizeof(weight_y2),1,f);
fwrite(&weight_z2,sizeof(weight_z2),1,f);
fwrite(&weight_x3,sizeof(weight_x3),1,f);
fwrite(&weight_y3,sizeof(weight_y3),1,f);
fwrite(&weight_z3,sizeof(weight_z3),1,f);
fclose(g);
return 0;
}
and the brain
Code:
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int input_x3 = 1; //bias
int input_x2 = 1; //bias
int input_x = 1; //bias
int input_y; //bias
int input_z; //bias
float weight_x = 0.0f, weight_y = 0.0f, weight_z = 0.0f; //tezine percept 1
float threshold = 0.5f; //granica percept 1
bool percept; //percept 1
float weight_x2 = 0.0f, weight_y2 = 0.0f, weight_z2 = 0.0f; //tezine percept 2
float threshold2 = 0.5f; //granica percept 2
bool percept2; //percept 2
float weight_x3 = 0.0f, weight_y3 = 0.0f, weight_z3 = 0.0f; //tezine percept 3
float threshold3 = 0.5f; //granica percept 3
bool percept3; //percept 3
/*main*/
int main(){
printf("\n");
FILE *g;
g = fopen("per.dat","rb");
fread(&weight_x,sizeof(weight_x),1,g);
fread(&weight_y,sizeof(weight_y),1,g);
fread(&weight_z,sizeof(weight_z),1,g);
fread(&weight_x2,sizeof(weight_x2),1,g);
fread(&weight_y2,sizeof(weight_y2),1,g);
fread(&weight_z2,sizeof(weight_z2),1,g);
fread(&weight_x3,sizeof(weight_x3),1,g);
fread(&weight_y3,sizeof(weight_y3),1,g);
fread(&weight_z3,sizeof(weight_z3),1,g);
printf("weights:\n");
printf("%f\n", weight_x);
printf("%f\n", weight_y);
printf("%f\n", weight_z);
printf("%f\n", weight_x2);
printf("%f\n", weight_y2);
printf("%f\n", weight_z2);
printf("%f\n", weight_x3);
printf("%f\n", weight_y3);
printf("%f\n", weight_z3);
printf("\n");
fclose(g);
printf("inputs:\n");
printf("input_y: ");
scanf("%d", &input_y);
printf("input_z: ");
scanf("%d", &input_z);
percept = (weight_x*input_x+weight_y*input_y+weight_z*input_z > threshold);
percept2 = (weight_x2*input_x2+weight_y2*input_y+weight_z2*input_z > threshold2);
percept3 = (weight_x3*input_x3+weight_y3*percept+weight_z3*percept2 > threshold3);
printf("\n");
printf("outputs:\n");
printf("perceptron: ");
printf("%d\n", percept);
printf("perceptron2: ");
printf("%d\n", percept2);
printf("perceptron3 (the network output): ");
printf("%d\n", percept3);
printf("\n");
}
first compile the first 3 programs, then run them, then compile the bran, rum it and thats all!
I'm now looking forward for making some bigger networks, and i will need more efficient ways of teaching percepts. I mean, to be able of teaching them inside the brain(i didn't do that because of the return function).
Tell me what do you think...
,yann