Beispiel #1
0
        /// <summary>
        /// Performs one stochastic gradient update step based on the mini-batch of data and labels supplied.
        /// </summary>
        /// <typeparam name="T">The type of element in the matrix.</typeparam>
        /// <param name="trainer">The trainer object of <see cref="LossMetric"/>.</param>
        /// <param name="data">The training data.</param>
        /// <param name="label">The label.</param>
        /// <exception cref="ArgumentNullException"><paramref name="trainer"/>, <paramref name="data"/> or <paramref name="label"/> is null.</exception>
        /// <exception cref="ObjectDisposedException"><paramref name="trainer"/> is disposed.</exception>
        /// <exception cref="NotSupportedException">The specified type of element in the matrix does not supported.</exception>
        public static void TrainOneStep <T>(DnnTrainer <LossMetric> trainer, IEnumerable <Matrix <T> > data, IEnumerable <uint> label)
            where T : struct
        {
            if (trainer == null)
            {
                throw new ArgumentNullException(nameof(trainer));
            }
            if (data == null)
            {
                throw new ArgumentNullException(nameof(data));
            }
            if (label == null)
            {
                throw new ArgumentNullException(nameof(label));
            }

            trainer.ThrowIfDisposed();

            Matrix <T> .TryParse <T>(out var dataElementTypes);

            using (var dataVec = new StdVector <Matrix <T> >(data))
                using (var labelVec = new StdVector <uint>(label))
                {
                    var ret = NativeMethods.dnn_trainer_loss_metric_train_one_step(trainer.NativePtr,
                                                                                   trainer.Type,
                                                                                   dataElementTypes.ToNativeMatrixElementType(),
                                                                                   dataVec.NativePtr,
                                                                                   NativeMethods.MatrixElementType.UInt32,
                                                                                   labelVec.NativePtr);
                    Cuda.ThrowCudaException(ret);

                    switch (ret)
                    {
                    case NativeMethods.ErrorType.MatrixElementTypeNotSupport:
                        throw new NotSupportedException($"{dataElementTypes} does not support");
                    }
                }
        }
        public static void TestOneStep <T>(DnnTrainer <LossMulticlassLogPerPixel> trainer, IEnumerable <Matrix <T> > data, IEnumerable <Matrix <ushort> > label)
            where T : struct
        {
            if (trainer == null)
            {
                throw new ArgumentNullException(nameof(trainer));
            }
            if (data == null)
            {
                throw new ArgumentNullException(nameof(data));
            }
            if (label == null)
            {
                throw new ArgumentNullException(nameof(label));
            }

            Matrix <T> .TryParse <T>(out var dataElementTypes);

            using (var dataVec = new StdVector <Matrix <T> >(data))
                using (var labelVec = new StdVector <Matrix <ushort> >(label))
                {
                    var ret = NativeMethods.LossMulticlassLogPerPixel_trainer_test_one_step(trainer.Type,
                                                                                            trainer.NativePtr,
                                                                                            dataElementTypes.ToNativeMatrixElementType(),
                                                                                            dataVec.NativePtr,
                                                                                            NativeMethods.MatrixElementType.UInt32,
                                                                                            labelVec.NativePtr);
                    Cuda.ThrowCudaException(ret);

                    switch (ret)
                    {
                    case NativeMethods.ErrorType.MatrixElementTypeNotSupport:
                        throw new NotSupportedException($"{dataElementTypes} does not support");
                    }
                }
        }