├── .gitignore ├── LICENSE.txt ├── README.md ├── src └── AI │ ├── RL │ ├── DQN.cpp │ ├── DQN.hpp │ ├── Environment.cpp │ ├── Environment.hpp │ ├── IntelligentAgent.cpp │ ├── IntelligentAgent.hpp │ ├── Kbandits.cpp │ ├── Kbandits.hpp │ ├── TicTacToe.cpp │ └── TicTacToe.hpp │ ├── classical │ ├── genetic_programming.cpp │ ├── genetic_programming.hpp │ ├── linear_regression.cpp │ ├── linear_regression.hpp │ ├── logistic_regression.cpp │ └── logistic_regression.hpp │ ├── datamining │ ├── kmeans.cpp │ └── kmeans.hpp │ ├── datasets │ ├── CIFAR10_loader.cpp │ ├── CIFAR10_loader.hpp │ ├── CSV.cpp │ ├── CSV.hpp │ ├── MNIST_loader.cpp │ ├── MNIST_loader.hpp │ ├── mnist_binary_loader.cpp │ └── mnist_binary_loader.hpp │ ├── deeplearning │ ├── .vscode │ │ └── launch.json │ ├── Addition.cpp │ ├── Addition.hpp │ ├── Autoencoder.cpp │ ├── Autoencoder.hpp │ ├── Averagepooling.cpp │ ├── Averagepooling.hpp │ ├── CPU_backend.cpp │ ├── CPU_backend.hpp │ ├── CUDA_backend.cu │ ├── CUDA_backend.hpp │ ├── CapsulesDense.cpp │ ├── CapsulesDense.hpp │ ├── Concatenate.cpp │ ├── Concatenate.hpp │ ├── Convolution.cpp │ ├── Convolution.hpp │ ├── Cost.cpp │ ├── Cost.hpp │ ├── DataAugmentation.cpp │ ├── DataAugmentation.hpp │ ├── Dropout.cpp │ ├── Dropout.hpp │ ├── Linear.cpp │ ├── Linear.hpp │ ├── Maxpooling.cpp │ ├── Maxpooling.hpp │ ├── NetworkNode.cpp │ ├── NetworkNode.hpp │ ├── NeuralNetwork.cpp │ ├── NeuralNetwork.hpp │ ├── Normalization.cpp │ ├── Normalization.hpp │ ├── Operation.cpp │ ├── Operation.hpp │ ├── Optimizer.cpp │ ├── Optimizer.hpp │ ├── OptimizerDFA.cpp │ ├── OptimizerDFA.hpp │ ├── OptimizerSDG.cpp │ ├── OptimizerSDG.hpp │ ├── Partial.cpp │ ├── Partial.hpp │ ├── Recurrent.cpp │ ├── Recurrent.hpp │ ├── Relu.cpp │ ├── Relu.hpp │ ├── ResidualBlock.cpp │ ├── ResidualBlock.hpp │ ├── Selu.cpp │ ├── Selu.hpp │ ├── Sigmoid.cpp │ ├── Sigmoid.hpp │ ├── Softmax.cpp │ ├── Softmax.hpp │ ├── Tanh.cpp │ ├── Tanh.hpp │ ├── Variable.cpp │ ├── Variable.hpp │ ├── WeightRegularization.cpp │ └── WeightRegularization.hpp │ ├── optimization │ ├── GeneticAlgorithm.cpp │ ├── GeneticAlgorithm.hpp │ ├── pso.cpp │ └── pso.hpp │ ├── python_interface │ ├── ailib.py │ ├── ailib.pyc │ ├── ailib.tmpcpp │ ├── openai_gym.py │ ├── setup.py │ ├── test.py │ └── test.tmp │ ├── util │ ├── Files.cpp │ ├── Files.hpp │ ├── IOData.cpp │ ├── IOData.hpp │ ├── Macros.hpp │ ├── Point.cpp │ ├── Point.hpp │ ├── Tensor.cpp │ ├── Tensor.hpp │ ├── TensorCUDA.cu │ ├── TensorCUDA.hpp │ ├── Util.cpp │ ├── Util.hpp │ ├── dirent_win.h │ ├── ensure.cpp │ └── ensure.hpp │ └── visualization │ ├── Bitmap.cpp │ ├── Bitmap.hpp │ ├── TextRendering.cpp │ ├── TextRendering.hpp │ ├── font8x8_basic.h │ ├── stb_image.h │ ├── stb_image_write.h │ ├── visualization.cpp │ └── visualization.hpp └── test ├── Bitmap ├── main ├── main.cpp └── makefile ├── CIFAR10 ├── CIFAR10 ├── CIFAR10.cpp └── makefile ├── CIFAR10_cuda ├── CIFAR10_build_CUDA.sh ├── CIFAR10_cuda ├── CIFAR10_cuda.cpp ├── loss.txt ├── makefile ├── smallCIFAR_1900.net └── test.txt ├── CUDA_tests ├── CUDA_tests.cu └── CUDA_tests.sh ├── IOData_tests ├── iotest ├── iotest.cpp └── iotest.sh ├── MNIST ├── MNIST ├── MNIST.cpp └── makefile ├── MNIST_cuda ├── MNIST.net ├── MNIST_cuda ├── MNIST_cuda.cpp └── makefile ├── RL_kbandits ├── kbandits ├── kbandits.cpp └── kbandits.sh ├── RL_tictactoe ├── TicTacToe.cpp ├── TicTacToe.sh └── tictactoe ├── RNN_PI ├── PI ├── PI.cpp └── PI_build.sh ├── XOR ├── XOR ├── XOR.cpp └── makefile ├── charRNN ├── charRNN ├── charRNN.cpp ├── charRNN_CUDA.cpp ├── charRNN_CUDA_build.sh ├── charRNN_build.sh └── dataset.txt ├── clock.h ├── data_augmentation ├── data_augmentation ├── data_augmentation.cpp └── makefile ├── data_augmentation_cuda ├── data_augmentation_cuda ├── data_augmentation_cuda.cpp └── makefile ├── fashion_MNIST ├── fashion_MNIST ├── fashion_MNIST.cpp └── makefile ├── genetic_alg ├── data.png ├── evolution ├── evolution_build.sh ├── evolution_test.cpp └── result.png ├── genetic_programming ├── gp_build.sh ├── gp_play.sh ├── gp_test └── gp_test.cpp ├── kmeans ├── kmeans ├── kmeans_build.sh └── kmeans_test.cpp ├── linear_logistic_regression ├── linear_reg_mnist ├── linear_reg_mnist.cpp └── linear_reg_mnist.sh └── particle_swarm_optimization ├── pso_build.sh └── pso_test.cpp /.gitignore: -------------------------------------------------------------------------------- 1 | test/*/obj/* 2 | -------------------------------------------------------------------------------- /LICENSE.txt: -------------------------------------------------------------------------------- 1 | License 2 | ====== 3 | The MIT License (MIT) 4 | 5 | Copyright (c) 2023 Carlo Meroni 6 | 7 | Permission is hereby granted, free of charge, to any person obtaining a copy 8 | of this software and associated documentation files (the "Software"), to deal 9 | in the Software without restriction, including without limitation the rights 10 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 11 | copies of the Software, and to permit persons to whom the Software is 12 | furnished to do so, subject to the following conditions: 13 | 14 | The above copyright notice and this permission notice shall be included in 15 | all copies or substantial portions of the Software. 16 | 17 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 18 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 19 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 20 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 21 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 22 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 23 | THE SOFTWARE. 24 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | Artificial intelligence library 2 | ======================== 3 | My C++ deep learning framework (with GPU support) & other machine learning algorithms implementations 4 | 5 | DeepLearning Operations 6 | -------------- 7 | 1. Convolution 8 | 2. Dropout 9 | 3. Softmax 10 | 4. Recurrent 11 | 5. Linear 12 | 6. Sigmoid, Tanh, Relu, Selu activations 13 | 7. Layer normalization 14 | 8. Addition 15 | 9. Concatenation 16 | 10. Maxpooling 17 | 11. Averagepooling 18 | 12. ResidualBlock 19 | 13. Autoencoder 20 | 14. L1 e L2 regularizations 21 | 15. Gradient clipping 22 | 23 | Neural network optimizers 24 | -------------- 25 | 1. Stochastic gradient descent with minibatch and momentum 26 | 2. Direct Feedback Alignment 27 | 28 | Deep Reinforcement Learning agents 29 | --------------- 30 | 1. Deep Qlearning Agents 31 | 32 | Deep Reinforcement Learning environments 33 | --------------- 34 | 1. Kbandits 35 | 2. TicTacToe 36 | 37 | Optimization algorithms 38 | ------------- 39 | 1. Genetic algorithms (with multi-core and multi-nodes features for high performance) 40 | 2. Particle Swarm Optimization 41 | 42 | Other machine learning algorithms 43 | ------------ 44 | 1. linear regression 45 | 2. logistic regression 46 | 3. genetic programming 47 | 48 | Data Mining 49 | ------------ 50 | 1. K-means clustering 51 | 52 | Visualization 53 | ----------- 54 | 1. Bitmap class for loading, saving and processing multiple image formats 55 | 2. Visualizaiton namespace for fast data visualization 56 | 57 | License 58 | ====== 59 | The MIT License (MIT) 60 | 61 | Copyright (c) 2023 Carlo Meroni 62 | 63 | Permission is hereby granted, free of charge, to any person obtaining a copy 64 | of this software and associated documentation files (the "Software"), to deal 65 | in the Software without restriction, including without limitation the rights 66 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 67 | copies of the Software, and to permit persons to whom the Software is 68 | furnished to do so, subject to the following conditions: 69 | 70 | The above copyright notice and this permission notice shall be included in 71 | all copies or substantial portions of the Software. 72 | 73 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 74 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 75 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 76 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 77 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 78 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 79 | THE SOFTWARE. 80 | 81 | -------------------------------------------------------------------------------- /src/AI/RL/Environment.cpp: -------------------------------------------------------------------------------- 1 | //////////////////////////////////////////////////////////// 2 | /// INCLUDES 3 | //////////////////////////////////////////////////////////// 4 | #include "Environment.hpp" 5 | #include "../util/ensure.hpp" 6 | 7 | //////////////////////////////////////////////////////////// 8 | /// NAMESPACE AI 9 | //////////////////////////////////////////////////////////// 10 | namespace ai 11 | { 12 | 13 | //////////////////////////////////////////////////////////// 14 | Environment::~Environment() {} 15 | 16 | //////////////////////////////////////////////////////////// 17 | void Environment::observe(ai::Tensor_float& experience, float& previous_reward) { ensure(false); } 18 | 19 | //////////////////////////////////////////////////////////// 20 | void Environment::act(ai::Tensor_float action) { ensure(false); } 21 | 22 | //////////////////////////////////////////////////////////// 23 | const unsigned int Environment::input_width() const { return _input_width; } 24 | 25 | //////////////////////////////////////////////////////////// 26 | const unsigned int Environment::input_height() const { return _input_height; } 27 | 28 | //////////////////////////////////////////////////////////// 29 | const unsigned int Environment::input_depth() const { return _input_depth; } 30 | 31 | //////////////////////////////////////////////////////////// 32 | const unsigned int Environment::input_size() const { return _input_width * _input_height * _input_depth; } 33 | 34 | //////////////////////////////////////////////////////////// 35 | const unsigned int Environment::actions_count() const { return _actions_count; } 36 | 37 | } /* namespace ai */ 38 | -------------------------------------------------------------------------------- /src/AI/RL/Environment.hpp: -------------------------------------------------------------------------------- 1 | #ifndef ENVIRONMENT_HPP 2 | #define ENVIRONMENT_HPP 3 | 4 | //////////////////////////////////////////////////////////// 5 | /// INCLUDES 6 | //////////////////////////////////////////////////////////// 7 | #include "../util/Tensor.hpp" 8 | 9 | //////////////////////////////////////////////////////////// 10 | /// NAMESPACE AI 11 | //////////////////////////////////////////////////////////// 12 | namespace ai 13 | { 14 | 15 | class Environment 16 | { 17 | public: 18 | virtual ~Environment(); 19 | virtual void observe(ai::Tensor_float& experience, float& previous_reward); 20 | virtual void act(ai::Tensor_float action); 21 | const unsigned int input_width() const; 22 | const unsigned int input_height() const; 23 | const unsigned int input_depth() const; 24 | const unsigned int input_size() const; 25 | const unsigned int actions_count() const; 26 | 27 | protected: 28 | unsigned int _input_width, _input_height, _input_depth; 29 | unsigned int _actions_count; 30 | }; 31 | 32 | } /* namespace ai */ 33 | 34 | #endif /* end of include guard: ENVIRONMENT_HPP */ 35 | 36 | -------------------------------------------------------------------------------- /src/AI/RL/IntelligentAgent.cpp: -------------------------------------------------------------------------------- 1 | //////////////////////////////////////////////////////////// 2 | /// INCLUDES 3 | //////////////////////////////////////////////////////////// 4 | #include "IntelligentAgent.hpp" 5 | 6 | //////////////////////////////////////////////////////////// 7 | /// NAMESPACE AI 8 | //////////////////////////////////////////////////////////// 9 | namespace ai 10 | { 11 | 12 | int IntelligentAgent::act(ai::Tensor_float& state) { return 0; } 13 | int IntelligentAgent::act(ai::Tensor_float& state, std::vector& allowed_actions) { return 0; } 14 | void IntelligentAgent::teach(ai::Tensor_float& state, const int actionid) {} 15 | void IntelligentAgent::observe(ai::Tensor_float& newstate, const float oldreward) {} 16 | 17 | } /* namespace ai */ 18 | -------------------------------------------------------------------------------- /src/AI/RL/IntelligentAgent.hpp: -------------------------------------------------------------------------------- 1 | #ifndef INTELLIGENTAGENT_HPP 2 | #define INTELLIGENTAGENT_HPP 3 | 4 | //////////////////////////////////////////////////////////// 5 | /// INCLUDES 6 | //////////////////////////////////////////////////////////// 7 | #include "../util/Tensor.hpp" 8 | #include 9 | 10 | //////////////////////////////////////////////////////////// 11 | /// NAMESPACE AI 12 | //////////////////////////////////////////////////////////// 13 | namespace ai 14 | { 15 | class IntelligentAgent 16 | { 17 | public: 18 | virtual int act(ai::Tensor_float& state); 19 | virtual int act(ai::Tensor_float& state, std::vector& allowed_actions); 20 | virtual void teach(ai::Tensor_float& state, const int actionid); 21 | virtual void observe(ai::Tensor_float& newstate, const float oldreward); 22 | }; 23 | 24 | } /* namespace ai */ 25 | 26 | #endif /* end of include guard: INTELLIGENTAGENT_HPP */ 27 | 28 | -------------------------------------------------------------------------------- /src/AI/RL/Kbandits.cpp: -------------------------------------------------------------------------------- 1 | //////////////////////////////////////////////////////////// 2 | /// INCLUDES 3 | //////////////////////////////////////////////////////////// 4 | #include "Kbandits.hpp" 5 | #include "../util/Util.hpp" 6 | 7 | //////////////////////////////////////////////////////////// 8 | /// NAMESPACE AI 9 | //////////////////////////////////////////////////////////// 10 | namespace ai 11 | { 12 | 13 | //////////////////////////////////////////////////////////// 14 | Kbandits::Kbandits(int bandits_count, const float min_reward, const float max_reward) 15 | { 16 | ensure(min_reward < max_reward); 17 | ensure(min_reward >= -1.f && min_reward <= 1.f); 18 | ensure(max_reward >= -1.f && max_reward <= 1.f); 19 | ensure(bandits_count > 0 && bandits_count < 1000); 20 | _input_width = 1; 21 | _input_height = 1; 22 | _input_depth = 1; 23 | _actions_count = bandits_count; 24 | _reward = 0; 25 | _reward_probability = std::vector(bandits_count); 26 | fill_reward_vector(min_reward, max_reward); 27 | shuffle_reward_vector(); 28 | } 29 | 30 | //////////////////////////////////////////////////////////// 31 | void Kbandits::fill_reward_vector(float min_reward, float max_reward) 32 | { 33 | for (unsigned int i = 0; i < _reward_probability.size(); i++) 34 | _reward_probability[i] = min_reward + (max_reward - min_reward) * ((float)i / (float)_reward_probability.size()); 35 | } 36 | 37 | //////////////////////////////////////////////////////////// 38 | void Kbandits::shuffle_reward_vector() 39 | { 40 | if (_reward_probability.size() <= 1) return; 41 | 42 | float tmp_value = 0; 43 | unsigned int tmp_index = 0; 44 | for (unsigned int i = 0; i < _reward_probability.size(); i++) { 45 | tmp_index = ai::util::randint() % _reward_probability.size(); 46 | if (tmp_index == i) continue; 47 | tmp_value = _reward_probability[i]; 48 | _reward_probability[i] = _reward_probability[tmp_index]; 49 | _reward_probability[tmp_index] = tmp_value; 50 | } 51 | } 52 | 53 | //////////////////////////////////////////////////////////// 54 | void Kbandits::observe(ai::Tensor_float& experience, float& previous_reward) 55 | { 56 | experience.setshape(_input_width, _input_height, _input_depth); 57 | experience.fill(1); 58 | previous_reward = _reward; 59 | } 60 | 61 | //////////////////////////////////////////////////////////// 62 | void Kbandits::act(int action_id) 63 | { 64 | ensure(action_id >= 0 && action_id < (int)_actions_count); 65 | if (ai::util::randf() < _reward_probability[action_id]) _reward = 1; 66 | else _reward = 0; 67 | } 68 | 69 | //////////////////////////////////////////////////////////// 70 | const std::vector Kbandits::get_reward_prob_vector() 71 | { 72 | return _reward_probability; 73 | } 74 | 75 | } /* namespace ai */ 76 | -------------------------------------------------------------------------------- /src/AI/RL/Kbandits.hpp: -------------------------------------------------------------------------------- 1 | #ifndef KBANDITS_HPP 2 | #define KBANDITS_HPP 3 | 4 | //////////////////////////////////////////////////////////// 5 | /// INCLUDES 6 | //////////////////////////////////////////////////////////// 7 | #include "Environment.hpp" 8 | #include 9 | 10 | //////////////////////////////////////////////////////////// 11 | /// NAMESPACE AI 12 | //////////////////////////////////////////////////////////// 13 | namespace ai 14 | { 15 | 16 | class Kbandits : public Environment 17 | { 18 | public: 19 | Kbandits(int bandits_count, const float min_reward = 0.3, const float max_reward = 0.8); 20 | void observe(ai::Tensor_float& experience, float& previous_reward); 21 | void act(int action_id); 22 | const std::vector get_reward_prob_vector(); 23 | private: 24 | void fill_reward_vector(float min_reward, float max_reward); 25 | void shuffle_reward_vector(); 26 | std::vector _reward_probability; 27 | float _reward; 28 | }; 29 | 30 | } /* namespace ai */ 31 | 32 | #endif /* end of include guard: KBANDITS_HPP */ 33 | 34 | -------------------------------------------------------------------------------- /src/AI/RL/TicTacToe.hpp: -------------------------------------------------------------------------------- 1 | #ifndef TICTACTOE_HPP 2 | #define TICTACTOE_HPP 3 | 4 | //////////////////////////////////////////////////////////// 5 | /// INCLUDES 6 | //////////////////////////////////////////////////////////// 7 | #include "Environment.hpp" 8 | #include 9 | 10 | 11 | //////////////////////////////////////////////////////////// 12 | /// NAMESPACE AI 13 | //////////////////////////////////////////////////////////// 14 | namespace ai 15 | { 16 | 17 | class TicTacToe : public Environment 18 | { 19 | public: 20 | 21 | enum BoardState 22 | { 23 | Empty, 24 | AgentMark, 25 | OpponentMark 26 | }; 27 | 28 | enum Winner 29 | { 30 | Unknown, 31 | Agent, 32 | Opponent, 33 | Draw 34 | }; 35 | 36 | TicTacToe(); 37 | void observe(ai::Tensor_float& experience, float& previous_reward); 38 | void act(int action_id); 39 | void user_play(int actionid, Tensor_float& experience); 40 | const std::vector& act_mask(); 41 | void print_table(); 42 | const Winner getWinner(); 43 | 44 | void _unit_testing(); 45 | 46 | 47 | private: 48 | void recalculate_act_mask(); 49 | void clear_table(); 50 | void flip_table(); 51 | void compute_winner(); 52 | float reward_function(); 53 | void opponent_take_action(); 54 | 55 | std::vector _board_states; 56 | std::vector _act_mask; 57 | Winner _winner; 58 | float _reward; 59 | 60 | static const int board_size = 9; 61 | static const int board_width = 3; 62 | static const int board_height = 3; 63 | }; 64 | 65 | } /* namespace ai */ 66 | 67 | #endif /* end of include guard: TICTACTOE_HPP */ 68 | 69 | -------------------------------------------------------------------------------- /src/AI/classical/genetic_programming.hpp: -------------------------------------------------------------------------------- 1 | #ifndef GENETIC_PROGRAMMING_HPP 2 | #define GENETIC_PROGRAMMING_HPP 3 | 4 | //////////////////////////////////////////////////////////// 5 | /// INCLUDES 6 | //////////////////////////////////////////////////////////// 7 | #include 8 | #include 9 | 10 | //////////////////////////////////////////////////////////// 11 | /// NAMESPACE AI 12 | //////////////////////////////////////////////////////////// 13 | namespace ai 14 | { 15 | //////////////////////////////////////////////////////////// 16 | /// NAMESPACE GENETIC PROGRAMMING 17 | //////////////////////////////////////////////////////////// 18 | namespace gp 19 | { 20 | 21 | enum NodeType 22 | { 23 | Operation, 24 | Variable, 25 | Constant 26 | }; 27 | 28 | enum OperationType 29 | { 30 | Summation, 31 | Subtraction, 32 | Multiplication, 33 | Division, 34 | OperationsCount 35 | }; 36 | 37 | struct ExpressionNode { 38 | int node_type; 39 | int operation_type; 40 | int variable_id; 41 | float value; 42 | ExpressionNode* child_left; 43 | ExpressionNode* child_right; 44 | }; 45 | 46 | std::string program_parse(ExpressionNode* root); 47 | float program_evaluate(ExpressionNode* root, const std::vector& variables); 48 | ExpressionNode* program_random_initialization(int depth, int variables, float min_constant, float max_constant); 49 | ExpressionNode* program_crossover_initialization(ExpressionNode* first_root, ExpressionNode* second_root); 50 | ExpressionNode* program_copy_initialization(ExpressionNode* root); 51 | void program_mutate(ExpressionNode*& root, float mutation_probability, int variables_count, float min_constant, float max_constant); 52 | void program_free(ExpressionNode* node); 53 | 54 | } /* namespace gp */ 55 | 56 | } /* namespace ai */ 57 | 58 | #endif /* end of include guard: GENETIC_PROGRAMMING_HPP */ 59 | 60 | -------------------------------------------------------------------------------- /src/AI/classical/linear_regression.hpp: -------------------------------------------------------------------------------- 1 | #ifndef LINEAR_REGRESSION_HPP 2 | #define LINEAR_REGRESSION_HPP 3 | 4 | #include "../util/Tensor.hpp" 5 | 6 | //////////////////////////////////////////////////////////// 7 | /// NAMESPACE AI 8 | //////////////////////////////////////////////////////////// 9 | namespace ai 10 | { 11 | 12 | class linear_regression 13 | { 14 | public: 15 | linear_regression(const unsigned int input_size, const unsigned int output_size); 16 | linear_regression(const std::string filepath); 17 | const Tensor_float& predict(const Tensor_float input); 18 | void fit(Tensor_float inputs, const Tensor_float targets, const float starting_learningrate = 0.01, 19 | const unsigned int epochs = 20, const bool verbose=true); 20 | const Tensor_float& get_output(); 21 | void save(const std::string filepath); 22 | 23 | private: 24 | unsigned int _input_size, _output_size; 25 | Tensor_float _weights, _bias; 26 | Tensor_float _outputs; 27 | Tensor_float _errors; 28 | }; 29 | 30 | } /* namespace ai */ 31 | 32 | #endif /* end of include guard: LINEAR_REGRESSION_HPP */ 33 | 34 | -------------------------------------------------------------------------------- /src/AI/classical/logistic_regression.hpp: -------------------------------------------------------------------------------- 1 | #ifndef LOGISTIC_REGRESSION_HPP 2 | #define LOGISTIC_REGRESSION_HPP 3 | 4 | #include "../util/Tensor.hpp" 5 | #include 6 | 7 | //////////////////////////////////////////////////////////// 8 | /// NAMESPACE AI 9 | //////////////////////////////////////////////////////////// 10 | namespace ai 11 | { 12 | 13 | class logistic_regression 14 | { 15 | public: 16 | logistic_regression(); 17 | logistic_regression(const unsigned int input_size, const unsigned int output_size); 18 | logistic_regression(const std::string filepath); 19 | const Tensor_float& predict(const Tensor_float input); 20 | void fit(Tensor_float inputs, Tensor_float targets, const float starting_learningrate = 0.01, const unsigned int epochs = 20, const bool verbose=true); 21 | void test(Tensor_float inputs, Tensor_float targets); 22 | const float fit_single_sample(Tensor_float input, Tensor_float target, const float learningrate); 23 | const Tensor_float& get_output(); 24 | void save(const std::string filepath); 25 | void load(std::ifstream& filestream); 26 | void save(std::ofstream& filestream); 27 | 28 | private: 29 | const float sigmoid(const float x); 30 | const float sigmoid_deriv(const float x); 31 | unsigned int _input_size, _output_size; 32 | Tensor_float _weights, _bias; 33 | Tensor_float _outputs; 34 | Tensor_float _errors; 35 | }; 36 | 37 | } /* namespace ai */ 38 | 39 | #endif /* end of include guard: LOGISTIC_REGRESSION_HPP */ 40 | 41 | -------------------------------------------------------------------------------- /src/AI/datamining/kmeans.hpp: -------------------------------------------------------------------------------- 1 | #ifndef KMEANS_HPP 2 | #define KMEANS_HPP 3 | 4 | //////////////////////////////////////////////////////////// 5 | /// INCLUDES 6 | //////////////////////////////////////////////////////////// 7 | #include 8 | #include 9 | #include 10 | 11 | //////////////////////////////////////////////////////////// 12 | /// NAMESPACE AI 13 | //////////////////////////////////////////////////////////// 14 | namespace ai 15 | { 16 | 17 | //Shortcuts 18 | typedef std::vector vec; 19 | typedef std::vector< vec > vec2; 20 | 21 | //////////////////////////////////////////////////////////// 22 | /// \brief K-means clustering is a method of vector 23 | /// quantization, originally from signal processing, 24 | /// that is popular for cluster analysis in data mining. 25 | /// k-means clustering aims to partition n observations 26 | /// into k clusters in which each observation belongs to 27 | /// the cluster with the nearest mean, serving as a 28 | /// prototype of the cluster 29 | /// 30 | //////////////////////////////////////////////////////////// 31 | class kmeans 32 | { 33 | public: 34 | //////////////////////////////////////////////////////////// 35 | /// \brief Construct kmeans model by passing the number of 36 | /// clusters k, the number of dimensions of the datavecs 37 | /// and the centroid initialization mean and deviation 38 | /// 39 | //////////////////////////////////////////////////////////// 40 | kmeans(int k, int dimensions, float init_mean = 0.0, float init_dev = 1.0); 41 | 42 | //////////////////////////////////////////////////////////// 43 | /// \brief Fit datavecs into k-clusters 44 | /// 45 | //////////////////////////////////////////////////////////// 46 | void fit(const std::vector< vec > &data, int max_iterations); 47 | 48 | //////////////////////////////////////////////////////////// 49 | /// \brief Fit datavecs into k-clusters with continuous 50 | /// datavecs stream 51 | /// 52 | //////////////////////////////////////////////////////////// 53 | void fit_continuous(const std::vector< vec > &data, float learningrate, int iterations); 54 | 55 | //////////////////////////////////////////////////////////// 56 | /// \brief Get number of iterations used for fitting 57 | /// the datavecs into k-clusters 58 | /// 59 | //////////////////////////////////////////////////////////// 60 | int getiterations(); 61 | 62 | //////////////////////////////////////////////////////////// 63 | /// \brief Get cluster id from datavec 64 | /// 65 | //////////////////////////////////////////////////////////// 66 | int getcluster(const vec &data); 67 | 68 | //////////////////////////////////////////////////////////// 69 | /// \brief Get centroinds position 70 | /// 71 | //////////////////////////////////////////////////////////// 72 | const std::vector< vec > &getcentroids(); 73 | 74 | //////////////////////////////////////////////////////////// 75 | /// \brief Set centroind position 76 | /// 77 | //////////////////////////////////////////////////////////// 78 | void setcentroid(int id, const vec& centroid); 79 | 80 | private: 81 | int _k, _dimensions; 82 | float _init_mean, _init_dev; 83 | int _fit_iterations; 84 | std::vector< vec > _centroids; 85 | }; 86 | 87 | } //namespace ai 88 | 89 | #endif /* end of include guard: KMEANS_HPP */ 90 | 91 | -------------------------------------------------------------------------------- /src/AI/datasets/CIFAR10_loader.cpp: -------------------------------------------------------------------------------- 1 | //////////////////////////////////////////////////////////// 2 | /// INCLUDES 3 | //////////////////////////////////////////////////////////// 4 | #include "MNIST_loader.hpp" 5 | #include "../util/Files.hpp" 6 | #include "../visualization/Bitmap.hpp" 7 | #include "CSV.hpp" 8 | 9 | //////////////////////////////////////////////////////////// 10 | /// NAMESPACE AI 11 | //////////////////////////////////////////////////////////// 12 | namespace ai 13 | { 14 | 15 | void loadCIFAR10(std::string folder_path, Tensor_float& trainingset, Tensor_float& training_labels, 16 | Tensor_float& testingset, Tensor_float& testing_labels) 17 | { 18 | const int image_area = 32 * 32; 19 | const int image_channels = 3; 20 | const int sample_size = image_area * image_channels; 21 | const int target_size = 10; 22 | 23 | //Load labels from csv files 24 | int trainingset_size = 0, testingset_size = 0; 25 | std::vector< std::vector< std::string > > training_csv = ai::loadCSV(folder_path + "/training_labels.csv"); 26 | std::vector< std::vector< std::string > > testing_csv = ai::loadCSV(folder_path + "/testing_labels.csv"); 27 | trainingset_size = training_csv.size(); 28 | testingset_size = testing_csv.size(); 29 | 30 | //Allocate tensors 31 | trainingset.setshape(sample_size, trainingset_size); 32 | training_labels.setshape(target_size, trainingset_size); 33 | training_labels.fill(0); 34 | testingset.setshape(sample_size, testingset_size); 35 | testing_labels.setshape(target_size, testingset_size); 36 | testing_labels.fill(0); 37 | 38 | //Log 39 | printf("Loading trainingset...\n"); 40 | 41 | //Load and normalize all the training images and compute the labels 42 | for (int i = 0; i < trainingset_size; i++) { 43 | Bitmap bm(folder_path + "/training/" + std::to_string(i) + ".png", Bitmap::RGB); 44 | for (int c = 0; c < image_channels; c++) 45 | for (int x = 0; x < image_area; x++) 46 | trainingset.at(i, c * image_area + x) = bm.getData()[x * image_channels + c] / 255.f; 47 | training_labels.at(i, std::stoi(training_csv[i][1])) = 1.f; 48 | if (i % (trainingset_size / 10) == 0) printf("Progress: %f\n", (double)i / (double)trainingset_size); 49 | } 50 | 51 | //Log 52 | printf("Loading testingset...\n"); 53 | 54 | //Load and normalize all the testing images and compute the labels 55 | for (int i = 0; i < testingset_size; i++) { 56 | Bitmap bm(folder_path + "/testing/" + std::to_string(i) + ".png", Bitmap::RGB); 57 | for (int c = 0; c < image_channels; c++) 58 | for (int x = 0; x < image_area; x++) 59 | testingset.at(i, c * image_area + x) = bm.getData()[x * image_channels + c] / 255.f; 60 | testing_labels.at(i, std::stoi(testing_csv[i][1])) = 1.f; 61 | if (i % (testingset_size / 10) == 0) printf("Progress: %f\n", (double)i / (double)testingset_size); 62 | } 63 | } 64 | 65 | } /* namespace ai */ 66 | -------------------------------------------------------------------------------- /src/AI/datasets/CIFAR10_loader.hpp: -------------------------------------------------------------------------------- 1 | #ifndef MNIST_LOADER_HPP 2 | #define MNIST_LOADER_HPP 3 | 4 | //////////////////////////////////////////////////////////// 5 | /// INCLUDES 6 | //////////////////////////////////////////////////////////// 7 | #include 8 | #include "../util/Tensor.hpp" 9 | 10 | //////////////////////////////////////////////////////////// 11 | /// NAMESPACE AI 12 | //////////////////////////////////////////////////////////// 13 | namespace ai 14 | { 15 | void loadCIFAR10(std::string folder_path, Tensor_float& trainingset, Tensor_float& training_labels, 16 | Tensor_float& testingset, Tensor_float& testing_labels); 17 | 18 | } /* namespace ai */ 19 | 20 | #endif /* end of include guard: MNIST_LOADER_HPP */ 21 | 22 | -------------------------------------------------------------------------------- /src/AI/datasets/CSV.cpp: -------------------------------------------------------------------------------- 1 | //////////////////////////////////////////////////////////// 2 | /// INCLUDES 3 | //////////////////////////////////////////////////////////// 4 | #include "CSV.hpp" 5 | #include 6 | #include 7 | 8 | //////////////////////////////////////////////////////////// 9 | /// NAMESPACE AI 10 | //////////////////////////////////////////////////////////// 11 | namespace ai 12 | { 13 | 14 | //////////////////////////////////////////////////////////// 15 | std::vector< std::vector > loadCSV(std::string csv_filepath) 16 | { 17 | //Create output vector 18 | std::vector< std::vector > data; 19 | 20 | //Open file 21 | std::ifstream file(csv_filepath); 22 | 23 | //Check for errors 24 | if (!file) { 25 | printf("Error, can't load CSV file of path %s\n", csv_filepath.c_str()); 26 | return data; 27 | } 28 | 29 | //Read file line by line 30 | std::string line; 31 | std::string cell; 32 | while (std::getline(file, line)) { 33 | //Add dimnesion to data 34 | data.push_back(std::vector()); 35 | 36 | //Read all the cells in the line 37 | std::stringstream lineStream(line); 38 | while (std::getline(lineStream, cell, ',')) { 39 | 40 | //Store the content of every cells 41 | data.back().push_back(cell); 42 | } 43 | } 44 | 45 | return data; 46 | } 47 | 48 | } /* namespace ai */ 49 | -------------------------------------------------------------------------------- /src/AI/datasets/CSV.hpp: -------------------------------------------------------------------------------- 1 | #ifndef CSV_HPP 2 | #define CSV_HPP 3 | 4 | #include 5 | #include 6 | 7 | //////////////////////////////////////////////////////////// 8 | /// NAMESPACE AI 9 | //////////////////////////////////////////////////////////// 10 | namespace ai 11 | { 12 | //////////////////////////////////////////////////////////// 13 | /// \brief Load a comma-separated values file in a 2D vector 14 | /// from file path 15 | /// 16 | //////////////////////////////////////////////////////////// 17 | std::vector< std::vector > loadCSV(std::string csv_filepath); 18 | 19 | } /* namespace ai */ 20 | 21 | #endif /* end of include guard: CSV_HPP */ 22 | 23 | -------------------------------------------------------------------------------- /src/AI/datasets/MNIST_loader.cpp: -------------------------------------------------------------------------------- 1 | //////////////////////////////////////////////////////////// 2 | /// INCLUDES 3 | //////////////////////////////////////////////////////////// 4 | #include "MNIST_loader.hpp" 5 | #include "mnist_binary_loader.hpp" 6 | #include "../util/Files.hpp" 7 | #include "../visualization/Bitmap.hpp" 8 | 9 | //////////////////////////////////////////////////////////// 10 | /// NAMESPACE AI 11 | //////////////////////////////////////////////////////////// 12 | namespace ai 13 | { 14 | 15 | void loadMNIST(std::string folder_path, Tensor_float& trainingset, Tensor_float& training_labels, 16 | Tensor_float& testingset, Tensor_float& testing_labels) 17 | { 18 | const int digits_count = 10; 19 | const int sample_size = 28 * 28; 20 | const int target_size = 10; 21 | 22 | //Get all files names and calculate trainingset and testingset size 23 | int trainingset_size = 0, testingset_size = 0; 24 | std::vector< std::vector > training_files(digits_count); 25 | for (int i = 0; i < digits_count; i++) { 26 | training_files[i] = ai::files::listdir(folder_path + "/training/" + std::to_string(i) + "/"); 27 | trainingset_size += (int)training_files[i].size(); 28 | } 29 | std::vector< std::vector > testing_files(digits_count); 30 | for (int i = 0; i < digits_count; i++) { 31 | testing_files[i] = ai::files::listdir(folder_path + "/testing/" + std::to_string(i) + "/"); 32 | testingset_size += (int)testing_files[i].size(); 33 | } 34 | 35 | //Allocate tensors 36 | trainingset.setshape(sample_size, trainingset_size); 37 | training_labels.setshape(target_size, trainingset_size); 38 | training_labels.fill(0); 39 | testingset.setshape(sample_size, testingset_size); 40 | testing_labels.setshape(target_size, testingset_size); 41 | testing_labels.fill(0); 42 | 43 | //Log 44 | printf("Loading trainingset...\n"); 45 | 46 | //Load and normalize all the training images and compute the labels 47 | int offset = 0; 48 | for (int i = 0; i < digits_count; i++) { 49 | for (int k = 0; k < (int)training_files[i].size(); k++) { 50 | Bitmap bm(folder_path + "/training/" + std::to_string(i) + "/" + training_files[i][k], Bitmap::MONO); 51 | bm.convertToMono(); 52 | for (int j = 0; j < sample_size; j++) 53 | trainingset.at(offset + k, j) = bm.getData()[j] / 255.f; 54 | training_labels.at(offset + k, i) = 1.f; 55 | } 56 | offset += (int)training_files[i].size(); 57 | printf("Progress: %f\n", (double)offset / (double)trainingset_size); 58 | } 59 | 60 | //Log 61 | printf("Loading testingset...\n"); 62 | 63 | //Load and normalize all the testing images and compute the labels 64 | offset = 0; 65 | for (int i = 0; i < digits_count; i++) { 66 | for (int k = 0; k < (int)testing_files[i].size(); k++) { 67 | Bitmap bm(folder_path + "/testing/" + std::to_string(i) + "/" + testing_files[i][k], Bitmap::MONO); 68 | bm.convertToMono(); 69 | for (int j = 0; j < sample_size; j++) 70 | testingset.at(offset + k, j) = bm.getData()[j] / 255.f; 71 | testing_labels.at(offset + k, i) = 1.f; 72 | } 73 | offset += (int)testing_files[i].size(); 74 | printf("Progress: %f\n", (double)offset / (double)testingset_size); 75 | } 76 | } 77 | 78 | void loadMNIST_from_binary(const std::string train_images_path, const std::string test_images_path, 79 | const std::string train_labels_path, const std::string test_labels_path, 80 | Tensor_float& trainingset, Tensor_float& training_labels, Tensor_float& testingset, Tensor_float& testing_labels) 81 | { 82 | printf("Loading MNIST dataset...\n"); 83 | 84 | //Load all the raw data 85 | mnist_binary_loader mnist(train_images_path, test_images_path, train_labels_path, test_labels_path); 86 | 87 | //Allocate trainingset tensor and training_labels tensor 88 | trainingset.setshape(28 * 28, (int)mnist.get_train_images().size()); 89 | training_labels.setshape(10, (int)mnist.get_train_images().size()); 90 | training_labels.fill(0); 91 | 92 | //Fill trainingset tensor 93 | for (int i = 0; i < (int)mnist.get_train_images().size(); i++) 94 | for (int k = 0; k < 28 * 28; k++) 95 | trainingset.at(i, k) = mnist.get_train_images()[i][k] / 255.f; 96 | 97 | //Fill training_labels tensor 98 | for (int i = 0; i < (int)mnist.get_train_images().size(); i++) 99 | training_labels.at(i, (int)mnist.get_train_labels()[i]) = 1.f; 100 | 101 | //Allocate testingset tensor and testing_labels tensor 102 | testingset.setshape(28 * 28, (int)mnist.get_test_images().size()); 103 | testing_labels.setshape(10, (int)mnist.get_test_images().size()); 104 | testing_labels.fill(0); 105 | 106 | //Fill testingset tensor 107 | for (int i = 0; i < (int)mnist.get_test_images().size(); i++) 108 | for (int k = 0; k < 28 * 28; k++) 109 | testingset.at(i, k) = mnist.get_test_images()[i][k] / 255.f; 110 | 111 | //Fill testing_labels tensor 112 | for (int i = 0; i < (int)mnist.get_test_images().size(); i++) 113 | testing_labels.at(i, (int)mnist.get_test_labels()[i]) = 1.f; 114 | } 115 | 116 | } /* namespace ai */ 117 | -------------------------------------------------------------------------------- /src/AI/datasets/MNIST_loader.hpp: -------------------------------------------------------------------------------- 1 | #ifndef MNIST_LOADER_HPP 2 | #define MNIST_LOADER_HPP 3 | 4 | //////////////////////////////////////////////////////////// 5 | /// INCLUDES 6 | //////////////////////////////////////////////////////////// 7 | #include 8 | #include "../util/Tensor.hpp" 9 | 10 | //////////////////////////////////////////////////////////// 11 | /// NAMESPACE AI 12 | //////////////////////////////////////////////////////////// 13 | namespace ai 14 | { 15 | void loadMNIST(std::string folder_path, Tensor_float& trainingset, Tensor_float& training_labels, 16 | Tensor_float& testingset, Tensor_float& testing_labels); 17 | 18 | void loadMNIST_from_binary(const std::string train_images_path, const std::string test_images_path, 19 | const std::string train_labels_path, const std::string test_labels_path, 20 | Tensor_float& trainingset, Tensor_float& training_labels, Tensor_float& testingset, Tensor_float& testing_labels); 21 | 22 | } /* namespace ai */ 23 | 24 | #endif /* end of include guard: MNIST_LOADER_HPP */ 25 | 26 | -------------------------------------------------------------------------------- /src/AI/datasets/mnist_binary_loader.cpp: -------------------------------------------------------------------------------- 1 | //////////////////////////////////////////////////////////// 2 | /// INCLUDES 3 | //////////////////////////////////////////////////////////// 4 | #include "mnist_binary_loader.hpp" 5 | #include 6 | #include 7 | #include 8 | 9 | //////////////////////////////////////////////////////////// 10 | mnist_binary_loader::mnist_binary_loader(const std::string train_images_path, const std::string test_images_path, 11 | const std::string train_labels_path, const std::string test_labels_path) 12 | { 13 | std::ifstream file(train_images_path, std::ios::binary); 14 | assert(file && "Unable to open filepath while loading mnist dataset"); 15 | load_images(file, _train_images); 16 | file = std::ifstream(test_images_path, std::ios::binary); 17 | assert(file && "Unable to open filepath while loading mnist dataset"); 18 | load_images(file, _test_images); 19 | file = std::ifstream(train_labels_path, std::ios::binary); 20 | assert(file && "Unable to open filepath while loading mnist dataset"); 21 | load_labels(file, _train_labels); 22 | file = std::ifstream(test_labels_path, std::ios::binary); 23 | assert(file && "Unable to open filepath while loading mnist dataset"); 24 | load_labels(file, _test_labels); 25 | } 26 | 27 | //////////////////////////////////////////////////////////// 28 | int big_endian_to_small(int big_endian) 29 | { 30 | return ((big_endian >> 24) &0xff) | // move byte 3 to byte 0 31 | ((big_endian << 8) &0xff0000) | // move byte 1 to byte 2 32 | ((big_endian >> 8) &0xff00) | // move byte 2 to byte 1 33 | ((big_endian << 24) &0xff000000); // byte 0 to byte 3 34 | } 35 | 36 | //////////////////////////////////////////////////////////// 37 | void mnist_binary_loader::load_images(std::ifstream& file, std::vector< std::vector >& images) 38 | { 39 | //Check first 32 bit magic number 40 | int MSB_check; 41 | file.read(reinterpret_cast(&MSB_check), sizeof(int)); 42 | MSB_check = big_endian_to_small(MSB_check); 43 | assert(MSB_check == 2051); 44 | 45 | //Get number of images 46 | int images_count; 47 | file.read(reinterpret_cast(&images_count), sizeof(int)); 48 | images_count = big_endian_to_small(images_count); 49 | 50 | //Get images with and height 51 | int images_width, images_height; 52 | file.read(reinterpret_cast(&images_width), sizeof(int)); 53 | file.read(reinterpret_cast(&images_height), sizeof(int)); 54 | images_width = big_endian_to_small(images_width); 55 | images_height = big_endian_to_small(images_height); 56 | assert(images_width == 28 && images_height == 28); 57 | 58 | //Allocate memory for all the images 59 | images = std::vector< std::vector< unsigned char> >(images_count); 60 | for (int i = 0; i < images_count; i++) 61 | images[i] = std::vector< unsigned char >(images_width * images_height); 62 | 63 | //Read data for each image 64 | for (int i = 0; i < images_count; i++) 65 | file.read(reinterpret_cast(&images[i][0]), sizeof(unsigned char) * images_width * images_height); 66 | } 67 | 68 | //////////////////////////////////////////////////////////// 69 | void mnist_binary_loader::load_labels(std::ifstream& file, std::vector& labels) 70 | { 71 | //Check first 32 bit magic number 72 | int MSB_check; 73 | file.read(reinterpret_cast(&MSB_check), sizeof(int)); 74 | MSB_check = big_endian_to_small(MSB_check); 75 | assert(MSB_check == 2049); 76 | 77 | //Get number of images 78 | int images_count; 79 | file.read(reinterpret_cast(&images_count), sizeof(int)); 80 | images_count = big_endian_to_small(images_count); 81 | 82 | //Allocate memory for all labels 83 | labels = std::vector< unsigned char >(images_count); 84 | 85 | //Read all labels values into out buffer 86 | file.read(reinterpret_cast(&labels[0]), sizeof(unsigned char) * images_count); 87 | } 88 | 89 | //////////////////////////////////////////////////////////// 90 | const std::vector< std::vector< unsigned char > >& mnist_binary_loader::get_train_images() const 91 | { 92 | return _train_images; 93 | } 94 | 95 | //////////////////////////////////////////////////////////// 96 | const std::vector< std::vector< unsigned char > >& mnist_binary_loader::get_test_images() const 97 | { 98 | return _test_images; 99 | } 100 | 101 | //////////////////////////////////////////////////////////// 102 | const std::vector< unsigned char >& mnist_binary_loader::get_train_labels() const 103 | { 104 | return _train_labels; 105 | } 106 | 107 | //////////////////////////////////////////////////////////// 108 | const std::vector< unsigned char >& mnist_binary_loader::get_test_labels() const 109 | { 110 | return _test_labels; 111 | } 112 | 113 | -------------------------------------------------------------------------------- /src/AI/datasets/mnist_binary_loader.hpp: -------------------------------------------------------------------------------- 1 | #ifndef MNIST_BINARY_LOADER_HPP 2 | #define MNIST_BINARY_LOADER_HPP 3 | 4 | //////////////////////////////////////////////////////////// 5 | /// INCLUDES 6 | //////////////////////////////////////////////////////////// 7 | #include 8 | #include 9 | 10 | class mnist_binary_loader 11 | { 12 | public: 13 | mnist_binary_loader(const std::string train_images_path, const std::string test_images_path, 14 | const std::string train_labels_path, const std::string test_labels_path); 15 | const std::vector< std::vector< unsigned char > >& get_train_images() const; 16 | const std::vector< std::vector< unsigned char > >& get_test_images() const; 17 | const std::vector< unsigned char >& get_train_labels() const; 18 | const std::vector< unsigned char >& get_test_labels() const; 19 | 20 | private: 21 | void load_images(std::ifstream& file, std::vector< std::vector >& images); 22 | void load_labels(std::ifstream& file, std::vector& labels); 23 | 24 | //Data 25 | std::vector< std::vector< unsigned char > > _train_images; 26 | std::vector< std::vector< unsigned char > > _test_images; 27 | std::vector< unsigned char > _train_labels; 28 | std::vector< unsigned char > _test_labels; 29 | }; 30 | 31 | #endif /* end of include guard: MNIST_BINARY_LOADER_HPP */ 32 | 33 | -------------------------------------------------------------------------------- /src/AI/deeplearning/.vscode/launch.json: -------------------------------------------------------------------------------- 1 | { 2 | "version": "0.2.0", 3 | "configurations": [ 4 | { 5 | "name": "(gdb) Launch", 6 | "type": "cppdbg", 7 | "request": "launch", 8 | "program": "enter program name, for example ${workspaceRoot}/a.out", 9 | "args": [], 10 | "stopAtEntry": false, 11 | "cwd": "${workspaceRoot}", 12 | "environment": [], 13 | "externalConsole": true, 14 | "MIMode": "gdb", 15 | "setupCommands": [ 16 | { 17 | "description": "Enable pretty-printing for gdb", 18 | "text": "-enable-pretty-printing", 19 | "ignoreFailures": true 20 | } 21 | ] 22 | } 23 | ] 24 | } -------------------------------------------------------------------------------- /src/AI/deeplearning/Addition.cpp: -------------------------------------------------------------------------------- 1 | //////////////////////////////////////////////////////////// 2 | /// INCLUDES 3 | //////////////////////////////////////////////////////////// 4 | #include "Addition.hpp" 5 | #include 6 | #include "../util/ensure.hpp" 7 | #ifdef CUDA_BACKEND 8 | #include "CUDA_backend.hpp" 9 | #endif 10 | 11 | //////////////////////////////////////////////////////////// 12 | /// NAMESPACE AI 13 | //////////////////////////////////////////////////////////// 14 | namespace ai 15 | { 16 | //////////////////////////////////////////////////////////// 17 | std::shared_ptr Addition::make() 18 | { 19 | return std::shared_ptr(new Addition()); 20 | } 21 | 22 | //////////////////////////////////////////////////////////// 23 | Addition::Addition() {} 24 | 25 | //////////////////////////////////////////////////////////// 26 | Addition::Addition(ai::IOData& data) 27 | { 28 | ai::IOData* size = data.findNode("size"); 29 | ensure(size != NULL); 30 | ai::IOData* width = data.findNode("width"); 31 | ensure(width != NULL); 32 | ai::IOData* height = data.findNode("height"); 33 | ensure(height != NULL); 34 | ai::IOData* depth = data.findNode("depth"); 35 | ensure(depth != NULL); 36 | size->get(_size); 37 | width->get(_width); 38 | height->get(_height); 39 | depth->get(_depth); 40 | _outputs.setshape(_width, _height, _depth); 41 | _outputs.fill(0); 42 | _errors.setshape(_size); 43 | _outputs.fill(0); 44 | } 45 | 46 | //////////////////////////////////////////////////////////// 47 | void Addition::initialize(std::vector &inputs) 48 | { 49 | //Check for errors 50 | ensure(inputs.size() > 0); 51 | _size = inputs[0]->_outputs.size(); 52 | for (int i = 0; i < (int)inputs.size(); i++) 53 | ensure_print(_size == inputs[i]->_outputs.size(), "%d %d\n", _size, inputs[i]->_outputs.size()); 54 | 55 | //Calculate size 56 | _width = inputs[0]->_outputs.width(); 57 | _height = inputs[0]->_outputs.height(); 58 | _depth = inputs[0]->_outputs.depth(); 59 | 60 | //Initialize vectors 61 | _outputs.setshape(_width, _height, _depth); 62 | _outputs.fill(0); 63 | _errors.setshape(_size); 64 | _outputs.fill(0); 65 | } 66 | 67 | //////////////////////////////////////////////////////////// 68 | void Addition::save(ai::IOData& data) 69 | { 70 | data.pushNode("size", _size); 71 | data.pushNode("width", _width); 72 | data.pushNode("height", _height); 73 | data.pushNode("depth", _depth); 74 | } 75 | 76 | //////////////////////////////////////////////////////////// 77 | void Addition::run(std::vector &inputs, const bool training) 78 | { 79 | #ifdef CUDA_BACKEND 80 | 81 | for (int i = 0; i < (int)inputs.size(); i++) { 82 | if (i == 0) TensorCUDA_float_copy(inputs[i]->_outputs, _outputs); 83 | else TensorCUDA_float_sum(inputs[i]->_outputs, _outputs); 84 | } 85 | 86 | #else 87 | for (int i = 0; i < (int)inputs.size(); i++) { 88 | 89 | //Shortcut 90 | const Tensor_float& in = inputs[i]->_outputs; 91 | 92 | if (i == 0) 93 | { 94 | for (int j = 0; j < _size; j++) 95 | _outputs[j] = in[j]; 96 | } 97 | else 98 | { 99 | for (int j = 0; j < _size; j++) 100 | _outputs[j] += in[j]; 101 | } 102 | } 103 | #endif 104 | } 105 | 106 | //////////////////////////////////////////////////////////// 107 | void Addition::backprop(std::vector &inputs) 108 | { 109 | #ifdef CUDA_BACKEND 110 | 111 | for (int i = 0; i < (int)inputs.size(); i++) 112 | TensorCUDA_float_sum(_errors, inputs[i]->_errors); 113 | 114 | #else 115 | for (int i = 0; i < (int)inputs.size(); i++) { 116 | 117 | //Shortcuts 118 | Tensor_float &out_errors = inputs[i]->_errors; 119 | 120 | //Feedforeward 121 | for (int i = 0; i < _size; i++) 122 | out_errors[i] += _errors[i]; 123 | } 124 | #endif 125 | } 126 | 127 | //////////////////////////////////////////////////////////// 128 | const Operation::Type Addition::get_type() const 129 | { 130 | return Operation::Addition; 131 | } 132 | 133 | //////////////////////////////////////////////////////////// 134 | void Addition::print() 135 | { 136 | printf("Type: Addition, Size: %d", _size); 137 | } 138 | 139 | } /* namespace ai */ 140 | -------------------------------------------------------------------------------- /src/AI/deeplearning/Addition.hpp: -------------------------------------------------------------------------------- 1 | #ifndef ADDITION_HPP 2 | #define ADDITION_HPP 3 | 4 | //////////////////////////////////////////////////////////// 5 | /// INCLUDES 6 | //////////////////////////////////////////////////////////// 7 | #include 8 | #include 9 | #include "Operation.hpp" 10 | 11 | //////////////////////////////////////////////////////////// 12 | /// NAMESPACE AI 13 | //////////////////////////////////////////////////////////// 14 | namespace ai 15 | { 16 | class Addition : public Operation 17 | { 18 | public: 19 | Addition(); 20 | Addition(ai::IOData& data); 21 | void save(ai::IOData& data); 22 | void initialize(std::vector &inputs); 23 | void run(std::vector &inputs, const bool training); 24 | void backprop(std::vector &inputs); 25 | const Operation::Type get_type() const; 26 | void print(); 27 | 28 | static std::shared_ptr make(); 29 | 30 | private: 31 | int _width, _height, _depth; 32 | }; 33 | 34 | } /* namespace ai */ 35 | 36 | #endif /* end of include guard: ADDITION_HPP */ 37 | 38 | -------------------------------------------------------------------------------- /src/AI/deeplearning/Autoencoder.hpp: -------------------------------------------------------------------------------- 1 | #ifndef Autoencoder_HPP 2 | #define Autoencoder_HPP 3 | 4 | //////////////////////////////////////////////////////////// 5 | /// INCLUDES 6 | //////////////////////////////////////////////////////////// 7 | #include 8 | #include "Operation.hpp" 9 | 10 | //////////////////////////////////////////////////////////// 11 | /// NAMESPACE AI 12 | //////////////////////////////////////////////////////////// 13 | namespace ai 14 | { 15 | class Autoencoder : public Operation 16 | { 17 | public: 18 | Autoencoder(); 19 | Autoencoder(const int size, const float noise); 20 | Autoencoder(ai::IOData& data); 21 | void initialize(std::vector &inputs); 22 | void initialize(int input_size); 23 | void save(ai::IOData& data); 24 | void run(std::vector &inputs, const bool training); 25 | void backprop(std::vector &inputs); 26 | void accumulate_deltas(std::vector &inputs); 27 | void update_parameters(const float learningrate); 28 | void reset_deltas(const double momentum); 29 | void reset_outputs(); 30 | void setFixedParameters(const bool fixedparameters); 31 | const float getPredictionError(); 32 | const Operation::Type get_type() const; 33 | void print(); 34 | 35 | static std::shared_ptr make(const int size, const float noise); 36 | 37 | 38 | #ifdef CUDA_BACKEND 39 | 40 | void run(const TensorCUDA_float& input, bool accumulate, bool training); 41 | void backprop(TensorCUDA_float& out_errors); 42 | void accumulate_deltas(const TensorCUDA_float& input); 43 | 44 | ai::TensorCUDA_float _weights, _bias, _w_deltas, _b_deltas, _prediction, _prediction_error, _hidden_errors, _noise_mask; 45 | ai::TensorCUDA_int _activations; 46 | 47 | #else 48 | 49 | void run(const Tensor_float input, bool accumulate, bool training); 50 | void backprop(Tensor_float out_errors); 51 | void accumulate_deltas(const Tensor_float input); 52 | 53 | ai::Tensor_float _weights, _bias, _w_deltas, _b_deltas, _prediction, _prediction_error, _hidden_errors, _noise_mask; 54 | ai::Tensor_int _activations; 55 | 56 | #endif 57 | float _error; 58 | float _noise; 59 | float _learningrate; 60 | bool _fixed_parameters; 61 | int _input_size; 62 | }; 63 | 64 | } /* namespace ai */ 65 | 66 | #endif /* end of include guard: Autoencoder_HPP */ 67 | 68 | -------------------------------------------------------------------------------- /src/AI/deeplearning/Averagepooling.hpp: -------------------------------------------------------------------------------- 1 | #ifndef AVERAGEPOOLING_HPP 2 | #define AVERAGEPOOLING_HPP 3 | 4 | //////////////////////////////////////////////////////////// 5 | /// INCLUDES 6 | //////////////////////////////////////////////////////////// 7 | #include 8 | #include 9 | #include "Operation.hpp" 10 | 11 | //////////////////////////////////////////////////////////// 12 | /// NAMESPACE AI 13 | //////////////////////////////////////////////////////////// 14 | namespace ai 15 | { 16 | class Averagepooling : public Operation 17 | { 18 | public: 19 | Averagepooling(const int filter_size, const int stride); 20 | Averagepooling(ai::IOData& data); 21 | void initialize(std::vector &inputs); 22 | void save(ai::IOData& data); 23 | void run(std::vector &inputs, const bool training); 24 | void backprop(std::vector &inputs); 25 | void print(); 26 | const Operation::Type get_type() const; 27 | static std::shared_ptr make(const int filter_size, const int stride); 28 | 29 | int _input_width; 30 | int _input_height; 31 | int _input_count; 32 | int _filter_size; 33 | int _stride; 34 | int _output_width; 35 | int _output_height; 36 | int _output_size; 37 | int _input_size; 38 | 39 | #ifdef CUDA_BACKEND 40 | ai::cudnn::Pooling _cuda_pooling; 41 | #else 42 | Tensor_float _average_in; 43 | #endif 44 | }; 45 | 46 | } /* namespace ai */ 47 | 48 | #endif /* end of include guard: AVERAGEPOOLING_HPP */ 49 | 50 | -------------------------------------------------------------------------------- /src/AI/deeplearning/CPU_backend.hpp: -------------------------------------------------------------------------------- 1 | #ifndef CPU_BACKEND_HPP 2 | #define CPU_BACKEND_HPP 3 | 4 | //////////////////////////////////////////////////////////// 5 | /// NAMESPACE AI 6 | //////////////////////////////////////////////////////////// 7 | namespace ai 8 | { 9 | void conv_foreward(float* weights, float* bias, float* inputs, float* outputs, int* out_in_map, int input_width, int input_height, int input_count, int stride, int output_width, int output_height, int filters_count, int filter_area); 10 | void conv_backward(float* weights, float* out_errors, float* errors, int* in_weight_map, int* in_out_map, int input_count, int output_size, int input_width, int input_height, int filter_area, int filters_count); 11 | void conv_accumulate_deltas(float* weights_deltas, float* bias_deltas, float* errors, float* inputs, float* outputs, int* out_in_map, int input_count, int input_width, int input_height, int output_size, int filter_area, int filters_count); 12 | void conv_update_parameters(float* weights, float* bias, float* weights_deltas, float* bias_deltas, int filter_area, int input_count, int filter_count, float learningrate); 13 | void linear_foreward(float* weights, float* bias, float* inputs, float* outputs, int input_size, int output_size, bool use_bias, bool accumulate); 14 | void linear_backward(float* weights, float* out_errors, float* errors, int input_size, int output_size); 15 | void linear_accumulate_deltas(float* deltas, float* inputs, float* errors, int input_size, int output_size, bool use_bias); 16 | void linear_update_parameters(float* weights, float* bias, float* deltas, float learningrate, int input_size, int output_size); 17 | void capsules_dense_foreward(float* weights, float* bias, float* inputs, float* outputs, float* input_coupling_coeff, 18 | int input_size, int input_capsule_size, int output_size, int output_capsule_size, int capsule_size, bool use_bias); 19 | void normalization_foreward(float* inputs, float* deviation, float* normalized, float* outputs, float* variance, float* gamma, float* beta, float epsilon, int size); 20 | void normalization_backward(float* errors, float* out_errors, float* deviation, float* variance, float* gamma, float* beta, float epsilon, int size); 21 | void normalization_accumulate_deltas(float* errors, float* deviation, float* variance, float* d_gamma, float* d_beta, float epsilon, int size); 22 | void normalization_update_parameters(float* gamma, float* beta, float* d_gamma, float* d_beta, float momentum, int size, float learningrate); 23 | void dropout_foreward(const float* inputs, float* outputs, const unsigned int size, const float drop_probability, const bool training); 24 | void dropout_backward(const float* errors, float* out_errors, const float* outputs, const unsigned int size, const float drop_probability); 25 | void maxpooling_foreward(float* inputs, float* outputs, int* maxbuffer, int input_width, int input_height, int input_count, int stride, int filter_size, int output_width, int output_height); 26 | void maxpooling_backward(float* out_errors, float* errors, int* maxbuffer, int input_width, int input_height, int input_count, int stride, int filter_size, int output_width, int output_height); 27 | void averagepooling_foreward(float* inputs, float* outputs, int input_width, int input_height, int input_count, int stride, int filter_size, int output_width, int output_height); 28 | void averagepooling_backward(float* out_errors, float* errors, int input_width, int input_height, int input_count, int stride, int filter_size, int output_width, int output_height); 29 | void relu_foreward(const float* inputs, float* outputs, const unsigned int size); 30 | void relu_backward(const float* errors, float* out_errors, const float* outputs, const unsigned int size); 31 | void tanh_foreward(const float* inputs, float* outputs, const unsigned int size); 32 | void tanh_backward(const float* errors, float* out_errors, const float* outputs, const unsigned int size); 33 | void sigmoid_foreward(const float* inputs, float* outputs, const unsigned int size); 34 | void sigmoid_backward(const float* errors, float* out_errors, const float* outputs, const unsigned int size); 35 | void selu_foreward(const float* inputs, float* outputs, const unsigned int size); 36 | void selu_backward(const float* errors, float* out_errors, const float* outputs, const unsigned int size); 37 | void softmax_foreward(float* inputs, float* outputs, float scale, int size, float epsilon); 38 | void softmax_backward(float* errors, float* out_errors, float* outputs, int size); 39 | void capsule_squashing_foreward(float* inputs, float* outputs, int size); 40 | void capsule_squashing_backward(float* errors, float* out_errors, float* outputs, int size); 41 | 42 | void gradient_clipping(float* deltas, int size, const float clipping_deviation); 43 | void l1_regularization(float* weights, const float l1_factor, const float learningrate, int size); 44 | void l2_regularization(float* weights, const float l2_factor, const float learningrate, int size); 45 | 46 | } /* namespace ai */ 47 | 48 | #endif /* end of include guard: CPU_BACKEND_HPP */ 49 | 50 | -------------------------------------------------------------------------------- /src/AI/deeplearning/CapsulesDense.hpp: -------------------------------------------------------------------------------- 1 | #ifndef CAPSULESDENSE_HPP 2 | #define CAPSULESDENSE_HPP 3 | 4 | //////////////////////////////////////////////////////////// 5 | /// INCLUDES 6 | //////////////////////////////////////////////////////////// 7 | #include 8 | #include "Operation.hpp" 9 | 10 | //////////////////////////////////////////////////////////// 11 | /// NAMESPACE AI 12 | //////////////////////////////////////////////////////////// 13 | namespace ai 14 | { 15 | class CapsulesDense : public Operation 16 | { 17 | public: 18 | CapsulesDense(); 19 | CapsulesDense(const int size, bool use_bias = true, const float gradient_clipping = 0, float l1_regularization = 0, float l2_regularization = 0); 20 | CapsulesDense(ai::IOData& data); 21 | void initialize(std::vector &inputs); 22 | void initialize(int input_size); 23 | void save(ai::IOData& data); 24 | void run(std::vector &inputs, const bool training); 25 | void backprop(std::vector &inputs); 26 | void accumulate_deltas(std::vector &inputs); 27 | void update_parameters(const float learningrate); 28 | void reset_deltas(const double momentum); 29 | void reset_outputs(); 30 | void setFixedParameters(const bool fixedparameters); 31 | const Operation::Type get_type() const; 32 | void print(); 33 | 34 | static std::shared_ptr make(const int size, bool use_bias = true, 35 | const float gradient_clipping = 0, float l1_regularization = 0, float l2_regularization = 0); 36 | 37 | 38 | #ifdef CUDA_BACKEND 39 | 40 | void run(const TensorCUDA_float& input, bool accumulate); 41 | void backprop(TensorCUDA_float& out_errors); 42 | void accumulate_deltas(const TensorCUDA_float& input); 43 | 44 | TensorCUDA_float _weights; 45 | TensorCUDA_float _bias; 46 | TensorCUDA_float _deltas; 47 | TensorCUDA_int _sparse_indices; 48 | TensorCUDA_int _sparse_indices_tmp; 49 | TensorCUDA_int _sparse_indices_count; 50 | 51 | #else 52 | 53 | static void gradient_check(); 54 | void run(const Tensor_float input, bool accumulate); 55 | void backprop(Tensor_float out_errors); 56 | void accumulate_deltas(const Tensor_float input); 57 | 58 | Tensor_float _weights; 59 | Tensor_float _bias; 60 | Tensor_float _deltas; 61 | 62 | #endif 63 | 64 | bool _fixed_parameters; 65 | int _input_size; 66 | bool _use_bias; 67 | float _gradient_clipping; 68 | float _l1_regularization; 69 | float _l2_regularization; 70 | }; 71 | 72 | } /* namespace ai */ 73 | 74 | #endif /* end of include guard: CAPSULESDENSE_HPP */ 75 | 76 | -------------------------------------------------------------------------------- /src/AI/deeplearning/Concatenate.hpp: -------------------------------------------------------------------------------- 1 | #ifndef CONCATENATE_HPP 2 | #define CONCATENATE_HPP 3 | 4 | //////////////////////////////////////////////////////////// 5 | /// INCLUDES 6 | //////////////////////////////////////////////////////////// 7 | #include 8 | #include 9 | #include "Operation.hpp" 10 | 11 | //////////////////////////////////////////////////////////// 12 | /// NAMESPACE AI 13 | //////////////////////////////////////////////////////////// 14 | namespace ai 15 | { 16 | class Concatenate : public Operation 17 | { 18 | public: 19 | Concatenate(); 20 | Concatenate(ai::IOData& data); 21 | void initialize(std::vector &inputs); 22 | void save(ai::IOData& data); 23 | void run(std::vector &inputs, const bool training); 24 | void backprop(std::vector &inputs); 25 | const Operation::Type get_type() const; 26 | void print(); 27 | static std::shared_ptr make(); 28 | 29 | private: 30 | int _width, _height, _depth; 31 | #ifdef CUDA_BACKEND 32 | TensorCUDA_float_ptr _inputs_pointers; 33 | TensorCUDA_float_ptr _outerrors_pointers; 34 | TensorCUDA_int _pointers_sizes; 35 | #endif 36 | }; 37 | 38 | } /* namespace ai */ 39 | 40 | #endif /* end of include guard: CONCATENATE_HPP */ 41 | 42 | -------------------------------------------------------------------------------- /src/AI/deeplearning/Convolution.hpp: -------------------------------------------------------------------------------- 1 | #ifndef CONVOLUTION_HPP 2 | #define CONVOLUTION_HPP 3 | 4 | //////////////////////////////////////////////////////////// 5 | /// INCLUDES 6 | //////////////////////////////////////////////////////////// 7 | #include 8 | #include 9 | #include "Operation.hpp" 10 | #include "../util/Point.hpp" 11 | 12 | //////////////////////////////////////////////////////////// 13 | /// NAMESPACE AI ///////////////////////////////////////////////////////// 14 | namespace ai 15 | { 16 | class Convolution : public Operation 17 | { 18 | public: 19 | Convolution(const int filter_size, const int filter_count, const int stride, 20 | const int padding = 0, const float gradient_clipping = 0, const float l1_regularization = 0.f, 21 | const float l2_regularization = 0.f); 22 | Convolution(const ai::Point filter_size, const int filter_count, 23 | const int stride, const int padding = 0, const float gradient_clipping = 0, const float l1_regularization = 0.f, 24 | const float l2_regularization = 0.f); 25 | Convolution(ai::IOData& data); 26 | void initialize(std::vector &inputs); 27 | void initialize(const int input_width, const int input_height, const int input_count); 28 | void save(ai::IOData& data); 29 | void run(std::vector &inputs, const bool training); 30 | void backprop(std::vector &inputs); 31 | void accumulate_deltas(std::vector &inputs); 32 | void update_parameters(const float learningrate); 33 | void reset_deltas(const double momentum); 34 | void setFixedParameters(const bool fixedparameters); 35 | void saveParameters(std::string filepath); 36 | void loadParameters(std::string filepath); 37 | void print(); 38 | const Operation::Type get_type() const; 39 | 40 | static std::shared_ptr make(const int filter_size, const int filter_count, 41 | const int stride, const int padding = 0, const float gradient_clipping = 0, 42 | const float l1_regularization = 0.f, const float l2_regularization = 0.f); 43 | static std::shared_ptr make(const ai::Point filter_size, const int filter_count, 44 | const int stride, const int padding = 0, const float gradient_clipping = 0, 45 | const float l1_regularization = 0.f, const float l2_regularization = 0.f); 46 | 47 | #ifdef CUDA_BACKEND 48 | 49 | void run(const TensorCUDA_float &input, const bool training); 50 | void backprop(TensorCUDA_float &out_errors); 51 | void accumulate_deltas(const TensorCUDA_float &input); 52 | 53 | ai::cudnn::Convolution cudaconv; 54 | TensorCUDA_float _weights; 55 | TensorCUDA_float _bias; 56 | TensorCUDA_float _bias_deltas; 57 | TensorCUDA_float _weights_deltas; 58 | TensorCUDA_float _workspace; 59 | TensorCUDA_int _in_weights_map; 60 | TensorCUDA_int _in_out_map; 61 | TensorCUDA_int _out_in_map; 62 | 63 | #else 64 | 65 | static void gradient_check(); 66 | void run(const Tensor_float &input, const bool training); 67 | void backprop(Tensor_float &out_errors); 68 | void accumulate_deltas(const Tensor_float &input); 69 | 70 | Tensor_float _weights; 71 | Tensor_float _bias; 72 | Tensor_float _bias_deltas; 73 | Tensor_float _weights_deltas; 74 | 75 | #endif 76 | bool _fixed_parameters; 77 | int _input_width; 78 | int _input_height; 79 | int _input_count; 80 | int _filter_width; 81 | int _filter_height; 82 | int _filter_count; 83 | int _stride; 84 | int _output_width; 85 | int _output_height; 86 | int _output_size; 87 | int _input_size; 88 | int _padding; 89 | float _gradient_clipping; 90 | float _l1_regularization; 91 | float _l2_regularization; 92 | std::vector > _convmap; 93 | }; 94 | 95 | } /* namespace ai */ 96 | 97 | #endif /* end of include guard: CONVOLUTION_HPP */ 98 | 99 | -------------------------------------------------------------------------------- /src/AI/deeplearning/Cost.cpp: -------------------------------------------------------------------------------- 1 | //////////////////////////////////////////////////////////// 2 | /// INCLUDES 3 | //////////////////////////////////////////////////////////// 4 | #include "Cost.hpp" 5 | #include 6 | #ifdef CUDA_BACKEND 7 | #include "CUDA_backend.hpp" 8 | #endif 9 | 10 | //////////////////////////////////////////////////////////// 11 | /// NAMESPACcE 12 | //////////////////////////////////////////////////////////// 13 | namespace ai 14 | { 15 | 16 | //////////////////////////////////////////////////////////// 17 | Cost::Cost() 18 | { 19 | _type = Cost::SquaredError; 20 | } 21 | 22 | //////////////////////////////////////////////////////////// 23 | Cost::Cost(const CostType type) 24 | { 25 | _type = type; 26 | } 27 | 28 | #ifdef CUDA_BACKEND 29 | //////////////////////////////////////////////////////////// 30 | float Cost::getErrorCUDA(TensorCUDA_float& prediction, TensorCUDA_float& target) 31 | { 32 | if (_gpu_errors.size() == 0) { 33 | _gpu_errors.setshape(target.size()); 34 | _host_errors.setshape(target.size()); 35 | } 36 | TensorCUDA_float_diff(target, prediction, _gpu_errors); 37 | _gpu_errors.copyToHost(_host_errors.pointer(), _host_errors.size()); 38 | float error = 0; 39 | for (int i = 0; i < _host_errors.size(); i++) 40 | error += fabs(_host_errors[i]); 41 | return error; 42 | //TODO 43 | } 44 | 45 | //////////////////////////////////////////////////////////// 46 | void Cost::getDeltaCUDA(TensorCUDA_float& prediction, TensorCUDA_float& target, TensorCUDA_float& errors) 47 | { 48 | switch (_type) { 49 | case Cost::SquaredError: 50 | TensorCUDA_float_diff(target, prediction, errors); 51 | break; 52 | 53 | case Cost::CrossEntropy: 54 | cuda::cost_crossentropy(prediction.pointer(), target.pointer(), errors.pointer(), errors.size()); 55 | break; 56 | } 57 | } 58 | 59 | #else 60 | 61 | //////////////////////////////////////////////////////////// 62 | float Cost::getError(Tensor_float& prediction, Tensor_float& target) 63 | { 64 | float error = 0; 65 | switch (_type) { 66 | case Cost::SquaredError: 67 | for (int i = 0; i < (int)prediction.size(); i++) 68 | error += pow(target[i] - prediction[i], 2) / 2.f; 69 | break; 70 | 71 | case Cost::CrossEntropy: 72 | float epsilon = 1e-04; 73 | for (int i = 0; i < (int)prediction.size(); i++) 74 | error += target[i] * log(prediction[i] + epsilon) + (1 - target[i]) * log(1 - prediction[i] + epsilon); 75 | error = -error; 76 | break; 77 | } 78 | 79 | return error; 80 | } 81 | 82 | //////////////////////////////////////////////////////////// 83 | void Cost::getDelta(Tensor_float& prediction, Tensor_float& target, Tensor_float& errors) 84 | { 85 | switch (_type) { 86 | case Cost::SquaredError: 87 | for (int i = 0; i < (int)errors.size(); i++) 88 | errors[i] = target[i] - prediction[i]; 89 | break; 90 | 91 | case Cost::CrossEntropy: 92 | double denominator; 93 | double epsilon = 1e-4; 94 | for (int i = 0; i < (int)errors.size(); i++) { 95 | denominator = prediction[i] - prediction[i]*prediction[i]; 96 | if (denominator < epsilon) denominator = epsilon; 97 | errors[i] = (target[i] - prediction[i]) / denominator; 98 | } 99 | break; 100 | } 101 | } 102 | 103 | #endif 104 | 105 | } //namespace ai 106 | -------------------------------------------------------------------------------- /src/AI/deeplearning/Cost.hpp: -------------------------------------------------------------------------------- 1 | #ifndef COST_HPP 2 | #define COST_HPP 3 | 4 | //////////////////////////////////////////////////////////// 5 | /// INCLUDES 6 | //////////////////////////////////////////////////////////// 7 | #include "../util/Tensor.hpp" 8 | #include "../util/Macros.hpp" 9 | #ifdef CUDA_BACKEND 10 | #include "../util/TensorCUDA.hpp" 11 | #endif 12 | 13 | //////////////////////////////////////////////////////////// 14 | /// NAMESPACE AI 15 | //////////////////////////////////////////////////////////// 16 | namespace ai 17 | { 18 | 19 | //////////////////////////////////////////////////////////// 20 | /// \brief A function that return a number representing how 21 | /// well the neural network performed to map inputs and outputs 22 | /// 23 | //////////////////////////////////////////////////////////// 24 | class Cost 25 | { 26 | public: 27 | 28 | //Cost function types 29 | enum CostType 30 | { 31 | SquaredError = 0, 32 | CrossEntropy = 1, 33 | }; 34 | 35 | //////////////////////////////////////////////////////////// 36 | /// \brief Default constructor 37 | /// 38 | //////////////////////////////////////////////////////////// 39 | Cost(); 40 | 41 | //////////////////////////////////////////////////////////// 42 | /// \brief Construct from cost function type 43 | /// 44 | //////////////////////////////////////////////////////////// 45 | Cost(const CostType type); 46 | 47 | #ifdef CUDA_BACKEND 48 | 49 | //////////////////////////////////////////////////////////// 50 | /// \brief Calculate errors 51 | /// 52 | //////////////////////////////////////////////////////////// 53 | float getErrorCUDA(TensorCUDA_float& prediction, TensorCUDA_float& target); 54 | 55 | //////////////////////////////////////////////////////////// 56 | /// \brief Calculate deltas 57 | /// 58 | //////////////////////////////////////////////////////////// 59 | void getDeltaCUDA(TensorCUDA_float& prediction, TensorCUDA_float& target, TensorCUDA_float& errors); 60 | 61 | #else 62 | 63 | //////////////////////////////////////////////////////////// 64 | /// \brief Calculate errors 65 | /// 66 | //////////////////////////////////////////////////////////// 67 | float getError(Tensor_float& prediction, Tensor_float& target); 68 | 69 | //////////////////////////////////////////////////////////// 70 | /// \brief Calculate deltas 71 | /// 72 | //////////////////////////////////////////////////////////// 73 | void getDelta(Tensor_float& prediction, Tensor_float& target, Tensor_float& errors); 74 | 75 | #endif 76 | 77 | private: 78 | CostType _type; 79 | #ifdef CUDA_BACKEND 80 | TensorCUDA_float _gpu_errors; 81 | Tensor_float _host_errors; 82 | #endif 83 | }; 84 | 85 | } //namespace ai 86 | 87 | #endif /* end of include guard: COST_HPP */ 88 | 89 | -------------------------------------------------------------------------------- /src/AI/deeplearning/DataAugmentation.hpp: -------------------------------------------------------------------------------- 1 | #ifndef DATAUGMENTATION_HPP 2 | #define DATAUGMENTATION_HPP 3 | 4 | //////////////////////////////////////////////////////////// 5 | /// INCLUDES 6 | //////////////////////////////////////////////////////////// 7 | #include "../util/Tensor.hpp" 8 | #include "../util/Macros.hpp" 9 | #ifdef CUDA_BACKEND 10 | #include "../util/TensorCUDA.hpp" 11 | #endif 12 | #include 13 | #include 14 | 15 | //////////////////////////////////////////////////////////// 16 | /// NAMESPACE AI 17 | //////////////////////////////////////////////////////////// 18 | namespace ai 19 | { 20 | 21 | //////////////////////////////////////////////////////////// 22 | /// NAMESPACE AUGMENTATION 23 | //////////////////////////////////////////////////////////// 24 | namespace augmentation 25 | { 26 | 27 | #ifdef CUDA_BACKEND 28 | 29 | void translate(TensorCUDA_float& t, int image_width, int image_height, int image_channels, int tx, int ty); 30 | void rotate(TensorCUDA_float& t, int image_width, int image_height, int image_channels, float degrees); 31 | void noise(TensorCUDA_float& t, int image_width, int image_height, int image_channels, float noise); 32 | void vflip(TensorCUDA_float& t, int width, int height, int channels); 33 | void hflip(TensorCUDA_float& t, int width, int height, int channels); 34 | void scaling(TensorCUDA_float& t, int width, int height, int channel, float scale_factor); 35 | 36 | #else 37 | 38 | void translate(Tensor_float& t, int image_width, int image_height, int image_channels, int tx, int ty); 39 | void rotate(Tensor_float& t, int image_width, int image_height, int image_channels, float degrees); 40 | void noise(Tensor_float& t, int image_width, int image_height, int image_channels, float noise); 41 | void vflip(Tensor_float& t, int width, int height, int channels); 42 | void hflip(Tensor_float& t, int width, int height, int channels); 43 | void scaling(Tensor_float& t, int width, int height, int channel, float scale_factor); 44 | 45 | #endif 46 | 47 | } /* namespace augmentation */ 48 | 49 | } /* namespace ai */ 50 | 51 | #endif /* end of include guard: DATAUGMENTATION_HPP */ 52 | 53 | -------------------------------------------------------------------------------- /src/AI/deeplearning/Dropout.hpp: -------------------------------------------------------------------------------- 1 | #ifndef DROPOUT_HPP 2 | #define DROPOUT_HPP 3 | 4 | //////////////////////////////////////////////////////////// 5 | /// INCLUDES 6 | //////////////////////////////////////////////////////////// 7 | #include 8 | #include 9 | #include "Operation.hpp" 10 | 11 | //////////////////////////////////////////////////////////// 12 | /// NAMESPACE AI 13 | //////////////////////////////////////////////////////////// 14 | namespace ai 15 | { 16 | class Dropout : public Operation 17 | { 18 | public: 19 | Dropout(const double drop_probability); 20 | Dropout(ai::IOData& data); 21 | void save(ai::IOData& data); 22 | void initialize(std::vector &inputs); 23 | void run(std::vector &inputs, const bool training); 24 | void backprop(std::vector &inputs); 25 | const Operation::Type get_type() const; 26 | void print(); 27 | static std::shared_ptr make(const double drop_probability); 28 | 29 | float _drop_probability; 30 | private: 31 | #ifdef CUDA_BACKEND 32 | ai::cudnn::Dropout _cuda_dropout; 33 | ai::TensorCUDA_float _state_buffer, _reserve_space_buffer; 34 | #endif 35 | int _width, _height, _depth; 36 | }; 37 | 38 | } /* namespace ai */ 39 | 40 | #endif /* end of include guard: DROPOUT_HPP */ 41 | 42 | -------------------------------------------------------------------------------- /src/AI/deeplearning/Linear.hpp: -------------------------------------------------------------------------------- 1 | #ifndef LINEAR_HPP 2 | #define LINEAR_HPP 3 | 4 | //////////////////////////////////////////////////////////// 5 | /// INCLUDES 6 | //////////////////////////////////////////////////////////// 7 | #include 8 | #include "Operation.hpp" 9 | 10 | //////////////////////////////////////////////////////////// 11 | /// NAMESPACE AI 12 | //////////////////////////////////////////////////////////// 13 | namespace ai 14 | { 15 | class Linear : public Operation 16 | { 17 | public: 18 | Linear(); 19 | Linear(const int size, bool use_bias = true, const float gradient_clipping = 0, float l1_regularization = 0, float l2_regularization = 0); 20 | Linear(ai::IOData& data); 21 | void initialize(std::vector &inputs); 22 | void initialize(int input_size); 23 | void save(ai::IOData& data); 24 | void run(std::vector &inputs, const bool training); 25 | void backprop(std::vector &inputs); 26 | void accumulate_deltas(std::vector &inputs); 27 | void update_parameters(const float learningrate); 28 | void reset_deltas(const double momentum); 29 | void reset_outputs(); 30 | void setFixedParameters(const bool fixedparameters); 31 | const Operation::Type get_type() const; 32 | void print(); 33 | 34 | static std::shared_ptr make(const int size, bool use_bias = true, 35 | const float gradient_clipping = 0, float l1_regularization = 0, float l2_regularization = 0); 36 | 37 | 38 | #ifdef CUDA_BACKEND 39 | 40 | void run(const TensorCUDA_float& input, bool accumulate); 41 | void backprop(TensorCUDA_float& out_errors); 42 | void accumulate_deltas(const TensorCUDA_float& input); 43 | 44 | TensorCUDA_float _weights; 45 | TensorCUDA_float _bias; 46 | TensorCUDA_float _deltas; 47 | TensorCUDA_int _sparse_indices; 48 | TensorCUDA_int _sparse_indices_tmp; 49 | TensorCUDA_int _sparse_indices_count; 50 | 51 | #else 52 | 53 | static void gradient_check(); 54 | void run(const Tensor_float input, bool accumulate); 55 | void backprop(Tensor_float out_errors); 56 | void accumulate_deltas(const Tensor_float input); 57 | 58 | Tensor_float _weights; 59 | Tensor_float _bias; 60 | Tensor_float _deltas; 61 | 62 | #endif 63 | bool _fixed_parameters; 64 | int _input_size; 65 | bool _use_bias; 66 | float _gradient_clipping; 67 | float _l1_regularization; 68 | float _l2_regularization; 69 | }; 70 | 71 | } /* namespace ai */ 72 | 73 | #endif /* end of include guard: LINEAR_HPP */ 74 | 75 | -------------------------------------------------------------------------------- /src/AI/deeplearning/Maxpooling.hpp: -------------------------------------------------------------------------------- 1 | #ifndef MAXPOOLING_HPP 2 | #define MAXPOOLING_HPP 3 | 4 | //////////////////////////////////////////////////////////// 5 | /// INCLUDES 6 | //////////////////////////////////////////////////////////// 7 | #include 8 | #include 9 | #include "Operation.hpp" 10 | 11 | //////////////////////////////////////////////////////////// 12 | /// NAMESPACE AI 13 | //////////////////////////////////////////////////////////// 14 | namespace ai 15 | { 16 | class Maxpooling : public Operation 17 | { 18 | public: 19 | Maxpooling(const int filter_size, const int stride); 20 | Maxpooling(ai::IOData& data); 21 | void initialize(std::vector &inputs); 22 | void save(ai::IOData& data); 23 | void run(std::vector &inputs, const bool training); 24 | void backprop(std::vector &inputs); 25 | void print(); 26 | const Operation::Type get_type() const; 27 | static std::shared_ptr make(const int filter_size, const int stride); 28 | 29 | int _input_width; 30 | int _input_height; 31 | int _input_count; 32 | int _filter_size; 33 | int _stride; 34 | int _output_width; 35 | int _output_height; 36 | int _output_size; 37 | int _input_size; 38 | 39 | #ifdef CUDA_BACKEND 40 | ai::cudnn::Pooling _cuda_pooling; 41 | #else 42 | std::vector _maxin; 43 | #endif 44 | }; 45 | 46 | } /* namespace ai */ 47 | 48 | #endif /* end of include guard: MAXPOOLING_HPP */ 49 | 50 | -------------------------------------------------------------------------------- /src/AI/deeplearning/NetworkNode.hpp: -------------------------------------------------------------------------------- 1 | #ifndef NETWORKNODE_HPP 2 | #define NETWORKNODE_HPP 3 | 4 | //////////////////////////////////////////////////////////// 5 | /// INCLUDES 6 | //////////////////////////////////////////////////////////// 7 | #include 8 | #include "Operation.hpp" 9 | #include "../util/IOData.hpp" 10 | 11 | //Nodes 12 | #include "Variable.hpp" 13 | #include "Linear.hpp" 14 | #include "Sigmoid.hpp" 15 | #include "Tanh.hpp" 16 | #include "Relu.hpp" 17 | #include "Softmax.hpp" 18 | #include "Recurrent.hpp" 19 | #include "Partial.hpp" 20 | #include "Dropout.hpp" 21 | #include "Convolution.hpp" 22 | #include "Normalization.hpp" 23 | #include "Addition.hpp" 24 | #include "Concatenate.hpp" 25 | #include "Maxpooling.hpp" 26 | #include "Averagepooling.hpp" 27 | #include "Selu.hpp" 28 | 29 | //////////////////////////////////////////////////////////// 30 | /// NAMESPACE AI 31 | //////////////////////////////////////////////////////////// 32 | namespace ai 33 | { 34 | 35 | class NeuralNetwork; 36 | 37 | class NetworkNode 38 | { 39 | public: 40 | NetworkNode(std::string node_name, std::vector input_names, 41 | NeuralNetwork* network, std::shared_ptr operation); 42 | NetworkNode(ai::IOData& data, NeuralNetwork* network); 43 | 44 | void run(bool training = false); 45 | void backprop(); 46 | void accumulate_deltas(); 47 | void update_parameters(const float learningrate); 48 | void reset_errors(); 49 | void reset_deltas(const float momentum); 50 | void save(ai::IOData& data); 51 | const std::string getName() const; 52 | const std::vector getInputsNames(); 53 | const std::vector getInputsIndicies(); 54 | void print(); 55 | #ifdef CUDA_BACKEND 56 | Tensor_float getOperationOutput(); 57 | const TensorCUDA_float& getOperationOutputDevice(); 58 | void setOperationOutput(TensorCUDA_float& output); 59 | #else 60 | Tensor_float& getOperationOutput(); 61 | void setOperationOutput(Tensor_float& output); 62 | #endif 63 | Operation* getOperation(); 64 | 65 | private: 66 | void load(ai::IOData& data); 67 | void checkInvalidInputNames(); 68 | void initInputsIndiciesVector(); 69 | void initInputsOperationsVector(); 70 | 71 | NeuralNetwork* _network; 72 | std::shared_ptr _operation; 73 | std::string _name; 74 | std::vector _input_names; 75 | std::vector _input_indicies; 76 | std::vector _input_operations; 77 | }; 78 | 79 | } /* namespace ai */ 80 | 81 | #endif /* end of include guard: NETWORKNODE_HPP */ 82 | 83 | -------------------------------------------------------------------------------- /src/AI/deeplearning/NeuralNetwork.hpp: -------------------------------------------------------------------------------- 1 | #ifndef NEURALNETWORK_HPP 2 | #define NEURALNETWORK_HPP 3 | 4 | //////////////////////////////////////////////////////////// 5 | /// INCLUDES 6 | //////////////////////////////////////////////////////////// 7 | #include 8 | #include 9 | #include "Operation.hpp" 10 | #include "NetworkNode.hpp" 11 | #include "Optimizer.hpp" 12 | #include "../util/Macros.hpp" 13 | #include 14 | 15 | //////////////////////////////////////////////////////////// 16 | /// NAMESPACE AI 17 | //////////////////////////////////////////////////////////// 18 | namespace ai 19 | { 20 | 21 | class NeuralNetwork 22 | { 23 | public: 24 | NeuralNetwork(); 25 | NeuralNetwork(std::string filepath); 26 | ~NeuralNetwork(); 27 | void save(std::string filepath); 28 | void load(std::string filepath); 29 | void push(std::string node_name, std::string inputs_names, std::shared_ptr operation); 30 | void clear(); 31 | NetworkNode* get_byname(std::string node_name); 32 | #ifdef CUDA_BACKEND 33 | void run(TensorCUDA_float input, const bool training = false); 34 | float optimize(TensorCUDA_float input, TensorCUDA_float target, Optimizer* opt); 35 | bool test(TensorCUDA_float input, TensorCUDA_float target); 36 | TensorCUDA_float& get_output(std::string node_name); 37 | #else 38 | void run(Tensor_float input, const bool training = false); 39 | float optimize(Tensor_float input, Tensor_float target, Optimizer* opt); 40 | Tensor_float& get_output(std::string node_name); 41 | #endif 42 | std::vector& getNodes(); 43 | void printstack(); 44 | 45 | private: 46 | void resetOperationsErrors(); 47 | std::vector splitString(std::string s, char delimiter); 48 | 49 | std::vector _nodes; 50 | }; 51 | 52 | } /* namespace ai */ 53 | 54 | #endif /* end of include guard: NEURALNETWORK_HPP */ 55 | 56 | -------------------------------------------------------------------------------- /src/AI/deeplearning/Normalization.hpp: -------------------------------------------------------------------------------- 1 | #ifndef NORMALIZATION_HPP 2 | #define NORMALIZATION_HPP 3 | 4 | //////////////////////////////////////////////////////////// 5 | /// INCLUDES 6 | //////////////////////////////////////////////////////////// 7 | #include 8 | #include 9 | #include "Operation.hpp" 10 | 11 | //////////////////////////////////////////////////////////// 12 | /// NAMESPACE AI 13 | //////////////////////////////////////////////////////////// 14 | namespace ai 15 | { 16 | class Normalization : public Operation 17 | { 18 | public: 19 | Normalization(float momentum = 0.1); 20 | Normalization(ai::IOData& data); 21 | void save(ai::IOData& data); 22 | void initialize(std::vector &inputs); 23 | void run(std::vector &inputs, const bool training); 24 | void backprop(std::vector &inputs); 25 | void accumulate_deltas(std::vector &inputs); 26 | void update_parameters(const float learningrate); 27 | const Operation::Type get_type() const; 28 | void print(); 29 | static std::shared_ptr make(float momentum = 0.1); 30 | 31 | private: 32 | int _width, _height, _depth; 33 | float _gamma; 34 | float _beta; 35 | float _epsilon; 36 | float _momentum; 37 | 38 | //Foreward informations 39 | double _mean; 40 | double _variance; 41 | #ifdef CUDA_BACKEND 42 | TensorCUDA_float _deviation; 43 | TensorCUDA_float _normalized; 44 | TensorCUDA_float _params; 45 | #else 46 | Tensor_float _deviation; 47 | Tensor_float _normalized; 48 | #endif 49 | 50 | //Backward informations 51 | float _d_beta; 52 | float _d_gamma; 53 | }; 54 | 55 | } /* namespace ai */ 56 | 57 | #endif /* end of include guard: NORMALIZATION_HPP */ 58 | 59 | -------------------------------------------------------------------------------- /src/AI/deeplearning/Operation.hpp: -------------------------------------------------------------------------------- 1 | #ifndef OPERATION_HPP 2 | #define OPERATION_HPP 3 | 4 | //////////////////////////////////////////////////////////// 5 | /// INCLUDES 6 | //////////////////////////////////////////////////////////// 7 | #include 8 | #include 9 | #include 10 | #include "../util/Tensor.hpp" 11 | #include "../util/IOData.hpp" 12 | #include "../util/Macros.hpp" 13 | #ifdef CUDA_BACKEND 14 | #include "../util/TensorCUDA.hpp" 15 | #include "CUDA_backend.hpp" 16 | #endif 17 | 18 | //////////////////////////////////////////////////////////// 19 | /// NAMESPACE AI 20 | //////////////////////////////////////////////////////////// 21 | namespace ai 22 | { 23 | 24 | class Operation 25 | { 26 | public: 27 | 28 | //////////////////////////////////////////////////////////// 29 | /// OPERATIONS TYPES AVAILABLE 30 | //////////////////////////////////////////////////////////// 31 | enum Type 32 | { 33 | Unknown, 34 | Variable, 35 | Linear, 36 | Sigmoid, 37 | Tanh, 38 | Relu, 39 | Softmax, 40 | Recurrent, 41 | Partial, 42 | Dropout, 43 | Convolution, 44 | Normalization, 45 | Addition, 46 | Concatenate, 47 | Maxpooling, 48 | Averagepooling, 49 | Selu, 50 | Autoencoder, 51 | ResidualBlock, 52 | CapsulesDense, 53 | Types_Count 54 | }; 55 | 56 | virtual ~Operation(); 57 | virtual void initialize(std::vector &inputs); 58 | virtual void run(std::vector& inputs, const bool training); 59 | virtual void backprop(std::vector& inputs); 60 | virtual void accumulate_deltas(std::vector& inputs); 61 | virtual void update_parameters(const float learningrate); 62 | virtual void print(); 63 | virtual const Type get_type() const; 64 | virtual void reset_deltas(const double momentum); 65 | virtual void reset_errors(); 66 | 67 | static std::shared_ptr loadFromFile(ai::IOData& data); 68 | static void saveToFile(std::shared_ptr& operation, ai::IOData& data); 69 | 70 | int _size; 71 | #ifdef CUDA_BACKEND 72 | TensorCUDA_float _outputs; 73 | TensorCUDA_float _errors; 74 | #else 75 | Tensor_float _outputs; 76 | Tensor_float _errors; 77 | #endif 78 | 79 | private: 80 | virtual void save(ai::IOData& data); 81 | }; 82 | 83 | } /* namespace ai */ 84 | 85 | #endif /* end of include guard: OPERATION_HPP */ 86 | 87 | -------------------------------------------------------------------------------- /src/AI/deeplearning/Optimizer.cpp: -------------------------------------------------------------------------------- 1 | //////////////////////////////////////////////////////////// 2 | /// INCLUDES 3 | //////////////////////////////////////////////////////////// 4 | #include "Optimizer.hpp" 5 | 6 | //////////////////////////////////////////////////////////// 7 | /// NAMESPACE AI 8 | //////////////////////////////////////////////////////////// 9 | namespace ai 10 | { 11 | 12 | //////////////////////////////////////////////////////////// 13 | void Optimizer::fit(NeuralNetwork& net, Tensor_float &inputs, Tensor_float &targets) {} 14 | 15 | #ifdef CUDA_BACKEND 16 | //////////////////////////////////////////////////////////// 17 | void Optimizer::fit(NeuralNetwork& net, TensorCUDA_float &inputs, TensorCUDA_float &targets) {} 18 | #endif 19 | 20 | //////////////////////////////////////////////////////////// 21 | void Optimizer::setLearningrate(const float learningrate) { _learningrate = learningrate; } 22 | 23 | //////////////////////////////////////////////////////////// 24 | void Optimizer::setMomentum(const float momentum) { _momentum = momentum; } 25 | 26 | //////////////////////////////////////////////////////////// 27 | const float Optimizer::getLearningrate() const { return _learningrate; } 28 | 29 | //////////////////////////////////////////////////////////// 30 | const float Optimizer::getMomentum() const { return _momentum; } 31 | 32 | //////////////////////////////////////////////////////////// 33 | const float Optimizer::getError() const { return _error; } 34 | 35 | } /* namespace ai */ 36 | -------------------------------------------------------------------------------- /src/AI/deeplearning/Optimizer.hpp: -------------------------------------------------------------------------------- 1 | #ifndef OPTIMIZER_HPP 2 | #define OPTIMIZER_HPP 3 | 4 | //////////////////////////////////////////////////////////// 5 | /// INCLUDES 6 | //////////////////////////////////////////////////////////// 7 | #include "Cost.hpp" 8 | #include "../util/Macros.hpp" 9 | #ifdef CUDA_BACKEND 10 | #include "../util/TensorCUDA.hpp" 11 | #endif 12 | 13 | //////////////////////////////////////////////////////////// 14 | /// NAMESPACE AI 15 | //////////////////////////////////////////////////////////// 16 | namespace ai 17 | { 18 | 19 | class NeuralNetwork; 20 | 21 | class Optimizer 22 | { 23 | public: 24 | virtual void fit(NeuralNetwork& net, Tensor_float &inputs, Tensor_float &targets); 25 | #ifdef CUDA_BACKEND 26 | virtual void fit(NeuralNetwork& net, TensorCUDA_float &inputs, TensorCUDA_float &targets); 27 | #endif 28 | void setLearningrate(const float learningrate); 29 | void setMomentum(const float momentum); 30 | const float getLearningrate() const; 31 | const float getMomentum() const; 32 | const float getError() const; 33 | 34 | protected: 35 | float _learningrate; 36 | float _momentum; 37 | float _error; 38 | Cost _costfunction; 39 | }; 40 | 41 | } /* namespace ai */ 42 | 43 | #endif /* end of include guard: OPTIMIZER_HPP */ 44 | 45 | -------------------------------------------------------------------------------- /src/AI/deeplearning/OptimizerDFA.hpp: -------------------------------------------------------------------------------- 1 | #ifndef OPTIMIZERDFA_HPP 2 | #define OPTIMIZERDFA_HPP 3 | 4 | //////////////////////////////////////////////////////////// 5 | /// INCLUDES 6 | //////////////////////////////////////////////////////////// 7 | #include "Optimizer.hpp" 8 | #include "../util/Macros.hpp" 9 | 10 | //////////////////////////////////////////////////////////// 11 | /// NAMESPACE AI 12 | //////////////////////////////////////////////////////////// 13 | namespace ai 14 | { 15 | 16 | class OptimizerDFA : public Optimizer 17 | { 18 | public: 19 | OptimizerDFA(); 20 | OptimizerDFA(const int batch_size, const double learningrate, const double momentum, 21 | const Cost::CostType cost_function = Cost::SquaredError); 22 | #ifdef CUDA_BACKEND 23 | void fit(NeuralNetwork& net, TensorCUDA_float &inputs, TensorCUDA_float &targets); 24 | #else 25 | void fit(NeuralNetwork& net, Tensor_float &inputs, Tensor_float &targets); 26 | #endif 27 | 28 | private: 29 | int _batch_size; 30 | int _current_sample; 31 | #ifdef CUDA_BACKEND 32 | TensorCUDA_float _targets; 33 | TensorCUDA_float _feedback_weights; 34 | TensorCUDA_float _feedback_errors; 35 | #else 36 | Tensor_float _feedback_weights; 37 | Tensor_float _feedback_errors; 38 | #endif 39 | }; 40 | } /* namespace ai */ 41 | 42 | #endif /* end of include guard: OPTIMIZERDFA_HPP */ 43 | 44 | -------------------------------------------------------------------------------- /src/AI/deeplearning/OptimizerSDG.hpp: -------------------------------------------------------------------------------- 1 | #ifndef OPTIMIZERSDG_HPP 2 | #define OPTIMIZERSDG_HPP 3 | 4 | //////////////////////////////////////////////////////////// 5 | /// INCLUDES 6 | //////////////////////////////////////////////////////////// 7 | #include "Optimizer.hpp" 8 | #include "../util/Macros.hpp" 9 | 10 | //////////////////////////////////////////////////////////// 11 | /// NAMESPACE AI 12 | //////////////////////////////////////////////////////////// 13 | namespace ai 14 | { 15 | 16 | class OptimizerSDG : public Optimizer 17 | { 18 | public: 19 | OptimizerSDG(); 20 | OptimizerSDG(const int batch_size, const double learningrate, const double momentum, 21 | const Cost::CostType cost_function = Cost::SquaredError); 22 | #ifdef CUDA_BACKEND 23 | void fit(NeuralNetwork& net, TensorCUDA_float &inputs, TensorCUDA_float &targets); 24 | #else 25 | void fit(NeuralNetwork& net, Tensor_float &inputs, Tensor_float &targets); 26 | #endif 27 | 28 | private: 29 | int _batch_size; 30 | int _current_sample; 31 | #ifdef CUDA_BACKEND 32 | TensorCUDA_float _targets; 33 | #endif 34 | }; 35 | } /* namespace ai */ 36 | 37 | #endif /* end of include guard: OPTIMIZERSDG_HPP */ 38 | 39 | -------------------------------------------------------------------------------- /src/AI/deeplearning/Partial.hpp: -------------------------------------------------------------------------------- 1 | #ifndef PARTIAL_HPP 2 | #define PARTIAL_HPP 3 | 4 | //////////////////////////////////////////////////////////// 5 | /// INCLUDES 6 | //////////////////////////////////////////////////////////// 7 | #include 8 | #include "Operation.hpp" 9 | 10 | //////////////////////////////////////////////////////////// 11 | /// NAMESPACE AI 12 | //////////////////////////////////////////////////////////// 13 | namespace ai 14 | { 15 | class Partial : public Operation 16 | { 17 | public: 18 | Partial(const int size, const double connectivity); 19 | Partial(ai::IOData& data); 20 | void initialize(std::vector &inputs); 21 | void save(ai::IOData& data); 22 | void run(std::vector &inputs, const bool training); 23 | void backprop(std::vector &inputs); 24 | void accumulate_deltas(std::vector &inputs); 25 | void update_parameters(const float learningrate); 26 | void reset_deltas(const double momentum); 27 | void pruning(float alpha); 28 | float pruned_percent(); 29 | const Operation::Type get_type() const; 30 | void print(); 31 | static std::shared_ptr make(const int size, const double connectivity); 32 | 33 | #ifdef CUDA_BACKEND 34 | TensorCUDA_float _weights; 35 | TensorCUDA_float _bias; 36 | TensorCUDA_float _deltas; 37 | #else 38 | Tensor_float _weights; 39 | Tensor_float _bias; 40 | Tensor_float _deltas; 41 | #endif 42 | std::vector< std::vector< int > > _foreward_map; 43 | int _input_size; 44 | double _connectivity; 45 | }; 46 | 47 | } /* namespace ai */ 48 | 49 | #endif /* end of include guard: PARTIAL_HPP */ 50 | 51 | -------------------------------------------------------------------------------- /src/AI/deeplearning/Recurrent.hpp: -------------------------------------------------------------------------------- 1 | #ifndef RECURRENT_HPP 2 | #define RECURRENT_HPP 3 | 4 | //////////////////////////////////////////////////////////// 5 | /// INCLUDES 6 | //////////////////////////////////////////////////////////// 7 | #include 8 | #include "Operation.hpp" 9 | #include "Linear.hpp" 10 | 11 | //////////////////////////////////////////////////////////// 12 | /// NAMESPACE AI 13 | //////////////////////////////////////////////////////////// 14 | namespace ai 15 | { 16 | class Recurrent : public Operation 17 | { 18 | public: 19 | Recurrent(const int size, int btt_steps = 3); 20 | Recurrent(ai::IOData& data); 21 | void initialize(std::vector &inputs); 22 | void initialize(const int input_size); 23 | void save(ai::IOData& data); 24 | void run(std::vector &inputs, const bool training); 25 | void backprop(std::vector &inputs); 26 | void accumulate_deltas(std::vector &inputs); 27 | void update_parameters(const float learningrate); 28 | const Operation::Type get_type() const; 29 | void reset_hidden_state(); 30 | void print(); 31 | 32 | static void gradient_check(); 33 | static std::shared_ptr make(const int size, int btt_steps = 3); 34 | 35 | //Weights and bias 36 | ai::Linear _x; 37 | ai::Linear _rec; 38 | ai::Linear _out; 39 | 40 | #ifdef CUDA_BACKEND 41 | 42 | void run(const TensorCUDA_float input, bool accumulate); 43 | void backprop(TensorCUDA_float out_errors); 44 | void accumulate_deltas(const TensorCUDA_float input); 45 | 46 | //Memory parameters for unrolling 47 | TensorCUDA_float _mem_outputs; 48 | TensorCUDA_float _mem_tmp_errors; 49 | TensorCUDA_float _mem_inputs; 50 | 51 | #else 52 | 53 | void run(const Tensor_float input, bool accumulate); 54 | void backprop(Tensor_float out_errors); 55 | void accumulate_deltas(const Tensor_float input); 56 | 57 | //Memory parameters for unrolling 58 | Tensor_float _mem_outputs; 59 | Tensor_float _mem_tmp_errors; 60 | Tensor_float _mem_inputs; 61 | 62 | #endif 63 | 64 | int _btt_steps; 65 | int _btt_pos; 66 | int _input_size; 67 | }; 68 | 69 | } /* namespace ai */ 70 | 71 | #endif /* end of include guard: RECURRENT_HPP */ 72 | 73 | -------------------------------------------------------------------------------- /src/AI/deeplearning/Relu.cpp: -------------------------------------------------------------------------------- 1 | //////////////////////////////////////////////////////////// 2 | /// INCLUDES 3 | //////////////////////////////////////////////////////////// 4 | #include "Relu.hpp" 5 | #include 6 | #include "../util/ensure.hpp" 7 | #ifdef CUDA_BACKEND 8 | #include "CUDA_backend.hpp" 9 | #endif 10 | 11 | //////////////////////////////////////////////////////////// 12 | /// NAMESPACE AI 13 | //////////////////////////////////////////////////////////// 14 | namespace ai 15 | { 16 | //////////////////////////////////////////////////////////// 17 | std::shared_ptr Relu::make() 18 | { 19 | return std::shared_ptr(new Relu()); 20 | } 21 | 22 | //////////////////////////////////////////////////////////// 23 | Relu::Relu() {} 24 | 25 | //////////////////////////////////////////////////////////// 26 | Relu::Relu(ai::IOData& data) 27 | { 28 | ai::IOData* size = data.findNode("size"); 29 | ensure(size != NULL); 30 | ai::IOData* width = data.findNode("width"); 31 | ensure(width != NULL); 32 | ai::IOData* height = data.findNode("height"); 33 | ensure(height != NULL); 34 | ai::IOData* depth = data.findNode("depth"); 35 | ensure(depth != NULL); 36 | size->get(_size); 37 | width->get(_width); 38 | height->get(_height); 39 | depth->get(_depth); 40 | _outputs.setshape(_width, _height, _depth); 41 | _outputs.fill(0); 42 | _errors.setshape(_size); 43 | _outputs.fill(0); 44 | 45 | #ifdef CUDA_BACKEND 46 | _cudnnactivation.create(_size, 1, ai::cudnn::ACTIVATION_RELU); 47 | #endif 48 | } 49 | 50 | //////////////////////////////////////////////////////////// 51 | void Relu::initialize(std::vector &inputs) 52 | { 53 | //Check for errors 54 | ensure(inputs.size() == 1); 55 | 56 | //Calculate size 57 | _size = inputs[0]->_outputs.size(); 58 | _width = inputs[0]->_outputs.width(); 59 | _height = inputs[0]->_outputs.height(); 60 | _depth = inputs[0]->_outputs.depth(); 61 | 62 | //Initialize vectors 63 | _outputs.setshape(_width, _height, _depth); 64 | _outputs.fill(0); 65 | _errors.setshape(_size); 66 | _outputs.fill(0); 67 | 68 | #ifdef CUDA_BACKEND 69 | _cudnnactivation.create(_size, 1, ai::cudnn::ACTIVATION_RELU); 70 | #endif 71 | } 72 | 73 | //////////////////////////////////////////////////////////// 74 | void Relu::save(ai::IOData& data) 75 | { 76 | data.pushNode("size", _size); 77 | data.pushNode("width", _width); 78 | data.pushNode("height", _height); 79 | data.pushNode("depth", _depth); 80 | } 81 | 82 | //////////////////////////////////////////////////////////// 83 | void Relu::run(std::vector &inputs, const bool training) 84 | { 85 | //Check for correct input size 86 | ensure(inputs.size() == 1); 87 | ensure(inputs[0]->_outputs.size() == _outputs.size()); 88 | 89 | #ifdef CUDA_BACKEND 90 | 91 | _cudnnactivation.foreward(inputs[0]->_outputs.pointer(), _outputs.pointer()); 92 | 93 | #else 94 | //Shortcuts 95 | const Tensor_float& in = inputs[0]->_outputs; 96 | 97 | //Feedforeward 98 | for (int i = 0; i < (int)_outputs.size(); i++) 99 | _outputs[i] = in[i] * (in[i] > 0); 100 | #endif 101 | } 102 | 103 | //////////////////////////////////////////////////////////// 104 | void Relu::backprop(std::vector &inputs) 105 | { 106 | //Check for correct input size 107 | ensure(inputs.size() == 1); 108 | 109 | #ifdef CUDA_BACKEND 110 | 111 | _cudnnactivation.backward(inputs[0]->_outputs.pointer(), _outputs.pointer(), _errors.pointer(), inputs[0]->_errors.pointer()); 112 | 113 | #else 114 | 115 | //Shortcuts 116 | Tensor_float &out_errors = inputs[0]->_errors; 117 | 118 | //Feedforeward 119 | for (int i = 0; i < (int)out_errors.size(); i++) 120 | out_errors[i] = _errors[i] * (_outputs[i] > 0); 121 | #endif 122 | } 123 | 124 | //////////////////////////////////////////////////////////// 125 | const Operation::Type Relu::get_type() const 126 | { 127 | return Operation::Relu; 128 | } 129 | 130 | //////////////////////////////////////////////////////////// 131 | void Relu::print() 132 | { 133 | printf("Type: Relu, Size: %d", _size); 134 | } 135 | 136 | #ifndef CUDA_BACKEND 137 | 138 | //////////////////////////////////////////////////////////// 139 | void Relu::foreward(const Tensor_float input, Tensor_float output) 140 | { 141 | //Check for errors 142 | ensure(output.size() == input.size()); 143 | 144 | //Feedforeward 145 | for (int i = 0; i < (int)output.size(); i++) 146 | output[i] = input[i] * (input[i] > 0); 147 | } 148 | 149 | //////////////////////////////////////////////////////////// 150 | void Relu::backward(const Tensor_float errors, const Tensor_float outputs, Tensor_float out_errors) 151 | { 152 | //Check for errors 153 | ensure(errors.size() == out_errors.size()); 154 | 155 | //Backward 156 | for (int i = 0; i < (int)out_errors.size(); i++) 157 | out_errors[i] = errors[i] * (outputs[i] > 0); 158 | } 159 | 160 | #endif 161 | 162 | } /* namespace ai */ 163 | -------------------------------------------------------------------------------- /src/AI/deeplearning/Relu.hpp: -------------------------------------------------------------------------------- 1 | #ifndef RELU_HPP 2 | #define RELU_HPP 3 | 4 | //////////////////////////////////////////////////////////// 5 | /// INCLUDES 6 | //////////////////////////////////////////////////////////// 7 | #include 8 | #include 9 | #include "Operation.hpp" 10 | 11 | //////////////////////////////////////////////////////////// 12 | /// NAMESPACE AI 13 | //////////////////////////////////////////////////////////// 14 | namespace ai 15 | { 16 | class Relu : public Operation 17 | { 18 | public: 19 | Relu(); 20 | Relu(ai::IOData& data); 21 | void initialize(std::vector &inputs); 22 | void save(ai::IOData& data); 23 | void run(std::vector &inputs, const bool training); 24 | void backprop(std::vector &inputs); 25 | const Operation::Type get_type() const; 26 | void print(); 27 | static std::shared_ptr make(); 28 | 29 | //////////////////////////////////////////////////////////// 30 | /// RAW OPERATIONS 31 | //////////////////////////////////////////////////////////// 32 | #ifndef CUDA_BACKEND 33 | static void foreward(const Tensor_float input, Tensor_float output); 34 | static void backward(const Tensor_float errors, const Tensor_float outputs, Tensor_float out_errors); 35 | #else 36 | ai::cudnn::Activation _cudnnactivation; 37 | #endif 38 | 39 | private: 40 | int _width, _height, _depth; 41 | }; 42 | 43 | } /* namespace ai */ 44 | 45 | #endif /* end of include guard: RELU_HPP */ 46 | 47 | -------------------------------------------------------------------------------- /src/AI/deeplearning/ResidualBlock.hpp: -------------------------------------------------------------------------------- 1 | #ifndef RESIDUALBLOCK_HPP 2 | #define RESIDUALBLOCK_HPP 3 | 4 | //////////////////////////////////////////////////////////// 5 | /// INCLUDES 6 | //////////////////////////////////////////////////////////// 7 | #include 8 | #include "Operation.hpp" 9 | #include "Convolution.hpp" 10 | #include "Normalization.hpp" 11 | #include "Relu.hpp" 12 | #include "Addition.hpp" 13 | #include 14 | #include 15 | 16 | //////////////////////////////////////////////////////////// 17 | /// NAMESPACE AI 18 | //////////////////////////////////////////////////////////// 19 | namespace ai 20 | { 21 | class ResidualBlock : public Operation 22 | { 23 | public: 24 | ResidualBlock(const int filter_size, const int filer_count, const int stride, const int padding, const unsigned int blocks_count); 25 | ResidualBlock(ai::IOData& data); 26 | void initialize(std::vector &inputs); 27 | void initialize(const int input_width, const int input_height, const int input_count); 28 | void save(ai::IOData& data); 29 | void run(std::vector &inputs, const bool training); 30 | void backprop(std::vector &inputs); 31 | void accumulate_deltas(std::vector &inputs); 32 | void update_parameters(const float learningrate); 33 | void reset_deltas(const double momentum); 34 | void reset_errors(); 35 | void reset_outputs(); 36 | const Operation::Type get_type() const; 37 | void print(); 38 | 39 | static std::shared_ptr make(const int filter_size, const int filter_count, const int stride, const int padding, const unsigned int blocks_count); 40 | 41 | std::vector< std::unique_ptr< ai::Operation > > _layers; 42 | std::vector< std::vector< Operation* > > _layers_connections; 43 | 44 | std::vector< Operation* > link_layers(Operation* op); 45 | std::vector< Operation* > link_layers(Operation* op1, Operation* op2); 46 | 47 | unsigned int _input_width; 48 | unsigned int _input_height; 49 | unsigned int _input_count; 50 | unsigned int _filter_width; 51 | unsigned int _filter_height; 52 | unsigned int _filter_count; 53 | unsigned int _stride; 54 | unsigned int _input_size; 55 | unsigned int _padding; 56 | unsigned int _blocks_count; 57 | }; 58 | 59 | } /* namespace ai */ 60 | 61 | #endif /* end of include guard: RESIDUALBLOCK_HPP */ 62 | 63 | -------------------------------------------------------------------------------- /src/AI/deeplearning/Selu.hpp: -------------------------------------------------------------------------------- 1 | #ifndef SELU_HPP 2 | #define SELU_HPP 3 | 4 | //////////////////////////////////////////////////////////// 5 | /// INCLUDES 6 | //////////////////////////////////////////////////////////// 7 | #include 8 | #include 9 | #include "Operation.hpp" 10 | 11 | //////////////////////////////////////////////////////////// 12 | /// NAMESPACE AI 13 | //////////////////////////////////////////////////////////// 14 | namespace ai 15 | { 16 | class Selu : public Operation 17 | { 18 | public: 19 | Selu(); 20 | Selu(ai::IOData& data); 21 | void initialize(std::vector &inputs); 22 | void save(ai::IOData& data); 23 | void run(std::vector &inputs, const bool training); 24 | void backprop(std::vector &inputs); 25 | const Operation::Type get_type() const; 26 | void print(); 27 | static std::shared_ptr make(); 28 | 29 | //////////////////////////////////////////////////////////// 30 | /// RAW OPERATIONS 31 | //////////////////////////////////////////////////////////// 32 | #ifndef CUDA_BACKEND 33 | static void foreward(const Tensor_float input, Tensor_float output); 34 | static void backward(const Tensor_float errors, const Tensor_float outputs, Tensor_float out_errors); 35 | #else 36 | ai::cudnn::Activation _cudnnactivation; 37 | #endif 38 | 39 | private: 40 | int _width, _height, _depth; 41 | }; 42 | 43 | } /* namespace ai */ 44 | 45 | #endif /* end of include guard: SELU_HPP */ 46 | 47 | -------------------------------------------------------------------------------- /src/AI/deeplearning/Sigmoid.cpp: -------------------------------------------------------------------------------- 1 | //////////////////////////////////////////////////////////// 2 | /// INCLUDES 3 | //////////////////////////////////////////////////////////// 4 | #include "Sigmoid.hpp" 5 | #include 6 | #include "../util/ensure.hpp" 7 | #ifdef CUDA_BACKEND 8 | #include "CUDA_backend.hpp" 9 | #endif 10 | 11 | //////////////////////////////////////////////////////////// 12 | /// NAMESPACE AI 13 | //////////////////////////////////////////////////////////// 14 | namespace ai 15 | { 16 | //////////////////////////////////////////////////////////// 17 | std::shared_ptr Sigmoid::make() 18 | { 19 | return std::shared_ptr(new Sigmoid()); 20 | } 21 | 22 | //////////////////////////////////////////////////////////// 23 | Sigmoid::Sigmoid() {} 24 | 25 | //////////////////////////////////////////////////////////// 26 | Sigmoid::Sigmoid(ai::IOData& data) 27 | { 28 | ai::IOData* size = data.findNode("size"); 29 | ensure(size != NULL); 30 | ai::IOData* width = data.findNode("width"); 31 | ensure(width != NULL); 32 | ai::IOData* height = data.findNode("height"); 33 | ensure(height != NULL); 34 | ai::IOData* depth = data.findNode("depth"); 35 | ensure(depth != NULL); 36 | size->get(_size); 37 | width->get(_width); 38 | height->get(_height); 39 | depth->get(_depth); 40 | _outputs.setshape(_width, _height, _depth); 41 | _outputs.fill(0); 42 | _errors.setshape(_size); 43 | _errors.fill(0); 44 | 45 | #ifdef CUDA_BACKEND 46 | _cudnnactivation.create(_size, 1, ai::cudnn::ACTIVATION_SIGMOID); 47 | #endif 48 | } 49 | 50 | //////////////////////////////////////////////////////////// 51 | void Sigmoid::initialize(std::vector &inputs) 52 | { 53 | //Check for errors 54 | ensure(inputs.size() == 1); 55 | 56 | //Calculate size 57 | _size = inputs[0]->_outputs.size(); 58 | _width = inputs[0]->_outputs.width(); 59 | _height = inputs[0]->_outputs.height(); 60 | _depth = inputs[0]->_outputs.depth(); 61 | 62 | //Initialize vectors 63 | _outputs.setshape(_width, _height, _depth); 64 | _outputs.fill(0); 65 | _errors.setshape(_size); 66 | _errors.fill(0); 67 | 68 | #ifdef CUDA_BACKEND 69 | _cudnnactivation.create(_size, 1, ai::cudnn::ACTIVATION_SIGMOID); 70 | #endif 71 | } 72 | 73 | //////////////////////////////////////////////////////////// 74 | void Sigmoid::save(ai::IOData& data) 75 | { 76 | data.pushNode("size", _size); 77 | data.pushNode("width", _width); 78 | data.pushNode("height", _height); 79 | data.pushNode("depth", _depth); 80 | } 81 | 82 | //////////////////////////////////////////////////////////// 83 | void Sigmoid::run(std::vector &inputs, const bool training) 84 | { 85 | //Check for correct input size 86 | ensure(inputs.size() == 1); 87 | ensure(inputs[0]->_outputs.size() == _outputs.size()); 88 | 89 | #ifdef CUDA_BACKEND 90 | 91 | _cudnnactivation.foreward(inputs[0]->_outputs.pointer(), _outputs.pointer()); 92 | 93 | #else 94 | //Shortcuts 95 | const Tensor_float& in = inputs[0]->_outputs; 96 | 97 | //Feedforeward 98 | for (int i = 0; i < (int)_outputs.size(); i++) 99 | _outputs[i] = 1.0 / (1.0 + exp(-in[i])); 100 | #endif 101 | } 102 | 103 | //////////////////////////////////////////////////////////// 104 | void Sigmoid::backprop(std::vector &inputs) 105 | { 106 | //Check for correct input size 107 | ensure(inputs.size() == 1); 108 | 109 | #ifdef CUDA_BACKEND 110 | 111 | _cudnnactivation.backward(inputs[0]->_outputs.pointer(), _outputs.pointer(), _errors.pointer(), inputs[0]->_errors.pointer()); 112 | 113 | #else 114 | //Shortcuts 115 | Tensor_float &out_errors = inputs[0]->_errors; 116 | 117 | //Feedforeward 118 | for (int i = 0; i < (int)out_errors.size(); i++) 119 | out_errors[i] = _outputs[i] * (1.f - _outputs[i]) * _errors[i]; 120 | #endif 121 | } 122 | 123 | //////////////////////////////////////////////////////////// 124 | const Operation::Type Sigmoid::get_type() const 125 | { 126 | return Operation::Sigmoid; 127 | } 128 | 129 | //////////////////////////////////////////////////////////// 130 | void Sigmoid::print() 131 | { 132 | printf("Type: Sigmoid, Size: %d", _size); 133 | } 134 | 135 | #ifndef CUDA_BACKEND 136 | 137 | //////////////////////////////////////////////////////////// 138 | void Sigmoid::foreward(const Tensor_float input, Tensor_float output) 139 | { 140 | //Check for errors 141 | ensure(output.size() == input.size()); 142 | 143 | //Feedforeward 144 | for (int i = 0; i < (int)output.size(); i++) 145 | output[i] = 1.0 / (1.0 + exp(-input[i])); 146 | } 147 | 148 | //////////////////////////////////////////////////////////// 149 | void Sigmoid::backward(const Tensor_float errors, const Tensor_float outputs, Tensor_float out_errors) 150 | { 151 | //Check for errors 152 | ensure(errors.size() == out_errors.size()); 153 | 154 | //Backward 155 | for (int i = 0; i < (int)out_errors.size(); i++) 156 | out_errors[i] = outputs[i] * (1.f - outputs[i]) * errors[i]; 157 | } 158 | 159 | #endif 160 | 161 | } /* namespace ai */ 162 | -------------------------------------------------------------------------------- /src/AI/deeplearning/Sigmoid.hpp: -------------------------------------------------------------------------------- 1 | #ifndef SIGMOID_HPP 2 | #define SIGMOID_HPP 3 | 4 | //////////////////////////////////////////////////////////// 5 | /// INCLUDES 6 | //////////////////////////////////////////////////////////// 7 | #include 8 | #include 9 | #include "Operation.hpp" 10 | 11 | //////////////////////////////////////////////////////////// 12 | /// NAMESPACE AI 13 | //////////////////////////////////////////////////////////// 14 | namespace ai 15 | { 16 | class Sigmoid : public Operation 17 | { 18 | public: 19 | Sigmoid(); 20 | Sigmoid(ai::IOData& data); 21 | void save(ai::IOData& data); 22 | void initialize(std::vector &inputs); 23 | void run(std::vector &inputs, const bool training); 24 | void backprop(std::vector &inputs); 25 | const Operation::Type get_type() const; 26 | void print(); 27 | static std::shared_ptr make(); 28 | 29 | //////////////////////////////////////////////////////////// 30 | /// RAW OPERATIONS 31 | //////////////////////////////////////////////////////////// 32 | #ifndef CUDA_BACKEND 33 | static void foreward(const Tensor_float input, Tensor_float output); 34 | static void backward(const Tensor_float errors, const Tensor_float outputs, Tensor_float out_errors); 35 | #else 36 | ai::cudnn::Activation _cudnnactivation; 37 | #endif 38 | 39 | private: 40 | int _width, _height, _depth; 41 | }; 42 | 43 | } /* namespace ai */ 44 | 45 | #endif /* end of include guard: SIGMOID_HPP */ 46 | 47 | -------------------------------------------------------------------------------- /src/AI/deeplearning/Softmax.cpp: -------------------------------------------------------------------------------- 1 | //////////////////////////////////////////////////////////// 2 | /// INCLUDES 3 | //////////////////////////////////////////////////////////// 4 | #include "Softmax.hpp" 5 | #include 6 | #include "../util/ensure.hpp" 7 | #ifdef CUDA_BACKEND 8 | #include "CUDA_backend.hpp" 9 | #endif 10 | 11 | //////////////////////////////////////////////////////////// 12 | /// NAMESPACE AI 13 | //////////////////////////////////////////////////////////// 14 | namespace ai 15 | { 16 | //////////////////////////////////////////////////////////// 17 | std::shared_ptr Softmax::make(double input_scale) 18 | { 19 | return std::shared_ptr(new Softmax(input_scale)); 20 | } 21 | 22 | //////////////////////////////////////////////////////////// 23 | Softmax::Softmax(double input_scale) 24 | { 25 | _input_scale = input_scale; 26 | _epsilon = 1e-4; 27 | } 28 | 29 | //////////////////////////////////////////////////////////// 30 | Softmax::Softmax(ai::IOData& data) 31 | { 32 | ai::IOData* size = data.findNode("size"); 33 | ensure(size != NULL); 34 | ai::IOData* input_scale = data.findNode("input_scale"); 35 | ensure(input_scale != NULL); 36 | ai::IOData* epsilon = data.findNode("epsilon"); 37 | ensure(epsilon != NULL); 38 | size->get(_size); 39 | input_scale->get(_input_scale); 40 | epsilon->get(_epsilon); 41 | _outputs.setshape(_size); 42 | _outputs.fill(0); 43 | _errors.setshape(_size); 44 | _errors.fill(0); 45 | } 46 | 47 | //////////////////////////////////////////////////////////// 48 | void Softmax::initialize(std::vector &inputs) 49 | { 50 | //Check for errors 51 | ensure(inputs.size() == 1); 52 | 53 | //Calculate size 54 | _size = inputs[0]->_size; 55 | 56 | //Initialize vectors 57 | _outputs.setshape(_size); 58 | _outputs.fill(0); 59 | _errors.setshape(_size); 60 | _errors.fill(0); 61 | } 62 | 63 | //////////////////////////////////////////////////////////// 64 | void Softmax::save(ai::IOData& data) 65 | { 66 | data.pushNode("size", _size); 67 | data.pushNode("input_scale", _input_scale); 68 | data.pushNode("epsilon", _epsilon); 69 | } 70 | 71 | //////////////////////////////////////////////////////////// 72 | void Softmax::run(std::vector &inputs, const bool training) 73 | { 74 | //Check for correct input size 75 | ensure(inputs.size() == 1); 76 | ensure(inputs[0]->_outputs.size() == _outputs.size()); 77 | 78 | #ifdef CUDA_BACKEND 79 | 80 | cuda::softmax_foreward(inputs[0]->_outputs.pointer(), _outputs.pointer(), _input_scale, _size, _epsilon); 81 | 82 | #else 83 | 84 | //Shortcuts 85 | Tensor_float& in = inputs[0]->_outputs; 86 | double sum = 0; 87 | 88 | //Calculate sum of all the inp 89 | for (int i = 0; i < _size; i++) { 90 | _outputs[i] = std::exp(in[i] * _input_scale); 91 | sum += _outputs[i]; 92 | } 93 | 94 | //Calculate outputs 95 | for (int i = 0; i < _size; i++) 96 | _outputs[i] = _outputs[i] / (sum + _epsilon); 97 | 98 | #endif 99 | } 100 | 101 | //////////////////////////////////////////////////////////// 102 | void Softmax::backprop(std::vector& inputs) 103 | { 104 | //Check for correct input size 105 | ensure(inputs.size() == 1); 106 | 107 | #ifdef CUDA_BACKEND 108 | 109 | cuda::softmax_backward(_errors.pointer(), inputs[0]->_errors.pointer(), _outputs.pointer(), _size); 110 | 111 | #else 112 | //Shortcuts 113 | Tensor_float &out_errors = inputs[0]->_errors; 114 | 115 | //Feedforeward 116 | for (int i = 0; i < (int)out_errors.size(); i++) 117 | out_errors[i] = _outputs[i] * (1.f - _outputs[i]) * _errors[i]; 118 | #endif 119 | } 120 | 121 | //////////////////////////////////////////////////////////// 122 | const Operation::Type Softmax::get_type() const 123 | { 124 | return Operation::Softmax; 125 | } 126 | 127 | //////////////////////////////////////////////////////////// 128 | void Softmax::print() 129 | { 130 | printf("Type: Softmax, Size: %d, Input_Scale: %f", _size, _input_scale); 131 | } 132 | 133 | } /* namespace ai */ 134 | -------------------------------------------------------------------------------- /src/AI/deeplearning/Softmax.hpp: -------------------------------------------------------------------------------- 1 | #ifndef SOFTMAX_HPP 2 | #define SOFTMAX_HPP 3 | 4 | //////////////////////////////////////////////////////////// 5 | /// INCLUDES 6 | //////////////////////////////////////////////////////////// 7 | #include 8 | #include 9 | #include "Operation.hpp" 10 | 11 | //////////////////////////////////////////////////////////// 12 | /// NAMESPACE AI 13 | //////////////////////////////////////////////////////////// 14 | namespace ai 15 | { 16 | class Softmax : public Operation 17 | { 18 | public: 19 | Softmax(double input_scale = 1.f); 20 | Softmax(ai::IOData& data); 21 | void initialize(std::vector &inputs); 22 | void save(ai::IOData& data); 23 | void run(std::vector &inputs, const bool training); 24 | void backprop(std::vector &inputs); 25 | const Operation::Type get_type() const; 26 | void print(); 27 | static std::shared_ptr make(double input_scale = 1.f); 28 | 29 | float _input_scale; 30 | 31 | private: 32 | float _epsilon; 33 | }; 34 | 35 | } /* namespace ai */ 36 | 37 | #endif /* end of include guard: SOFTMAX_HPP */ 38 | 39 | -------------------------------------------------------------------------------- /src/AI/deeplearning/Tanh.cpp: -------------------------------------------------------------------------------- 1 | //////////////////////////////////////////////////////////// 2 | /// INCLUDES 3 | //////////////////////////////////////////////////////////// 4 | #include "Tanh.hpp" 5 | #include 6 | #include "../util/ensure.hpp" 7 | #ifdef CUDA_BACKEND 8 | #include "CUDA_backend.hpp" 9 | #endif 10 | 11 | //////////////////////////////////////////////////////////// 12 | /// NAMESPACE AI 13 | //////////////////////////////////////////////////////////// 14 | namespace ai 15 | { 16 | //////////////////////////////////////////////////////////// 17 | std::shared_ptr Tanh::make() 18 | { 19 | return std::shared_ptr(new Tanh()); 20 | } 21 | 22 | //////////////////////////////////////////////////////////// 23 | Tanh::Tanh() {} 24 | 25 | //////////////////////////////////////////////////////////// 26 | Tanh::Tanh(ai::IOData& data) 27 | { 28 | ai::IOData* size = data.findNode("size"); 29 | ensure(size != NULL); 30 | ai::IOData* width = data.findNode("width"); 31 | ensure(width != NULL); 32 | ai::IOData* height = data.findNode("height"); 33 | ensure(height != NULL); 34 | ai::IOData* depth = data.findNode("depth"); 35 | ensure(depth != NULL); 36 | size->get(_size); 37 | width->get(_width); 38 | height->get(_height); 39 | depth->get(_depth); 40 | _outputs.setshape(_width, _height, _depth); 41 | _outputs.fill(0); 42 | _errors.setshape(_size); 43 | _outputs.fill(0); 44 | 45 | #ifdef CUDA_BACKEND 46 | _cudnnactivation.create(_size, 1, ai::cudnn::ACTIVATION_TANH); 47 | #endif 48 | } 49 | 50 | //////////////////////////////////////////////////////////// 51 | void Tanh::initialize(std::vector &inputs) 52 | { 53 | //Check for errors 54 | ensure(inputs.size() == 1); 55 | 56 | //Calculate size 57 | _size = inputs[0]->_outputs.size(); 58 | _width = inputs[0]->_outputs.width(); 59 | _height = inputs[0]->_outputs.height(); 60 | _depth = inputs[0]->_outputs.depth(); 61 | 62 | //Initialize vectors 63 | _outputs.setshape(_width, _height, _depth); 64 | _outputs.fill(0); 65 | _errors.setshape(_size); 66 | _outputs.fill(0); 67 | 68 | #ifdef CUDA_BACKEND 69 | _cudnnactivation.create(_size, 1, ai::cudnn::ACTIVATION_TANH); 70 | #endif 71 | } 72 | 73 | //////////////////////////////////////////////////////////// 74 | void Tanh::save(ai::IOData& data) 75 | { 76 | data.pushNode("size", _size); 77 | data.pushNode("width", _width); 78 | data.pushNode("height", _height); 79 | data.pushNode("depth", _depth); 80 | } 81 | 82 | //////////////////////////////////////////////////////////// 83 | void Tanh::run(std::vector &inputs, const bool training) 84 | { 85 | //Check for correct input size 86 | ensure(inputs.size() == 1); 87 | 88 | //Check for errors 89 | ensure(_outputs.size() == inputs[0]->_outputs.size()); 90 | 91 | #ifdef CUDA_BACKEND 92 | 93 | _cudnnactivation.foreward(inputs[0]->_outputs.pointer(), _outputs.pointer()); 94 | 95 | #else 96 | 97 | foreward(inputs[0]->_outputs, _outputs); 98 | 99 | #endif 100 | } 101 | 102 | //////////////////////////////////////////////////////////// 103 | void Tanh::backprop(std::vector &inputs) 104 | { 105 | //Check for correct input size 106 | ensure(inputs.size() == 1); 107 | 108 | //Check for errors 109 | ensure(_errors.size() == inputs[0]->_errors.size()); 110 | 111 | #ifdef CUDA_BACKEND 112 | 113 | _cudnnactivation.backward(inputs[0]->_outputs.pointer(), _outputs.pointer(), _errors.pointer(), inputs[0]->_errors.pointer()); 114 | 115 | #else 116 | 117 | backward(_errors, _outputs, inputs[0]->_errors); 118 | 119 | #endif 120 | } 121 | 122 | //////////////////////////////////////////////////////////// 123 | const Operation::Type Tanh::get_type() const 124 | { 125 | return Operation::Tanh; 126 | } 127 | 128 | //////////////////////////////////////////////////////////// 129 | void Tanh::print() 130 | { 131 | printf("Type: Tanh, Size: %d", _size); 132 | } 133 | 134 | //////////////////////////////////////////////////////////// 135 | /// RAW OPERATIONS 136 | //////////////////////////////////////////////////////////// 137 | 138 | #ifndef CUDA_BACKEND 139 | 140 | //////////////////////////////////////////////////////////// 141 | void Tanh::foreward(const Tensor_float input, Tensor_float output) 142 | { 143 | //Feedforeward 144 | for (int i = 0; i < (int)output.size(); i++) 145 | output[i] = tanh(input[i]); 146 | } 147 | 148 | //////////////////////////////////////////////////////////// 149 | void Tanh::backward(const Tensor_float errors, const Tensor_float outputs, Tensor_float out_errors) 150 | { 151 | //Backward 152 | for (int i = 0; i < (int)out_errors.size(); i++) 153 | out_errors[i] = (1.f - outputs[i] * outputs[i]) * errors[i]; 154 | } 155 | 156 | #endif 157 | 158 | } /* namespace ai */ 159 | -------------------------------------------------------------------------------- /src/AI/deeplearning/Tanh.hpp: -------------------------------------------------------------------------------- 1 | #ifndef TANH_HPP 2 | #define TANH_HPP 3 | 4 | //////////////////////////////////////////////////////////// 5 | /// INCLUDES 6 | //////////////////////////////////////////////////////////// 7 | #include 8 | #include 9 | #include "Operation.hpp" 10 | 11 | //////////////////////////////////////////////////////////// 12 | /// NAMESPACE AI 13 | //////////////////////////////////////////////////////////// 14 | namespace ai 15 | { 16 | class Tanh : public Operation 17 | { 18 | public: 19 | Tanh(); 20 | Tanh(ai::IOData& data); 21 | void initialize(std::vector &inputs); 22 | void save(ai::IOData& data); 23 | void run(std::vector &inputs, const bool training); 24 | void backprop(std::vector &inputs); 25 | const Operation::Type get_type() const; 26 | void print(); 27 | static std::shared_ptr make(); 28 | 29 | //////////////////////////////////////////////////////////// 30 | /// RAW OPERATIONS 31 | //////////////////////////////////////////////////////////// 32 | #ifndef CUDA_BACKEND 33 | static void foreward(const Tensor_float input, Tensor_float output); 34 | static void backward(const Tensor_float errors, const Tensor_float outputs, Tensor_float out_errors); 35 | #else 36 | ai::cudnn::Activation _cudnnactivation; 37 | #endif 38 | 39 | private: 40 | int _width, _height, _depth; 41 | }; 42 | 43 | } /* namespace ai */ 44 | 45 | #endif /* end of include guard: TANH_HPP */ 46 | 47 | -------------------------------------------------------------------------------- /src/AI/deeplearning/Variable.cpp: -------------------------------------------------------------------------------- 1 | //////////////////////////////////////////////////////////// 2 | /// INCLUDES 3 | //////////////////////////////////////////////////////////// 4 | #include "Variable.hpp" 5 | 6 | //////////////////////////////////////////////////////////// 7 | /// NAMESPACE AI 8 | //////////////////////////////////////////////////////////// 9 | namespace ai 10 | { 11 | //////////////////////////////////////////////////////////// 12 | std::shared_ptr Variable::make(int width) 13 | { 14 | return std::shared_ptr(new Variable(width)); 15 | } 16 | 17 | //////////////////////////////////////////////////////////// 18 | std::shared_ptr Variable::make(int width, int height) 19 | { 20 | return std::shared_ptr(new Variable(width, height)); 21 | } 22 | 23 | //////////////////////////////////////////////////////////// 24 | std::shared_ptr Variable::make(int width, int height, int depth) 25 | { 26 | return std::shared_ptr(new Variable(width, height, depth)); 27 | } 28 | 29 | //////////////////////////////////////////////////////////// 30 | Variable::Variable(int width) 31 | { 32 | _size = width; 33 | _width = width; 34 | _height = 1; 35 | _depth = 1; 36 | _outputs.setshape(_size); 37 | _outputs.fill(0); 38 | } 39 | 40 | //////////////////////////////////////////////////////////// 41 | Variable::Variable(int width, int height) 42 | { 43 | _size = width * height; 44 | _width = width; 45 | _height = height; 46 | _depth = 1; 47 | _outputs.setshape(width, height); 48 | _outputs.fill(0); 49 | } 50 | 51 | //////////////////////////////////////////////////////////// 52 | Variable::Variable(int width, int height, int depth) 53 | { 54 | _size = width * height * depth; 55 | _width = width; 56 | _height = height; 57 | _depth = depth; 58 | _outputs.setshape(width, height, depth); 59 | _outputs.fill(0); 60 | } 61 | 62 | //////////////////////////////////////////////////////////// 63 | Variable::Variable(ai::IOData& data) 64 | { 65 | ai::IOData* size = data.findNode("size"); 66 | ensure(size != NULL); 67 | ai::IOData* width = data.findNode("width"); 68 | ensure(width != NULL); 69 | ai::IOData* height = data.findNode("height"); 70 | ensure(height != NULL); 71 | ai::IOData* depth = data.findNode("depth"); 72 | ensure(depth != NULL); 73 | size->get(_size); 74 | width->get(_width); 75 | height->get(_height); 76 | depth->get(_depth); 77 | _outputs.setshape(_width, _height, _depth); 78 | _outputs.fill(0); 79 | } 80 | 81 | //////////////////////////////////////////////////////////// 82 | void Variable::save(ai::IOData& data) 83 | { 84 | data.pushNode("size", _size); 85 | data.pushNode("width", _width); 86 | data.pushNode("height", _height); 87 | data.pushNode("depth", _depth); 88 | } 89 | 90 | //////////////////////////////////////////////////////////// 91 | void Variable::print() 92 | { 93 | printf("Type: Variable, Size: %dx%dx%d", _width, _height, _depth); 94 | } 95 | 96 | //////////////////////////////////////////////////////////// 97 | const Operation::Type Variable::get_type() const 98 | { 99 | return Operation::Variable; 100 | } 101 | 102 | } /* namespace ai */ 103 | -------------------------------------------------------------------------------- /src/AI/deeplearning/Variable.hpp: -------------------------------------------------------------------------------- 1 | #ifndef VARIABLE_HPP 2 | #define VARIABLE_HPP 3 | 4 | //////////////////////////////////////////////////////////// 5 | /// INCLUDES 6 | //////////////////////////////////////////////////////////// 7 | #include "Operation.hpp" 8 | 9 | //////////////////////////////////////////////////////////// 10 | /// NAMESPACE AI 11 | //////////////////////////////////////////////////////////// 12 | namespace ai 13 | { 14 | 15 | class Variable : public Operation 16 | { 17 | public: 18 | 19 | Variable(int width); 20 | Variable(int width, int height); 21 | Variable(int width, int height, int depth); 22 | Variable(ai::IOData& data); 23 | void save(ai::IOData& data); 24 | void print(); 25 | const Operation::Type get_type() const; 26 | static std::shared_ptr make(int width); 27 | static std::shared_ptr make(int width, int height); 28 | static std::shared_ptr make(int width, int height, int depth); 29 | 30 | private: 31 | int _width, _height, _depth; 32 | 33 | }; 34 | 35 | } /* namespace ai */ 36 | 37 | 38 | #endif /* end of include guard: VARIABLE_HPP */ 39 | 40 | -------------------------------------------------------------------------------- /src/AI/deeplearning/WeightRegularization.cpp: -------------------------------------------------------------------------------- 1 | //////////////////////////////////////////////////////////// 2 | /// INCLUDES 3 | //////////////////////////////////////////////////////////// 4 | #include "WeightRegularization.hpp" 5 | #ifdef CUDA_BACKEND 6 | #include "CUDA_backend.hpp" 7 | #endif 8 | 9 | //////////////////////////////////////////////////////////// 10 | /// NAMESPACE AI 11 | //////////////////////////////////////////////////////////// 12 | namespace ai 13 | { 14 | 15 | //////////////////////////////////////////////////////////// 16 | /// NAMESPACE WEIGHTREGULARIZATION 17 | //////////////////////////////////////////////////////////// 18 | namespace weightreg 19 | { 20 | 21 | #ifdef CUDA_BACKEND 22 | 23 | //////////////////////////////////////////////////////////// 24 | void l1_regularization(TensorCUDA_float& weights, const float l1_factor, const float learningrate) 25 | { 26 | cuda::l1_regularization(weights.pointer(), l1_factor, learningrate, weights.size()); 27 | } 28 | 29 | //////////////////////////////////////////////////////////// 30 | void l2_regularization(TensorCUDA_float& weights, const float l2_factor, const float learningrate) 31 | { 32 | cuda::l2_regularization(weights.pointer(), l2_factor, learningrate, weights.size()); 33 | } 34 | 35 | #else 36 | 37 | //////////////////////////////////////////////////////////// 38 | void l1_regularization(Tensor_float& weights, const float l1_factor, const float learningrate) 39 | { 40 | for (int i = 0; i < weights.size(); i++) 41 | weights[i] += (weights[i] > 0 ? -1.f : 1.f) * l1_factor * learningrate; 42 | } 43 | 44 | //////////////////////////////////////////////////////////// 45 | void l2_regularization(Tensor_float& weights, const float l2_factor, const float learningrate) 46 | { 47 | for (int i = 0; i < weights.size(); i++) 48 | weights[i] += (0 - weights[i]) * l2_factor * learningrate; 49 | } 50 | 51 | #endif 52 | 53 | } /* namespace weightreg */ 54 | 55 | } /* namespace ai */ 56 | -------------------------------------------------------------------------------- /src/AI/deeplearning/WeightRegularization.hpp: -------------------------------------------------------------------------------- 1 | #ifndef WEIGHTREGULARIZATION_HPP 2 | #define WEIGHTREGULARIZATION_HPP 3 | 4 | //////////////////////////////////////////////////////////// 5 | /// INCLUDES 6 | //////////////////////////////////////////////////////////// 7 | #include "../util/Macros.hpp" 8 | #include "../util/Tensor.hpp" 9 | #ifdef CUDA_BACKEND 10 | #include "../util/TensorCUDA.hpp" 11 | #endif 12 | 13 | //////////////////////////////////////////////////////////// 14 | /// NAMESPACE AI 15 | //////////////////////////////////////////////////////////// 16 | namespace ai 17 | { 18 | 19 | //////////////////////////////////////////////////////////// 20 | /// NAMESPACE WEIGHTREGULARIZATION 21 | //////////////////////////////////////////////////////////// 22 | namespace weightreg 23 | { 24 | 25 | #ifdef CUDA_BACKEND 26 | 27 | void l1_regularization(TensorCUDA_float& weights, const float l1_factor, const float learningrate); 28 | void l2_regularization(TensorCUDA_float& weights, const float l2_factor, const float learningrate); 29 | 30 | #else 31 | 32 | void l1_regularization(Tensor_float& weights, const float l1_factor, const float learningrate); 33 | void l2_regularization(Tensor_float& weights, const float l2_factor, const float learningrate); 34 | 35 | #endif 36 | 37 | } /* namespace weightregularization */ 38 | 39 | } /* namespace ai */ 40 | 41 | #endif /* end of include guard: WEIGHTREGULARIZATION_HPP */ 42 | 43 | -------------------------------------------------------------------------------- /src/AI/optimization/GeneticAlgorithm.hpp: -------------------------------------------------------------------------------- 1 | /* 2 | MIT License 3 | 4 | Copyright (c) 2023 Carlo Meroni 5 | 6 | Permission is hereby granted, free of charge, to any person obtaining a copy 7 | of this software and associated documentation files (the "Software"), to deal 8 | in the Software without restriction, including without limitation the rights 9 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | copies of the Software, and to permit persons to whom the Software is 11 | furnished to do so, subject to the following conditions: 12 | 13 | The above copyright notice and this permission notice shall be included in all 14 | copies or substantial portions of the Software. 15 | 16 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | SOFTWARE. 23 | */ 24 | 25 | #ifndef GENETICALGORITHM_HPP 26 | #define GENETICALGORITHM_HPP 27 | 28 | #include 29 | #include 30 | #include 31 | 32 | typedef void* Chromosome; 33 | 34 | class GeneticAlgorithm { 35 | public: 36 | 37 | enum MigrationMode { 38 | ELITIST, 39 | RANDOM, 40 | }; 41 | 42 | enum StopConditionType { 43 | ITERATIONS, 44 | SECONDS, 45 | }; 46 | 47 | GeneticAlgorithm(); 48 | 49 | Chromosome getBestChromosome(); 50 | float getBestChromosomeFitness(); 51 | const std::vector& getPopulation(); 52 | void optimize(unsigned int populationSize, double iterations, 53 | unsigned int cores = 1, float migrationInterval = 5, bool verbose = true); 54 | const std::vector& getFitnessLog(); 55 | 56 | // functions to overload 57 | virtual Chromosome initSolution(unsigned int wid); 58 | virtual float computeFitness(Chromosome& c, unsigned int wid); 59 | virtual Chromosome crossingOver(const Chromosome& mother, 60 | const Chromosome& father, 61 | unsigned int wid); 62 | virtual void mutate(Chromosome& c, float mutationRate, unsigned int wid); 63 | virtual void freeChromosome(Chromosome& c); 64 | virtual void printChromosome(Chromosome& c); 65 | virtual void copyChromosome(Chromosome& src, Chromosome& dst); 66 | 67 | virtual unsigned int roundSchedule(const float time); 68 | virtual float mutationSchedule(const float time); 69 | virtual unsigned int getSolutionSize(); 70 | 71 | void setMigrationMode(MigrationMode mode); 72 | void setMigrationSize(const unsigned int size); 73 | void setSelectionProbability(const float p); 74 | void setStopConditionType(StopConditionType type); 75 | 76 | int randSafe(unsigned int wid); 77 | float randfSafe(unsigned int wid); 78 | 79 | private: 80 | void _initializing_multiple(unsigned int workers, unsigned int wid); 81 | void _run_multiple(unsigned int populationSize, unsigned int wid); 82 | void _clearPopulation(); 83 | void _migrateIsland_Elitist(unsigned int wid); 84 | void _migrateIsland_Random(unsigned int wid, unsigned int count); 85 | 86 | unsigned int getGoodChromosomeID(unsigned int wid); 87 | unsigned int getBadChromosomeID(unsigned int wid); 88 | float getMeanFitness(); 89 | 90 | void setupRandomSeeds(unsigned int workers); 91 | 92 | unsigned int populationSize; 93 | int islandId; 94 | int islandsCount; 95 | std::vector islandsState; 96 | unsigned int migrationInterval; 97 | std::chrono::high_resolution_clock::time_point lastMigrationTime; 98 | std::vector population; 99 | std::vector populationFitness; 100 | std::vector seeds; 101 | 102 | std::vector fitnessLog; 103 | 104 | int bestChromosomeID; 105 | float bestFitness; 106 | 107 | MigrationMode migrationMode; 108 | unsigned int migrationSize; 109 | bool verbose; 110 | double currentIteration; 111 | double totalIterations; 112 | 113 | unsigned int roundCount; 114 | float selectionProbability; 115 | StopConditionType stopConditionType; 116 | 117 | // FOr multithreadhing control 118 | std::mutex g_lock; 119 | }; 120 | 121 | #endif // !GENETICALGORITHM_HPP 122 | -------------------------------------------------------------------------------- /src/AI/optimization/pso.cpp: -------------------------------------------------------------------------------- 1 | //////////////////////////////////////////////////////////// 2 | /// INCLUDES 3 | //////////////////////////////////////////////////////////// 4 | #include "pso.hpp" 5 | #include 6 | 7 | //////////////////////////////////////////////////////////// 8 | /// AI 9 | //////////////////////////////////////////////////////////// 10 | namespace ai 11 | { 12 | //////////////////////////////////////////////////////////// 13 | double randuniform() 14 | { 15 | return (double)rand() / RAND_MAX; 16 | } 17 | 18 | //////////////////////////////////////////////////////////// 19 | pso::pso(int particles_count, int params_count, double(*eval)(std::vector&), double learning_rate_local, double learning_rate_global) 20 | { 21 | //Set population parameters 22 | _particles_count = particles_count; 23 | _params_count = params_count; 24 | _learning_rate_local = learning_rate_local; 25 | _learning_rate_global = learning_rate_global; 26 | 27 | //Set evaluation function 28 | _eval = eval; 29 | 30 | //Set global best score to a very low number and create random best positon 31 | _global_bestscore = -0xFFFFFF; 32 | _global_bestpos = std::vector(_params_count); 33 | for (int i = 0; i < _params_count; i++) 34 | _global_bestpos[i] = randuniform(); 35 | 36 | //Randomly generate particles 37 | for (int i = 0; i < _particles_count; i++) { 38 | particle p; 39 | 40 | //Create random velicity vector 41 | p.v = std::vector(_params_count); 42 | for (int i = 0; i < _params_count; i++) 43 | p.v[i] = randuniform(); 44 | 45 | //Place the particle in a random position 46 | p.pos = std::vector(_params_count); 47 | for (int i = 0; i < _params_count; i++) 48 | p.pos[i] = randuniform(); 49 | 50 | //Initialize best position 51 | p.bestpos = p.pos; 52 | p.bestscore = -0xFFFFFF; 53 | 54 | //Add particle 55 | _particles.push_back(p); 56 | } 57 | } 58 | 59 | //////////////////////////////////////////////////////////// 60 | void pso::run(unsigned long steps) 61 | { 62 | double score; 63 | for (unsigned int s = 0; s < steps; s++) { 64 | 65 | //Test each particle 66 | for (int p = 0; p < _particles_count; p++) { 67 | 68 | //Evaluate particle position 69 | score = _eval(_particles[p].pos); 70 | 71 | //Update best particle score 72 | if (score > _particles[p].bestscore) { 73 | _particles[p].bestscore = score; 74 | _particles[p].bestpos = _particles[p].pos; 75 | } 76 | 77 | //Update global best 78 | if (score > _global_bestscore) { 79 | _global_bestscore = score; 80 | _global_bestpos = _particles[p].pos; 81 | } 82 | } 83 | 84 | //Move each particle 85 | for (int p = 0; p < _particles_count; p++) { 86 | for (int j = 0; j < _params_count; j++) { 87 | //Accelerate in the direction of the particle best pos and the global best pos 88 | //with randomized velocities 89 | _particles[p].v[j] += _learning_rate_local * randuniform() * (_particles[p].bestpos[j] - _particles[p].pos[j]) + 90 | _learning_rate_global * randuniform() * (_global_bestpos[j] - _particles[p].pos[j]); 91 | if (_particles[p].v[j] > 20) _particles[p].v[j] = 20; 92 | if (_particles[p].v[j] < -20) _particles[p].v[j] = -20; 93 | _particles[p].pos[j] += _particles[p].v[j]; 94 | } 95 | } 96 | } 97 | } 98 | 99 | //////////////////////////////////////////////////////////// 100 | std::vector pso::getBestParams() 101 | { 102 | return _global_bestpos; 103 | } 104 | 105 | //////////////////////////////////////////////////////////// 106 | double pso::getBestScore() 107 | { 108 | return _global_bestscore; 109 | } 110 | 111 | //////////////////////////////////////////////////////////// 112 | void pso::setLocalLearningrate(double val) 113 | { 114 | _learning_rate_local = val; 115 | } 116 | 117 | //////////////////////////////////////////////////////////// 118 | void pso::setGlobalLearningrate(double val) 119 | { 120 | _learning_rate_global = val; 121 | } 122 | 123 | //////////////////////////////////////////////////////////// 124 | double pso::getLocalLearningrate() 125 | { 126 | return _learning_rate_local; 127 | } 128 | 129 | //////////////////////////////////////////////////////////// 130 | double pso::getGlobalLearningrate() 131 | { 132 | return _learning_rate_global; 133 | } 134 | 135 | //////////////////////////////////////////////////////////// 136 | const std::vector& pso::getParticles() 137 | { 138 | return _particles; 139 | } 140 | 141 | } //namespace ai 142 | 143 | -------------------------------------------------------------------------------- /src/AI/optimization/pso.hpp: -------------------------------------------------------------------------------- 1 | #ifndef EVOLUTION_HPP 2 | #define EVOLUTION_HPP 3 | 4 | //////////////////////////////////////////////////////////// 5 | /// INCLUDES 6 | //////////////////////////////////////////////////////////// 7 | #include 8 | #include 9 | 10 | 11 | //////////////////////////////////////////////////////////// 12 | /// AI 13 | //////////////////////////////////////////////////////////// 14 | namespace ai 15 | { 16 | 17 | struct particle 18 | { 19 | std::vector v; //velocities 20 | std::vector pos; //position in the parameterspace 21 | std::vector bestpos; //best position in the parameterspace 22 | double bestscore; //best score so far 23 | }; 24 | 25 | //////////////////////////////////////////////////////////// 26 | /// Particle Swarm Optimization 27 | //////////////////////////////////////////////////////////// 28 | class pso 29 | { 30 | public: 31 | 32 | //////////////////////////////////////////////////////////// 33 | /// \brief Initialize pso 34 | /// 35 | //////////////////////////////////////////////////////////// 36 | pso(int particles_count, int params_count, double(*eval)(std::vector&), double learning_rate_local, double learning_rate_global); 37 | 38 | //////////////////////////////////////////////////////////// 39 | /// \brief Run the particle swarm for X steps 40 | /// 41 | //////////////////////////////////////////////////////////// 42 | void run(unsigned long steps); 43 | 44 | //////////////////////////////////////////////////////////// 45 | /// \brief Get the best position found in the parameterspace 46 | /// 47 | //////////////////////////////////////////////////////////// 48 | std::vector getBestParams(); 49 | 50 | //////////////////////////////////////////////////////////// 51 | /// \brief Get the best score 52 | /// 53 | //////////////////////////////////////////////////////////// 54 | double getBestScore(); 55 | 56 | //////////////////////////////////////////////////////////// 57 | /// \brief Set local learning rate 58 | /// 59 | //////////////////////////////////////////////////////////// 60 | void setLocalLearningrate(double val); 61 | 62 | //////////////////////////////////////////////////////////// 63 | /// \brief Set global learning rate 64 | /// 65 | //////////////////////////////////////////////////////////// 66 | void setGlobalLearningrate(double val); 67 | 68 | //////////////////////////////////////////////////////////// 69 | /// \brief Get local learning rate 70 | /// 71 | //////////////////////////////////////////////////////////// 72 | double getLocalLearningrate(); 73 | 74 | //////////////////////////////////////////////////////////// 75 | /// \brief Get global learning rate 76 | /// 77 | //////////////////////////////////////////////////////////// 78 | double getGlobalLearningrate(); 79 | 80 | //////////////////////////////////////////////////////////// 81 | /// \brief Get the particles 82 | /// 83 | //////////////////////////////////////////////////////////// 84 | const std::vector& getParticles(); 85 | 86 | private: 87 | 88 | //Population parameters 89 | int _particles_count; 90 | int _params_count; 91 | double _learning_rate_local; 92 | double _learning_rate_global; 93 | std::vector _particles; 94 | 95 | //Best position 96 | double _global_bestscore; 97 | std::vector _global_bestpos; 98 | 99 | //Evaluation function 100 | double(*_eval)(std::vector& params); 101 | }; 102 | 103 | } //namespace ai 104 | 105 | #endif /* end of include guard: EVOLUTION_HPP */ 106 | 107 | -------------------------------------------------------------------------------- /src/AI/python_interface/ailib.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Flowx08/artificial_intelligence/a93e405702b0e37eb58ad092d409111a96b2483a/src/AI/python_interface/ailib.pyc -------------------------------------------------------------------------------- /src/AI/python_interface/openai_gym.py: -------------------------------------------------------------------------------- 1 | import gym 2 | import ailib 3 | import numpy as np 4 | 5 | #env = gym.make("Acrobot-v1") 6 | env = gym.make('CartPole-v1') 7 | 8 | agent_model = ailib.neuralnetwork() 9 | agent_model.push_variable("INPUT", env.observation_space.shape[0]) 10 | agent_model.push_linear("FC1", "INPUT", 32) 11 | agent_model.push_normalization("NOR1", "FC1", 0.0) 12 | agent_model.push_relu("ACT1", "NOR1") 13 | agent_model.push_linear("FC2", "ACT1", 32) 14 | agent_model.push_normalization("NOR2", "FC2", 0.0) 15 | agent_model.push_relu("ACT2", "NOR2") 16 | agent_model.push_linear("FC3", "ACT2", env.action_space.n) 17 | 18 | agent_model.printstack() 19 | 20 | agent = ailib.dqn_agent(agent_model, 200000, 0.003, 0.9, 0.97) 21 | 22 | for i_episode in range(1000): 23 | observation = env.reset() 24 | agent.set_curiosity(agent.get_curiosity() * 0.99) 25 | for t in range(500): 26 | if i_episode % 10 == 0: env.render() 27 | #env.render() 28 | action = agent.act(observation.astype("f")) 29 | observation, reward, done, info = env.step(action) 30 | #print(agent_model.getoutput("FC3")) 31 | if done: agent.observe(np.array([]).astype("f"), reward) 32 | else: agent.observe(observation.astype("f"), reward) 33 | if done: 34 | print("Episode finished after {} timesteps, lr: {} curiosity: {} lte: {}".format(t+1, 35 | agent.get_learningrate(), agent.get_curiosity(), agent.get_longtermexploitation())) 36 | break 37 | -------------------------------------------------------------------------------- /src/AI/python_interface/setup.py: -------------------------------------------------------------------------------- 1 | from distutils.core import setup, Extension 2 | from distutils import sysconfig 3 | import numpy.distutils.misc_util 4 | import os, glob 5 | 6 | print(sysconfig.get_config_vars()['CFLAGS']) 7 | 8 | cpp_files = [] 9 | cpp_files.append("ailib.cpp") 10 | 11 | #compile deeplearning folder 12 | for file in os.listdir("../deeplearning/"): 13 | if file == "Dataset.cpp": continue 14 | if file.endswith(".cpp"): cpp_files.append(os.path.join("../deeplearning/", file)) 15 | 16 | #compile util folder 17 | for file in os.listdir("../util/"): 18 | if file.endswith(".cpp"): 19 | cpp_files.append(os.path.join("../util/", file)) 20 | 21 | #compile reinforcementlearning folder 22 | for file in os.listdir("../RL/"): 23 | if file.endswith(".cpp"): 24 | cpp_files.append(os.path.join("../RL/", file)) 25 | 26 | #compile visualization folder 27 | """ 28 | for file in os.listdir("../visualization/"): 29 | if file.endswith(".cpp"): 30 | cpp_files.append(os.path.join("../visualization/", file)) 31 | """ 32 | 33 | c_ext = Extension("_ailib", cpp_files, extra_compile_args=['-std=c++11'], language="c++") 34 | 35 | setup( 36 | ext_modules=[c_ext], 37 | include_dirs=numpy.distutils.misc_util.get_numpy_include_dirs(), 38 | ) 39 | -------------------------------------------------------------------------------- /src/AI/python_interface/test.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | import ailib 3 | import random 4 | 5 | import tensorflow.examples.tutorials.mnist.input_data as input_data 6 | mnist = input_data.read_data_sets("MNIST", one_hot=True, validation_size=10000) 7 | 8 | random.seed(0) 9 | net = ailib.neuralnetwork() 10 | net.push_variable("INPUT1", 28, 28) 11 | 12 | net.push_convolution("CONV1", "INPUT1", 4, 4, 10, 1, 1) 13 | net.push_normalization("NORM1", "CONV1", 0.5) 14 | net.push_relu("REL1", "NORM1") 15 | net.push_maxpooling("MAX1", "REL1") 16 | #net.push_dropout("DROP1", "MAX1", 0.1) 17 | net.push_convolution("CONV2", "MAX1", 3, 3, 8, 1, 1) 18 | net.push_normalization("NORM2", "CONV2", 0.5) 19 | net.push_relu("REL2", "NORM2") 20 | net.push_averagepooling("AVG", "REL2") 21 | #net.push_dropout("DROP2", "AVG", 0.1) 22 | net.push_convolution("CONV3", "AVG", 3, 3, 8, 1, 1) 23 | net.push_normalization("NORM3", "CONV3", 0.5) 24 | net.push_relu("REL3", "NORM3") 25 | net.push_linear("FC1", "REL3", 128) 26 | net.push_normalization("NORM4", "FC1", 0.5) 27 | net.push_relu("REL4", "NORM4") 28 | #net.push_dropout("DROP3", "REL4", 0.4) 29 | net.push_linear("FC2", "REL4", 10) 30 | net.push_softmax("OUTPUT", "FC2") 31 | 32 | """ 33 | random.seed(0) 34 | net.push_linear("FC1", "INPUT1", 100) 35 | net.push_normalization("NORM4", "FC1", 0.5) 36 | net.push_relu("REL4", "NORM4") 37 | net.push_linear("FC2", "REL4", 10) 38 | net.push_softmax("OUTPUT", "FC2", 1.0) 39 | a = np.array([ float(i)/32.0 for i in range(32)]).astype("f") 40 | print(a) 41 | net.run(a) 42 | print(net.getoutput("OUTPUT")) 43 | 44 | net.push_convolution("CONV1", "INPUT1", 4, 4, 4, 1, 1) 45 | net.push_linear("FC1", "CONV1", 128) 46 | net.push_normalization("NORM4", "FC1") 47 | net.push_relu("REL4", "NORM4") 48 | net.push_linear("FC2", "REL4", 10) 49 | net.push_softmax("OUTPUT", "FC2", 1.0) 50 | """ 51 | 52 | net.printstack() 53 | 54 | baselr = 0.01 55 | opt = ailib.optimizer_sdg(10, baselr, 0.5, ailib.CostFunction.CROSSENTROPY) 56 | 57 | """ 58 | a = np.array([ float(i)/(28.0 * 28.0) for i in range(28 * 28)]).astype("f") 59 | b = np.array([ float(i == 0) for i in range(10)]).astype("f") 60 | print(b) 61 | err = net.optimize(a, b, opt) 62 | print(net.getoutput("OUTPUT")) 63 | print(err) 64 | """ 65 | 66 | loss = 0.0 67 | for i in range(100000): 68 | 69 | #Update learningrate 70 | opt.set_learningrate(baselr * (1.0 - float(i) / 100000.0)) 71 | 72 | #Select random sample and optimize network 73 | rand_sample = random.randint(0, len(mnist.train.images)-1) 74 | loss += net.optimize(mnist.train.images[rand_sample], mnist.train.labels[rand_sample].astype(np.float32), opt) 75 | #print(net.getoutput("OUTPUT")) 76 | if (loss != loss): print("Error: {}".format(i)) 77 | 78 | 79 | #Log 80 | if i % 1000 == 0 and i != 0: 81 | print("Cicle: {} MediumLoss: {} learningrate: {}".format(i, loss / 1000.0, opt.get_learningrate())) 82 | loss = 0.0 83 | 84 | #Test on validationset to see generalization 85 | if i % 25000 == 0 and i != 0: 86 | errors = 0 87 | for k in range(len(mnist.test.images)): 88 | net.run(mnist.test.images[k]) 89 | prediction = np.argmax(net.getoutput("OUTPUT")) 90 | correct_solution = np.argmax(mnist.test.labels[k]) 91 | if (prediction != correct_solution): errors += 1 92 | print("Testing errors: {}".format(errors)) 93 | -------------------------------------------------------------------------------- /src/AI/util/Files.cpp: -------------------------------------------------------------------------------- 1 | //////////////////////////////////////////////////////////// 2 | /// INCLUDES 3 | //////////////////////////////////////////////////////////// 4 | #include "Files.hpp" 5 | #include 6 | #if defined(_WIN32) || defined(_WIN64) 7 | #include "../util/dirent_win.h" 8 | #else 9 | #include 10 | #endif 11 | 12 | //////////////////////////////////////////////////////////// 13 | /// NAMESPACE AI 14 | //////////////////////////////////////////////////////////// 15 | namespace ai 16 | { 17 | 18 | //////////////////////////////////////////////////////////// 19 | /// FILES 20 | //////////////////////////////////////////////////////////// 21 | namespace files 22 | { 23 | 24 | //////////////////////////////////////////////////////////// 25 | std::vector listdir(std::string folderpath) 26 | { 27 | //Open directory 28 | DIR *dir_count; 29 | struct dirent *ent_count; 30 | dir_count = opendir(folderpath.c_str()); 31 | 32 | //Check for errrors 33 | if (dir_count == NULL) 34 | printf("Error, can't open directory of path %s\n", folderpath.c_str()); 35 | 36 | //Store all file names 37 | std::vector list_files; 38 | while ((ent_count = readdir(dir_count)) != NULL) { 39 | if (std::string(ent_count->d_name) == "." || std::string(ent_count->d_name) == "..") continue; 40 | list_files.push_back(std::string(ent_count->d_name)); 41 | } 42 | 43 | return list_files; 44 | } 45 | 46 | //////////////////////////////////////////////////////////// 47 | std::string get_extension(std::string path) 48 | { 49 | size_t lastdot = path.find_last_of("."); 50 | if (lastdot == std::string::npos) return ""; 51 | return path.substr(lastdot, path.size()); 52 | } 53 | 54 | //////////////////////////////////////////////////////////// 55 | std::string remove_extension(std::string path) 56 | { 57 | size_t lastdot = path.find_last_of("."); 58 | if (lastdot == std::string::npos) return path; 59 | return path.substr(0, lastdot); 60 | } 61 | 62 | //////////////////////////////////////////////////////////// 63 | bool exists(std::string filepath) 64 | { 65 | std::ifstream file(filepath); 66 | if (file) return true; 67 | return false; 68 | } 69 | 70 | } /* namespace files */ 71 | 72 | } /* namespace ai */ 73 | -------------------------------------------------------------------------------- /src/AI/util/Files.hpp: -------------------------------------------------------------------------------- 1 | #ifndef FILES_HPP 2 | #define FILES_HPP 3 | 4 | //////////////////////////////////////////////////////////// 5 | /// INCLUDES 6 | //////////////////////////////////////////////////////////// 7 | #include 8 | #include 9 | 10 | //////////////////////////////////////////////////////////// 11 | /// NAMESPACE AI 12 | //////////////////////////////////////////////////////////// 13 | namespace ai 14 | { 15 | 16 | //////////////////////////////////////////////////////////// 17 | /// FILES 18 | //////////////////////////////////////////////////////////// 19 | namespace files 20 | { 21 | 22 | std::vector listdir(std::string folderpath); 23 | std::string get_extension(std::string path); 24 | std::string remove_extension(std::string path); 25 | bool exists(std::string filepath); 26 | 27 | } /* namespace files */ 28 | 29 | } /* namespace ai */ 30 | 31 | #endif /* end of include guard: FILES_HPP */ 32 | 33 | 34 | -------------------------------------------------------------------------------- /src/AI/util/IOData.hpp: -------------------------------------------------------------------------------- 1 | #ifndef IODATA_HPP 2 | #define IODATA_HPP 3 | 4 | //////////////////////////////////////////////////////////// 5 | /// INCLUDES 6 | //////////////////////////////////////////////////////////// 7 | #include 8 | #include 9 | #include 10 | 11 | //////////////////////////////////////////////////////////// 12 | /// NAMESPACE AI 13 | //////////////////////////////////////////////////////////// 14 | namespace ai 15 | { 16 | class IOData 17 | { 18 | public: 19 | 20 | IOData(std::string name); 21 | IOData(std::string name, const char* data, unsigned int size); 22 | IOData(std::string name, const int data); 23 | IOData(std::string name, const std::string data); 24 | IOData(std::string name, const float* data, unsigned int size); 25 | IOData(std::string name, const float data); 26 | IOData(std::string name, const bool data); 27 | IOData(std::ifstream& filestream); 28 | ~IOData(); 29 | 30 | bool loadFromFile(std::string filename); 31 | void loadFromStream(std::ifstream& filestream); 32 | bool writeToFile(std::string filename); 33 | void writeToStream(std::ofstream& filestream); 34 | 35 | void get(char* data) const; 36 | void get(int& data) const; 37 | void get(std::string& data) const; 38 | void get(float* data) const; 39 | void get(float& data) const; 40 | void get(bool& data) const; 41 | void setName(std::string name); 42 | const std::string getName() const; 43 | 44 | void pushNode(std::string name); 45 | void pushNode(std::string name, const char* data, unsigned int size); 46 | void pushNode(std::string name, const int data); 47 | void pushNode(std::string name, const std::string data); 48 | void pushNode(std::string name, const float* data, unsigned int size); 49 | void pushNode(std::string name, const float data); 50 | void pushNode(std::string name, const bool data); 51 | void removeNode(std::string label); 52 | IOData* findNode(std::string label); 53 | int findNodeIndex(std::string label); 54 | bool existsNode(std::string label); 55 | const std::vector getSubNodes() const; 56 | 57 | private: 58 | void setData(const char* data, unsigned int size); 59 | std::string readString(std::ifstream &filestream); 60 | void writeString(std::ofstream &filestream, const std::string s); 61 | 62 | std::string _name; 63 | int _data_size; 64 | std::shared_ptr _data; 65 | std::vector _subnodes; 66 | }; 67 | 68 | } /* namespace ai */ 69 | 70 | #endif /* end of include guard: IODATA_HPP */ 71 | 72 | -------------------------------------------------------------------------------- /src/AI/util/Macros.hpp: -------------------------------------------------------------------------------- 1 | #ifndef MACROS_HPP 2 | #define MACROS_HPP 3 | 4 | //Uncomment to use nvdia gpu with cuda 5 | //#define CUDA_BACKEND 6 | 7 | //We most use cuda if we compile the source code using the nvcc compiler 8 | #ifdef __NVCC__ 9 | #define CUDA_BACKEND 10 | #endif 11 | 12 | #define CUDA_MAX_THREADS 1024 13 | #define CUDA_MAX_CORES 65535 14 | 15 | #endif /* end of include guard: MACROS_HPP */ 16 | 17 | -------------------------------------------------------------------------------- /src/AI/util/Point.cpp: -------------------------------------------------------------------------------- 1 | //////////////////////////////////////////////////////////// 2 | /// INCLUDES 3 | //////////////////////////////////////////////////////////// 4 | #include "Point.hpp" 5 | 6 | //////////////////////////////////////////////////////////// 7 | /// NAMESPACE AI 8 | //////////////////////////////////////////////////////////// 9 | namespace ai 10 | { 11 | //////////////////////////////////////////////////////////// 12 | Point::Point() 13 | { 14 | x = 0; 15 | y = 0; 16 | } 17 | 18 | //////////////////////////////////////////////////////////// 19 | Point::Point(int x, int y) 20 | { 21 | this->x = x; 22 | this->y = y; 23 | } 24 | } /* namespace ai */ 25 | -------------------------------------------------------------------------------- /src/AI/util/Point.hpp: -------------------------------------------------------------------------------- 1 | #ifndef POINT_HPP 2 | #define POINT_HPP 3 | 4 | //////////////////////////////////////////////////////////// 5 | /// NAMESPACE AI 6 | //////////////////////////////////////////////////////////// 7 | namespace ai 8 | { 9 | 10 | class Point 11 | { 12 | public: 13 | Point(); 14 | Point(int x, int y); 15 | 16 | int x, y; 17 | }; 18 | 19 | } /* namespace ai */ 20 | 21 | #endif /* end of include guard: POINT_HPP */ 22 | 23 | -------------------------------------------------------------------------------- /src/AI/util/Tensor.hpp: -------------------------------------------------------------------------------- 1 | #ifndef TENSOR_HPP 2 | #define TENSOR_HPP 3 | 4 | //////////////////////////////////////////////////////////// 5 | /// INCLUDES 6 | //////////////////////////////////////////////////////////// 7 | #include 8 | #include 9 | #include 10 | #include "ensure.hpp" 11 | #include "IOData.hpp" 12 | 13 | //Uncomment this define to debug tensor accessing 14 | //#define TENSOR_CHECK_BADACCESS 15 | 16 | //////////////////////////////////////////////////////////// 17 | /// NAMESPACE AI 18 | //////////////////////////////////////////////////////////// 19 | namespace ai 20 | { 21 | template < typename T > 22 | class Tensor 23 | { 24 | public: 25 | Tensor(); 26 | Tensor(const Tensor& t); 27 | Tensor(const int width); 28 | Tensor(const int width, const int height); 29 | Tensor(const int width, const int height, const int depth); 30 | 31 | void load(ai::IOData& data, std::string dataname); 32 | void load(std::ifstream& file); 33 | void save(ai::IOData& data, std::string dataname); 34 | void save(std::ofstream& file); 35 | void setshape(const int width); 36 | void setshape(const int width, const int height); 37 | void setshape(const int width, const int height, const int depth); 38 | void fill(const T val); 39 | void fill(const float mean, const float dev); 40 | void clear(); 41 | void point(const Tensor& t); 42 | void point(const Tensor& t, const unsigned int offset_d); 43 | void point(const Tensor& t, const unsigned int offset_d, const unsigned int offset_y); 44 | void point_raw(T* raw_data, const unsigned int width, const unsigned int height, const unsigned int depth); 45 | void copy(const Tensor& t); 46 | void max(T* max, int* pos) const; 47 | void max_at(T* max, int* pos, unsigned int offset_d, unsigned int offset_y) const; 48 | void min(T* min, int* pos) const; 49 | void min_at(T* min, int* pos, unsigned int offset_d, unsigned int offset_y) const; 50 | void get_mean_and_variance(float* mean, float* variance); 51 | bool isNaN(); 52 | Tensor ptr() const; 53 | Tensor ptr(const int d) const; 54 | Tensor ptr(const int d, const int y) const; 55 | const std::string tostring() const; 56 | 57 | inline const int size() const { return _size; } 58 | inline const int width() const { return _width; } 59 | inline const int height() const { return _height; } 60 | inline const int depth() const { return _depth; } 61 | inline T* pointer() const { return &_data[0]; } 62 | inline operator T*() const { return &_data[0]; } 63 | inline void operator =(const Tensor& t) { copy(t); } 64 | 65 | //////////////////////////////////////////////////////////// 66 | /// INDEXING 67 | //////////////////////////////////////////////////////////// 68 | 69 | inline T& at(const int d, const int y, const int x) const 70 | { 71 | #ifdef TENSOR_CHECK_BADACCESS 72 | ensure( d < _depth && y < _height && x < _width); 73 | #endif 74 | return _data[d * _depth_size + y * _width + x]; 75 | } 76 | 77 | inline T& at(const int y, const int x) const 78 | { 79 | #ifdef TENSOR_CHECK_BADACCESS 80 | ensure(y < _height && x < _width); 81 | #endif 82 | return _data[y * _width + x]; 83 | } 84 | 85 | inline T& at(const int x) const 86 | { 87 | #ifdef TENSOR_CHECK_BADACCESS 88 | ensure(x < _size); 89 | #endif 90 | return _data[x]; 91 | } 92 | 93 | inline T& operator [](const int x) 94 | { 95 | #ifdef TENSOR_CHECK_BADACCESS 96 | ensure(x < _size); 97 | #endif 98 | return _data[x]; 99 | } 100 | 101 | inline const T& operator [](const int x) const 102 | { 103 | #ifdef TENSOR_CHECK_BADACCESS 104 | ensure(x < _size); 105 | #endif 106 | return _data[x]; 107 | } 108 | 109 | //////////////////////////////////////////////////////////// 110 | /// OPERATIONS 111 | //////////////////////////////////////////////////////////// 112 | void mul(const Tensor& t); 113 | void mul(const Tensor& t1, const Tensor& t2); 114 | void set(const Tensor& t); 115 | void add(const Tensor& t); 116 | 117 | private: 118 | T* _data = NULL; 119 | std::shared_ptr _data_secure; 120 | int _size = 0; 121 | int _width = 0; 122 | int _height = 0; 123 | int _depth = 0; 124 | int _depth_size; 125 | }; 126 | 127 | //Shortcuts 128 | typedef Tensor Tensor_float; 129 | typedef Tensor Tensor_int; 130 | 131 | } /* namespace ai */ 132 | 133 | #endif //TENSOR_HPP 134 | -------------------------------------------------------------------------------- /src/AI/util/TensorCUDA.hpp: -------------------------------------------------------------------------------- 1 | #ifndef TENSORCUDA_HPP 2 | #define TENSORCUDA_HPP 3 | 4 | //////////////////////////////////////////////////////////// 5 | /// INCLUDES 6 | //////////////////////////////////////////////////////////// 7 | #include "Tensor.hpp" 8 | #include "IOData.hpp" 9 | 10 | //////////////////////////////////////////////////////////// 11 | /// NAMESPACE AI 12 | //////////////////////////////////////////////////////////// 13 | namespace ai 14 | { 15 | template < typename T > 16 | class TensorCUDA 17 | { 18 | public: 19 | TensorCUDA(); 20 | TensorCUDA(const TensorCUDA& t); 21 | TensorCUDA(int width); 22 | TensorCUDA(int width, int height); 23 | TensorCUDA(int width, int height, int depth); 24 | ~TensorCUDA(); 25 | 26 | void load(ai::IOData& data, std::string dataname); 27 | void load(std::ifstream& file); 28 | void save(ai::IOData& data, std::string dataname); 29 | void save(std::ofstream& file); 30 | void setshape(const int width); 31 | void setshape(const int width, const int height); 32 | void setshape(const int width, const int height, const int depth); 33 | void setshape(Tensor& host_tensor); 34 | void point(const TensorCUDA& t); 35 | void point(const TensorCUDA& t, const unsigned int offset_d); 36 | void point(const TensorCUDA& t, const unsigned int offset_d, const unsigned int offset_y); 37 | void clear(); 38 | void copy(const TensorCUDA& tensor); 39 | void copyToHost(T *arr, int size) const; 40 | void copyToDevice(const T *arr, int size); 41 | void fill(T val); 42 | void fill(float mean, float dev); 43 | TensorCUDA ptr(const int d); 44 | TensorCUDA ptr(const int d, const int y); 45 | inline T* pointer() const { return _data;} 46 | inline const int size() const { return _size; } 47 | inline const int width() const { return _width; } 48 | inline const int height() const { return _height; } 49 | inline const int depth() const { return _depth; } 50 | 51 | 52 | private: 53 | T* _data = NULL; 54 | int _size = 0; 55 | int _width = 0, _height = 0, _depth = 0; 56 | bool _owner = false; 57 | }; 58 | 59 | //Shortcut 60 | typedef TensorCUDA TensorCUDA_float; 61 | typedef TensorCUDA TensorCUDA_float_ptr; 62 | typedef TensorCUDA TensorCUDA_int; 63 | 64 | //////////////////////////////////////////////////////////// 65 | /// TYPE SPECIFIC FUNCTIONS 66 | //////////////////////////////////////////////////////////// 67 | void TensorCUDA_float_fill(TensorCUDA_float& t, float val); 68 | void TensorCUDA_float_fill(TensorCUDA_float& t, float mean, float dev); 69 | void TensorCUDA_float_scale(TensorCUDA_float& t, float factor); 70 | void TensorCUDA_float_diff(TensorCUDA_float& t1, TensorCUDA_float& t2, TensorCUDA_float& tout); 71 | void TensorCUDA_float_sum(TensorCUDA_float& t, TensorCUDA_float& tout); 72 | void TensorCUDA_float_copy(TensorCUDA_float& t, TensorCUDA_float& tout); 73 | 74 | } /* namespace ai */ 75 | 76 | #endif /* end of include guard: TENSORCUDA_HPP */ 77 | -------------------------------------------------------------------------------- /src/AI/util/Util.cpp: -------------------------------------------------------------------------------- 1 | //////////////////////////////////////////////////////////// 2 | /// INCLUDES 3 | //////////////////////////////////////////////////////////// 4 | #include "Util.hpp" 5 | 6 | //////////////////////////////////////////////////////////// 7 | /// NAMESPACE AI 8 | //////////////////////////////////////////////////////////// 9 | namespace ai 10 | { 11 | 12 | //////////////////////////////////////////////////////////// 13 | /// UTILITY 14 | //////////////////////////////////////////////////////////// 15 | namespace util 16 | { 17 | //Xorshift 32bit 18 | uint32_t xorshift32() 19 | { 20 | static uint32_t y = 2463534242UL; 21 | y ^= (y << 13); 22 | y ^= (y >> 17); 23 | y ^= (y << 15); 24 | return y; 25 | } 26 | 27 | //random number between 0 and 2^32 28 | int randint() 29 | { 30 | return xorshift32(); 31 | } 32 | 33 | //random number between 0 and 1 34 | float randf() 35 | { 36 | return (double)xorshift32() / (double)UINT_MAX; 37 | } 38 | 39 | //Random number in range (mean - deviation) to (mean + deviation) 40 | double gaussian(const double mean, const double deviation) 41 | { 42 | return (mean - deviation) + randf() * deviation * 2.f; 43 | } 44 | 45 | } /* namespace util */ 46 | 47 | } /* namespace ai */ 48 | -------------------------------------------------------------------------------- /src/AI/util/Util.hpp: -------------------------------------------------------------------------------- 1 | #ifndef UTIL_HPP 2 | #define UTIL_HPP 3 | 4 | //////////////////////////////////////////////////////////// 5 | /// INCLUDES 6 | //////////////////////////////////////////////////////////// 7 | #include 8 | #include 9 | #include 10 | 11 | //////////////////////////////////////////////////////////// 12 | /// NAMESPACE AI 13 | //////////////////////////////////////////////////////////// 14 | namespace ai 15 | { 16 | //////////////////////////////////////////////////////////// 17 | /// UTILITY 18 | //////////////////////////////////////////////////////////// 19 | namespace util 20 | { 21 | //Xorshift 32bit 22 | uint32_t xorshift32(); 23 | 24 | //random number between 0 and 2^32 25 | int randint(); 26 | 27 | //random number between 0 and 1 28 | float randf(); 29 | 30 | //Random number in range (mean - deviation) to (mean + deviation) 31 | double gaussian(const double mean, const double deviation); 32 | 33 | } //namespace util 34 | 35 | } //namespace ai 36 | 37 | #endif /* end of include guard: UTIL_HPP */ 38 | 39 | -------------------------------------------------------------------------------- /src/AI/util/ensure.cpp: -------------------------------------------------------------------------------- 1 | //////////////////////////////////////////////////////////// 2 | /// INCLUDES 3 | //////////////////////////////////////////////////////////// 4 | #include "ensure.hpp" 5 | #include 6 | #include 7 | #ifdef __unix__ 8 | #include 9 | #include 10 | #include 11 | #endif 12 | 13 | //////////////////////////////////////////////////////////// 14 | #ifdef __unix__ 15 | void __print_trace() { 16 | char pid_buf[30]; 17 | sprintf(pid_buf, "%d", getpid()); 18 | char name_buf[512]; 19 | name_buf[readlink("/proc/self/exe", name_buf, 511)]=0; 20 | int child_pid = fork(); 21 | if (!child_pid) { 22 | dup2(2,1); // redirect output to stderr 23 | fprintf(stdout,"stack trace for %s pid=%s\n",name_buf,pid_buf); 24 | execlp("gdb", "gdb", "--batch", "-n", "-ex", "thread", "-ex", "bt", name_buf, pid_buf, NULL); 25 | abort(); /* If gdb failed to start */ 26 | } else { 27 | waitpid(child_pid,NULL,0); 28 | } 29 | } 30 | #endif 31 | 32 | //////////////////////////////////////////////////////////// 33 | void __error_exit(const char* str_condition, const char* file, const int line) 34 | { 35 | //Show errors 36 | printf("Assertion failed FILE: %s LINE: %d CONDITION: %s\n", file, line, str_condition); 37 | 38 | //Show backtrace 39 | #ifdef __unix__ 40 | __print_trace(); 41 | #endif 42 | 43 | //terminate program 44 | exit(-1); 45 | } 46 | -------------------------------------------------------------------------------- /src/AI/util/ensure.hpp: -------------------------------------------------------------------------------- 1 | #ifndef ENSURE_HPP 2 | #define ENSURE_HPP 3 | 4 | //////////////////////////////////////////////////////////// 5 | /// INCLUDES 6 | //////////////////////////////////////////////////////////// 7 | #include 8 | 9 | #ifdef __unix__ 10 | void __print_trace(); 11 | #endif 12 | 13 | //////////////////////////////////////////////////////////// 14 | void __error_exit(const char* str_condition, const char* file, const int line); 15 | 16 | //////////////////////////////////////////////////////////// 17 | #define ensure(x) if(!(x)) __error_exit(#x, __FILE__, __LINE__) 18 | #define ensure_print(x, fmt, ...) if(!(x)) { printf(fmt, ##__VA_ARGS__); __error_exit(#x, __FILE__, __LINE__); } else 19 | 20 | #endif /* end of include guard: ENSURE_HPP */ 21 | 22 | -------------------------------------------------------------------------------- /src/AI/visualization/TextRendering.cpp: -------------------------------------------------------------------------------- 1 | #include "TextRendering.hpp" 2 | #include "font8x8_basic.h" 3 | #include 4 | 5 | void render_char(float* image_data, const uint image_width, const uint image_height, const uint image_channels, 6 | char *char_bitmap, const uint x, const uint y, const uint size, long color) 7 | { 8 | int set; 9 | int mask; 10 | float scale = 8.f / (float)size; 11 | #define GETBYTE(n, byte_pos) (n >> (8 * byte_pos)) & 0xFF 12 | for (uint i = 0; i < size; i++) { 13 | if (x + i >= image_width) continue; 14 | for (uint j = 0; j < size; j++) { 15 | if (y + j >= image_height) continue; 16 | set = char_bitmap[(int)(j * scale)] & 1 << (int)(i * scale); 17 | if (set) { 18 | for (int c = 0; c < image_channels; c++) { 19 | image_data[((y + j) * image_width + x + i) * image_channels + c] = GETBYTE(color, (image_channels -1 -c)); 20 | } 21 | } 22 | } 23 | } 24 | } 25 | 26 | void text_draw(float* image_data, const uint image_width, const uint image_height, const uint image_channels, 27 | const char* text, const uint x, const uint y, const uint char_size, const long color) 28 | { 29 | const int padding = char_size / 4.f; 30 | const int text_lengt = strlen(text); 31 | for (int i = 0; i < text_lengt; i++) 32 | render_char(image_data, image_width, image_height, image_channels, font8x8_basic[(int)text[i]], 33 | x + i * (char_size + padding), y, char_size, color); 34 | } 35 | -------------------------------------------------------------------------------- /src/AI/visualization/TextRendering.hpp: -------------------------------------------------------------------------------- 1 | #ifndef TEXTRENDERING_HPP 2 | #define TEXTRENDERING_HPP 3 | 4 | typedef unsigned int uint; 5 | 6 | void text_draw(float* image_data, const uint image_width, const uint image_height, const uint image_channels, 7 | const char* text, const uint x, const uint y, const uint char_size, const long color); 8 | 9 | #endif /* end of include guard: TEXTRENDERING_HPP */ 10 | 11 | -------------------------------------------------------------------------------- /src/AI/visualization/visualization.hpp: -------------------------------------------------------------------------------- 1 | #ifndef VISUALIZATION_HPP 2 | #define VISUALIZATION_HPP 3 | 4 | //////////////////////////////////////////////////////////// 5 | /// INCLUDES 6 | //////////////////////////////////////////////////////////// 7 | #include "Bitmap.hpp" 8 | #include 9 | #include 10 | #include "../util/Tensor.hpp" 11 | 12 | //////////////////////////////////////////////////////////// 13 | /// NAMESPACE AI 14 | //////////////////////////////////////////////////////////// 15 | namespace ai 16 | { 17 | //////////////////////////////////////////////////////////// 18 | /// NAMESPACE VISUALIZATION 19 | //////////////////////////////////////////////////////////// 20 | namespace visualization 21 | { 22 | //////////////////////////////////////////////////////////// 23 | /// \brief Save vector to image 24 | /// 25 | //////////////////////////////////////////////////////////// 26 | void save_vec(std::string path, const ai::Tensor_float& vector); 27 | 28 | //////////////////////////////////////////////////////////// 29 | /// \brief Save vectors to image 30 | /// 31 | //////////////////////////////////////////////////////////// 32 | void save_multiple_vec(std::string path, const ai::Tensor_float vector); 33 | 34 | //////////////////////////////////////////////////////////// 35 | /// \brief Save vectors to image, all the vectors must be 36 | /// of the same size 37 | /// 38 | //////////////////////////////////////////////////////////// 39 | void save_multiple_vec(std::string path, const ai::Tensor_float vector, int table_width, int table_height); 40 | 41 | //////////////////////////////////////////////////////////// 42 | /// \brief Save vector to image using a specific width and 43 | /// height for the image 44 | /// 45 | //////////////////////////////////////////////////////////// 46 | void save_vec(std::string path, const ai::Tensor_float& vector, int width, int height); 47 | 48 | } //namespace visualization 49 | 50 | } //namespace ai 51 | 52 | #endif /* end of include guard: VISUALIZATION_HPP */ 53 | -------------------------------------------------------------------------------- /test/Bitmap/main: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Flowx08/artificial_intelligence/a93e405702b0e37eb58ad092d409111a96b2483a/test/Bitmap/main -------------------------------------------------------------------------------- /test/Bitmap/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include "../../src/AI/visualization/Bitmap.hpp" 3 | 4 | int main(int argc, const char *argv[]) 5 | { 6 | Bitmap bm(100, 100, Bitmap::RGB, 0xFFFFFF); 7 | bm.drawRect(10, 10, 64, 64, 0x00FF00, 24); 8 | bm.drawRect(10, 10, 64, 64, 0xFF0000, 3); 9 | bm.rotate(bm.getWidth()/2, bm.getHeight()/2, 30, 0x000000); 10 | bm.drawCircle(32, 32, 32, 0x0000FF, 3); 11 | bm.save("test.png"); 12 | return 0; 13 | } 14 | -------------------------------------------------------------------------------- /test/Bitmap/makefile: -------------------------------------------------------------------------------- 1 | CPP_FILES := $(wildcard ../../src/AI/*/*.cpp) main.cpp 2 | OBJ_FILES = $(addprefix obj/, $(notdir $(CPP_FILES:.cpp=.o))) 3 | CC = g++ 4 | FLAGS = -std=c++11 -Wall -O3 5 | LIBS = 6 | EXE_NAME = main 7 | 8 | all: makedirs main 9 | 10 | clear: 11 | rm -r obj/ 12 | 13 | obj/%.o : ../../src/AI/*/%.cpp 14 | $(CC) $(FLAGS) -c $< -o $@ $(LIBS) 15 | 16 | obj/%.o : %.cpp 17 | $(CC) $(FLAGS) -c $< -o $@ $(LIBS) 18 | 19 | makedirs: 20 | mkdir -p obj 21 | 22 | main: $(OBJ_FILES) 23 | $(CC) $(FLAGS) $^ -o $(EXE_NAME) $(LIBS) 24 | -------------------------------------------------------------------------------- /test/CIFAR10/CIFAR10: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Flowx08/artificial_intelligence/a93e405702b0e37eb58ad092d409111a96b2483a/test/CIFAR10/CIFAR10 -------------------------------------------------------------------------------- /test/CIFAR10/makefile: -------------------------------------------------------------------------------- 1 | CPP_FILES := $(wildcard ../../src/AI/*/*.cpp) CIFAR10.cpp 2 | OBJ_FILES = $(addprefix obj/, $(notdir $(CPP_FILES:.cpp=.o))) 3 | CC = g++ 4 | FLAGS = -std=c++11 -Wall -O3 5 | LIBS = 6 | EXE_NAME = CIFAR10 7 | 8 | all: makedirs main 9 | 10 | clear: 11 | rm -r obj/ 12 | 13 | obj/%.o : ../../src/AI/*/%.cpp 14 | $(CC) $(FLAGS) -c $< -o $@ $(LIBS) 15 | 16 | obj/%.o : %.cpp 17 | $(CC) $(FLAGS) -c $< -o $@ $(LIBS) 18 | 19 | makedirs: 20 | mkdir -p obj 21 | 22 | main: $(OBJ_FILES) 23 | $(CC) $(FLAGS) $^ -o $(EXE_NAME) $(LIBS) 24 | -------------------------------------------------------------------------------- /test/CIFAR10_cuda/CIFAR10_build_CUDA.sh: -------------------------------------------------------------------------------- 1 | sudo nvcc -std=c++11 -O3 -D_FORCE_INLINES -lcudnn -lcublas CIFAR10_cuda.cpp ../src/AI/deeplearning/*.cpp ../src/AI/util/*.cpp ../src/AI/util/*.cu ../src/AI/deeplearning/*.cu ../src/AI/visualization/*.cpp -o CIFAR10 2 | -------------------------------------------------------------------------------- /test/CIFAR10_cuda/CIFAR10_cuda: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Flowx08/artificial_intelligence/a93e405702b0e37eb58ad092d409111a96b2483a/test/CIFAR10_cuda/CIFAR10_cuda -------------------------------------------------------------------------------- /test/CIFAR10_cuda/loss.txt: -------------------------------------------------------------------------------- 1 | 1000 nan 2 | 2000 nan 3 | 3000 nan 4 | 4000 nan 5 | 1000 nan 6 | 1000 nan 7 | 1000 nan 8 | 1000 1.767253 9 | 2000 1.685826 10 | 3000 1.645551 11 | 4000 1.590870 12 | 5000 1.548920 13 | 6000 1.618662 14 | 7000 1.559545 15 | 8000 1.526764 16 | 9000 1.491092 17 | 10000 1.474030 18 | 11000 1.466243 19 | 12000 1.438975 20 | 13000 1.371346 21 | 14000 1.377435 22 | 15000 1.392270 23 | 16000 1.347864 24 | 17000 1.366989 25 | 18000 1.331473 26 | 19000 1.330345 27 | 20000 1.270672 28 | 21000 1.293343 29 | 22000 1.291509 30 | 23000 1.283371 31 | 24000 1.264612 32 | 25000 1.257068 33 | 26000 1.243264 34 | 27000 1.203396 35 | 28000 1.219066 36 | 29000 1.195216 37 | 30000 1.184337 38 | 31000 1.191098 39 | 32000 1.171278 40 | 33000 1.181602 41 | 34000 1.181492 42 | 35000 1.174664 43 | 36000 1.184262 44 | 37000 1.177256 45 | 38000 1.124855 46 | 39000 1.149900 47 | 40000 1.145856 48 | 41000 1.131420 49 | 42000 1.092072 50 | 43000 1.097420 51 | 44000 1.092737 52 | 45000 1.098044 53 | 46000 1.075630 54 | 47000 1.051236 55 | 48000 1.039337 56 | 49000 1.069444 57 | 50000 1.074295 58 | 51000 1.063132 59 | 52000 1.061382 60 | 53000 1.032687 61 | 54000 1.062958 62 | 55000 1.044980 63 | 56000 1.003810 64 | 57000 1.021127 65 | 58000 1.019049 66 | 59000 1.004253 67 | 60000 0.990474 68 | 61000 0.962619 69 | 62000 0.984022 70 | 63000 1.008694 71 | 64000 1.010050 72 | 65000 1.015940 73 | 66000 0.984298 74 | 67000 0.980167 75 | 68000 0.983773 76 | 69000 0.962397 77 | 70000 0.979340 78 | 71000 0.980433 79 | 72000 0.948768 80 | 73000 0.950446 81 | 74000 0.948971 82 | 75000 0.975798 83 | 76000 0.949382 84 | 77000 0.963619 85 | 78000 0.940853 86 | 79000 0.897295 87 | 80000 0.911198 88 | 81000 0.955931 89 | 82000 0.878623 90 | 83000 0.870615 91 | 84000 0.896579 92 | 85000 0.925392 93 | 86000 0.902464 94 | 87000 0.890864 95 | 88000 0.909410 96 | 89000 0.889018 97 | 90000 0.905483 98 | 91000 0.883900 99 | 92000 0.872742 100 | 93000 0.848482 101 | 94000 0.899371 102 | 95000 0.845382 103 | 96000 0.870586 104 | 97000 0.799870 105 | 98000 0.841557 106 | 99000 0.806154 107 | 100000 0.833626 108 | 101000 0.858749 109 | 102000 0.822763 110 | 103000 0.765480 111 | 104000 0.824233 112 | 105000 0.830034 113 | 106000 0.847396 114 | 107000 0.846399 115 | 108000 0.823694 116 | 109000 0.802837 117 | 110000 0.795473 118 | 111000 0.801032 119 | 112000 0.836447 120 | 113000 0.841237 121 | 114000 0.809992 122 | 115000 0.775346 123 | 116000 0.777003 124 | 117000 0.788169 125 | 118000 0.774381 126 | 119000 0.786449 127 | 120000 0.797545 128 | 121000 0.748479 129 | 122000 0.723167 130 | 123000 0.766273 131 | 124000 0.777781 132 | 125000 0.751828 133 | 126000 0.777686 134 | 127000 0.771170 135 | 128000 0.754160 136 | 129000 0.767786 137 | 130000 0.770383 138 | 131000 0.747697 139 | 132000 0.735507 140 | 133000 0.734011 141 | 134000 0.756295 142 | 135000 0.720147 143 | 136000 0.724013 144 | 137000 0.778076 145 | 138000 0.771070 146 | 139000 0.715607 147 | 140000 0.766876 148 | 141000 0.709297 149 | 142000 0.696473 150 | 143000 0.710129 151 | 144000 0.731921 152 | 145000 0.743078 153 | 146000 0.690546 154 | 147000 0.673921 155 | 148000 0.706133 156 | 149000 0.717800 157 | 150000 0.744786 158 | 151000 0.715825 159 | 1000 nan 160 | 2000 nan 161 | 3000 nan 162 | 1000 nan 163 | 2000 nan 164 | 1000 nan 165 | 0 0.001670 166 | 1000 nan 167 | 1000 nan 168 | 1000 nan 169 | 2000 nan 170 | 3000 nan 171 | 1000 nan 172 | 2000 nan 173 | 1000 nan 174 | 2000 nan 175 | 1000 nan 176 | 1000 nan 177 | 2000 nan 178 | 1000 nan 179 | 2000 nan 180 | -------------------------------------------------------------------------------- /test/CIFAR10_cuda/makefile: -------------------------------------------------------------------------------- 1 | CPP_FILES := $(wildcard ../../src/AI/deeplearning/*.cpp) $(wildcard ../../src/AI/datasets/*.cpp) $(wildcard ../../src/AI/util/*.cpp) $(wildcard ../../src/AI/visualization/*.cpp) CIFAR10_cuda.cpp 2 | CU_FILES := $(wildcard ../../src/AI/deeplearning/*.cu) $(wildcard ../../src/AI/util/*.cu) 3 | OBJ_FILES = $(addprefix obj/, $(notdir $(CPP_FILES:.cpp=.o))) $(addprefix obj/, $(notdir $(CU_FILES:.cu=.o))) 4 | CC = nvcc 5 | FLAGS = -std=c++11 -O3 -D_FORCE_INLINES 6 | LIBS = -lcudnn -lcublas 7 | EXE_NAME = CIFAR10_cuda 8 | 9 | all: makedirs main 10 | 11 | clear: 12 | rm -r obj/ 13 | 14 | obj/%.o : ../../src/AI/*/%.cpp 15 | $(CC) $(FLAGS) -c $< -o $@ $(LIBS) 16 | 17 | obj/%.o : ../../src/AI/*/%.cu 18 | $(CC) $(FLAGS) -c $< -o $@ $(LIBS) 19 | 20 | obj/%.o : %.cpp 21 | $(CC) $(FLAGS) -c $< -o $@ $(LIBS) 22 | 23 | makedirs: 24 | mkdir -p obj 25 | 26 | main: $(OBJ_FILES) 27 | $(CC) $(FLAGS) $^ -o $(EXE_NAME) $(LIBS) 28 | -------------------------------------------------------------------------------- /test/CIFAR10_cuda/smallCIFAR_1900.net: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Flowx08/artificial_intelligence/a93e405702b0e37eb58ad092d409111a96b2483a/test/CIFAR10_cuda/smallCIFAR_1900.net -------------------------------------------------------------------------------- /test/CIFAR10_cuda/test.txt: -------------------------------------------------------------------------------- 1 | 50000 4435 2 | 100000 3406 3 | 150000 3009 4 | -------------------------------------------------------------------------------- /test/CUDA_tests/CUDA_tests.sh: -------------------------------------------------------------------------------- 1 | nvcc -std=c++11 CUDA_tests.cu ../src/AI/deeplearning/*.cu ../src/AI/deeplearning/*.cpp ../src/AI/visualization/*.cpp -o CUDA_tests 2 | -------------------------------------------------------------------------------- /test/IOData_tests/iotest: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Flowx08/artificial_intelligence/a93e405702b0e37eb58ad092d409111a96b2483a/test/IOData_tests/iotest -------------------------------------------------------------------------------- /test/IOData_tests/iotest.cpp: -------------------------------------------------------------------------------- 1 | #include "../src/AI/util/IOData.hpp" 2 | #include "../src/AI/util/Tensor.hpp" 3 | 4 | int main(int argc, const char *argv[]) 5 | { 6 | 7 | ai::IOData test("test", 10); 8 | ai::Tensor_float test3(20); 9 | test3.fill(0, 1); 10 | test.pushNode("drop_proba", 0.435f); 11 | test.pushNode("input_size", 20); 12 | test.pushNode("label", "qwertyuio"); 13 | test3.save(test, "weights"); 14 | 15 | for (int i = 0; i < (int)test.getSubNodes().size(); i++) 16 | printf("%d %s\n", i, test.getSubNodes()[i].getName().c_str()); 17 | 18 | printf("A1\n"); 19 | test.writeToFile("test.txt"); 20 | printf("A2\n"); 21 | 22 | ai::IOData t2(""); 23 | t2.loadFromFile("test.txt"); 24 | printf("A3\n"); 25 | printf("%s\n", t2.getName().c_str()); 26 | int k; 27 | t2.get(k); 28 | printf("%s %d\n", t2.getName().c_str(), k); 29 | printf("%d\n", t2.existsNode("input_size")); 30 | printf("%d\n", t2.existsNode("drop_proba")); 31 | printf("%d\n", t2.existsNode("test")); 32 | for (int i = 0; i < (int)t2.getSubNodes().size(); i++) 33 | printf("%d %s\n", i, t2.getSubNodes()[i].getName().c_str()); 34 | 35 | int is; 36 | t2.findNode("input_size")->get(is); 37 | printf("%d\n", is); 38 | float dp; 39 | t2.findNode("drop_proba")->get(dp); 40 | printf("%f\n", dp); 41 | std::string label; 42 | t2.findNode("label")->get(label); 43 | printf("%s\n", label.c_str()); 44 | ai::Tensor_float test4(20); 45 | test4.load(t2, "weights"); 46 | printf("%s\n", test4.tostring().c_str()); 47 | printf("%d %d %d\n", test4.width(), test4.height(), test4.depth()); 48 | return 0; 49 | } 50 | -------------------------------------------------------------------------------- /test/IOData_tests/iotest.sh: -------------------------------------------------------------------------------- 1 | g++ -std=c++11 -O3 -Wall -march=native -g iotest.cpp ../src/AI/util/*.cpp ../src/AI/visualization/*.cpp -o iotest 2 | -------------------------------------------------------------------------------- /test/MNIST/MNIST: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Flowx08/artificial_intelligence/a93e405702b0e37eb58ad092d409111a96b2483a/test/MNIST/MNIST -------------------------------------------------------------------------------- /test/MNIST/makefile: -------------------------------------------------------------------------------- 1 | CPP_FILES := $(wildcard ../../src/AI/*/*.cpp) MNIST.cpp 2 | OBJ_FILES = $(addprefix obj/, $(notdir $(CPP_FILES:.cpp=.o))) 3 | CC = g++ 4 | FLAGS = -std=c++11 -Wall -O3 5 | LIBS = 6 | EXE_NAME = MNIST 7 | 8 | all: makedirs main 9 | 10 | clear: 11 | rm -r obj/ 12 | 13 | obj/%.o : ../../src/AI/*/%.cpp 14 | $(CC) $(FLAGS) -c $< -o $@ $(LIBS) 15 | 16 | obj/%.o : %.cpp 17 | $(CC) $(FLAGS) -c $< -o $@ $(LIBS) 18 | 19 | makedirs: 20 | mkdir -p obj 21 | 22 | main: $(OBJ_FILES) 23 | $(CC) $(FLAGS) $^ -o $(EXE_NAME) $(LIBS) 24 | -------------------------------------------------------------------------------- /test/MNIST_cuda/MNIST.net: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Flowx08/artificial_intelligence/a93e405702b0e37eb58ad092d409111a96b2483a/test/MNIST_cuda/MNIST.net -------------------------------------------------------------------------------- /test/MNIST_cuda/MNIST_cuda: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Flowx08/artificial_intelligence/a93e405702b0e37eb58ad092d409111a96b2483a/test/MNIST_cuda/MNIST_cuda -------------------------------------------------------------------------------- /test/MNIST_cuda/makefile: -------------------------------------------------------------------------------- 1 | CPP_FILES := $(wildcard ../../src/AI/deeplearning/*.cpp) $(wildcard ../../src/AI/util/*.cpp) $(wildcard ../../src/AI/datasets/*.cpp) $(wildcard ../../src/AI/visualization/*.cpp) MNIST_cuda.cpp 2 | CU_FILES := $(wildcard ../../src/AI/deeplearning/*.cu) $(wildcard ../../src/AI/util/*.cu) 3 | OBJ_FILES = $(addprefix obj/, $(notdir $(CPP_FILES:.cpp=.o))) $(addprefix obj/, $(notdir $(CU_FILES:.cu=.o))) 4 | CC = nvcc 5 | FLAGS = -std=c++11 -O3 -D_FORCE_INLINES 6 | LIBS = -lcudnn -lcublas 7 | EXE_NAME = MNIST_cuda 8 | 9 | all: makedirs main 10 | 11 | clear: 12 | rm -r obj/ 13 | 14 | obj/%.o : ../../src/AI/*/%.cpp 15 | $(CC) $(FLAGS) -c $< -o $@ $(LIBS) 16 | 17 | obj/%.o : ../../src/AI/*/%.cu 18 | $(CC) $(FLAGS) -c $< -o $@ $(LIBS) 19 | 20 | obj/%.o : %.cpp 21 | $(CC) $(FLAGS) -c $< -o $@ $(LIBS) 22 | 23 | makedirs: 24 | mkdir -p obj 25 | 26 | main: $(OBJ_FILES) 27 | $(CC) $(FLAGS) $^ -o $(EXE_NAME) $(LIBS) 28 | -------------------------------------------------------------------------------- /test/RL_kbandits/kbandits: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Flowx08/artificial_intelligence/a93e405702b0e37eb58ad092d409111a96b2483a/test/RL_kbandits/kbandits -------------------------------------------------------------------------------- /test/RL_kbandits/kbandits.cpp: -------------------------------------------------------------------------------- 1 | #include "../../src/AI/deeplearning/NeuralNetwork.hpp" 2 | #include "../../src/AI/RL/Kbandits.hpp" 3 | #include "../../src/AI/RL/DQN.hpp" 4 | #include "../../src/AI/deeplearning/OptimizerSDG.hpp" 5 | #include 6 | #include 7 | 8 | int main(int argc, const char *argv[]) 9 | { 10 | srand((int)time(NULL)); 11 | 12 | //Create environment 13 | ai::Kbandits environment(10, 0.1, 0.9); 14 | ai::Tensor_float experience(environment.input_width(), 15 | environment.input_height(), environment.input_depth()); 16 | experience.fill(0); 17 | float previous_reward; 18 | 19 | //Create agent 20 | int action_id; 21 | ai::NeuralNetwork ai_model; 22 | ai_model.push("INPUT", "", ai::Variable::make(environment.input_width())); 23 | ai_model.push("L1", "INPUT", ai::Linear::make(5)); 24 | ai_model.push("TANH", "L1", ai::Tanh::make()); 25 | ai_model.push("OUTPUT", "TANH", ai::Linear::make(environment.actions_count())); 26 | 27 | ai::DQN agent(30, 0.005, 0.25, 0.0); 28 | agent.set_neuralnetwork(ai_model); 29 | 30 | //World steps 31 | const int max_steps = 200000; 32 | 33 | //Statistics 34 | float medium_reward = 0; 35 | float total_area_reward = 0; 36 | std::vector action_performed_count(environment.actions_count(), 0); 37 | 38 | for (int i = 0; i < max_steps; i++) { 39 | 40 | //Agent-World interaction 41 | action_id = agent.act(experience); 42 | environment.act(action_id); 43 | environment.observe(experience, previous_reward); 44 | agent.observe(experience, previous_reward); 45 | 46 | //Update statistics 47 | medium_reward += previous_reward; 48 | total_area_reward += previous_reward; 49 | action_performed_count[action_id]++; 50 | 51 | if (i % 1000 == 0 && i != 0) { 52 | medium_reward /= (float)1000.f; 53 | printf("WorldStep: %d Medium reward: %f\n", i, medium_reward); 54 | } 55 | 56 | } 57 | 58 | for (int i = 0; i < (int)environment.get_reward_prob_vector().size(); i++) { 59 | printf("Action: %d RewardProability: %f PerformedCount: %d\n", i, 60 | environment.get_reward_prob_vector()[i], action_performed_count[i]); 61 | } 62 | printf("TotalReward: %f\n", total_area_reward); 63 | 64 | 65 | return 0; 66 | } 67 | -------------------------------------------------------------------------------- /test/RL_kbandits/kbandits.sh: -------------------------------------------------------------------------------- 1 | g++ -std=c++11 kbandits.cpp ../../src/AI/*/*.cpp -o kbandits 2 | -------------------------------------------------------------------------------- /test/RL_tictactoe/TicTacToe.cpp: -------------------------------------------------------------------------------- 1 | #include "../../src/AI/deeplearning/NeuralNetwork.hpp" 2 | #include "../../src/AI/RL/TicTacToe.hpp" 3 | #include "../../src/AI/RL/DQN.hpp" 4 | #include "../../src/AI/deeplearning/OptimizerSDG.hpp" 5 | #include 6 | #include 7 | 8 | int main(int argc, const char *argv[]) 9 | { 10 | srand((int)time(NULL)); 11 | 12 | //Create environment 13 | ai::TicTacToe environment; 14 | ai::Tensor_float experience(environment.input_width(), 15 | environment.input_height(), environment.input_depth()); 16 | experience.fill(0); 17 | float previous_reward; 18 | 19 | environment._unit_testing(); 20 | 21 | //Create agent 22 | int action_id; 23 | 24 | ai::NeuralNetwork net; 25 | net.push("INPUT", "", ai::Variable::make(environment.input_width(), environment.input_height())); 26 | net.push("L1", "INPUT", ai::Linear::make(32)); 27 | net.push("REL1", "L1", ai::Relu::make()); 28 | net.push("NORM", "REL1", ai::Normalization::make(0.5)); 29 | net.push("L2", "NORM", ai::Linear::make(32)); 30 | net.push("NORM2", "L2", ai::Normalization::make(0.5)); 31 | net.push("REL2", "NORM2", ai::Relu::make()); 32 | net.push("OUTPUT", "REL2", ai::Linear::make(environment.actions_count())); 33 | 34 | ai::DQN ai(net, 50000, 0.001, 0.6, 0.9); 35 | 36 | //World steps 37 | const int max_steps = 30000; 38 | 39 | //Statistics 40 | float medium_reward = 0; 41 | float total_area_reward = 0; 42 | std::vector action_performed_count(environment.actions_count(), 0); 43 | int won_counter = 0, lost_counter = 0, draw_counter = 0; 44 | 45 | for (int i = 0; i < max_steps; i++) { 46 | 47 | if (i < max_steps * 0.95) ai.setcuriosity( 1.f - ((float)i / (max_steps * 0.95)) ); 48 | else ai.setcuriosity(0); 49 | 50 | //Agent-World interaction 51 | action_id = ai.act(experience, environment.act_mask()); 52 | environment.act(action_id); 53 | environment.observe(experience, previous_reward); 54 | ai.observe(experience, previous_reward); 55 | if (environment.getWinner() == ai::TicTacToe::Agent) won_counter++; 56 | else if (environment.getWinner() == ai::TicTacToe::Opponent) lost_counter++; 57 | else if (environment.getWinner() == ai::TicTacToe::Draw) draw_counter++; 58 | 59 | if (i > max_steps - 30) { 60 | environment.print_table(); 61 | } 62 | 63 | //Update statistics 64 | medium_reward += previous_reward; 65 | total_area_reward += previous_reward; 66 | action_performed_count[action_id]++; 67 | 68 | if (i % 1000 == 0 && i != 0) { 69 | medium_reward /= (float)1000.f; 70 | printf("WorldStep: %d Medium reward: %f Curiosity: %f Won: %d Lost: %d Draw: %d\n", 71 | i, medium_reward, ai.getcuriosity(), won_counter, lost_counter, draw_counter); 72 | won_counter = 0; 73 | lost_counter = 0; 74 | draw_counter = 0; 75 | } 76 | 77 | } 78 | 79 | printf("TotalReward: %f\n", total_area_reward); 80 | 81 | for (int i = 0; i < 20; i++) { 82 | action_id = ai.act(experience, environment.act_mask()); 83 | environment.act(action_id); 84 | environment.print_table(); 85 | int useraction = 0; 86 | scanf("action: %d\n", &useraction); 87 | printf("my actiond: %d\n", useraction); 88 | environment.user_play(useraction, experience); 89 | } 90 | 91 | return 0; 92 | } 93 | -------------------------------------------------------------------------------- /test/RL_tictactoe/TicTacToe.sh: -------------------------------------------------------------------------------- 1 | g++ -std=c++11 TicTacToe.cpp ../../src/AI/*/*.cpp -o tictactoe 2 | -------------------------------------------------------------------------------- /test/RL_tictactoe/tictactoe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Flowx08/artificial_intelligence/a93e405702b0e37eb58ad092d409111a96b2483a/test/RL_tictactoe/tictactoe -------------------------------------------------------------------------------- /test/RNN_PI/PI: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Flowx08/artificial_intelligence/a93e405702b0e37eb58ad092d409111a96b2483a/test/RNN_PI/PI -------------------------------------------------------------------------------- /test/RNN_PI/PI_build.sh: -------------------------------------------------------------------------------- 1 | g++ -std=c++11 -Wall -O3 -march=native PI.cpp ../../src/AI/*/*.cpp -o PI 2 | -------------------------------------------------------------------------------- /test/XOR/XOR: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Flowx08/artificial_intelligence/a93e405702b0e37eb58ad092d409111a96b2483a/test/XOR/XOR -------------------------------------------------------------------------------- /test/XOR/XOR.cpp: -------------------------------------------------------------------------------- 1 | #include "../../src/AI/deeplearning/NeuralNetwork.hpp" 2 | #include "../../src/AI/deeplearning/OptimizerSDG.hpp" 3 | 4 | int main(int argc, const char *argv[]) 5 | { 6 | //Create network 7 | ai::NeuralNetwork gg; 8 | gg.push("INPUT1", "", ai::Variable::make(2)); 9 | gg.push("L1", "INPUT1", ai::Linear::make(3)); 10 | gg.push("L2", "L1", ai::Tanh::make()); 11 | gg.push("L3", "L2", ai::Linear::make(1)); 12 | gg.push("L4", "L3", ai::Sigmoid::make()); 13 | 14 | //Show network structure 15 | gg.printstack(); 16 | 17 | //Create XOR trainingset 18 | ai::Tensor_float dataset(2, 4); 19 | ai::Tensor_float dataset_target(1, 4); 20 | dataset.at(0, 0) = 1.0; dataset.at(0, 1) = 0.0; dataset_target.at(0, 0) = 1; 21 | dataset.at(1, 0) = 0.0; dataset.at(1, 1) = 1.0; dataset_target.at(1, 0) = 1; 22 | dataset.at(2, 0) = 0.0; dataset.at(2, 1) = 0.0; dataset_target.at(2, 0) = 0; 23 | dataset.at(3, 0) = 1.0; dataset.at(3, 1) = 1.0; dataset_target.at(3, 0) = 0; 24 | ai::Tensor_float input; 25 | ai::Tensor_float target; 26 | 27 | printf("%s\n", dataset.tostring().c_str()); 28 | printf("%s\n", dataset_target.tostring().c_str()); 29 | 30 | //Create optimizer 31 | ai::OptimizerSDG opt(4, 0.05, 0.8, ai::Cost::SquaredError); 32 | 33 | //Train 34 | double error = 0; 35 | const int cicles = 300000; 36 | for (int i = 0; i < cicles; i++) { 37 | 38 | input.point(dataset, 0, i % 4); 39 | target.point(dataset_target, 0, i % 4); 40 | error += gg.optimize(input, target, &opt); 41 | 42 | if (i % 15000 == 0 && i != 0) { 43 | printf("E: %f\n", error / 15000.f); 44 | error = 0; 45 | } 46 | } 47 | 48 | //Test 49 | for (int i = 0; i < 4; i++) { 50 | input.point(dataset, 0, i); 51 | gg.run(input); 52 | printf("%s -> %s\n", input.tostring().c_str(), 53 | gg.get_output("L4").tostring().c_str()); 54 | } 55 | 56 | printf("Done\n"); 57 | return 0; 58 | } 59 | -------------------------------------------------------------------------------- /test/XOR/makefile: -------------------------------------------------------------------------------- 1 | CPP_FILES := $(wildcard ../../src/AI/*/*.cpp) XOR.cpp 2 | OBJ_FILES = $(addprefix obj/, $(notdir $(CPP_FILES:.cpp=.o))) 3 | CC = g++ 4 | FLAGS = -std=c++11 -Wall -O3 5 | LIBS = 6 | EXE_NAME = XOR 7 | 8 | all: makedirs main 9 | 10 | clear: 11 | rm -r obj/ 12 | 13 | obj/%.o : ../../src/AI/*/%.cpp 14 | $(CC) $(FLAGS) -c $< -o $@ $(LIBS) 15 | 16 | obj/%.o : %.cpp 17 | $(CC) $(FLAGS) -c $< -o $@ $(LIBS) 18 | 19 | makedirs: 20 | mkdir -p obj 21 | 22 | main: $(OBJ_FILES) 23 | $(CC) $(FLAGS) $^ -o $(EXE_NAME) $(LIBS) 24 | -------------------------------------------------------------------------------- /test/charRNN/charRNN: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Flowx08/artificial_intelligence/a93e405702b0e37eb58ad092d409111a96b2483a/test/charRNN/charRNN -------------------------------------------------------------------------------- /test/charRNN/charRNN_CUDA_build.sh: -------------------------------------------------------------------------------- 1 | sudo nvcc -std=c++11 -Wall -O3 -D_FORCE_INLINES charRNN_cuda.cpp ../src/AI/*/*.cpp -o charRNN 2 | -------------------------------------------------------------------------------- /test/charRNN/charRNN_build.sh: -------------------------------------------------------------------------------- 1 | g++ -std=c++11 -Wall -O3 -march=native charRNN.cpp ../../src/AI/util/*.cpp ../../src/AI/deeplearning/*.cpp ../../src/AI/visualization/*.cpp -o charRNN 2 | -------------------------------------------------------------------------------- /test/clock.h: -------------------------------------------------------------------------------- 1 | #ifndef CLOCK_H 2 | #define CLOCK_H 3 | 4 | //////////////////////////////////////////////////////////// 5 | /// INCLUDES 6 | //////////////////////////////////////////////////////////// 7 | #include 8 | 9 | //Vars 10 | static struct timeval tv1, tv2; 11 | static double total_elapsed = 0; 12 | 13 | //////////////////////////////////////////////////////////// 14 | void measure_start() 15 | { 16 | gettimeofday(&tv1, NULL); 17 | } 18 | 19 | //////////////////////////////////////////////////////////// 20 | double measure_stop() 21 | { 22 | gettimeofday(&tv2, NULL); 23 | return (double) (tv2.tv_usec - tv1.tv_usec) / 1000000.f + (double) (tv2.tv_sec - tv1.tv_sec); 24 | } 25 | 26 | #endif /* end of include guard: CLOCK_H */ 27 | -------------------------------------------------------------------------------- /test/data_augmentation/data_augmentation: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Flowx08/artificial_intelligence/a93e405702b0e37eb58ad092d409111a96b2483a/test/data_augmentation/data_augmentation -------------------------------------------------------------------------------- /test/data_augmentation/data_augmentation.cpp: -------------------------------------------------------------------------------- 1 | #include "../../src/AI/deeplearning/DataAugmentation.hpp" 2 | #include "../../src/AI/datasets/MNIST_loader.hpp" 3 | #include "../../src/AI/visualization/Bitmap.hpp" 4 | 5 | void print(const ai::Tensor_float& t) 6 | { 7 | for (int y = 0; y < 28; y++) { 8 | for (int x = 0; x < 28; x++) { 9 | if (t[y * 28 + x] > 0.5) printf("#"); 10 | else printf(" "); 11 | } 12 | printf("\n"); 13 | } 14 | } 15 | 16 | int main(int argc, const char *argv[]) 17 | { 18 | ai::Tensor_float trainingset, training_labels, testingset, testing_labels; 19 | ai::loadMNIST("/home/flowx08/mnist_png", trainingset, training_labels, testingset, testing_labels); 20 | 21 | ai::Tensor_float tmp; 22 | tmp.setshape(28 * 28); 23 | tmp.copy(trainingset.ptr(0, 32000)); 24 | printf("Normal image:\n"); 25 | print(tmp); 26 | ai::augmentation::rotate(tmp, 28, 28, 1, 20); 27 | printf("Rotated by 20 degree:\n"); 28 | print(tmp); 29 | ai::augmentation::rotate(tmp, 28, 28, 1, 90); 30 | printf("Rotated by 90 degree:\n"); 31 | print(tmp); 32 | tmp.copy(trainingset.ptr(0, 32000)); 33 | ai::augmentation::translate(tmp, 28, 28, 1, 5, 5); 34 | printf("Traslated by [5, 5]:\n"); 35 | print(tmp); 36 | tmp.copy(trainingset.ptr(0, 32000)); 37 | ai::augmentation::scaling(tmp, 28, 28, 1, 0.5); 38 | printf("Scaled by 0.5:\n"); 39 | print(tmp); 40 | tmp.copy(trainingset.ptr(0, 32000)); 41 | ai::augmentation::scaling(tmp, 28, 28, 1, 2); 42 | printf("Scaled by 2:\n"); 43 | print(tmp); 44 | tmp.copy(trainingset.ptr(0, 32000)); 45 | ai::augmentation::hflip(tmp, 28, 28, 1); 46 | printf("Flipped horizzontally:\n"); 47 | print(tmp); 48 | tmp.copy(trainingset.ptr(0, 32000)); 49 | ai::augmentation::vflip(tmp, 28, 28, 1); 50 | printf("Flipped vertically:\n"); 51 | print(tmp); 52 | tmp.copy(trainingset.ptr(0, 32000)); 53 | ai::augmentation::noise(tmp, 28, 28, 1, 0.1); 54 | printf("Random noise with probability 0.1:\n"); 55 | print(tmp); 56 | 57 | return 0; 58 | } 59 | -------------------------------------------------------------------------------- /test/data_augmentation/makefile: -------------------------------------------------------------------------------- 1 | CPP_FILES := $(wildcard ../../src/AI/*/*.cpp) data_augmentation.cpp 2 | OBJ_FILES = $(addprefix obj/, $(notdir $(CPP_FILES:.cpp=.o))) 3 | CC = g++ 4 | FLAGS = -std=c++11 -Wall -O3 5 | LIBS = 6 | EXE_NAME = data_augmentation 7 | 8 | all: makedirs main 9 | 10 | clear: 11 | rm -r obj/ 12 | 13 | obj/%.o : ../../src/AI/*/%.cpp 14 | $(CC) $(FLAGS) -c $< -o $@ $(LIBS) 15 | 16 | obj/%.o : %.cpp 17 | $(CC) $(FLAGS) -c $< -o $@ $(LIBS) 18 | 19 | makedirs: 20 | mkdir -p obj 21 | 22 | main: $(OBJ_FILES) 23 | $(CC) $(FLAGS) $^ -o $(EXE_NAME) $(LIBS) 24 | -------------------------------------------------------------------------------- /test/data_augmentation_cuda/data_augmentation_cuda: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Flowx08/artificial_intelligence/a93e405702b0e37eb58ad092d409111a96b2483a/test/data_augmentation_cuda/data_augmentation_cuda -------------------------------------------------------------------------------- /test/data_augmentation_cuda/data_augmentation_cuda.cpp: -------------------------------------------------------------------------------- 1 | #include "../../src/AI/deeplearning/DataAugmentation.hpp" 2 | #include "../../src/AI/deeplearning/CUDA_backend.hpp" 3 | #include "../../src/AI/util/TensorCUDA.hpp" 4 | #include "../../src/AI/datasets/MNIST_loader.hpp" 5 | #include "../../src/AI/visualization/Bitmap.hpp" 6 | 7 | void print(const ai::TensorCUDA_float& t) 8 | { 9 | ai::Tensor_float host(28 * 28); 10 | t.copyToHost(host.pointer(), host.size()); 11 | for (int y = 0; y < 28; y++) { 12 | for (int x = 0; x < 28; x++) { 13 | if (host[y * 28 + x] > 0.5) printf("#"); 14 | else printf(" "); 15 | } 16 | printf("\n"); 17 | } 18 | } 19 | 20 | int main(int argc, const char *argv[]) 21 | { 22 | ai::Tensor_float trainingset, training_targets, testingset, testing_targets; 23 | ai::loadMNIST("/home/flowx08/mnist_png", trainingset, training_targets, testingset, testing_targets); 24 | 25 | //Upload trainingset on device 26 | printf("Uploading trainingset and testingset on device...\n"); 27 | ai::TensorCUDA_float dev_trainingset, dev_training_targets, dev_testingset, dev_testing_targets; 28 | dev_trainingset.setshape(trainingset.width(), trainingset.height()); 29 | dev_trainingset.copyToDevice(trainingset.pointer(), trainingset.size()); 30 | dev_testingset.setshape(testingset.width(), testingset.height()); 31 | dev_testingset.copyToDevice(testingset.pointer(), testingset.size()); 32 | dev_training_targets.setshape(training_targets.width(), training_targets.height()); 33 | dev_training_targets.copyToDevice(training_targets.pointer(), training_targets.size()); 34 | dev_testing_targets.setshape(testing_targets.width(), testing_targets.height()); 35 | dev_testing_targets.copyToDevice(testing_targets.pointer(), testing_targets.size()); 36 | printf("Done\n"); 37 | 38 | int digit_type = 6400; 39 | ai::TensorCUDA_float tmp(28 * 28); 40 | tmp.copy(dev_trainingset.ptr(0, 6400)); 41 | printf("Normal image:\n"); 42 | print(tmp); 43 | ai::augmentation::hflip(tmp, 28, 28, 1); 44 | printf("Flipped horizontally:\n"); 45 | print(tmp); 46 | tmp.copy(dev_trainingset.ptr(0, 6400)); 47 | ai::augmentation::vflip(tmp, 28, 28, 1); 48 | printf("Flipped vertically:\n"); 49 | print(tmp); 50 | tmp.copy(dev_trainingset.ptr(0, 6400)); 51 | ai::augmentation::rotate(tmp, 28, 28, 1, 25); 52 | printf("Rotated 25 degree:\n"); 53 | print(tmp); 54 | tmp.copy(dev_trainingset.ptr(0, 6400)); 55 | ai::augmentation::rotate(tmp, 28, 28, 1, 90); 56 | printf("Rotated 90 degree:\n"); 57 | print(tmp); 58 | tmp.copy(dev_trainingset.ptr(0, 6400)); 59 | ai::augmentation::translate(tmp, 28, 28, 1, 8, 8); 60 | printf("Shifted by [+8, +8]:\n"); 61 | print(tmp); 62 | tmp.copy(dev_trainingset.ptr(0, 6400)); 63 | ai::augmentation::scaling(tmp, 28, 28, 1, 0.5); 64 | printf("Scaled by 0.5:\n"); 65 | print(tmp); 66 | tmp.copy(dev_trainingset.ptr(0, 6400)); 67 | ai::augmentation::scaling(tmp, 28, 28, 1, 2); 68 | printf("Scaled by 2:\n"); 69 | print(tmp); 70 | tmp.copy(dev_trainingset.ptr(0, 6400)); 71 | ai::augmentation::noise(tmp, 28, 28, 1, 0.10); 72 | printf("Noise with 0.10 probability:\n"); 73 | print(tmp); 74 | 75 | return 0; 76 | } 77 | -------------------------------------------------------------------------------- /test/data_augmentation_cuda/makefile: -------------------------------------------------------------------------------- 1 | CPP_FILES := $(wildcard ../../src/AI/deeplearning/*.cpp) $(wildcard ../../src/AI/util/*.cpp) $(wildcard ../../src/AI/datasets/*.cpp) $(wildcard ../../src/AI/visualization/*.cpp) data_augmentation_cuda.cpp 2 | CU_FILES := $(wildcard ../../src/AI/deeplearning/*.cu) $(wildcard ../../src/AI/util/*.cu) 3 | OBJ_FILES = $(addprefix obj/, $(notdir $(CPP_FILES:.cpp=.o))) $(addprefix obj/, $(notdir $(CU_FILES:.cu=.o))) 4 | CC = nvcc 5 | FLAGS = -std=c++11 -O3 -D_FORCE_INLINES 6 | LIBS = -lcudnn -lcublas 7 | EXE_NAME = data_augmentation_cuda 8 | 9 | all: makedirs main 10 | 11 | clear: 12 | rm -r obj/ 13 | 14 | obj/%.o : ../../src/AI/*/%.cpp 15 | $(CC) $(FLAGS) -c $< -o $@ $(LIBS) 16 | 17 | obj/%.o : ../../src/AI/*/%.cu 18 | $(CC) $(FLAGS) -c $< -o $@ $(LIBS) 19 | 20 | obj/%.o : %.cpp 21 | $(CC) $(FLAGS) -c $< -o $@ $(LIBS) 22 | 23 | makedirs: 24 | mkdir -p obj 25 | 26 | main: $(OBJ_FILES) 27 | $(CC) $(FLAGS) $^ -o $(EXE_NAME) $(LIBS) 28 | -------------------------------------------------------------------------------- /test/fashion_MNIST/fashion_MNIST: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Flowx08/artificial_intelligence/a93e405702b0e37eb58ad092d409111a96b2483a/test/fashion_MNIST/fashion_MNIST -------------------------------------------------------------------------------- /test/fashion_MNIST/makefile: -------------------------------------------------------------------------------- 1 | CPP_FILES := $(wildcard ../../src/AI/*/*.cpp) fashion_MNIST.cpp 2 | OBJ_FILES = $(addprefix obj/, $(notdir $(CPP_FILES:.cpp=.o))) 3 | CC = g++ 4 | FLAGS = -std=c++11 -Wall -O3 5 | LIBS = 6 | EXE_NAME = fashion_MNIST 7 | 8 | all: makedirs main 9 | 10 | clear: 11 | rm -r obj/ 12 | 13 | obj/%.o : ../../src/AI/*/%.cpp 14 | $(CC) $(FLAGS) -c $< -o $@ $(LIBS) 15 | 16 | obj/%.o : %.cpp 17 | $(CC) $(FLAGS) -c $< -o $@ $(LIBS) 18 | 19 | makedirs: 20 | mkdir -p obj 21 | 22 | main: $(OBJ_FILES) 23 | $(CC) $(FLAGS) $^ -o $(EXE_NAME) $(LIBS) 24 | -------------------------------------------------------------------------------- /test/genetic_alg/data.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Flowx08/artificial_intelligence/a93e405702b0e37eb58ad092d409111a96b2483a/test/genetic_alg/data.png -------------------------------------------------------------------------------- /test/genetic_alg/evolution: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Flowx08/artificial_intelligence/a93e405702b0e37eb58ad092d409111a96b2483a/test/genetic_alg/evolution -------------------------------------------------------------------------------- /test/genetic_alg/evolution_build.sh: -------------------------------------------------------------------------------- 1 | g++ -std=c++11 evolution_test.cpp ../../src/AI/optimization/*.cpp ../../src/AI/util/*.cpp ../../src/AI/visualization/*.cpp -pthread -o evolution 2 | -------------------------------------------------------------------------------- /test/genetic_alg/evolution_test.cpp: -------------------------------------------------------------------------------- 1 | #include "../../src/AI/optimization/GeneticAlgorithm.hpp" 2 | #include "../../src/AI/visualization/Bitmap.hpp" 3 | #include 4 | #include 5 | #include 6 | #include 7 | 8 | struct Solution { 9 | unsigned char data[512 * 512 * 4]; 10 | }; 11 | 12 | class ImageOptimizer : public GeneticAlgorithm { 13 | private: 14 | std::string m_filepath; 15 | Bitmap *m_bm; 16 | 17 | public: 18 | ImageOptimizer(const std::string filepath) { 19 | m_filepath = filepath; 20 | printf("Loading image...\n"); 21 | m_bm = new Bitmap(filepath, Bitmap::RGB); 22 | printf("Image loaded: %dx%dx%d\n", m_bm->m_width, m_bm->m_height, m_bm->m_channels); 23 | assert(m_bm->m_width <= 512 && m_bm->m_height <= 512); 24 | } 25 | 26 | ~ImageOptimizer() { 27 | delete m_bm; 28 | } 29 | 30 | const Bitmap* getBitmap() const { 31 | return m_bm; 32 | } 33 | 34 | Chromosome initSolution(unsigned int wid) 35 | { 36 | assert(m_bm != NULL); 37 | 38 | Solution* s = new Solution(); 39 | const int img_size = m_bm->m_width * m_bm->m_height * m_bm->m_channels; 40 | assert(img_size < 512 * 512 * 4); 41 | for (int i = 0; i < img_size; i++) 42 | s->data[i] = rand() % 256; 43 | return (Chromosome)s; 44 | } 45 | 46 | float computeFitness(Chromosome& c, unsigned int wid) 47 | { 48 | assert(m_bm != NULL); 49 | 50 | Solution* s = (Solution*)c; 51 | float score = 0.0; 52 | const int img_size = m_bm->m_width * m_bm->m_height * m_bm->m_channels; 53 | for (int i = 0; i < img_size; i++) 54 | score += 255 - abs(m_bm->m_data[i] - s->data[i]); 55 | return score; 56 | } 57 | 58 | Chromosome crossingOver(const Chromosome& mother, 59 | const Chromosome& father, 60 | unsigned int wid) 61 | { 62 | Solution* m = (Solution*)mother; 63 | Solution* f = (Solution*)father; 64 | Solution* s = new Solution(); 65 | const int img_size = m_bm->m_width * m_bm->m_height * m_bm->m_channels; 66 | for (int i = 0; i < img_size; i++) { 67 | s->data[i] = rand() % 2 ? m->data[i] : f->data[i]; 68 | } 69 | return (Chromosome)s; 70 | } 71 | 72 | void mutate(Chromosome& c, float mutationRate, unsigned int wid) 73 | { 74 | Solution* ch = (Solution*)c; 75 | const int img_size = m_bm->m_width * m_bm->m_height * m_bm->m_channels; 76 | ch->data[rand() % img_size] = rand() % 256; 77 | } 78 | 79 | void copyChromosome(Chromosome& src, Chromosome& dst) 80 | { 81 | Solution* s = (Solution*)src; 82 | Solution* d = (Solution*)dst; 83 | memcpy(d->data, s->data, sizeof(Solution)); 84 | } 85 | 86 | void freeChromosome(Chromosome& c) 87 | { 88 | Solution* s = (Solution*)c; 89 | delete s; 90 | } 91 | 92 | unsigned int getSolutionSize() 93 | { 94 | return sizeof(Solution); 95 | } 96 | }; 97 | 98 | int main(int argc, const char *argv[]) 99 | { 100 | srand((int)time(NULL)); 101 | 102 | printf("Optimizing image...\n"); 103 | ImageOptimizer opt("./data.png"); 104 | opt.optimize(30, 200000, 8, 5, true); 105 | printf("Final score: %f\n", opt.getBestChromosomeFitness()); 106 | 107 | printf("Saving result...\n"); 108 | const Bitmap* bm = opt.getBitmap(); 109 | Bitmap result(bm->m_width, bm->m_height, Bitmap::RGB, 0x000000); 110 | Solution* s = (Solution*)opt.getBestChromosome(); 111 | const int img_size = bm->m_width * bm->m_height * bm->m_channels; 112 | for (int i = 0; i < img_size; i++) 113 | result.m_data[i] = s->data[i]; 114 | result.save("result.png"); 115 | printf("Done.\n"); 116 | return 0; 117 | } 118 | -------------------------------------------------------------------------------- /test/genetic_alg/result.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Flowx08/artificial_intelligence/a93e405702b0e37eb58ad092d409111a96b2483a/test/genetic_alg/result.png -------------------------------------------------------------------------------- /test/genetic_programming/gp_build.sh: -------------------------------------------------------------------------------- 1 | g++ -std=c++11 gp_test.cpp ../../src/AI/classical/*.cpp ../../src/AI/util/*.cpp -pthread -o gp_test 2 | -------------------------------------------------------------------------------- /test/genetic_programming/gp_play.sh: -------------------------------------------------------------------------------- 1 | while : 2 | do 3 | ./gp_test 4 | sleep 1 5 | done 6 | -------------------------------------------------------------------------------- /test/genetic_programming/gp_test: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Flowx08/artificial_intelligence/a93e405702b0e37eb58ad092d409111a96b2483a/test/genetic_programming/gp_test -------------------------------------------------------------------------------- /test/genetic_programming/gp_test.cpp: -------------------------------------------------------------------------------- 1 | #include "../../src/AI/classical/genetic_programming.hpp" 2 | #include "../../src/AI/optimization/GA.hpp" 3 | #include 4 | #include 5 | 6 | using namespace std; 7 | 8 | struct data_point { 9 | float x, y; 10 | } /* optional variable list */; 11 | 12 | 13 | std::vector< data_point> points; 14 | 15 | double evaluate(ai::gp::ExpressionNode*& prog) 16 | { 17 | double score = 0.0; 18 | for (int i = 0; i < 20; i++) { 19 | float px = -5.f + ((double)i / 20.f) * 10.f; 20 | float py = 1.f / (1.f + exp(-px)); 21 | float y = ai::gp::program_evaluate(prog, {px}); 22 | score -= fabs(py - y); 23 | } 24 | return score; 25 | } 26 | 27 | ai::gp::ExpressionNode* mutate(ai::gp::ExpressionNode*& p1, ai::gp::ExpressionNode*& p2) 28 | { 29 | ai::gp::ExpressionNode* child; 30 | if (rand() % 2 == 0) child = ai::gp::program_crossover_initialization(p1, p2); 31 | else child = ai::gp::program_copy_initialization(p1); 32 | ai::gp::program_mutate(child, 0.1, 1, -10, 10); 33 | return child; 34 | } 35 | 36 | void destroy(ai::gp::ExpressionNode*& node) 37 | { 38 | ai::gp::program_free(node); 39 | node = nullptr; 40 | } 41 | 42 | int main(int argc, const char *argv[]) 43 | { 44 | srand((int)time(NULL)); 45 | 46 | //Generate dataset 47 | points.clear(); 48 | for (int k = -2; k < 10; k++) { 49 | data_point pt; 50 | pt.x = k; 51 | pt.y = sin(k); //k * k + 3; 52 | std::cout << pt.x << " " << pt.y << std::endl; 53 | points.push_back(pt); 54 | } 55 | std::cout << "Dataset generated" << std::endl; 56 | 57 | //Generate initial population 58 | std::vector< ai::gp::ExpressionNode* > population(1000); 59 | for (int i = 0; i < population.size(); i++) 60 | population[i] = ai::gp::program_random_initialization(4, 1, -10, 10); 61 | std::cout << "Inizial population generated" << std::endl; 62 | 63 | //Initialize genetic algoritm framework 64 | ai::genetic_optimizer< ai::gp::ExpressionNode* > optimizer(population, 4, evaluate, mutate, destroy); 65 | std::cout << "Optimizer initialized" << std::endl; 66 | for (int i = 0; i < 5000; i++) { 67 | optimizer.run(400, 1); 68 | std::cout << "Cicle: " << i << " Score: " << optimizer.getBestGenomeFitness() << std::endl; 69 | } 70 | 71 | std::cout << ai::gp::program_parse(optimizer.getBestGenomeData()) << std::endl; 72 | float score = 0.0; 73 | for (int i = 0; i < points.size(); i++) { 74 | float y = ai::gp::program_evaluate(optimizer.getBestGenomeData(), {points[i].x}); 75 | score -= fabs(points[i].y - y); 76 | std::cout << points[i].x << " " << y << ":" << points[i].y << std::endl; 77 | } 78 | std::cout << score << std::endl; 79 | 80 | return 0; 81 | } 82 | -------------------------------------------------------------------------------- /test/kmeans/kmeans: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Flowx08/artificial_intelligence/a93e405702b0e37eb58ad092d409111a96b2483a/test/kmeans/kmeans -------------------------------------------------------------------------------- /test/kmeans/kmeans_build.sh: -------------------------------------------------------------------------------- 1 | g++ -std=c++11 kmeans_test.cpp ../../src/AI/datamining/*.cpp -o kmeans 2 | -------------------------------------------------------------------------------- /test/kmeans/kmeans_test.cpp: -------------------------------------------------------------------------------- 1 | //////////////////////////////////////////////////////////// 2 | /// INCLUDES 3 | //////////////////////////////////////////////////////////// 4 | #include "../../src/AI/datamining/kmeans.hpp" 5 | #include 6 | #include 7 | 8 | //#define STANDARD 9 | #define CONTINUOUS 10 | 11 | #if defined(STANDARD) 12 | 13 | int main(int argc, const char *argv[]) 14 | { 15 | srand((int)time(NULL)); 16 | 17 | ai::kmeans test(2, 2); 18 | std::vector< ai::vec > vecs(6); 19 | vecs[0] = ai::vec(2); 20 | vecs[0][0] = 1.0; 21 | vecs[0][1] = 1.0; 22 | 23 | vecs[1] = ai::vec(2); 24 | vecs[1][0] = 2.0; 25 | vecs[1][1] = 1.0; 26 | 27 | vecs[2] = ai::vec(2); 28 | vecs[2][0] = 1.0; 29 | vecs[2][1] = 2.0; 30 | 31 | vecs[3] = ai::vec(2); 32 | vecs[3][0] = 3+ 1.0; 33 | vecs[3][1] = 3+ 1.0; 34 | 35 | vecs[4] = ai::vec(2); 36 | vecs[4][0] = 3+ 2.0; 37 | vecs[4][1] = 3+ 1.0; 38 | 39 | vecs[5] = ai::vec(2); 40 | vecs[5][0] = 3+ 1.0; 41 | vecs[5][1] = 3+ 2.0; 42 | 43 | test.fit(vecs, 100); 44 | printf("Fitted in:%d\n", test.getiterations()); 45 | 46 | for (int i = 0; i < (int)vecs.size(); i++) { 47 | printf("Point:%d Cluster:%d\n", i, test.getcluster(vecs[i])); 48 | } 49 | 50 | return 0; 51 | } 52 | 53 | #elif defined(CONTINUOUS) 54 | 55 | int main(int argc, const char *argv[]) 56 | { 57 | srand((int)time(NULL)); 58 | 59 | ai::kmeans test(2, 2); 60 | std::vector< ai::vec > vecs(6); 61 | vecs[0] = ai::vec(2); 62 | vecs[0][0] = 1.0; 63 | vecs[0][1] = 1.0; 64 | 65 | vecs[1] = ai::vec(2); 66 | vecs[1][0] = 2.0; 67 | vecs[1][1] = 1.0; 68 | 69 | vecs[2] = ai::vec(2); 70 | vecs[2][0] = 1.0; 71 | vecs[2][1] = 2.0; 72 | 73 | vecs[3] = ai::vec(2); 74 | vecs[3][0] = 3+ 1.0; 75 | vecs[3][1] = 3+ 1.0; 76 | 77 | vecs[4] = ai::vec(2); 78 | vecs[4][0] = 3+ 2.0; 79 | vecs[4][1] = 3+ 1.0; 80 | 81 | vecs[5] = ai::vec(2); 82 | vecs[5][0] = 3+ 1.0; 83 | vecs[5][1] = 3+ 2.0; 84 | 85 | const int cicles = 3; 86 | for (int i = 0; i < cicles; i++) { 87 | for (int k = 0; k < (int)test.getcentroids().size(); k++) { 88 | printf("Centroid: %d X:%f Y:%f\n", k, test.getcentroids()[k][0], test.getcentroids()[k][1]); 89 | } 90 | std::vector< ai::vec > ps(1); 91 | ps[0] = vecs[rand() % (int)vecs.size()]; 92 | test.fit_continuous(vecs, 0.1, 1); 93 | } 94 | printf("Fitted in:%d\n", cicles); 95 | 96 | for (int i = 0; i < (int)vecs.size(); i++) { 97 | printf("Point:%d Cluster:%d\n", i, test.getcluster(vecs[i])); 98 | } 99 | 100 | return 0; 101 | } 102 | 103 | #endif 104 | -------------------------------------------------------------------------------- /test/linear_logistic_regression/linear_reg_mnist: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Flowx08/artificial_intelligence/a93e405702b0e37eb58ad092d409111a96b2483a/test/linear_logistic_regression/linear_reg_mnist -------------------------------------------------------------------------------- /test/linear_logistic_regression/linear_reg_mnist.cpp: -------------------------------------------------------------------------------- 1 | #include "../src/AI/classical/linear_regression.hpp" 2 | #include "../src/AI/classical/logistic_regression.hpp" 3 | #include "../src/AI/classical/ensemble_logistic_regression.hpp" 4 | #include "../src/AI/visualization/Bitmap.hpp" 5 | #include "../src/AI/util/Util.hpp" 6 | #include "datasetloader.hpp" 7 | #include 8 | 9 | int main(int argc, const char *argv[]) 10 | { 11 | //srand((int)time(NULL)); 12 | 13 | //Load trainingset 14 | std::vector trainingset, testingset; 15 | ai::Tensor_float training_targets, testing_targets; 16 | datasetloader::loadMNIST(trainingset, training_targets, testingset, testing_targets); 17 | 18 | ai::Tensor_float trains, tests, train_t, test_t; 19 | int trains_size = 0; 20 | int tests_size = 0; 21 | for (int i = 0; i < (int)trainingset.size(); i++) { 22 | trains_size += trainingset[i].height(); 23 | tests_size += testingset[i].height(); 24 | } 25 | trains.setshape(28 * 28, trains_size); 26 | tests.setshape(28 * 28, tests_size); 27 | train_t.setshape(10, trains_size); 28 | test_t.setshape(10, tests_size); 29 | int k = 0, m = 0; 30 | for (int i = 0; i < (int)trainingset.size(); i++) { 31 | for (int l = 0; l < trainingset[i].height(); l++) { 32 | for (int p = 0; p < trainingset[i].width(); p++) trains.at(k, p) = trainingset[i].at(l, p); 33 | for (int p = 0; p < training_targets.width(); p++) train_t.at(k, p) = training_targets.at(i, p); 34 | k++; 35 | } 36 | 37 | for (int l = 0; l < testingset[i].height(); l++) { 38 | for (int p = 0; p < testingset[i].width(); p++) tests.at(m, p) = testingset[i].at(l, p); 39 | for (int p = 0; p < testing_targets.width(); p++) test_t.at(m, p) = testing_targets.at(i, p); 40 | m++; 41 | } 42 | } 43 | 44 | //ai::linear_regression lr(28 * 28, 10); 45 | ai::logistic_regression lr(28 * 28, 10); 46 | //ai::linear_regression lr("test.txt"); 47 | ai::ensemble_logistic_regression elr(28 * 28, 10, 20); 48 | elr.fit(trains, train_t, 0.01, 5); 49 | //lr.test(tests, test_t); 50 | //elr.save("test.txt"); 51 | elr.test(tests, test_t); 52 | 53 | return 0; 54 | } 55 | -------------------------------------------------------------------------------- /test/linear_logistic_regression/linear_reg_mnist.sh: -------------------------------------------------------------------------------- 1 | g++ -std=c++11 -O3 -Wall -march=native -g linear_reg_mnist.cpp ../src/AI/classical/*.cpp ../src/AI/deeplearning/*.cpp ../src/AI/util/*.cpp ../src/AI/visualization/*.cpp -o linear_reg_mnist 2 | -------------------------------------------------------------------------------- /test/particle_swarm_optimization/pso_build.sh: -------------------------------------------------------------------------------- 1 | g++ -std=c++11 pso_test.cpp ../../src/AI/visualization/*.cpp ../../src/AI/optimization/*.cpp -o pso 2 | -------------------------------------------------------------------------------- /test/particle_swarm_optimization/pso_test.cpp: -------------------------------------------------------------------------------- 1 | #include "../../src/AI/optimization/pso.hpp" 2 | #include "../../src/AI/visualization/Bitmap.hpp" 3 | #include 4 | #include 5 | #include 6 | #include 7 | 8 | Bitmap *bm; 9 | 10 | double evaluate(std::vector& t) 11 | { 12 | double score = 0.0; 13 | for (int k = 0; k < (int)t.size(); k++) 14 | score += 255 - fabs(bm->m_data[k] - t[k]); 15 | return score; 16 | } 17 | 18 | int main(int argc, const char *argv[]) 19 | { 20 | srand((int)time(NULL)); 21 | 22 | bm = new Bitmap("data.png", Bitmap::RGB); 23 | 24 | printf("Starting...\n"); 25 | ai::pso order(30, 64*64*3, evaluate, 0.1, 0.1); 26 | for (int i = 0; i < 30; i++) { 27 | order.run(100); 28 | printf("Cicle: %d Score:%f MediumError:%f\n", i, order.getBestScore(), 255 - (order.getBestScore()/(64*64*3.0))); 29 | } 30 | 31 | Bitmap result(bm->m_width, bm->m_height, Bitmap::RGB, 0x000000); 32 | for (int i = 0; i < (int)order.getBestParams().size(); i++) { 33 | result.m_data[i] = order.getBestParams()[i]; 34 | } 35 | result.save("result.png"); 36 | return 0; 37 | } 38 | --------------------------------------------------------------------------------