Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #define NEURON_MAX_IO 64
- #define MNETWORK_MAX_HIDDEN_LAYERS 8
- typedef struct {
- unsigned int inputs[NEURON_MAX_IO];
- float input_weights[NEURON_MAX_IO];
- unsigned int input_count;
- float threshold;
- unsigned int output;
- unsigned int learn;
- unsigned int learn_input;
- } NEURON;
- typedef struct {
- NEURON *neurons;
- unsigned int width;
- unsigned int height;
- } LAYER;
- typedef struct {
- LAYER input;
- LAYER hidden[8];
- unsigned int hidden_count;
- LAYER output;
- } NEURALNETWORK;
- void SimulateNeuron(NEURON *n)
- {
- float total = 0.0f;
- unsigned int i = 0;
- for (i = 0; i < n->input_count; i++)
- totaly = n->inputs[i] * n->input_weights[i];
- if (total > n->threshold) n->output = 1;
- else n->output = 0;
- if ((learn > 0) && (output != learn_input))
- {
- //delta rule
- float Lr = 0.01f; //learning rate
- float Wi = 0.0f; //weight of ith input
- for (i = 0; i < n->input_count; i++)
- n->input_weights[i] += Lr * (learn_input - n->output) * n->inputs[i];
- }
- }
- int main()
- {
- unsigned int tst =
- {
- 1,1,1,
- 0,1,0,
- 0,1,0,
- };
- unsigned int imt =
- {
- 1,1,1,
- 0,1,0,
- 0,1,0,
- };
- unsigned int imh =
- {
- 1,0,1,
- 1,1,1,
- 1,0,1,
- };
- unsigned int out =
- {
- 0,
- 0,
- 0,
- }
- unsigned int t =
- {
- 1,
- 1,
- 1,
- }
- unsigned int h =
- {
- 0,
- 0,
- 0,
- }
- NEURON out0;
- out0.input_count = 3;
- out0.inputs[0] = 0;
- out0.inputs[1] = 0;
- out0.inputs[2] = 0;
- out0.threshold = 1.0f;
- out0.input_weights[0] = 0.5f;
- out0.input_weights[1] = 0.5f;
- out0.input_weights[2] = 0.5f;
- NEURON out1;
- out1.input_count = 3;
- out1.inputs[0] = 0;
- out1.inputs[1] = 0;
- out1.inputs[2] = 0;
- out1.threshold = 1.0f;
- out1.input_weights[0] = 0.5f;
- out1.input_weights[1] = 0.5f;
- out1.input_weights[2] = 0.5f;
- NEURON out2;
- out2.input_count = 3;
- out2.inputs[0] = 0;
- out2.inputs[1] = 0;
- out2.inputs[2] = 0;
- out2.threshold = 1.0f;
- out2.input_weights[0] = 0.5f;
- out2.input_weights[1] = 0.5f;
- out2.input_weights[2] = 0.5f;
- //learn
- out0.learn = 1;
- out1.learn = 1;
- out2.learn = 1;
- //train for T
- out0.inputs[0] = imt[0];
- out0.inputs[1] = imt[1];
- out0.inputs[2] = imt[2];
- out1.inputs[0] = imt[3];
- out1.inputs[1] = imt[4];
- out1.inputs[2] = imt[5];
- out2.inputs[0] = imt[6];
- out2.inputs[1] = imt[7];
- out2.inputs[2] = imt[8];
- out0.learn_input = t[0];
- out1.learn_input = t[1];
- out2.learn_input = t[2];
- unsigned int tj = 100;
- for (; tj > 0; tj--) {
- SimulateNeuron(&out0);
- SimulateNeuron(&out1);
- SimulateNeuron(&out2);
- }
- //train for H
- out0.inputs[0] = imh[0];
- out0.inputs[1] = imh[1];
- out0.inputs[2] = imh[2];
- out1.inputs[0] = imh[3];
- out1.inputs[1] = imh[4];
- out1.inputs[2] = imh[5];
- out2.inputs[0] = imh[6];
- out2.inputs[1] = imh[7];
- out2.inputs[2] = imh[8];
- out0.learn_input = h[0];
- out1.learn_input = h[1];
- out2.learn_input = h[2];
- unsigned int hj = 100;
- for (; hj > 0; hj--) {
- SimulateNeuron(&out0);
- SimulateNeuron(&out1);
- SimulateNeuron(&out2);
- }
- //stop learning
- out0.learn = 0;
- out1.learn = 0;
- out2.learn = 0;
- //try to detect what img is
- out0.inputs[0] = img[0];
- out0.inputs[1] = img[1];
- out0.inputs[2] = img[2];
- out1.inputs[0] = img[3];
- out1.inputs[1] = img[4];
- out1.inputs[2] = img[5];
- out2.inputs[0] = img[6];
- out2.inputs[1] = img[7];
- out2.inputs[2] = img[8];
- unsigned int j = 100;
- unsigned int cnt[2] = {0, 0};
- for (; j > 0; j--) {
- SimulateNeuron(&out0);
- SimulateNeuron(&out1);
- SimulateNeuron(&out2);
- if (out0.output == h[0] && out1.output == h[1] && out2.output == h[2])
- cnt[0] = cnt[0] + 1;
- else if (out0.output == t[0] && out1.output == t[1] && out2.output == t[2])
- cnt[1] = cnt[1] + 1;
- }
- printf("Detected H %d times.\nDetected T %d times.\n", cnt[0], cnt[1]);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement