From 85015b0c94de2b042b16353c0ab9d6c74414d29c Mon Sep 17 00:00:00 2001 From: Hyunil Date: Wed, 15 Jun 2022 15:44:27 +0900 Subject: [PATCH] [MachineLearning.Train] Add initial Optimizer class - Create Optimizer.cs and Interop.Optimizer.cs for Optimizer class - Add Optimizer(NNTrainerOptimizerType type) class to Optimizer.cs - Add Destroy() to Dispose() - Add NNTrainerOptimizerType to Common.cs - Add ml_train_optimizer_create() to interop - Add ml_train_optimizer_destroy() to interop Signed-off-by: Hyunil --- .../Interop/Interop.optimizer.cs | 33 ++++++++ .../Tizen.MachineLearning.Train/Commons.cs | 20 +++++ .../Tizen.MachineLearning.Train/Optimizer.cs | 99 ++++++++++++++++++++++ 3 files changed, 152 insertions(+) create mode 100644 src/Tizen.MachineLearning.Train/Interop/Interop.optimizer.cs create mode 100644 src/Tizen.MachineLearning.Train/Tizen.MachineLearning.Train/Optimizer.cs diff --git a/src/Tizen.MachineLearning.Train/Interop/Interop.optimizer.cs b/src/Tizen.MachineLearning.Train/Interop/Interop.optimizer.cs new file mode 100644 index 0000000..5ebbf94 --- /dev/null +++ b/src/Tizen.MachineLearning.Train/Interop/Interop.optimizer.cs @@ -0,0 +1,33 @@ +/* + * Copyright (c) 2022 Samsung Electronics Co., Ltd All Rights Reserved + * + * Licensed under the Apache License, Version 2.0 (the License); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an AS IS BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +using System; +using System.Runtime.InteropServices; +using Tizen.MachineLearning.Train; + +internal static partial class Interop +{ + internal static partial class Optimizer + { + /* int ml_train_optimizer_create(ml_train_optimizer_h *optimizer, ml_train_optimizer_type_e type) */ + [DllImport(Libraries.Nntrainer, EntryPoint = "ml_train_optimizer_create")] + public static extern NNTrainerError Create(out IntPtr optimizerHandle, NNTrainerOptimizerType type); + + /* int ml_train_optimizer_destroy(ml_train_optimizer_h optimizer) */ + [DllImport(Libraries.Nntrainer, EntryPoint = "ml_train_optimizer_destroy")] + public static extern NNTrainerError Destroy(IntPtr optimizerHandle); + } +} \ No newline at end of file diff --git a/src/Tizen.MachineLearning.Train/Tizen.MachineLearning.Train/Commons.cs b/src/Tizen.MachineLearning.Train/Tizen.MachineLearning.Train/Commons.cs index ad203d2..966bafb 100644 --- a/src/Tizen.MachineLearning.Train/Tizen.MachineLearning.Train/Commons.cs +++ b/src/Tizen.MachineLearning.Train/Tizen.MachineLearning.Train/Commons.cs @@ -207,6 +207,26 @@ namespace Tizen.MachineLearning.Train Unknown = 999 } + /// + /// Enumeration for the neural network optimizer type of NNTrainer. + /// + /// 10 + public enum NNTrainerOptimizerType + { + /// + /// Adam Optimizer + /// + Adam = 0, + /// + /// Stochastic Gradient Descent Optimizer + /// + Sgd = 1, + /// + /// Unknown Optimizer + /// + Unknown = 999 + } + internal static class NNTrainer { diff --git a/src/Tizen.MachineLearning.Train/Tizen.MachineLearning.Train/Optimizer.cs b/src/Tizen.MachineLearning.Train/Tizen.MachineLearning.Train/Optimizer.cs new file mode 100644 index 0000000..9c9c789 --- /dev/null +++ b/src/Tizen.MachineLearning.Train/Tizen.MachineLearning.Train/Optimizer.cs @@ -0,0 +1,99 @@ +/* +* Copyright (c) 2022 Samsung Electronics Co., Ltd. All Rights Reserved. +* +* Licensed under the Apache License, Version 2.0 (the License); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an AS IS BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ +using static Interop; +using System; +using System.IO; + +namespace Tizen.MachineLearning.Train +{ + /// + /// Creates a neural network optimizer. + /// + /// + /// Use this function to create neural network optimizer. If not set to + /// model, optimizer should be released using Dispose(). + /// If set to a model, optimizer is available until model is released. + /// + /// 10 + public class Optimizer: IDisposable + { + private IntPtr handle = IntPtr.Zero; + private bool disposed = false; + + /// + /// Creates a neural network optimizer. + /// + /// The nntrainer optimizer type. + /// 10 + public Optimizer(NNTrainerOptimizerType type) + { + NNTrainerError ret = Interop.Optimizer.Create(out handle, type); + NNTrainer.CheckException(ret, "Failed to create optimizer instance"); + Log.Info(NNTrainer.Tag, $"Create optimizer with type:{type}"); + } + /// + /// Frees the neural network optimizer. + /// + /// 10 + /// + /// Use this function to destroy neural network optimizer. Fails if layer is owned by a model. + /// + ~Optimizer() + { + Dispose(false); + } + + /// + /// Releases any unmanaged resources used by this object. + /// + /// 10 + public void Dispose() + { + Dispose(true); + GC.SuppressFinalize(this); + } + + /// + /// Releases any unmanaged resources used by this object including opened handle. + /// + /// If true, disposes any disposable objects. If false, does not dispose disposable objects. + /// 10 + protected virtual void Dispose(bool disposing) + { + if (disposed) + return; + if (disposing) + { + // release managed object + } + // release unmanaged object + if (handle != IntPtr.Zero) + { + // Destroy the neural network layer. + NNTrainerError ret = Interop.Optimizer.Destroy(handle); + NNTrainer.CheckException(ret, "Failed to destroy optimizer instance"); + + handle = IntPtr.Zero; + } + disposed = true; + } + + internal IntPtr GetHandle() + { + return handle; + } + } +} -- 2.7.4