-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPredictingfromTrainedwts.cpp
133 lines (113 loc) · 3.22 KB
/
PredictingfromTrainedwts.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
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
#include <iostream>
#include <fstream>
#include <vector>
#include <math.h>
using namespace std;
float sigmoid(float x){
return 1.0f / (1.0f + exp(-x));
}
int main()
{
// Define the structure of the weights and biases
int numInputs = 2;
int numHidden = 2;
int numOutputs = 1;
float loadedHiddenWeights[numInputs][numHidden];
float loadedHiddenBiases[numHidden];
float loadedOutputWeights[numHidden][numOutputs];
float loadedOutputBiases[numOutputs];
// Load weights and biases from the text file
std::ifstream inputFile("weights_and_biases.txt");
if (inputFile.is_open())
{
for (int i = 0; i < numHidden; i++)
{
for (int j = 0; j < numInputs; j++)
{
inputFile >> loadedHiddenWeights[j][i];
}
inputFile >> loadedHiddenBiases[i];
}
for (int i = 0; i < numOutputs; i++)
{
for (int j = 0; j < numHidden; j++)
{
inputFile >> loadedOutputWeights[j][i];
}
inputFile >> loadedOutputBiases[i];
}
inputFile.close();
}
else
{
std::cerr << "Unable to open the input file.\n";
return 1;
}
cout << "Hidden weights: " << endl;
for (int i = 0; i < numHidden; i++)
{
for (int j = 0; j < numInputs; j++)
{
cout << loadedHiddenWeights[j][i] << " ";
}
cout << endl;
}
cout << "Hidden biases: " << endl;
for (int i = 0; i < numHidden; i++)
{
cout << loadedHiddenBiases[i] << " ";
}
cout << endl;
cout << "Output weights: " << endl;
for (int i = 0; i < numOutputs; i++)
{
for (int j = 0; j < numHidden; j++)
{
cout << loadedOutputWeights[j][i] << " ";
}
cout << endl;
}
cout << "Output biases: " << endl;
for (int i = 0; i < numOutputs; i++)
{
cout << loadedOutputBiases[i] << " ";
}
cout << endl;
// Initialize new input data
double new_input[numInputs] = {0.0, 1.0}; // Replace with your desired input values
// Initialize arrays for the hidden layer and output layer
double new_hiddenLayer[numHidden];
double new_outputLayer[numOutputs];
// Perform forward propagation
for (int j = 0; j < numHidden; j++)
{
new_hiddenLayer[j] = 0.0;
for (int k = 0; k < numInputs; k++)
{
new_hiddenLayer[j] += new_input[k] * loadedHiddenWeights[k][j];
}
new_hiddenLayer[j] += loadedHiddenBiases[j];
new_hiddenLayer[j] = sigmoid(new_hiddenLayer[j]);
}
for (int j = 0; j < numOutputs; j++)
{
new_outputLayer[j] = 0.0;
for (int k = 0; k < numHidden; k++)
{
new_outputLayer[j] += new_hiddenLayer[k] * loadedOutputWeights[k][j];
}
new_outputLayer[j] += loadedOutputBiases[j];
new_outputLayer[j] = sigmoid(new_outputLayer[j]);
}
// The result is in new_outputLayer
double prediction = new_outputLayer[0];
if (prediction >0.9){
prediction = 1;
}
else{
prediction = 0;
}
// Print the prediction
cout << "Prediction: " << prediction << endl;
return 0;
}