-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNeural_Network.cpp
More file actions
295 lines (254 loc) · 8.44 KB
/
Copy pathNeural_Network.cpp
File metadata and controls
295 lines (254 loc) · 8.44 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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
#include <iostream>
#include <cmath>
#include <stdexcept>
#include <cstdlib>
using namespace std;
template <typename T>
class LinkedList {
public:
struct Node {
T value;
Node* next;
Node(T val) : value(val), next(nullptr) {}
};
Node* head;
LinkedList() : head(nullptr) {}
void append(T value) {
Node* newNode = new Node(value);
if (!head) {
head = newNode;
} else {
Node* temp = head;
while (temp->next) {
temp = temp->next;
}
temp->next = newNode;
}
}
int size() const {
int count = 0;
Node* temp = head;
while (temp) {
count++;
temp = temp->next;
}
return count;
}
T get(int index) const {
Node* temp = head;
int count = 0;
while (temp) {
if (count == index) return temp->value;
temp = temp->next;
count++;
}
}
void update(int index, T value) {
Node* temp = head;
int count = 0;
while (temp) {
if (count == index) {
temp->value = value;
return;
}
temp = temp->next;
count++;
}
}
LinkedList<T> copy() const {
LinkedList<T> newList;
Node* temp = head;
while (temp) {
newList.append(temp->value);
temp = temp->next;
}
return newList;
}
};
// Activation function enum
enum ActivationType {
Sigmoid,
ReLU
};
// Layer class with flexible neuron count and activation function
class Layer {
private:
LinkedList<double> weights; // Store weights for each neuron
LinkedList<double> neurons; // Store outputs of neurons
double bias; // Bias for the layer
ActivationType activationType;
// Activation functions
double sigmoid(double x) const {
return 1.0 / (1.0 + exp(-x));
}
double relu(double x) const {
return max(0.0, x);
}
public:
Layer(int inputSize, int neuronCount, ActivationType actType = Sigmoid)
: activationType(actType), bias(((double)rand() / RAND_MAX) - 0.5) {
// Initialize weights for each neuron
for (int i = 0; i < neuronCount * inputSize; i++) {
weights.append(((double)rand() / RAND_MAX) - 0.5);
}
// Initialize neurons (outputs)
for (int i = 0; i < neuronCount; i++) {
neurons.append(0.0);
}
}
// Forward pass through the layer
LinkedList<double> forward(const LinkedList<double>& inputs) {
LinkedList<double> outputs;
int inputSize = inputs.size();
int neuronCount = neurons.size();
// For each neuron in the layer
for (int n = 0; n < neuronCount; n++) {
double weightedSum = 0.0;
// Compute weighted sum for this neuron
for (int i = 0; i < inputSize; i++) {
weightedSum += inputs.get(i) * weights.get(n * inputSize + i);
}
// Add bias
weightedSum += bias;
// Apply activation function
double output;
if (activationType == Sigmoid) {
output = sigmoid(weightedSum);
} else { // ReLU
output = relu(weightedSum);
}
// Store neuron output
neurons.update(n, output);
outputs.append(output);
}
return outputs;
}
// Getters and setters
LinkedList<double>& getWeights() {
return weights;
}
double getBias() const {
return bias;
}
void updateWeight(int index, double newWeight) {
weights.update(index, newWeight);
}
void updateBias(double newBias) {
bias = newBias;
}
ActivationType getActivationType() const {
return activationType;
}
};
// Neural Network class to manage layers
class NeuralNetwork {
private:
LinkedList<Layer*> layers; // Linked list of layers
public:
NeuralNetwork(int inputSize, LinkedList<Layer*>& layerList) {
auto* temp = layerList.head; // Use the head of the LinkedList<Layer*>
// Add layers
while (temp) {
layers.append(temp->value); // Append the layer to the internal LinkedList
temp = temp->next; // Move to the next node
}
}
// Forward propagation through all layers
double predict(LinkedList<double>& inputs) {
LinkedList<double> currentInputs = inputs.copy();
auto* temp = layers.head;
while (temp) {
currentInputs = temp->value->forward(currentInputs);
temp = temp->next;
}
return currentInputs.get(0);
}
// Training method (simple gradient descent)
void train(LinkedList<double>& inputs, double target, double learningRate) {
double output = predict(inputs);
double error = target - output;
// Backpropagate through layers
auto temp = layers.head;
while (temp) {
Layer* currentLayer = reinterpret_cast<Layer*>(temp->value);
LinkedList<double>& currentWeights = currentLayer->getWeights();
// Update weights for each neuron in the layer
for (int i = 0; i < currentWeights.size(); i++) {
double currentWeight = currentWeights.get(i);
// Compute gradient of weights and apply to update the weights
double gradient = error * inputs.get(i); // Simplified gradient (depends on activation and layer)
double newWeight = currentWeight + learningRate * gradient;
currentLayer->updateWeight(i, newWeight);
}
// Update bias (should also consider the error signal here)
double newBias = currentLayer->getBias() + learningRate * error; // Update bias using the error
currentLayer->updateBias(newBias);
temp = temp->next;
}
}
// Print network parameters
void printParameters() {
auto temp = layers.head;
int layerIndex = 0;
while (temp) {
Layer* currentLayer = reinterpret_cast<Layer*>(temp->value);
cout << "Layer " << layerIndex << " Weights: ";
auto weightTemp = currentLayer->getWeights().head;
while (weightTemp) {
cout << weightTemp->value << " ";
weightTemp = weightTemp->next;
}
cout << endl;
cout << "Layer " << layerIndex << " Bias: " << currentLayer->getBias()
<< ", Activation: " << (currentLayer->getActivationType() == Sigmoid ? "Sigmoid" : "ReLU")
<< endl;
temp = temp->next;
layerIndex++;
}
}
};
int main() {
// Create layers with specified number of neurons and activation functions
Layer firstLayer(2, 2, ReLU); // 2 inputs, 4 neurons, ReLU
LinkedList<Layer*> layersList; // Creating layerslist object to store all the layers
layersList.append(&firstLayer);
// Create neural network with 2 inputs, and the layers defined above
NeuralNetwork nn(2, layersList);
// Training data for AND logic
double inputs[4][2] = {{1, 1}, {2, 2}, {3, 3}, {4, 4}};
double targets[4] = {2, 4, 6, 8};
double learningRate = 0.1;
int epochs = 10000000;
// Train the network
for (int epoch = 0; epoch < epochs; epoch++) {
for (int i = 0; i < 4; i++) {
LinkedList<double> inputList;
inputList.append(inputs[i][0]);
inputList.append(inputs[i][1]);
nn.train(inputList, targets[i], learningRate);
}
}
// Test the network
cout << "\nTesting Neural Network after " << epochs << " epochs:" << endl;
for (int i = 0; i < 4; i++) {
LinkedList<double> inputList;
inputList.append(inputs[i][0]);
inputList.append(inputs[i][1]);
double output = nn.predict(inputList);
cout << "Input: (" << inputs[i][0] << ", " << inputs[i][1]
<< ") -> Output: " << output << endl;
}
// Print final network parameters (weights and biases of all layers)
nn.printParameters();
cout<<"Testing on unseen data : "<<endl;
double input_test[2][2] = {{5, 5},{6,6}};
for (int i = 0; i < 2; i++) {
LinkedList<double> inputList2;
inputList2.append(input_test[i][0]);
inputList2.append(input_test[i][1]);
double output2 = nn.predict(inputList2);
cout << "Input: (" << input_test[i][0] << ", " << input_test[i][1]
<< ") -> Output: " << output2 << endl;
}
return 0;
}