Beispiel #1
0
        //private static void RunFlatExampleTest()
        //{
        //    IntPtr vw = VowpalWabbitInterface.Initialize("-q st -d 0002.dat -f out");

        //    VowpalWabbitInterface.StartParser(vw, false);

        //    int count = 0;
        //    IntPtr example = IntPtr.Zero;
        //    while (IntPtr.Zero != (example = VowpalWabbitInterface.GetExample(vw)))
        //    {
        //        count++;
        //        // make example flat
        //        IntPtr flatec = VowpalWabbitInterface.Flatten_Example(vw, example);
        //        VowpalWabbitInterface.FinishExample(vw, example);
        //    }

        //    VowpalWabbitInterface.EndParser(vw);
        //    VowpalWabbitInterface.Finish(vw);
        //}

        private static void RunFlatExampleTestEx()
        {
            IntPtr vw = VowpalWabbitInterface.Initialize("-q st -d rcv1.train.raw.txt -f out");

            //IntPtr vw = VowpalWabbitInterface.Initialize("-q st -d 0002.dat -f out");

            VowpalWabbitInterface.StartParser(vw, false);

            uint stride = VowpalWabbitInterface.Get_Stride(vw);
            uint mask   = (1 << 18) - 1;

            int    count   = 0;
            IntPtr example = IntPtr.Zero;

            while (IntPtr.Zero != (example = VowpalWabbitInterface.GetExample(vw)))
            {
                count++;
                IntPtr flatec = IntPtr.Zero;
                // make example flat
                flatec = VowpalWabbitInterface.Flatten_Example(vw, example);
                if (IntPtr.Zero == flatec)
                {
                    continue;
                }
                VowpalWabbitInterface.FLAT_RAW_EXAMPLE flat = new VowpalWabbitInterface.FLAT_RAW_EXAMPLE();
                flat = (VowpalWabbitInterface.FLAT_RAW_EXAMPLE)Marshal.PtrToStructure(flatec, typeof(VowpalWabbitInterface.FLAT_RAW_EXAMPLE));

                // Get IntPtr data
                VowpalWabbitInterface.FLAT_EXAMPLE flatEx = new VowpalWabbitInterface.FLAT_EXAMPLE();

                flatEx.final_prediction = flat.final_prediction;
                flatEx.example_counter  = flat.example_counter;
                flatEx.ft_offset        = flat.ft_offset;
                flatEx.global_weight    = flat.global_weight;
                flatEx.num_features     = flat.num_features;

                flatEx.ld = new VowpalWabbitInterface.LABEL();
                flatEx.ld = (VowpalWabbitInterface.LABEL)Marshal.PtrToStructure(flat.ld, typeof(VowpalWabbitInterface.LABEL));
                if (flat.tag_len > 0)
                {
                    flatEx.tag = new byte[flat.tag_len];
                    Marshal.Copy(flat.tag, flatEx.tag, 0, flat.tag_len);
                }

                IList <int> indices = new List <int>();
                if (flat.num_features > 0)
                {
                    flatEx.feature_map = new VowpalWabbitInterface.FEATURE[flat.num_features];
                    int feature_size = Marshal.SizeOf(typeof(VowpalWabbitInterface.FEATURE));
                    for (int i = 0; i < (int)flat.num_features; i++)
                    {
                        IntPtr curfeaturePos = new IntPtr(flat.feature_map.ToInt32() + i * feature_size);
                        flatEx.feature_map[i] = (VowpalWabbitInterface.FEATURE)Marshal.PtrToStructure(curfeaturePos, typeof(VowpalWabbitInterface.FEATURE));

                        int val = ((int)(flatEx.feature_map[i].weight_index / stride)) & (int)mask;
                        indices.Add(val);

                        flatEx.feature_map[i].weight_index = (uint)val;
                    }
                }

                VowpalWabbitInterface.FreeFlattenExample(flatec);
                VowpalWabbitInterface.FinishExample(vw, example);
            }

            VowpalWabbitInterface.EndParser(vw);
            VowpalWabbitInterface.Finish(vw);
        }