feat: ✨ Create Neural network for XOR
This commit is contained in:
98
use.c
Normal file
98
use.c
Normal file
@ -0,0 +1,98 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <math.h>
|
||||
#include <err.h>
|
||||
|
||||
// Function prototypes
|
||||
void getvalues(char filename[], double **HiddenWeights, double *HiddenBiases, double **FinalOutputWeights, double *FinalOutputBiases);
|
||||
double sigmoid(double x);
|
||||
double sigmoid_derivative(double x);
|
||||
|
||||
int main() {
|
||||
// Allocate memory for weights and biases
|
||||
double **HiddenWeights = (double **)malloc(2 * sizeof(double *));
|
||||
for (int i = 0; i < 2; i++) {
|
||||
HiddenWeights[i] = (double *)malloc(2 * sizeof(double));
|
||||
}
|
||||
double *HiddenBiases = (double *)malloc(2 * sizeof(double));
|
||||
double **FinalOutputWeights = (double **)malloc(2 * sizeof(double *));
|
||||
for (int i = 0; i < 2; i++) {
|
||||
FinalOutputWeights[i] = (double *)malloc(1 * sizeof(double));
|
||||
}
|
||||
double *FinalOutputBiases = (double *)malloc(1 * sizeof(double));
|
||||
|
||||
// Load weights and biases from CSV file
|
||||
getvalues("weights.csv", HiddenWeights, HiddenBiases, FinalOutputWeights, FinalOutputBiases);
|
||||
|
||||
// Prompt user for input values
|
||||
double input1, input2;
|
||||
printf("Enter two values to compute XOR: ");
|
||||
scanf("%lf %lf", &input1, &input2);
|
||||
|
||||
// Perform forward pass
|
||||
double hiddenLayer[2];
|
||||
double outputLayer[1];
|
||||
|
||||
// Compute hidden layer activation
|
||||
for (int j = 0; j < 2; j++) {
|
||||
double activation = HiddenBiases[j];
|
||||
activation += input1 * HiddenWeights[0][j];
|
||||
activation += input2 * HiddenWeights[1][j];
|
||||
hiddenLayer[j] = sigmoid(activation);
|
||||
}
|
||||
|
||||
// Compute output layer activation
|
||||
for (int j = 0; j < 1; j++) {
|
||||
double activation = FinalOutputBiases[j];
|
||||
activation += hiddenLayer[0] * FinalOutputWeights[0][j];
|
||||
activation += hiddenLayer[1] * FinalOutputWeights[1][j];
|
||||
outputLayer[j] = sigmoid(activation);
|
||||
}
|
||||
|
||||
// Print the result
|
||||
printf("Input: %g %g Predicted Output: %g Output round: %g\n", input1, input2, outputLayer[0], round(outputLayer[0]));
|
||||
|
||||
// Free allocated memory
|
||||
for (int i = 0; i < 2; i++) {
|
||||
free(HiddenWeights[i]);
|
||||
free(FinalOutputWeights[i]);
|
||||
}
|
||||
free(HiddenWeights);
|
||||
free(HiddenBiases);
|
||||
free(FinalOutputWeights);
|
||||
free(FinalOutputBiases);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
void getvalues(char filename[], double **HiddenWeights, double *HiddenBiases, double **FinalOutputWeights, double *FinalOutputBiases) {
|
||||
FILE *fp = fopen(filename, "r");
|
||||
if (fp == NULL) {
|
||||
errx(EXIT_FAILURE, "Could not open file %s", filename);
|
||||
}
|
||||
for (int i = 0; i < 2; i++) {
|
||||
for (int j = 0; j < 2; j++) {
|
||||
fscanf(fp, "%lf,", &HiddenWeights[i][j]);
|
||||
}
|
||||
}
|
||||
for (int i = 0; i < 2; i++) {
|
||||
fscanf(fp, "%lf,", &HiddenBiases[i]);
|
||||
}
|
||||
for (int i = 0; i < 2; i++) {
|
||||
for (int j = 0; j < 1; j++) {
|
||||
fscanf(fp, "%lf,", &FinalOutputWeights[i][j]);
|
||||
}
|
||||
}
|
||||
for (int i = 0; i < 1; i++) {
|
||||
fscanf(fp, "%lf,", &FinalOutputBiases[i]);
|
||||
}
|
||||
fclose(fp);
|
||||
}
|
||||
|
||||
double sigmoid(double x) {
|
||||
return 1.0 / (1.0 + exp(-x));
|
||||
}
|
||||
|
||||
double sigmoid_derivative(double x) {
|
||||
return x * (1.0 - x);
|
||||
}
|
Reference in New Issue
Block a user