Ejemplo n.º 1
0
        public void ToIntTest()
        {
            byte[] intBuffer = null; // TODO: 初始化为适当的值
            int    expected  = 0;    // TODO: 初始化为适当的值
            int    actual;

            actual = ConvertByteArray.ToInt(intBuffer);
            Assert.AreEqual(expected, actual);
            Assert.Inconclusive("验证此测试方法的正确性。");
        }
Ejemplo n.º 2
0
        public void ToArrayInByteTest()
        {
            string strString = string.Empty; // TODO: 初始化为适当的值

            byte[] expected = null;          // TODO: 初始化为适当的值
            byte[] actual;
            actual = ConvertByteArray.ToArrayInByte(strString);
            Assert.AreEqual(expected, actual);
            Assert.Inconclusive("验证此测试方法的正确性。");
        }
Ejemplo n.º 3
0
        public void ToSByteTest()
        {
            byte[] sbyteBuffer = null; // TODO: 初始化为适当的值
            sbyte  expected    = 0;    // TODO: 初始化为适当的值
            sbyte  actual;

            actual = ConvertByteArray.ToSByte(sbyteBuffer);
            Assert.AreEqual(expected, actual);
            Assert.Inconclusive("验证此测试方法的正确性。");
        }
Ejemplo n.º 4
0
        public void ToLongTest()
        {
            byte[] longBuffer = null; // TODO: 初始化为适当的值
            long   expected   = 0;    // TODO: 初始化为适当的值
            long   actual;

            actual = ConvertByteArray.ToLong(longBuffer);
            Assert.AreEqual(expected, actual);
            Assert.Inconclusive("验证此测试方法的正确性。");
        }
Ejemplo n.º 5
0
        public void ToStringTest3()
        {
            byte[] byteString = null;         // TODO: 初始化为适当的值
            string expected   = string.Empty; // TODO: 初始化为适当的值
            string actual;

            actual = ConvertByteArray.ToString(byteString);
            Assert.AreEqual(expected, actual);
            Assert.Inconclusive("验证此测试方法的正确性。");
        }
Ejemplo n.º 6
0
        public void ToShortTest1()
        {
            byte[] shortBuffer = null; // TODO: 初始化为适当的值
            short  expected    = 0;    // TODO: 初始化为适当的值
            short  actual;

            actual = ConvertByteArray.ToShort(shortBuffer);
            Assert.AreEqual(expected, actual);
            Assert.Inconclusive("验证此测试方法的正确性。");
        }
Ejemplo n.º 7
0
 public void CoalitionTest()
 {
     byte[] thisBuffer   = null; // TODO: 初始化为适当的值
     byte[] concatBuffer = null; // TODO: 初始化为适当的值
     byte[] expected     = null; // TODO: 初始化为适当的值
     byte[] actual;
     actual = ConvertByteArray.Coalition(thisBuffer, concatBuffer);
     Assert.AreEqual(expected, actual);
     Assert.Inconclusive("验证此测试方法的正确性。");
 }
Ejemplo n.º 8
0
 public void ConcatTest()
 {
     byte[] bufferA  = null; // TODO: 初始化为适当的值
     byte[] bufferB  = null; // TODO: 初始化为适当的值
     byte[] expected = null; // TODO: 初始化为适当的值
     byte[] actual;
     actual = ConvertByteArray.Concat(bufferA, bufferB);
     Assert.AreEqual(expected, actual);
     Assert.Inconclusive("验证此测试方法的正确性。");
 }
Ejemplo n.º 9
0
        public void ToCharTest1()
        {
            byte[] charBuffer = null; // TODO: 初始化为适当的值
            char   expected   = '\0'; // TODO: 初始化为适当的值
            char   actual;

            actual = ConvertByteArray.ToChar(charBuffer);
            Assert.AreEqual(expected, actual);
            Assert.Inconclusive("验证此测试方法的正确性。");
        }
Ejemplo n.º 10
0
        public void ToByteTest()
        {
            byte[] byteBuffer   = null; // TODO: 初始化为适当的值
            int    iBufferIndex = 0;    // TODO: 初始化为适当的值
            byte   expected     = 0;    // TODO: 初始化为适当的值
            byte   actual;

            actual = ConvertByteArray.ToByte(byteBuffer, iBufferIndex);
            Assert.AreEqual(expected, actual);
            Assert.Inconclusive("验证此测试方法的正确性。");
        }
Ejemplo n.º 11
0
        public void ConvertToUIntTest1()
        {
            byte[] uintBuffer   = null; // TODO: 初始化为适当的值
            int    iBufferIndex = 0;    // TODO: 初始化为适当的值
            uint   expected     = 0;    // TODO: 初始化为适当的值
            uint   actual;

            actual = ConvertByteArray.ConvertToUInt(uintBuffer, iBufferIndex);
            Assert.AreEqual(expected, actual);
            Assert.Inconclusive("验证此测试方法的正确性。");
        }
Ejemplo n.º 12
0
        public void ToULongTest1()
        {
            byte[] ulongBuffer  = null; // TODO: 初始化为适当的值
            int    iBufferIndex = 0;    // TODO: 初始化为适当的值
            ulong  expected     = 0;    // TODO: 初始化为适当的值
            ulong  actual;

            actual = ConvertByteArray.ToULong(ulongBuffer, iBufferIndex);
            Assert.AreEqual(expected, actual);
            Assert.Inconclusive("验证此测试方法的正确性。");
        }
Ejemplo n.º 13
0
        public void ToByteArrayTest10()
        {
            char cChar = '\0';                // TODO: 初始化为适当的值

            byte[] byteBuffer         = null; // TODO: 初始化为适当的值
            byte[] byteBufferExpected = null; // TODO: 初始化为适当的值
            int    iBufferIndex       = 0;    // TODO: 初始化为适当的值

            ConvertByteArray.ToByteArray(cChar, ref byteBuffer, iBufferIndex);
            Assert.AreEqual(byteBufferExpected, byteBuffer);
            Assert.Inconclusive("无法验证不返回值的方法。");
        }
Ejemplo n.º 14
0
        public void ToByteArrayTest3()
        {
            long iLong = 0;                   // TODO: 初始化为适当的值

            byte[] byteBuffer         = null; // TODO: 初始化为适当的值
            byte[] byteBufferExpected = null; // TODO: 初始化为适当的值
            int    iBufferIndex       = 0;    // TODO: 初始化为适当的值

            ConvertByteArray.ToByteArray(iLong, ref byteBuffer, iBufferIndex);
            Assert.AreEqual(byteBufferExpected, byteBuffer);
            Assert.Inconclusive("无法验证不返回值的方法。");
        }
Ejemplo n.º 15
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));
        }
Ejemplo n.º 16
0
        public void ConvertToStringTest2()
        {
            byte[] byteString = null;         // TODO: 初始化为适当的值
            long   iOffset    = 0;            // TODO: 初始化为适当的值
            long   iSize      = 0;            // TODO: 初始化为适当的值
            string expected   = string.Empty; // TODO: 初始化为适当的值
            string actual;

            actual = ConvertByteArray.ConvertToString(byteString, iOffset, iSize);
            Assert.AreEqual(expected, actual);
            Assert.Inconclusive("验证此测试方法的正确性。");
        }
Ejemplo n.º 17
0
        public void ToArrayInByteTest4()
        {
            uint iUInt = 0;                   // TODO: 初始化为适当的值

            byte[] byteBuffer         = null; // TODO: 初始化为适当的值
            byte[] byteBufferExpected = null; // TODO: 初始化为适当的值
            int    iBufferIndex       = 0;    // TODO: 初始化为适当的值

            ConvertByteArray.ToArrayInByte(iUInt, ref byteBuffer, iBufferIndex);
            Assert.AreEqual(byteBufferExpected, byteBuffer);
            Assert.Inconclusive("无法验证不返回值的方法。");
        }
Ejemplo n.º 18
0
        public void Encrypted_data_should_not_match_original()
        {
            var data = new EncryptData {
                Date = DateTime.Now.Subtract(TimeSpan.FromDays(5)), SecretText = "I am a fan of Chopin."
            };
            const string password = "******";
            var          crypted  = new AesEncrypter(password).Encrypt(data);
            var          msg      =
                Should.Throw <SerializationException>(
                    () => ConvertByteArray.ByteArrayToObject <EncryptData>(crypted.CipherBytes)).Message;

            (msg.Contains("The input stream is not a valid binary format") || msg.Contains("does not contain a valid BinaryHeader")).ShouldBeTrue();
        }
        protected internal override double[] TrainWeights(int[][][][] data, int[][] labels, IEvaluator[] evaluators, int pruneFeatureItr, double[][][][] featureVals)
        {
            CRFLogConditionalObjectiveFloatFunction func = new CRFLogConditionalObjectiveFloatFunction(data, labels, windowSize, classIndex, labelIndices, map, flags.backgroundSymbol, flags.sigma);

            cliquePotentialFunctionHelper = func;
            QNMinimizer minimizer;

            if (flags.interimOutputFreq != 0)
            {
                IFloatFunction monitor = new ResultStoringFloatMonitor(flags.interimOutputFreq, flags.serializeTo);
                minimizer = new QNMinimizer(monitor);
            }
            else
            {
                minimizer = new QNMinimizer();
            }
            if (pruneFeatureItr == 0)
            {
                minimizer.SetM(flags.QNsize);
            }
            else
            {
                minimizer.SetM(flags.QNsize2);
            }
            float[] initialWeights;
            if (flags.initialWeights == null)
            {
                initialWeights = func.Initial();
            }
            else
            {
                try
                {
                    log.Info("Reading initial weights from file " + flags.initialWeights);
                    using (DataInputStream dis = new DataInputStream(new BufferedInputStream(new GZIPInputStream(new FileInputStream(flags.initialWeights)))))
                    {
                        initialWeights = ConvertByteArray.ReadFloatArr(dis);
                    }
                }
                catch (IOException)
                {
                    throw new Exception("Could not read from float initial weight file " + flags.initialWeights);
                }
            }
            log.Info("numWeights: " + initialWeights.Length);
            float[] weightsArray = minimizer.Minimize(func, (float)flags.tolerance, initialWeights);
            return(ArrayMath.FloatArrayToDoubleArray(weightsArray));
        }
Ejemplo n.º 20
0
        public void CoalitionTest1()
        {
            byte[] thisBuffer  = null; // TODO: 初始化为适当的值
            long   iThisOffset = 0;    // TODO: 初始化为适当的值
            long   iThisSize   = 0;    // TODO: 初始化为适当的值

            byte[] bufferB  = null;    // TODO: 初始化为适当的值
            long   iOffsetB = 0;       // TODO: 初始化为适当的值
            long   iSizeB   = 0;       // TODO: 初始化为适当的值

            byte[] expected = null;    // TODO: 初始化为适当的值
            byte[] actual;
            actual = ConvertByteArray.Coalition(thisBuffer, iThisOffset, iThisSize, bufferB, iOffsetB, iSizeB);
            Assert.AreEqual(expected, actual);
            Assert.Inconclusive("验证此测试方法的正确性。");
        }
Ejemplo n.º 21
0
        public void ConcatTest1()
        {
            byte[] bufferA  = null; // TODO: 初始化为适当的值
            long   iOffsetA = 0;    // TODO: 初始化为适当的值
            long   iSizeA   = 0;    // TODO: 初始化为适当的值

            byte[] bufferB  = null; // TODO: 初始化为适当的值
            long   iOffsetB = 0;    // TODO: 初始化为适当的值
            long   iSizeB   = 0;    // TODO: 初始化为适当的值

            byte[] expected = null; // TODO: 初始化为适当的值
            byte[] actual;
            actual = ConvertByteArray.Concat(bufferA, iOffsetA, iSizeA, bufferB, iOffsetB, iSizeB);
            Assert.AreEqual(expected, actual);
            Assert.Inconclusive("验证此测试方法的正确性。");
        }
Ejemplo n.º 22
0
 public virtual double ValueAt(double[] x)
 {
     if (++i % outputFreq == 0)
     {
         log.Info("Storing interim (double) weights to " + filename + " ... ");
         try
         {
             DataOutputStream dos = new DataOutputStream(new BufferedOutputStream(new GZIPOutputStream(new FileOutputStream(filename))));
             ConvertByteArray.SaveDoubleArr(dos, x);
             dos.Close();
         }
         catch (IOException)
         {
             log.Error("!");
             return(1);
         }
         log.Info("DONE.");
     }
     return(0);
 }
Ejemplo n.º 23
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));
        }