Skip to content

Commit

Permalink
Merge pull request #11 from C-Text/feature/neural_network
Browse files Browse the repository at this point in the history
Close #9
  • Loading branch information
Vinetos authored Oct 25, 2020
2 parents 53b5a09 + 80605be commit 994de13
Show file tree
Hide file tree
Showing 19 changed files with 770 additions and 36 deletions.
6 changes: 4 additions & 2 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,7 @@ jobs:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: make
run: make
- name: CMake the project
run: cmake .
- name: Make the projet
run: make
32 changes: 32 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
cmake_minimum_required(VERSION 3.17)
project(CText C)

set(CMAKE_C_STANDARD 99)

add_executable(
CText src/main.c
src/neuralnetwork/neuron.c src/neuralnetwork/neuron.h
src/neuralnetwork/function.c src/neuralnetwork/function.h
src/neuralnetwork/layer.c src/neuralnetwork/layer.h
src/util.c src/util.h
)

add_executable(
CText-tests src/test/main.c
src/neuralnetwork/neuron.c src/neuralnetwork/neuron.h
src/neuralnetwork/function.c src/neuralnetwork/function.h
src/neuralnetwork/layer.c src/neuralnetwork/layer.h
src/util.c src/util.h
src/test/test.c src/test/test.h
src/test/test_method.c src/test/test_method.h
)

target_link_libraries(CText m)
target_link_libraries(CText-tests m)

if ( CMAKE_COMPILER_IS_GNUCC )
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Wextra -Wshadow -Wdouble-promotion -Wformat=2 -Wformat-truncation -Wformat-overflow -Wundef -fno-common -Wconversion")
endif()
if ( MSVC )
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /W4")
endif()
21 changes: 0 additions & 21 deletions Makefile

This file was deleted.

25 changes: 18 additions & 7 deletions README
Original file line number Diff line number Diff line change
@@ -1,22 +1,33 @@
CText
====== CText ======

This is a school project.
= Clone =

Depandencies :
git clone https://github.com/C-Text/CText.git

= Dependencies =

gcc
cmake
make

= Build =
cmake .
make

Prepare the project :
= Launch =
./CText
./CText-tests

= Contribute =

git clone git@github.com:C-Text/CText.git
git clone https://github.com/C-Text/CText.git
git flow init


Create a new feature :
= Create a new feature =

git flow feature start <my_feature>


Finish a feature :
= Finish a feature =
Open a Pull Request -> base: develop, compare: feature/<my_feature>
6 changes: 0 additions & 6 deletions main.c

This file was deleted.

50 changes: 50 additions & 0 deletions src/main.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
#include "neuralnetwork/layer.h"
#include <stdio.h>

void prints(layer *l1, layer *l2) {
lprint(l1, true);
printf("\n");
lprint(l2, true);
printf("\n");
}

void test(int ** arr) {
for (int i = 0; i < 5; ++i) {
printf("%d", arr[i][i]);
}
}

int main(void) {
const unsigned int EPOCHS = 100;
// XOR table
double inputs[4][2] = {
{0.0, 0.0},
{0.0, 1.0},
{1.0, 0},
{1.0, 1.0}
};
double outputs[4] = {0.0, 1.0, 1.0, 0.0};
// Creating layers
layer l = create_layer(2, 1);
layer l2 = create_layer(1, 1);

// Input
feed(&l, inputs[1]);
printf("================== INPUT ==================\n");
prints(&l,&l2);

// Activate the layer
activate(&l);
printf("================== ACTIV ==================\n");
prints(&l,&l2);

//Feed forward
feedforward(&l,&l2);
printf("================== FEEDF ==================\n");
prints(&l,&l2);

double out = sum(&l2, 0);
printf("OUTPUT: %lf", out);

return 0;
}
57 changes: 57 additions & 0 deletions src/neuralnetwork/function.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
#include "function.h"
#include "../util.h"
#include <math.h>

/**
* Softmax activation.
*
* @date 25/10/2020
* @version 0.0.1
* @author Vinetos
* @package network
*
* This method activates the given input array using the softmax
* classification method.
*
* @warning This method modify the given array.
*
* @throw error when size < 0.
* @param inputs the arrays of values to activate.
* @param size the size of the array
*
*/
void softmax(double *inputs, size_t size) {
if (size == 0)
return;

double m = find_min(inputs, size);

double sum = 0.0;
for (size_t i = 0; i < size; ++i)
sum += exp(inputs[i] - m);

double constant = m + log(sum);
for (size_t i = 0; i < size; ++i)
inputs[i] = exp(inputs[i] - constant);
}

/**
* Mean squared error.
*
* @date 25/10/2020
* @version 0.0.1
* @author Vinetos
* @package network
*
* This method calculate the mean squared error between the input and the
* expected output.
*
* @param input The activated value of the neuron
* @param expected The expected value
*
* @return the mean squared error value.
*/
double mse(double input, double expected) {
double diff = expected - input;
return diff * diff;
}
44 changes: 44 additions & 0 deletions src/neuralnetwork/function.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
#ifndef CTEXT_SRC_NEURALNETWORK_FUNCTION_H_
#define CTEXT_SRC_NEURALNETWORK_FUNCTION_H_

#include <stdlib.h>

/**
* Softmax activation.
*
* @date 25/10/2020
* @version 0.0.1
* @author Vinetos
* @package network
*
* This method activates the given input array using the softmax
* classification method.
*
* @warning This method modify the given array.
*
* @throw error when size < 0.
* @param inputs the arrays of values to activate.
* @param size the size of the array
*
*/
void softmax(double *inputs, size_t size);

/**
* Mean squared error.
*
* @date 25/10/2020
* @version 0.0.1
* @author Vinetos
* @package network
*
* This method calculate the mean squared error between the input and the
* expected output.
*
* @param input The activated value of the neuron
* @param expected The expected value
*
* @return the mean squared error value.
*/
double mse(double input, double expected);

#endif //CTEXT_SRC_NEURALNETWORK_FUNCTION_H_
Loading

0 comments on commit 994de13

Please sign in to comment.