From a13bd699bc396fb0876fcd3eb674456b47096540 Mon Sep 17 00:00:00 2001 From: "jijoong.moon" Date: Fri, 20 Mar 2020 09:07:34 +0900 Subject: [PATCH] Make Examples work properly Modify Exampels for the Updated NNtrainer - Classification - KNN - LogisticRegression - ReinforcementLearning - Training **Self evaluation:** 1. Build test: [X]Passed [ ]Failed [ ]Skipped 2. Run test: [X]Passed [ ]Failed [ ]Skipped Signed-off-by: jijoong.moon --- .../TensorFlow/Training_Keras.py | 0 .../{ => Classification}/TensorFlow/Training_tf.py | 0 .../TensorFlow/train_val.data.tar.gz | Bin Applications/LogisticRegression/jni/main.cpp | 4 +- .../ReinforcementLearning/DeepQ/jni/CMakeLists.txt | 8 ++- .../ReinforcementLearning/DeepQ/jni/DeepQ.ini | 20 +++--- .../DeepQ/jni/gym_binding.cpp | 67 ++++++++------------- .../ReinforcementLearning/DeepQ/jni/main.cpp | 12 ++-- .../Environment/CartPole/main.cpp | 4 +- Applications/Training/jni/main.cpp | 4 +- 10 files changed, 52 insertions(+), 67 deletions(-) rename Applications/{ => Classification}/TensorFlow/Training_Keras.py (100%) rename Applications/{ => Classification}/TensorFlow/Training_tf.py (100%) rename Applications/{ => Classification}/TensorFlow/train_val.data.tar.gz (100%) diff --git a/Applications/TensorFlow/Training_Keras.py b/Applications/Classification/TensorFlow/Training_Keras.py similarity index 100% rename from Applications/TensorFlow/Training_Keras.py rename to Applications/Classification/TensorFlow/Training_Keras.py diff --git a/Applications/TensorFlow/Training_tf.py b/Applications/Classification/TensorFlow/Training_tf.py similarity index 100% rename from Applications/TensorFlow/Training_tf.py rename to Applications/Classification/TensorFlow/Training_tf.py diff --git a/Applications/TensorFlow/train_val.data.tar.gz b/Applications/Classification/TensorFlow/train_val.data.tar.gz similarity index 100% rename from Applications/TensorFlow/train_val.data.tar.gz rename to Applications/Classification/TensorFlow/train_val.data.tar.gz diff --git a/Applications/LogisticRegression/jni/main.cpp b/Applications/LogisticRegression/jni/main.cpp index 6f9bfe9..a54bba4 100644 --- a/Applications/LogisticRegression/jni/main.cpp +++ b/Applications/LogisticRegression/jni/main.cpp @@ -123,7 +123,7 @@ int main(int argc, char *argv[]) { std::vector> in, label; in.push_back(inputVector[j]); label.push_back(outputVector[j]); - NN.backwarding(Tensor(in), Tensor(label), i); + NN.backwarding(Tensors::Tensor(in), Tensors::Tensor(label), i); } std::cout << "#" << i + 1 << "/" << NN.getEpoch() << " - Loss : " << NN.getLoss() << std::endl; NN.setLoss(0.0); @@ -137,7 +137,7 @@ int main(int argc, char *argv[]) { std::vector> in, label; in.push_back(inputVector[j]); label.push_back(outputVector[j]); - if (NN.forwarding(Tensor(in)).applyFunction(stepFunction).getValue(0, 0, 0) == label[0][0]) + if (NN.forwarding(Tensors::Tensor(in)).applyFunction(stepFunction).getValue(0, 0, 0) == label[0][0]) cn++; } std::cout << "[ Accuracy ] : " << ((float)(cn) / inputVector.size()) * 100.0 << "%" << std::endl; diff --git a/Applications/ReinforcementLearning/DeepQ/jni/CMakeLists.txt b/Applications/ReinforcementLearning/DeepQ/jni/CMakeLists.txt index 49e53fa..37e1675 100644 --- a/Applications/ReinforcementLearning/DeepQ/jni/CMakeLists.txt +++ b/Applications/ReinforcementLearning/DeepQ/jni/CMakeLists.txt @@ -17,13 +17,17 @@ link_libraries(${CURL_LIBRARIES}) pkg_check_modules(NNTRAINER nntrainer) link_libraries(${NNTRAINER_LIBRARIES}) +pkg_check_modules(INIPARSER iniparser) +link_libraries(${INIPARSER_LIBRARIES}) + set(ENVDIR ../../Environment) include_directories( ${include_directories} ${CMAKE_CURRENT_SOURCE_DIR}/include ${JSONCPP_INCLUDE_DIRS} ${ENVDIR} - ${NNTRAINER_INCLUDE_DIRS} + ${NNTRAINER_INCLUDE_DIRS} + ${INIPARSER_INCLUDE_DIRS} ) set(SRCS main.cpp @@ -32,7 +36,7 @@ set(SRCS ) add_executable(DeepQ ${SRCS}) -target_link_libraries( DeepQ ${JSONCPP_LIBRARIES} ${CURL_LIBRARIES} ${NNTRAINER_LIBRARIES}) +target_link_libraries( DeepQ ${JSONCPP_LIBRARIES} ${CURL_LIBRARIES} ${NNTRAINER_LIBRARIES} ${INIPARSER_INCLUDE_DIRS}) install (TARGETS DeepQ RUNTIME DESTINATION bin diff --git a/Applications/ReinforcementLearning/DeepQ/jni/DeepQ.ini b/Applications/ReinforcementLearning/DeepQ/jni/DeepQ.ini index bb92c45..2618085 100644 --- a/Applications/ReinforcementLearning/DeepQ/jni/DeepQ.ini +++ b/Applications/ReinforcementLearning/DeepQ/jni/DeepQ.ini @@ -9,11 +9,11 @@ Learning_rate = 0.001 # Learning Rate Epoch = 10000 # Epoch Optimizer = adam # Optimizer : sgd (stochastic gradien decent), # adam (Adamtive Moment Estimation) -Activation = tanh # activation : sigmoid, tanh + Cost = msr # Cost(loss) function : msr (mean square root error) # categorical ( for logistic regression ) Model = "model.bin" # model path to save / read -minibatch = 30 # mini batch size +minibatch = 32 # mini batch size beta1 = 0.9 # beta 1 for adam beta2 = 0.9999 # beta 2 for adam epsilon = 1e-8 # epsilon for adam @@ -22,27 +22,27 @@ epsilon = 1e-8 # epsilon for adam [inputlayer] Type = InputLayer Id = 0 # Layer Id -Height = 1 -Width = 4 # Input Layer Dimension +HiddenSize = 4 # Input Layer Dimension Bias_zero = true # Zero Bias +Activation = tanh # activation : sigmoid, tanh [fc1layer] Type = FullyConnectedLayer Id = 1 -Height = 4 # Input Dimension ( = Weight Height ) -Width = 50 # Hidden Layer Dimension ( = Weight Width ) +HiddenSize = 50 # Hidden Layer Dimension ( = Weight Width ) Bias_zero = true +Activation = tanh # activation : sigmoid, tanh [fc2layer] Type = FullyConnectedLayer Id = 2 -Height = 50 -Width = 50 +HiddenSize = 50 Bias_zero = true +Activation = tanh # activation : sigmoid, tanh [outputlayer] Type = OutputLayer Id = 3 -Height = 50 # Hidden Layer Dimension ( = Weight Height ) -Width = 2 # Output Layer Dimension ( = Weight Width ) +HiddenSize = 2 # Output Layer Dimension ( = Weight Width ) Bias_zero = true +Activation = sigmoid # activation : sigmoid, tanh \ No newline at end of file diff --git a/Applications/ReinforcementLearning/DeepQ/jni/gym_binding.cpp b/Applications/ReinforcementLearning/DeepQ/jni/gym_binding.cpp index 27e94e2..9c62e14 100644 --- a/Applications/ReinforcementLearning/DeepQ/jni/gym_binding.cpp +++ b/Applications/ReinforcementLearning/DeepQ/jni/gym_binding.cpp @@ -7,16 +7,16 @@ * This is from openai/gym-http-api to run gymAI for C/C++ */ #include "include/gym/gym.h" -#include #include +#include #include #include #include -#include #include +#include namespace Gym { @@ -59,9 +59,7 @@ static std::shared_ptr space_from_json(const Json::Value &j) { std::string type = require(v, "name"); if (type == "Discrete") { r->type = Space::DISCRETE; - r->discreet_n = - v["n"] - .asInt(); // will throw runtime_error if cannot be converted to int + r->discreet_n = v["n"].asInt(); // will throw runtime_error if cannot be converted to int } else if (type == "Box") { r->type = Space::BOX; @@ -97,17 +95,15 @@ static std::shared_ptr space_from_json(const Json::Value &j) { // curl -static std::size_t curl_save_to_string(void *buffer, std::size_t size, - std::size_t nmemb, void *userp) { +static std::size_t curl_save_to_string(void *buffer, std::size_t size, std::size_t nmemb, void *userp) { std::string *str = static_cast(userp); const std::size_t bytes = nmemb * size; str->append(static_cast(buffer), bytes); return bytes; } -class ClientReal : public Client, - public std::enable_shared_from_this { -public: +class ClientReal : public Client, public std::enable_shared_from_this { + public: std::string addr; int port; @@ -127,8 +123,7 @@ public: curl_error_buf.assign(CURL_ERROR_SIZE, 0); curl_easy_setopt(c, CURLOPT_ERRORBUFFER, curl_error_buf.data()); h.reset(c, std::ptr_fun(curl_easy_cleanup)); - headers.reset(curl_slist_append(0, "Content-Type: application/json"), - std::ptr_fun(curl_slist_free_all)); + headers.reset(curl_slist_append(0, "Content-Type: application/json"), std::ptr_fun(curl_slist_free_all)); } Json::Value GET(const std::string &route) { @@ -164,8 +159,7 @@ public: curl_easy_setopt(h.get(), CURLOPT_POST, 1); curl_easy_setopt(h.get(), CURLOPT_POSTFIELDS, post_data.c_str()); - curl_easy_setopt(h.get(), CURLOPT_POSTFIELDSIZE_LARGE, - (curl_off_t)post_data.size()); + curl_easy_setopt(h.get(), CURLOPT_POSTFIELDSIZE_LARGE, (curl_off_t)post_data.size()); curl_easy_setopt(h.get(), CURLOPT_HTTPHEADER, headers.get()); curl_easy_setopt(h.get(), CURLOPT_PROXY, ""); @@ -179,11 +173,9 @@ public: return j; } - void throw_server_error_or_response_code(const std::string &answer, - Json::Value &j) { + void throw_server_error_or_response_code(const std::string &answer, Json::Value &j) { long response_code; - CURLcode r = - curl_easy_getinfo(h.get(), CURLINFO_RESPONSE_CODE, &response_code); + CURLcode r = curl_easy_getinfo(h.get(), CURLINFO_RESPONSE_CODE, &response_code); if (r) throw std::runtime_error(curl_error_buf.data()); if (verbose) @@ -202,9 +194,7 @@ public: if (response_code != 200 && j.isObject() && j.isMember("message")) { throw std::runtime_error(j["message"].asString()); } else if (response_code != 200) { - throw std::runtime_error( - "bad HTTP response code, and also cannot parse server message: " + - answer); + throw std::runtime_error("bad HTTP response code, and also cannot parse server message: " + answer); } else { // 200, but maybe invalid json if (!parse_error.empty()) @@ -225,7 +215,7 @@ std::shared_ptr client_create(const std::string &addr, int port) { // environment class EnvironmentReal : public Environment { -public: + public: std::string instance_id; std::shared_ptr client; std::shared_ptr space_act; @@ -233,15 +223,13 @@ public: std::shared_ptr action_space() override { if (!space_act) - space_act = space_from_json( - client->GET("/v1/envs/" + instance_id + "/action_space")); + space_act = space_from_json(client->GET("/v1/envs/" + instance_id + "/action_space")); return space_act; } std::shared_ptr observation_space() override { if (!space_obs) - space_obs = space_from_json( - client->GET("/v1/envs/" + instance_id + "/observation_space")); + space_obs = space_from_json(client->GET("/v1/envs/" + instance_id + "/observation_space")); return space_obs; } @@ -259,43 +247,37 @@ public: observation_parse(ans["observation"], save_initial_state_here->observation); } - void step(const std::vector &action, bool render, - State *save_state_here) override { + void step(const std::vector &action, bool render, State *save_state_here) override { Json::Value act_json; std::shared_ptr aspace = action_space(); if (aspace->type == Space::DISCRETE) { act_json["action"] = (int)action[0]; } else if (aspace->type == Space::BOX) { Json::Value &array = act_json["action"]; - assert(action.size() == aspace->box_low.size()); // really assert, it's a - // programming error on - // C++ part + assert(action.size() == aspace->box_low.size()); // really assert, it's a + // programming error on + // C++ part for (int c = 0; c < (int)action.size(); ++c) array[c] = action[c]; } else { assert(0); } act_json["render"] = render; - Json::Value ans = client->POST("/v1/envs/" + instance_id + "/step/", - act_json.toStyledString()); + Json::Value ans = client->POST("/v1/envs/" + instance_id + "/step/", act_json.toStyledString()); observation_parse(ans["observation"], save_state_here->observation); save_state_here->done = ans["done"].asBool(); save_state_here->reward = ans["reward"].asFloat(); } - void monitor_start(const std::string &directory, bool force, - bool resume) override { + void monitor_start(const std::string &directory, bool force, bool resume) override { Json::Value data; data["directory"] = directory; data["force"] = force; data["resume"] = resume; - client->POST("/v1/envs/" + instance_id + "/monitor/start/", - data.toStyledString()); + client->POST("/v1/envs/" + instance_id + "/monitor/start/", data.toStyledString()); } - void monitor_stop() override { - client->POST("/v1/envs/" + instance_id + "/monitor/close/", ""); - } + void monitor_stop() override { client->POST("/v1/envs/" + instance_id + "/monitor/close/", ""); } }; std::shared_ptr ClientReal::make(const std::string &env_id) { @@ -306,12 +288,11 @@ std::shared_ptr ClientReal::make(const std::string &env_id) { std::string instance_id = require(ans, "instance_id"); if (verbose) - printf(" * created %s instance_id=%s\n", env_id.c_str(), - instance_id.c_str()); + printf(" * created %s instance_id=%s\n", env_id.c_str(), instance_id.c_str()); std::shared_ptr env(new EnvironmentReal); env->client = shared_from_this(); env->instance_id = instance_id; return env; } -} // namespace +} // namespace diff --git a/Applications/ReinforcementLearning/DeepQ/jni/main.cpp b/Applications/ReinforcementLearning/DeepQ/jni/main.cpp index 195feda..db45616 100644 --- a/Applications/ReinforcementLearning/DeepQ/jni/main.cpp +++ b/Applications/ReinforcementLearning/DeepQ/jni/main.cpp @@ -68,8 +68,8 @@ #include #include #include "include/gym/gym.h" -#include "tensor.h" #include "neuralnet.h" +#include "tensor.h" #ifdef USING_CUSTOM_ENV #include "CartPole/cartpole.h" @@ -317,7 +317,7 @@ int main(int argc, char **argv) { /** * @brief get action with input State with mainNet */ - Tensor test = mainNet.forwarding(Tensor({input})); + Tensors::Tensor test = mainNet.forwarding(Tensors::Tensor({input})); std::vector temp = test.Mat2Vec(); action.push_back(argmax(temp)); @@ -405,12 +405,12 @@ int main(int argc, char **argv) { /** * @brief run forward propagation with mainNet */ - Tensor Q = mainNet.forwarding(Tensor(inbatch)); + Tensors::Tensor Q = mainNet.forwarding(Tensors::Tensor(inbatch)); /** * @brief run forward propagation with targetNet */ - Tensor NQ = targetNet.forwarding(Tensor(next_inbatch)); + Tensors::Tensor NQ = targetNet.forwarding(Tensors::Tensor(next_inbatch)); std::vector nqa = NQ.Mat2Vec(); /** @@ -421,11 +421,11 @@ int main(int argc, char **argv) { Q.setValue(i, 0, (int)in_Exp[i].action[0], (float)in_Exp[i].reward); } else { float next = (nqa[i * NQ.getWidth()] > nqa[i * NQ.getWidth() + 1]) ? nqa[i * NQ.getWidth()] - : nqa[i * NQ.getWidth() + 1]; + : nqa[i * NQ.getWidth() + 1]; Q.setValue(i, 0, (int)in_Exp[i].action[0], (float)in_Exp[i].reward + DISCOUNT * next); } } - mainNet.backwarding(Tensor(inbatch), Q, iter); + mainNet.backwarding(Tensors::Tensor(inbatch), Q, iter); } writeFile << "mainNet Loss : " << mainNet.getLoss() << " : targetNet Loss : " << targetNet.getLoss() << "\n"; diff --git a/Applications/ReinforcementLearning/Environment/CartPole/main.cpp b/Applications/ReinforcementLearning/Environment/CartPole/main.cpp index b60f345..69d9835 100644 --- a/Applications/ReinforcementLearning/Environment/CartPole/main.cpp +++ b/Applications/ReinforcementLearning/Environment/CartPole/main.cpp @@ -21,9 +21,9 @@ * */ -#include "cartpole.h" -#include #include +#include +#include "cartpole.h" int main() { Env::State state; diff --git a/Applications/Training/jni/main.cpp b/Applications/Training/jni/main.cpp index 9423062..94be6f8 100644 --- a/Applications/Training/jni/main.cpp +++ b/Applications/Training/jni/main.cpp @@ -246,7 +246,7 @@ int main(int argc, char *argv[]) { */ for (int i = 0; i < ITERATION; i++) { for (unsigned int j = 0; j < inputVector.size(); j++) { - NN.backwarding(Tensor({inputVector[j]}), Tensor({outputVector[j]}), i); + NN.backwarding(Tensors::Tensor({inputVector[j]}), Tensors::Tensor({outputVector[j]}), i); } cout << "#" << i + 1 << "/" << ITERATION << " - Loss : " << NN.getLoss() << endl; NN.setLoss(0.0); @@ -266,7 +266,7 @@ int main(int argc, char *argv[]) { std::vector featureVector, resultVector; featureVector.resize(128); getFeature(img, featureVector); - Tensor X = Tensor({featureVector}); + Tensors::Tensor X = Tensors::Tensor({featureVector}); cout << NN.forwarding(X).applyFunction(stepFunction) << endl; } -- 2.7.4