[MachineLearning.Train] Add SetOptimizer method to Model class
authorHyunil <hyunil46.park@samsung.com>
Thu, 23 Jun 2022 04:18:38 +0000 (13:18 +0900)
committerJijoong Moon <jijoong.moon@samsung.com>
Tue, 23 Aug 2022 05:50:26 +0000 (14:50 +0900)
- Add SetOptimizer(Optimizer optimizer)
- Add ml_train_model_set_optimizer() to interop

Signed-off-by: Hyunil <hyunil46.park@samsung.com>
src/Tizen.MachineLearning.Train/Interop/Interop.Model.cs
src/Tizen.MachineLearning.Train/Tizen.MachineLearning.Train/Model.cs

index 21f9b42..e383f91 100644 (file)
@@ -62,5 +62,10 @@ internal static partial class Interop
         [DllImport(Libraries.Nntrainer, EntryPoint = "ml_train_model_get_layer")]
         public static extern NNTrainerError GetLayer(IntPtr modelHandle, string layerName, out IntPtr layer);
 
+        /* int ml_train_model_set_optimizer(ml_train_model_h model, ml_train_optimizer_h optimizer) */
+        [DllImport(Libraries.Nntrainer, EntryPoint = "ml_train_model_set_optimizer")]
+        public static extern NNTrainerError SetOptimizer(IntPtr modelHandle, IntPtr optimizerHandle);
+
+
     }
 }
index f15fe28..fc66f3b 100644 (file)
@@ -249,7 +249,7 @@ namespace Tizen.MachineLearning.Train
             if (layer == null)
                 NNTrainer.CheckException(NNTrainerError.InvalidParameter, "layer instance is null");
             NNTrainerError ret = Interop.Model.AddLayer(handle, layer.GetHandle());
-            NNTrainer.CheckException(ret, "Failed to compile model");
+            NNTrainer.CheckException(ret, "Failed to add layer");
             Log.Info("MLT", $"AddLayer:\n{layer.GetHandle()}");
         }
 
@@ -271,5 +271,22 @@ namespace Tizen.MachineLearning.Train
             NNTrainerError ret = Interop.Model.GetLayer(handle, layerName, out layerHandle);
             NNTrainer.CheckException(ret, "Failed to get layer");
         }
+        /// <summary>
+        /// Sets the optimizer for the neural network model.
+        /// </summary>
+        /// <remarks>
+        /// Use this function to set neural network optimizer. This transfers
+        /// the ownership of the optimizer to the network. No need to destroy the
+        /// optimizer if it is to a model.
+        /// </remarks>
+        /// <param name="optimizer"> The instance of Optimizer class </param>
+        /// <since_tizen> 10 </since_tizen>
+        public void SetOptimizer(Optimizer optimizer)
+        {
+            if (optimizer == null)
+                NNTrainer.CheckException(NNTrainerError.InvalidOperation, "optimizer instance is null");
+            NNTrainerError ret = Interop.Model.SetOptimizer(handle, optimizer.GetHandle());
+            NNTrainer.CheckException(ret, "Failed to set optimizer");
+        }
     } 
 }