[MachineLearning.Train] Add initial Optimizer class
authorHyunil <hyunil46.park@samsung.com>
Wed, 15 Jun 2022 06:44:27 +0000 (15:44 +0900)
committerJijoong Moon <jijoong.moon@samsung.com>
Tue, 23 Aug 2022 05:50:26 +0000 (14:50 +0900)
- 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 <hyunil46.park@samsung.com>
src/Tizen.MachineLearning.Train/Interop/Interop.optimizer.cs [new file with mode: 0644]
src/Tizen.MachineLearning.Train/Tizen.MachineLearning.Train/Commons.cs
src/Tizen.MachineLearning.Train/Tizen.MachineLearning.Train/Optimizer.cs [new file with mode: 0644]

diff --git a/src/Tizen.MachineLearning.Train/Interop/Interop.optimizer.cs b/src/Tizen.MachineLearning.Train/Interop/Interop.optimizer.cs
new file mode 100644 (file)
index 0000000..5ebbf94
--- /dev/null
@@ -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
index ad203d2..966bafb 100644 (file)
@@ -207,6 +207,26 @@ namespace Tizen.MachineLearning.Train
         Unknown = 999
     }
 
+    /// <summary>
+    /// Enumeration for the neural network optimizer type of NNTrainer.
+    /// </summary>
+    /// <since_tizen> 10 </since_tizen>
+    public enum NNTrainerOptimizerType
+    {
+        /// <summary>
+        /// Adam Optimizer
+        /// </summary>
+        Adam = 0,
+        /// <summary>
+        /// Stochastic Gradient Descent Optimizer
+        /// </summary>
+        Sgd = 1,
+        /// <summary>
+        /// Unknown Optimizer
+        /// </summary>
+        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 (file)
index 0000000..9c9c789
--- /dev/null
@@ -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
+{
+    /// <summary>
+    /// Creates a neural network optimizer.
+    /// </summary>
+    /// <remarks>
+    /// 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.
+    /// </remarks>
+    /// <since_tizen> 10 </since_tizen>
+    public class Optimizer: IDisposable
+    {
+        private IntPtr handle = IntPtr.Zero;
+        private bool disposed = false;
+
+        /// <summary>
+        /// Creates a neural network optimizer.
+        /// </summary>
+        /// <param name="type">The nntrainer optimizer type.</param>
+        /// <since_tizen> 10 </since_tizen>
+        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}");
+        }
+        /// <summary>
+        /// Frees the neural network optimizer.
+        /// </summary>
+        /// <since_tizen> 10 </since_tizen>
+        /// <remarks>
+        /// Use this function to destroy neural network optimizer. Fails if layer is owned by a model.
+        /// </remarks>
+        ~Optimizer()
+        {
+            Dispose(false);
+        }
+
+        /// <summary>
+        /// Releases any unmanaged resources used by this object.
+        /// </summary>
+        /// <since_tizen> 10 </since_tizen>
+        public void Dispose()
+        {
+            Dispose(true);
+            GC.SuppressFinalize(this);
+        }
+
+        /// <summary>
+        /// Releases any unmanaged resources used by this object including opened handle.
+        /// </summary>
+        /// <param name="disposing">If true, disposes any disposable objects. If false, does not dispose disposable objects.</param>
+        /// <since_tizen> 10 </since_tizen>
+        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;
+        }
+    } 
+}