-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
113 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,10 @@ | ||
#include "Activation.h" | ||
#include <cmath> | ||
|
||
double Activation::relu(Neuron neuron) { | ||
double Activation::relu(double i) { | ||
return 0; | ||
} | ||
|
||
double Activation::softmax(Neuron neuron) { | ||
return 0; | ||
double Activation::softmax(double i) { | ||
return exp(i); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,38 @@ | ||
#include <iostream> | ||
#include "NeuralLayer.h" | ||
#include "Activation.h" | ||
#include <iostream> | ||
|
||
NeuralLayer::NeuralLayer(int input_size, int layer_size, double (*activation)(Neuron)) : input_size(input_size), | ||
layer_size(layer_size), | ||
activation(activation) { | ||
neurons = new Neuron *[layer_size]; | ||
NeuralLayer::NeuralLayer(int input_size, int layer_size, double (*activation)(double)) : input_size(input_size), | ||
layer_size(layer_size), | ||
activation(activation) { | ||
neurons = new Neuron *[input_size]; | ||
for (int i = 0; i < input_size; i++) { | ||
neurons[i] = new Neuron(layer_size); | ||
} | ||
} | ||
|
||
double *NeuralLayer::forward(double *input) { | ||
std::cout << "starting forward neural layer input: " << input_size << " layer_size: " << layer_size << std::endl; | ||
auto output = new double[layer_size]; | ||
|
||
for (int i = 0; i < layer_size; i++) { | ||
std::cout << "neuron: " << i << " started" << std::endl; | ||
|
||
Neuron *neuron = neurons[i]; | ||
double value = neuron->forward(input); | ||
value += neuron->bias; | ||
|
||
output[i] = activation(value); | ||
std::cout << "neuron: " << i << " completed" << std::endl; | ||
} | ||
|
||
if (activation == Activation::softmax) { | ||
double sum = 0; | ||
for (int i = 0; i < layer_size; i++) | ||
sum += output[i]; | ||
for (int i = 0; i < layer_size; i++) | ||
output[i] /= sum; | ||
} | ||
return output; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,59 @@ | ||
#include "NeuralNetwork.h" | ||
#include <iostream> | ||
#include <fstream> | ||
|
||
void NeuralNetwork::addNeuralLayer(int layer_size, double (*activation)(Neuron)) { | ||
void NeuralNetwork::addNeuralLayer(int layer_size, double (*activation)(double)) { | ||
layers.push_back(*new NeuralLayer(layers.back().layer_size, layer_size, activation)); | ||
} | ||
|
||
void NeuralNetwork::addNeuralLayer(int input_size, int layer_size, double (*activation)(Neuron)) { | ||
void NeuralNetwork::addNeuralLayer(int input_size, int layer_size, double (*activation)(double)) { | ||
layers.push_back(*new NeuralLayer(input_size, layer_size, activation)); | ||
} | ||
|
||
double *NeuralNetwork::evaluate(double *input) { | ||
for (auto const &layer: layers) { | ||
double *value = input; | ||
for (auto &layer: layers) { | ||
value = layer.forward(value); | ||
} | ||
return value; | ||
} | ||
|
||
void NeuralNetwork::loadParameters(const std::string path) { | ||
std::ifstream in; | ||
in.open(path); | ||
|
||
int layer_count; | ||
in >> layer_count; | ||
|
||
for (int layer_index = 0; layer_index < layer_count; layer_index++) { | ||
int input_size, hidden_size; | ||
in >> input_size >> hidden_size; | ||
|
||
auto **layer_weights = new double *[input_size]; | ||
for (int i = 0; i < input_size; ++i) | ||
layer_weights[i] = new double[hidden_size]; | ||
|
||
auto *layer_biases = new double[hidden_size]; | ||
|
||
for (int i = 0; i < input_size; i++) { | ||
for (int j = 0; j < hidden_size; j++) { | ||
in >> layer_weights[i][j]; | ||
} | ||
} | ||
|
||
for (int j = 0; j < hidden_size; j++) { | ||
in >> layer_biases[j]; | ||
} | ||
|
||
for (int i = 0; i < input_size; i++) { | ||
auto neuron = layers[layer_index].neurons[i]; | ||
for (int j = 0; j < hidden_size; j++) { | ||
neuron->weights[j] = layer_weights[i][j]; | ||
} | ||
for (int j = 0; j < hidden_size; j++) { | ||
neuron->bias = layer_biases[j]; | ||
} | ||
} | ||
} | ||
in.close(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,15 @@ | ||
#include <iostream> | ||
#include "Neuron.h" | ||
|
||
Neuron::Neuron(int input_size) : input_size(input_size) { | ||
weights = new double[input_size]; | ||
bias = 0; | ||
} | ||
|
||
double Neuron::forward(double *input) { | ||
double sum = 0; | ||
for (int i = 0; i < input_size; i++) { | ||
sum += weights[i] * input[i]; | ||
} | ||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,6 +10,8 @@ class Neuron { | |
double bias; | ||
|
||
Neuron(int input_size); | ||
|
||
double forward(double *input); | ||
}; | ||
|
||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters