-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNeuralNetClasses.cpp
More file actions
145 lines (118 loc) · 3.38 KB
/
Copy pathNeuralNetClasses.cpp
File metadata and controls
145 lines (118 loc) · 3.38 KB
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
#include <iostream>
#include <stdexcept>
class Layer
{
private:
int numNodesIn, numNodesOut;
double** weights;
double* biases;
public:
Layer(int numNodesIn, int numNodesOut)
{
this->numNodesIn = numNodesIn;
this->numNodesOut = numNodesOut;
// Allocate memory for weights as a 2D array
weights = new double*[numNodesIn];
for (int i = 0; i < numNodesIn; i++)
{
weights[i] = new double[numNodesOut];
}
// Allocate memory for biases
biases = new double[numNodesOut];
}
double* CalculateOutputs(const double* inputs, double* weightedInputs)
{
for (int nodeOut = 0; nodeOut < numNodesOut; nodeOut++)
{
double weightedInput = biases[nodeOut];
for (int nodeIn = 0; nodeIn < numNodesIn; nodeIn++)
{
weightedInput += inputs[nodeIn] * weights[nodeIn][nodeOut];
}
weightedInputs[nodeOut] = weightedInput;
}
return weightedInputs;
}
~Layer()
{
// Deallocate memory for weights and biases
for (int i = 0; i < numNodesIn; i++)
{
delete[] weights[i];
}
delete[] weights;
delete[] biases;
}
};
class NeuralNetwork
{
private:
Layer* layers;
int numLayers;
public:
NeuralNetwork(const int* layerSizes, int numLayers)
{
this->numLayers = numLayers;
// Create an array of layers
// Create an array of layers
layers = new Layer[numLayers];
for (int i = 0; i < numLayers; i++)
{
layers[i] = Layer(layerSizes[i], layerSizes[i + 1]);
}
}
void CalculateOutputs(const double* inputs, double* outputs)
{
double* currentInputs = new double[layerSizes[0]];
std::memcpy(currentInputs, inputs, layerSizes[0] * sizeof(double));
for (int i = 0; i < numLayers; i++)
{
layers[i].CalculateOutputs(currentInputs, outputs);
std::memcpy(currentInputs, outputs, layerSizes[i + 1] * sizeof(double));
}
delete[] currentInputs;
}
int Classify(const double* inputs)
{
double* outputs = new double[layerSizes[numLayers]];
CalculateOutputs(inputs, outputs);
int maxIndex = IndexOfMaxValue(outputs);
delete[] outputs;
return maxIndex;
}
int IndexOfMaxValue(const double* array)
{
if (array == nullptr)
{
throw std::invalid_argument("Array is null.");
}
double maxValue = array[0];
int maxIndex = 0;
for (int i = 1; i < layerSizes[numLayers]; i++)
{
if (array[i] > maxValue)
{
maxValue = array[i];
maxIndex = i;
}
}
return maxIndex;
}
~NeuralNetwork()
{
delete[] layers;
}
};
int main()
{
// Create a neural network with specified layer sizes
const int layerSizes[] = {3, 4, 2};
int numLayers = sizeof(layerSizes) / sizeof(layerSizes[0]);
//NeuralNetwork network(layerSizes, numLayers);
// Example input values
const double inputs[] = {0.5, 0.3, 0.8};
// Run inputs through the network and classify
//int classification = NeuralNetwork.Classify(inputs);
//std::cout << "Classification: " << classification << std::endl;
return 0;
}