Advertisement
Trainlover08

include/ai_folder/ai_versions/ai_v0.2/linear_regression.cpp

Oct 30th, 2024
19
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.78 KB | None | 0 0
  1. // (include/ai_folder/ai_versions/ai_v0.2/linear_regression.cpp)
  2. // another testbed that I like to use
  3.  
  4.  
  5. #include <iostream>
  6. #include <vector>
  7. #include <cmath>
  8. #include <random>
  9. #include <cassert>
  10. #include <algorithm>
  11.  
  12. #include "neural_network.cpp"
  13.  
  14. using namespace std;
  15.  
  16. vector<vector<double>> generate_data(int num_samples) {
  17. vector<vector<double>> data(num_samples, vector<double>(2));
  18. random_device rd;
  19. mt19937 gen(rd());
  20. uniform_real_distribution<> dis(-10, 10);
  21.  
  22. for (int i = 0; i < num_samples; ++i) {
  23. data[i][0] = dis(gen);
  24. data[i][1] = 3.0 * data[i][0] + 5.0; // y = 3x + 5 + noise
  25. }
  26. return data;
  27. }
  28.  
  29. vector<vector<double>> extract_inputs(const vector<vector<double>>& data) {
  30. vector<vector<double>> inputs(data.size(), vector<double>(1));
  31. for (size_t i = 0; i < data.size(); ++i) {
  32. inputs[i][0] = data[i][0];
  33. }
  34. return inputs;
  35. }
  36.  
  37. vector<vector<double>> extract_outputs(const vector<vector<double>>& data) {
  38. vector<vector<double>> outputs(data.size(), vector<double>(1));
  39. for (size_t i = 0; i < data.size(); ++i) {
  40. outputs[i][0] = data[i][1];
  41. }
  42. return outputs;
  43. }
  44.  
  45. double mean_squared_error(const vector<vector<double>>& predicted, const vector<vector<double>>& actual) {
  46. double mse = 0.0;
  47. for (size_t i = 0; i < predicted.size(); ++i) {
  48. mse += pow(predicted[i][0] - actual[i][0], 2);
  49. }
  50. return mse / predicted.size();
  51. }
  52.  
  53. void linear_regression_test() {
  54. AdamWOptimizer optimizer(0.05, 0.9, 0.999, 0.001, 1e-4);
  55. NeuralNetwork nn;
  56. nn.add_layer(Layer(1, 1, "linear", optimizer));
  57.  
  58. // Generate data
  59. vector<vector<double>> data = generate_data(100);
  60. vector<vector<double>> inputs = extract_inputs(data);
  61. vector<vector<double>> targets = extract_outputs(data);
  62.  
  63. double CLIP_THRESHOLD = 1.05;
  64.  
  65. // Training
  66. for (int epoch = 0; epoch <= 70; ++epoch) {
  67. auto predictions = nn.forward(inputs);
  68. vector<vector<double>> errors(predictions.size(), vector<double>(1));
  69. for (size_t i = 0; i < predictions.size(); ++i) {
  70. errors[i][0] = -predictions[i][0] + targets[i][0];
  71. }
  72. nn.backward(errors, CLIP_THRESHOLD);
  73. nn.update_weights();
  74.  
  75. if (epoch % 10 == 0) {
  76. double mse = mean_squared_error(predictions, targets);
  77. std::cout << "Epoch " << epoch << ", MSE: " << mse << endl;
  78.  
  79. auto final_predictions = nn.forward(inputs);
  80.  
  81. vector<vector<double>> final_input = {{4}};
  82.  
  83. vector<vector<double>> intermediate_output = nn.forward(final_input);
  84.  
  85. cout << "(4 * 3) + 5 = " << intermediate_output[0][0] << endl;
  86. }
  87. }
  88. }
  89.  
  90. int main() {
  91. linear_regression_test();
  92. return 0;
  93. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement