Skip to content

Commit

Permalink
mark 2
Browse files Browse the repository at this point in the history
  • Loading branch information
Rabrg committed Nov 5, 2018
1 parent 1ca74dc commit 44d658c
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 17 deletions.
10 changes: 3 additions & 7 deletions NeuralLayer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,25 +6,21 @@
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);
neurons = new Neuron *[layer_size];
for (int i = 0; i < layer_size; i++) {
neurons[i] = new Neuron(input_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) {
Expand Down
11 changes: 5 additions & 6 deletions NeuralNetwork.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,14 +45,13 @@ void NeuralNetwork::loadParameters(const std::string path) {
in >> layer_biases[j];
}

for (int i = 0; i < input_size; i++) {
for (int i = 0; i < hidden_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];
for (int j = 0; j < input_size; j++) {
double weight = layer_weights[j][i];
neuron->weights[j] = weight;
}
neuron->bias = layer_biases[i];
}
}
in.close();
Expand Down
12 changes: 8 additions & 4 deletions main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,13 +56,17 @@ int main(void) {
// Constructing the neural network
auto *network = new NeuralNetwork();
network->addNeuralLayer(INPUT_SIZE, HIDDEN_LAYER_1_SIZE, Activation::relu);
network->addNeuralLayer(HIDDEN_LAYER_1_SIZE, HIDDEN_LAYER_2_SIZE, Activation::relu);
network->addNeuralLayer(HIDDEN_LAYER_2_SIZE, OUTPUT_SIZE, Activation::softmax);
network->addNeuralLayer(HIDDEN_LAYER_2_SIZE, Activation::relu);
network->addNeuralLayer(OUTPUT_SIZE, Activation::softmax);

network->loadParameters("network.txt");
std::cout << "loaded params" << std::endl;

// Evaluating the neural network with a set of inputs
auto *evaluationInput = new double[INPUT_SIZE] { 1.5, 1.2, 5.3, 7.3 };
network->evaluate(evaluationInput);
// auto *evaluationInput = new double[INPUT_SIZE] { 5.1, 3.5, 1.4, 0.2 };
// auto *evaluationInput = new double[INPUT_SIZE] { 4.9, 3.0, 1.4, 0.2 };
auto *evaluationInput = new double[INPUT_SIZE] { 5.9, 3.0, 5.1, 1.8 };
double *output = network->evaluate(evaluationInput);
for (int i = 0; i < OUTPUT_SIZE; i++)
std::cout << output[i] << " ";
}

0 comments on commit 44d658c

Please sign in to comment.