#ifndef PERCEPTRON_H__
#define PERCEPTRON_H__
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <string>
class perceptron
{
public:
float *weights;
float bias;
int input_num;
public:
perceptron(int inputNum);
int activator(float x);
int predict(int *input);
void train(int *input, int label, float learning_rate);
void update(int *input, int output, int label, float learning_rate);
};
#endif
#include "perceptron.h"
perceptron::perceptron(int inputNum)
{
input_num = inputNum;
weights = (float*) malloc(sizeof(float) * inputNum);
bzero(weights, sizeof(float) * inputNum);
weights[0] = 0; weights[1] = 0.00125;
bias = 0.13;
}
int perceptron::activator(float x)
{
if (x > 0) {
return 1;
} else {
return 0;
}
}
int perceptron::predict(int *input)
{
float y = bias;
for (int i = 0; i < input_num; ++i) {
y += weights[i] * input[i];
}
return activator(y);
}
void perceptron::train(int *input, int label, float learning_rate)
{
int y = predict(input);
update(input, y, label, learning_rate);
}
void perceptron::update(int *input, int output, int label, float learning_rate)
{
float delta = label - output;
for (int i = 0; i < input_num; ++i) {
weights[i] += learning_rate * delta * input[i];
}
bias += delta * learning_rate;
}
#include "perceptron.h"
#define epochs 10
#define input_num 2
#define learning_rate 0.1
void train_and_perceptron(perceptron &P)
{
for (int i = 0; i < epochs; ++i) {
int *input = (int *) malloc(sizeof(int) * input_num);
int output;
input[0] = 1, input[1] = 1, output = 1;
P.train(input, output, learning_rate);
input[0] = 1, input[1] = 0, output = 0;
P.train(input, output, learning_rate);
input[0] = 0, input[1] = 1, output = 0;
P.train(input, output, learning_rate);
input[0] = 0, input[1] = 0, output = 0;
P.train(input, output, learning_rate);
free(input);
}
}
int main()
{
perceptron P = perceptron(input_num);
train_and_perceptron(P);
printf("bias: %f weights[0]: %f weights[1]: %f\n", P.bias, P.weights[0], P.weights[1]);
int *input = (int *) malloc(sizeof(int) * input_num);
input[0] = 1, input[1] = 1;
printf("1 and 1 = %d\n", P.predict(input));
input[0] = 1, input[1] = 0;
printf("1 and 1 = %d\n", P.predict(input));
input[0] = 0, input[1] = 1;
printf("0 and 1 = %d\n", P.predict(input));
input[0] = 0, input[1] = 0;
printf("0 and 0 = %d\n", P.predict(input));
free(input);
return 0;
}
#include "perceptron.h"
#define epochs 10
#define input_num 2
#define learning_rate 0.1
void train_or_perceptron(perceptron &P)
{
for (int i = 0; i < epochs; ++i) {
int *input = (int *) malloc(sizeof(int) * input_num);
int output;
input[0] = 1, input[1] = 1, output = 1;
P.train(input, output, learning_rate);
input[0] = 1, input[1] = 0, output = 1;
P.train(input, output, learning_rate);
input[0] = 0, input[1] = 1, output = 1;
P.train(input, output, learning_rate);
input[0] = 0, input[1] = 0, output = 0;
P.train(input, output, learning_rate);
free(input);
}
}
int main()
{
perceptron P = perceptron(input_num);
train_or_perceptron(P);
printf("bias: %f weights[0]: %f weights[1]: %f\n", P.bias, P.weights[0], P.weights[1]);
int *input = (int *) malloc(sizeof(int) * input_num);
input[0] = 1, input[1] = 1;
printf("1 or 1 = %d\n", P.predict(input));
input[0] = 1, input[1] = 0;
printf("1 or 1 = %d\n", P.predict(input));
input[0] = 0, input[1] = 1;
printf("0 or 1 = %d\n", P.predict(input));
input[0] = 0, input[1] = 0;
printf("0 or 0 = %d\n", P.predict(input));
free(input);
return 0;
}