forked from lgallet/XOR-NeuralNetwork-C
110 lines
3.4 KiB
C
110 lines
3.4 KiB
C
#include "main.h"
|
|
#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(int argc, char *argv[]) {
|
|
// 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);
|
|
if (argc < 2) {
|
|
errx(0, "No arguments provided. Usage: %s <input1> <input2>", argv[0]);
|
|
return EXIT_FAILURE;
|
|
}
|
|
if (argc != 3) {
|
|
errx(0, "Usage: %s <input1> <input2>", argv[0]);
|
|
return EXIT_FAILURE;
|
|
}
|
|
|
|
double input1 = atof(argv[1]);
|
|
double input2 = atof(argv[2]);
|
|
// 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. Train the model first by executing ./main", 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);
|
|
}
|