-
Notifications
You must be signed in to change notification settings - Fork 0
/
neural_network.cpp
79 lines (62 loc) · 1.96 KB
/
neural_network.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
//
// Created by kunwar on 17/4/18.
//
#include <fstream>
#include "neural_network.h"
#include "iostream"
neural_network::neural_network(int n1,int n2,int n3,float lr)
{
input_nodes = n1;
hidden_nodes = n2;
output_nodes = n3;
learning_rate = lr;
weights_input_to_hidden = new double * [input_nodes];
weights_hidden_to_output = new double * [hidden_nodes];
for (int i = 0; i < input_nodes; ++i) {
weights_input_to_hidden[i] = new double[hidden_nodes];
}
for (int i = 0; i < hidden_nodes; ++i) {
weights_hidden_to_output[i] = new double[output_nodes];
}
}
void neural_network::activation_function(double* x, int data_size, int constant)
{
// linear activation
for (int i = 0; i < data_size; ++i) {
x[i] *= constant;
}
}
void neural_network::save_weights(std::string const &filename) {
std::ofstream weights;
weights.open("/home/kunwar/CLionProjects/NeuralNetwork/" + filename + ".data");
for (int i = 0; i < input_nodes; ++i) {
for (int j = 0; j < hidden_nodes; ++j) {
weights << weights_input_to_hidden[i][j] << "\n";
}
}
for (int i = 0; i < hidden_nodes; ++i) {
for (int j = 0; j < output_nodes; ++j) {
weights << weights_hidden_to_output[i][j] << "\n";
}
}
weights.close();
}
void neural_network::load_weights(std::string const &filename) {
std::ifstream weights;
weights.open("/home/kunwar/CLionProjects/NeuralNetwork/" + filename + ".data");
for (int i = 0; i < input_nodes; ++i) {
for (int j = 0; j < hidden_nodes; ++j) {
std::string val;
weights >> val;
weights_input_to_hidden[i][j] = std::stod(val);
}
}
for (int i = 0; i < hidden_nodes; ++i) {
for (int j = 0; j < output_nodes; ++j) {
std::string val;
weights >> val;
weights_hidden_to_output[i][j] = std::stod(val);
}
}
weights.close();
}