Beispiel #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;
        }
Beispiel #2
0
        public static void GetTypes <TScalar, TTrainer>(out Type trainerType,
                                                        out NativeMethods.SvmBatchTrainerType svmTrainerType,
                                                        out SvmKernelType svmKernelType,
                                                        out MatrixElementTypes sampleType)
            where TScalar : struct
            where TTrainer : Trainer <TScalar>
        {
            trainerType = typeof(TTrainer);
            var svmTrainer = trainerType.GetGenericTypeDefinition();

            if (!BatchTrainerTypesRepository.Types.TryGetValue(svmTrainer, out svmTrainerType))
            {
                throw new ArgumentException();
            }

            var kernelType = trainerType.GenericTypeArguments[1].GetGenericTypeDefinition();

            if (!KernelTypesRepository.KernelTypes.TryGetValue(kernelType, out svmKernelType))
            {
                throw new ArgumentException();
            }

            var elementType = trainerType.GenericTypeArguments[0];

            if (!KernelTypesRepository.ElementTypes.TryGetValue(elementType, out sampleType))
            {
                throw new ArgumentException();
            }
        }
Beispiel #3
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;
        }
Beispiel #4
0
        private static Bridge <TScalar> CreateBridge(KernelBaseParameter parameter, NativeMethods.SvmBatchTrainerType trainerType)
        {
            switch (parameter.SampleType)
            {
            case MatrixElementTypes.Float:
                return(new FloatBridge(parameter, trainerType) as Bridge <TScalar>);

            case MatrixElementTypes.Double:
                return(new DoubleBridge(parameter, trainerType) as Bridge <TScalar>);

            default:
                throw new NotSupportedException();
            }
        }
Beispiel #5
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);
        }
Beispiel #6
0
 public DoubleBridge(KernelBaseParameter parameter, NativeMethods.SvmBatchTrainerType trainerType) :
     base(parameter, trainerType)
 {
 }
Beispiel #7
0
 protected Bridge(KernelBaseParameter parameter, NativeMethods.SvmBatchTrainerType trainerType)
 {
     this.Parameter   = parameter;
     this.TrainerType = trainerType;
 }