Esempio n. 1
0
        /// <summary>Solves the problem using OWLQN.</summary>
        /// <remarks>
        /// Solves the problem using OWLQN.  The solution
        /// is stored in the
        /// <c>lambda</c>
        /// array of
        /// <c>prob</c>
        /// .  Note that the
        /// likelihood function will be a penalized L2 likelihood function unless you
        /// have turned this off via setting the priorSigmaS to 0.0.
        /// </remarks>
        /// <param name="weight">
        /// Controls the sparseness/regularization of the L1 solution.
        /// The bigger the number the sparser the solution.  Weights between
        /// 0.01 and 1.0 typically give good performance.
        /// </param>
        public virtual void SolveL1(double weight)
        {
            CGRunner.LikelihoodFunction df  = new CGRunner.LikelihoodFunction(prob, tol, useGaussianPrior, priorSigmaS, sigmaSquareds);
            IMinimizer <IDiffFunction>  owl = ReflectionLoading.LoadByReflection("edu.stanford.nlp.optimization.OWLQNMinimizer", weight);

            prob.lambda = owl.Minimize(df, tol, new double[df.DomainDimension()]);
            PrintOptimizationResults(df, null);
        }
Esempio n. 2
0
        private double[] TrainWeightsUsingNonLinearCRF(AbstractCachingDiffFunction func, IEvaluator[] evaluators)
        {
            IMinimizer <IDiffFunction> minimizer = GetMinimizer(0, evaluators);

            double[] initialWeights;
            if (flags.initialWeights == null)
            {
                initialWeights = func.Initial();
            }
            else
            {
                log.Info("Reading initial weights from file " + flags.initialWeights);
                try
                {
                    using (DataInputStream dis = new DataInputStream(new BufferedInputStream(new GZIPInputStream(new FileInputStream(flags.initialWeights)))))
                    {
                        initialWeights = ConvertByteArray.ReadDoubleArr(dis);
                    }
                }
                catch (IOException)
                {
                    throw new Exception("Could not read from double initial weight file " + flags.initialWeights);
                }
            }
            log.Info("numWeights: " + initialWeights.Length);
            if (flags.testObjFunction)
            {
                StochasticDiffFunctionTester tester = new StochasticDiffFunctionTester(func);
                if (tester.TestSumOfBatches(initialWeights, 1e-4))
                {
                    log.Info("Testing complete... exiting");
                    System.Environment.Exit(1);
                }
                else
                {
                    log.Info("Testing failed....exiting");
                    System.Environment.Exit(1);
                }
            }
            //check gradient
            if (flags.checkGradient)
            {
                if (func.GradientCheck())
                {
                    log.Info("gradient check passed");
                }
                else
                {
                    throw new Exception("gradient check failed");
                }
            }
            return(minimizer.Minimize(func, flags.tolerance, initialWeights));
        }
 /// <summary>
 /// <inheritDoc/>
 ///
 /// </summary>
 public virtual double[] Minimize(IDiffFunction function, double functionTolerance, double[] initial, int maxIterations)
 {
     double[] x = firstMinimizer.Minimize(function, functionTolerance, initial, iterationCutoff);
     return(secondMinimizer.Minimize(function, functionTolerance, x, maxIterations));
 }
Esempio n. 4
0
        protected internal override double[] TrainWeights(int[][][][] data, int[][] labels, IEvaluator[] evaluators, int pruneFeatureItr, double[][][][] featureVals)
        {
            int numFeatures  = featureIndex.Size();
            int numLopExpert = flags.numLopExpert;

            double[][] lopExpertWeights = new double[numLopExpert][];
            GetFeatureBoundaryIndices(numFeatures, numLopExpert);
            if (flags.initialLopWeights != null)
            {
                try
                {
                    using (BufferedReader br = IOUtils.ReaderFromString(flags.initialLopWeights))
                    {
                        log.Info("Reading initial LOP weights from file " + flags.initialLopWeights + " ...");
                        IList <double[]> listOfWeights = new List <double[]>(numLopExpert);
                        for (string line; (line = br.ReadLine()) != null;)
                        {
                            line = line.Trim();
                            string[] parts = line.Split("\t");
                            double[] wArr  = new double[parts.Length];
                            for (int i = 0; i < parts.Length; i++)
                            {
                                wArr[i] = double.ParseDouble(parts[i]);
                            }
                            listOfWeights.Add(wArr);
                        }
                        System.Diagnostics.Debug.Assert((listOfWeights.Count == numLopExpert));
                        log.Info("Done!");
                        for (int i_1 = 0; i_1 < numLopExpert; i_1++)
                        {
                            lopExpertWeights[i_1] = listOfWeights[i_1];
                        }
                    }
                }
                catch (IOException)
                {
                    // DataInputStream dis = new DataInputStream(new BufferedInputStream(new GZIPInputStream(new FileInputStream(
                    //     flags.initialLopWeights))));
                    // initialScales = Convert.readDoubleArr(dis);
                    throw new Exception("Could not read from double initial LOP weights file " + flags.initialLopWeights);
                }
            }
            else
            {
                for (int lopIter = 0; lopIter < numLopExpert; lopIter++)
                {
                    int[][][][] partialData = CreatePartialDataForLOP(lopIter, data);
                    if (flags.randomLopWeights)
                    {
                        lopExpertWeights[lopIter] = base.GetObjectiveFunction(partialData, labels).Initial();
                    }
                    else
                    {
                        lopExpertWeights[lopIter] = base.TrainWeights(partialData, labels, evaluators, pruneFeatureItr, null);
                    }
                }
                if (flags.includeFullCRFInLOP)
                {
                    double[][] newLopExpertWeights = new double[numLopExpert + 1][];
                    System.Array.Copy(lopExpertWeights, 0, newLopExpertWeights, 0, lopExpertWeights.Length);
                    if (flags.randomLopWeights)
                    {
                        newLopExpertWeights[numLopExpert] = base.GetObjectiveFunction(data, labels).Initial();
                    }
                    else
                    {
                        newLopExpertWeights[numLopExpert] = base.TrainWeights(data, labels, evaluators, pruneFeatureItr, null);
                    }
                    ICollection <int> newSet  = Generics.NewHashSet(numFeatures);
                    IList <int>       newList = new List <int>(numFeatures);
                    for (int fIndex = 0; fIndex < numFeatures; fIndex++)
                    {
                        newSet.Add(fIndex);
                        newList.Add(fIndex);
                    }
                    featureIndicesSetArray.Add(newSet);
                    featureIndicesListArray.Add(newList);
                    numLopExpert    += 1;
                    lopExpertWeights = newLopExpertWeights;
                }
            }
            // Dumb scales
            // double[] lopScales = new double[numLopExpert];
            // Arrays.fill(lopScales, 1.0);
            CRFLogConditionalObjectiveFunctionForLOP func = new CRFLogConditionalObjectiveFunctionForLOP(data, labels, lopExpertWeights, windowSize, classIndex, labelIndices, map, flags.backgroundSymbol, numLopExpert, featureIndicesSetArray, featureIndicesListArray
                                                                                                         , flags.backpropLopTraining);

            cliquePotentialFunctionHelper = func;
            IMinimizer <IDiffFunction> minimizer = GetMinimizer(0, evaluators);

            double[] initialScales;
            //TODO(mengqiu) clean this part up when backpropLogTraining == true
            if (flags.initialLopScales == null)
            {
                initialScales = func.Initial();
            }
            else
            {
                log.Info("Reading initial LOP scales from file " + flags.initialLopScales);
                try
                {
                    using (DataInputStream dis = new DataInputStream(new BufferedInputStream(new GZIPInputStream(new FileInputStream(flags.initialLopScales)))))
                    {
                        initialScales = ConvertByteArray.ReadDoubleArr(dis);
                    }
                }
                catch (IOException)
                {
                    throw new Exception("Could not read from double initial LOP scales file " + flags.initialLopScales);
                }
            }
            double[] learnedParams = minimizer.Minimize(func, flags.tolerance, initialScales);
            double[] rawScales     = func.SeparateLopScales(learnedParams);
            double[] lopScales     = ArrayMath.Softmax(rawScales);
            log.Info("After SoftMax Transformation, learned scales are:");
            for (int lopIter_1 = 0; lopIter_1 < numLopExpert; lopIter_1++)
            {
                log.Info("lopScales[" + lopIter_1 + "] = " + lopScales[lopIter_1]);
            }
            double[][] learnedLopExpertWeights = lopExpertWeights;
            if (flags.backpropLopTraining)
            {
                learnedLopExpertWeights = func.SeparateLopExpertWeights(learnedParams);
            }
            return(CRFLogConditionalObjectiveFunctionForLOP.CombineAndScaleLopWeights(numLopExpert, learnedLopExpertWeights, lopScales));
        }