Epochs = 10 # Epochs
Loss = cross # Loss function : mse (mean squared error)
# cross (cross entropy)
-Save_Path = "model_draw_cls.bin" # model path to save / read
+Save_Path = model_draw_cls.bin # model path to save / read
batch_size = 1 # batch size
[Optimizer]
} catch (nntrainer::exception::not_supported &e) {
ml_loge("%s %s", typeid(e).name(), e.what());
return ML_ERROR_INVALID_PARAMETER;
+ } catch (nntrainer::exception::permission_denied &e) {
+ ml_loge("%s %s", typeid(e).name(), e.what());
+ return ML_ERROR_PERMISSION_DENIED;
} catch (std::invalid_argument &e) {
ml_loge("%s %s", typeid(e).name(), e.what());
return ML_ERROR_INVALID_PARAMETER;
#include <node_exporter.h>
#include <parse_util.h>
#include <time_dist.h>
+#include <util_func.h>
#include <base_properties.h>
#include <common_properties.h>
/// not delegating for now as required logics are managable for now.
switch (format) {
case ml::train::ModelFormat::MODEL_FORMAT_BIN: {
- std::ofstream model_file(file_path, std::ios::out | std::ios::binary);
- /// @todo, if errno == EACCESS or EPERM, throw PERMISSION DENIED error
- NNTR_THROW_IF(!model_file.good(), std::invalid_argument)
- << "model file not opened, file path: " << file_path
- << " reason: " << strerror(errno);
+ auto model_file = checkedOpenStream<std::ofstream>(
+ file_path, std::ios::out | std::ios::binary);
for (auto iter = model_graph.cbegin(); iter != model_graph.cend(); iter++) {
(*iter)->save(model_file);
}
<< "Cannot load if not initialized yet, path: " << file_path
<< " format: " << static_cast<unsigned>(format);
- std::ifstream model_file(file_path, std::ios::in | std::ios::binary);
- /// @todo, if errno == EACCESS or EPERM, throw PERMISSION DENIED error
- NNTR_THROW_IF(!model_file.good(), std::invalid_argument)
- << "model file not opened, file path: " << file_path
- << " reason: " << strerror(errno);
-
+ auto model_file = checkedOpenStream<std::ifstream>(
+ file_path, std::ios::in | std::ios::binary);
+ std::cerr << file_path << '\n';
for (auto iter = model_graph.cbegin(); iter != model_graph.cend(); iter++) {
(*iter)->read(model_file);
}
int ret = loadFromConfig(file_path);
throw_status(ret);
if (!save_path.empty()) {
- /// @todo checkedOpenhere
+ checkedOpenStream<std::ifstream>(save_path,
+ std::ios::in | std::ios::binary);
load_path = save_path;
}
break;
* @brief Construct a new Error Notification object
*
*/
- explicit ErrorNotification() : cleanup_func([] {}) {}
+ explicit ErrorNotification() : cleanup_func() {}
+ /**
+ * @brief Construct a new Error Notification object
+ *
+ * @param cleanup_func_ clean up function
+ */
explicit ErrorNotification(std::function<void()> cleanup_func_) :
cleanup_func(cleanup_func_) {}
*
*/
~ErrorNotification() noexcept(false) {
- cleanup_func();
+ if (cleanup_func) {
+ cleanup_func();
+ }
throw Err(ss.str().c_str());
}
+ /**
+ * @brief Error notification stream wrapper
+ *
+ * @tparam T anything that will be delegated to the move
+ * @param out out stream
+ * @param e anything to pass to the stream
+ * @return ErrorNotification<Err>&& self
+ */
template <typename T>
friend ErrorNotification<Err> &&operator<<(ErrorNotification<Err> &&out,
T &&e) {
- out.ss << e;
+ out.ss << std::forward<T>(e);
return std::move(out);
}
using invalid_argument::invalid_argument;
};
+/**
+ * @brief derived class of invalid argument to represent permission is denied
+ */
+struct permission_denied : public std::invalid_argument {
+ using invalid_argument::invalid_argument;
+};
+
} // namespace exception
} // namespace nntrainer
#include <vector>
#include <ml-api-common.h>
+#include <nntrainer_error.h>
#include <props_util.h>
namespace nntrainer {
*/
unsigned int parseDataProperty(std::string property);
-/**
- * @brief print instance info. as <Type at (address)>
- * @param[in] std::ostream &out, T&& t
- * @param[in] t pointer to the instance
- */
-template <typename T,
- typename std::enable_if_t<std::is_pointer<T>::value, T> * = nullptr>
-void printInstance(std::ostream &out, const T &t) {
- out << '<' << typeid(*t).name() << " at " << t << '>' << std::endl;
-}
} /* namespace nntrainer */
#endif /* __cplusplus */
#define __UTIL_FUNC_H__
#ifdef __cplusplus
-#include <tensor.h>
+#include <cstring>
+#include <sstream>
+#include <nntrainer_error.h>
+#include <tensor.h>
namespace nntrainer {
/**
*/
bool endswith(const std::string &target, const std::string &suffix);
+/**
+ * @brief print instance info. as <Type at (address)>
+ * @param[in] std::ostream &out, T&& t
+ * @param[in] t pointer to the instance
+ */
+template <typename T,
+ typename std::enable_if_t<std::is_pointer<T>::value, T> * = nullptr>
+void printInstance(std::ostream &out, const T &t) {
+ out << '<' << typeid(*t).name() << " at " << t << '>' << std::endl;
+}
+
+/**
+ * @brief creat a stream, and if !stream.good() throw appropriate error code
+ * depending on @c errno
+ *
+ * @tparam T return type
+ * @param path path
+ * @param mode mode to open path
+ * @return T created stream
+ */
+template <typename T>
+T checkedOpenStream(const std::string &path, std::ios_base::openmode mode) {
+ T model_file(path, mode);
+ if (!model_file.good()) {
+ std::stringstream ss;
+ ss << "[parseutil] requested file not opened, file path: " << path
+ << " reason: " << std::strerror(errno);
+ if (errno == EPERM || errno == EACCES) {
+ throw nntrainer::exception::permission_denied(ss.str().c_str());
+ } else {
+ throw std::invalid_argument(ss.str().c_str());
+ }
+ }
+
+ return model_file;
+}
+
} /* namespace nntrainer */
#endif /* __cplusplus */