예제 #1
0
        public void Train(ILabeledExampleCollection <LblT, SparseVector <double> > dataset)
        {
            Utils.ThrowException(dataset == null ? new ArgumentNullException("dataset") : null);
            Utils.ThrowException(dataset.Count == 0 ? new ArgumentValueException("dataset") : null);
            Dispose();
            int[] trainSet = new int[dataset.Count];
            int[] labels   = new int[dataset.Count];
            int   j        = 0;

            foreach (LabeledExample <LblT, SparseVector <double> > lblEx in dataset)
            {
                SparseVector <double> vec = lblEx.Example;
                int[]   idx = new int[vec.Count];
                float[] val = new float[vec.Count];
                for (int i = 0; i < vec.Count; i++)
                {
                    idx[i] = vec.InnerIdx[i] + 1;
                    val[i] = (float)vec.InnerDat[i]; // *** loss of precision (double -> float)
                }
                int lbl;
                if (!mLblToId.TryGetValue(lblEx.Label, out lbl))
                {
                    mLblToId.Add(lblEx.Label, lbl = mLblToId.Count + 1);
                    mIdxToLbl.Add(lblEx.Label);
                }
                trainSet[j++] = SvmLightLib.NewFeatureVector(idx.Length, idx, val, lbl == 2 ? -1 : 1);
            }
            mModelId = SvmLightLib.TrainModel(string.Format(""), trainSet.Length, trainSet);
            // delete training vectors
            foreach (int vecIdx in trainSet)
            {
                SvmLightLib.DeleteFeatureVector(vecIdx);
            }
        }
예제 #2
0
        public void Train(ILabeledExampleCollection <LblT, SparseVector <double> > dataset)
        {
            Utils.ThrowException(dataset == null ? new ArgumentNullException("dataset") : null);
            Utils.ThrowException(dataset.Count == 0 ? new ArgumentValueException("dataset") : null);
            Dispose();
            int[] trainSet = new int[dataset.Count];
            int[] labels   = new int[dataset.Count];
            Dictionary <LblT, int> lblToIdx = new Dictionary <LblT, int>(mLblCmp);
            MultiSet <int>         lblCount = new MultiSet <int>();
            int j = 0;

            foreach (LabeledExample <LblT, SparseVector <double> > lblEx in dataset)
            {
                SparseVector <double> vec = lblEx.Example;
                int[]   idx = new int[vec.Count];
                float[] val = new float[vec.Count];
                for (int i = 0; i < vec.Count; i++)
                {
                    idx[i] = vec.InnerIdx[i] + 1;
                    val[i] = (float)vec.InnerDat[i]; // *** cast to float
                }
                int lbl;
                if (!lblToIdx.TryGetValue(lblEx.Label, out lbl))
                {
                    lblToIdx.Add(lblEx.Label, lbl = lblToIdx.Count);
                    mIdxToLbl.Add(lblEx.Label);
                }
                Utils.ThrowException(lbl == 2 ? new ArgumentValueException("dataset") : null);
                trainSet[j++] = SvmLightLib.NewFeatureVector(idx.Length, idx, val, lbl == 0 ? 1 : -1);
                lblCount.Add(lbl == 0 ? 1 : -1);
            }
            string costFactor = "";

            if (mBiasedCostFunction)
            {
                costFactor = "-j " + ((double)lblCount.GetCount(-1) / (double)lblCount.GetCount(1));
            }
            mModelId = SvmLightLib.TrainModel(string.Format(CultureInfo.InvariantCulture, "-v {0} -c {1} -t {2} -g {3} -d {4} -s {5} -r {6} -b {7} -e {8} -# {9} {10} {11}",
                                                            (int)mVerbosityLevel, mC, (int)mKernelType, mKernelParamGamma, mKernelParamD, mKernelParamS, mKernelParamC, mBiasedHyperplane ? 1 : 0,
                                                            mEps, mMaxIter, mCustomParams, costFactor), trainSet.Length, trainSet);
            // delete training vectors
            foreach (int vecIdx in trainSet)
            {
                SvmLightLib.DeleteFeatureVector(vecIdx);
            }
        }