Пример #1
0
        public BatchTrainer()
        {
            BatchTrainerHelper.GetTypes <TScalar, TTrainer>(out _,
                                                            out var svmTrainerType,
                                                            out var svmKernelType,
                                                            out var sampleType);

            this._Parameter      = new KernelBaseParameter(svmKernelType, sampleType, 0, 0);
            this._SvmTrainerType = svmTrainerType;
            this._Bridge         = CreateBridge(this._Parameter, this._SvmTrainerType);
            var error = NativeMethods.batch_trainer_new(this._Parameter.KernelType.ToNativeKernelType(),
                                                        this._Parameter.SampleType.ToNativeMatrixElementType(),
                                                        svmTrainerType,
                                                        out var ret);

            switch (error)
            {
            case NativeMethods.ErrorType.MatrixElementTypeNotSupport:
                throw new ArgumentException($"{sampleType} is not supported.");

            case NativeMethods.ErrorType.SvmBatchTrainerNotSupport:
                throw new ArgumentException($"{svmTrainerType} is not supported.");

            case NativeMethods.ErrorType.SvmKernelNotSupport:
                throw new ArgumentException($"{svmKernelType} is not supported.");
            }

            this.NativePtr = ret;
        }
Пример #2
0
        internal BatchTrainer(IntPtr ptr,
                              bool isEnabledDispose = true) :
            base(isEnabledDispose)
        {
            BatchTrainerHelper.GetTypes <TScalar, TTrainer>(out _,
                                                            out var svmTrainerType,
                                                            out var svmKernelType,
                                                            out var sampleType);

            this._Parameter      = new KernelBaseParameter(svmKernelType, sampleType, 0, 0);
            this._SvmTrainerType = svmTrainerType;
            this._Bridge         = CreateBridge(this._Parameter, this._SvmTrainerType);
            this.NativePtr       = ptr;
        }
Пример #3
0
        public BatchTrainer(TTrainer trainer,
                            TScalar minLearningRate,
                            bool verbose,
                            bool useCache,
                            int cacheSize)
        {
            trainer.ThrowIfDisposed();

            BatchTrainerHelper.GetTypes <TScalar, TTrainer>(out _,
                                                            out var svmTrainerType,
                                                            out var svmKernelType,
                                                            out var sampleType);

            this._Parameter      = new KernelBaseParameter(svmKernelType, sampleType, 0, 0);
            this._SvmTrainerType = svmTrainerType;
            this._Bridge         = CreateBridge(this._Parameter, svmTrainerType);
            this.NativePtr       = this._Bridge.Create(trainer.NativePtr, minLearningRate, verbose, useCache, cacheSize);
        }
Пример #4
0
                public override IntPtr Operator(TTrainer trainer, float minLearningRate, int cacheSize)
                {
                    BatchTrainerHelper.GetTypes <TScalar, TTrainer>(out _,
                                                                    out var svmTrainerType,
                                                                    out var svmKernelType,
                                                                    out var sampleType);

                    if (!(minLearningRate > 0))
                    {
                        throw new ArgumentOutOfRangeException($"{nameof(minLearningRate)} must be greater than 0.");
                    }
                    if (!(cacheSize > 0))
                    {
                        throw new ArgumentOutOfRangeException($"{nameof(cacheSize)} must be greater than 0.");
                    }

                    var error = NativeMethods.verbose_batch_cached_float(svmKernelType.ToNativeKernelType(),
                                                                         sampleType.ToNativeMatrixElementType(),
                                                                         svmTrainerType,
                                                                         trainer.NativePtr,
                                                                         minLearningRate,
                                                                         cacheSize,
                                                                         out var ret);

                    switch (error)
                    {
                    case NativeMethods.ErrorType.MatrixElementTypeNotSupport:
                        throw new ArgumentException($"{sampleType} is not supported.");

                    case NativeMethods.ErrorType.SvmBatchTrainerNotSupport:
                        throw new ArgumentException($"{svmTrainerType} is not supported.");

                    case NativeMethods.ErrorType.SvmKernelNotSupport:
                        throw new ArgumentException($"{svmKernelType} is not supported.");
                    }

                    return(ret);
                }
Пример #5
0
            public static IntPtr Operator(TTrainer trainer, TScalar minLearningRate, int cacheSize)
            {
                BatchTrainerHelper.GetTypes <TScalar, TTrainer>(out _,
                                                                out _,
                                                                out _,
                                                                out var sampleType);
                Bridge <TScalar> bridge;

                switch (sampleType)
                {
                case MatrixElementTypes.Float:
                    bridge = new FloatBridge() as Bridge <TScalar>;
                    break;

                case MatrixElementTypes.Double:
                    bridge = new DoubleBridge() as Bridge <TScalar>;
                    break;

                default:
                    throw new NotSupportedException();
                }

                return(bridge.Operator(trainer, minLearningRate, cacheSize));
            }