-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cc
More file actions
101 lines (87 loc) · 2.46 KB
/
main.cc
File metadata and controls
101 lines (87 loc) · 2.46 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
#include <iostream>
#include <vector>
#include <cmath>
#include <cstdlib>
using namespace std;
class NeuralNetwork
{
public:
vector<vector<int>> quantized_weights;
vector<int> quantized_biases;
double scale_factor;
int zero_point;
NeuralNetwork(int inputSize, int outputSize, int og_start, int og_end, int new_start, int new_end)
{
scale_factor = (double)(new_start - new_end) / (og_start - og_end);
zero_point = new_start - round(scale_factor * og_start);
for (int i = 0; i < inputSize; ++i)
{
vector<int> weightRow(outputSize);
for (int j = 0; j < outputSize; ++j)
{
double randWeight = randomWeight();
weightRow[j] = quantize(randWeight);
}
quantized_weights.push_back(weightRow);
}
for (int i = 0; i < outputSize; ++i)
{
double randBias = randomWeight();
quantized_biases.push_back(quantize(randBias));
}
}
vector<int> forward(const vector<int> &inputs)
{
vector<int> outputs(quantized_weights[0].size(), 0);
for (size_t i = 0; i < quantized_weights[0].size(); ++i)
{
for (size_t j = 0; j < quantized_weights.size(); ++j)
{
outputs[i] += inputs[j] * quantized_weights[j][i];
}
outputs[i] += quantized_biases[i];
}
return outputs;
}
private:
double randomWeight()
{
return (rand() / double(RAND_MAX)) * 2 - 1;
}
int quantize(double value)
{
return round(value * scale_factor + zero_point);
}
};
int main(int argc, char *argv[])
{
if (argc != 5)
{
cout << "Usage: " << argv[0] << " <og_start> <og_end> <new_start> <new_end>" << endl;
return 1;
}
int og_start = atoi(argv[1]);
int og_end = atoi(argv[2]);
int new_start = atoi(argv[3]);
int new_end = atoi(argv[4]);
NeuralNetwork nn(3, 2, og_start, og_end, new_start, new_end);
vector<int> inputs = {127, -64, 32};
vector<int> outputs = nn.forward(inputs);
for (int o : outputs)
{
cout << o << " ";
}
cout << endl;
void plotGraph(const vector<int> &data, const string &label)
{
vector<int> x(data.size());
for (size_t i = 0; i < data.size(); ++i)
{
x[i] = i;
};
plt::bar(x, data);
plt::title(label);
plt::show();
};
return 0;
}