~ubuntu-branches/ubuntu/vivid/vowpal-wabbit/vivid

« back to all changes in this revision

Viewing changes to cs_test/Program.cs

  • Committer: Package Import Robot
  • Author(s): Yaroslav Halchenko
  • Date: 2013-08-27 20:52:23 UTC
  • mfrom: (1.2.1) (7.1.2 experimental)
  • Revision ID: package-import@ubuntu.com-20130827205223-q005ps71tqinh25v
Tags: 7.3-1
New upstream release

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
using System;
 
2
using System.Collections.Generic;
 
3
using System.Linq;
 
4
using System.Text;
 
5
using System.IO;
 
6
 
 
7
using Microsoft.Research.MachineLearning;
 
8
using System.Runtime.InteropServices;
 
9
 
 
10
namespace cs_test
 
11
{
 
12
    class Program
 
13
    {
 
14
        static void Main(string[] args)
 
15
        {
 
16
            //RunFeaturesTest();
 
17
            //RunParserTest();
 
18
            RunParserTest2();
 
19
        }
 
20
 
 
21
        private static void RunFeaturesTest()
 
22
        {
 
23
            // this usually requires that the library script to update train.w or its moral equivalent needs to have been run 
 
24
            IntPtr vw = VowpalWabbitInterface.Initialize("-q st --noconstant --quiet");
 
25
            IntPtr example = VowpalWabbitInterface.ReadExample(vw, "1 |s p^the_man w^the w^man |t p^un_homme w^un w^homme");
 
26
            float score = VowpalWabbitInterface.Learn(vw, example);
 
27
            VowpalWabbitInterface.FinishExample(vw, example);
 
28
 
 
29
            VowpalWabbitInterface.FEATURE_SPACE[] featureSpace = new VowpalWabbitInterface.FEATURE_SPACE[2];//maximum number of index spaces
 
30
 
 
31
            VowpalWabbitInterface.FEATURE[] sfeatures = new VowpalWabbitInterface.FEATURE[3];// the maximum number of features
 
32
            VowpalWabbitInterface.FEATURE[] tfeatures = new VowpalWabbitInterface.FEATURE[3];// the maximum number of features
 
33
 
 
34
            GCHandle pinnedsFeatures = GCHandle.Alloc(sfeatures, GCHandleType.Pinned);
 
35
            GCHandle pinnedtFeatures = GCHandle.Alloc(tfeatures, GCHandleType.Pinned);
 
36
 
 
37
            featureSpace[0].features = pinnedsFeatures.AddrOfPinnedObject();
 
38
            featureSpace[1].features = pinnedtFeatures.AddrOfPinnedObject();
 
39
 
 
40
            GCHandle pinnedFeatureSpace = GCHandle.Alloc(featureSpace, GCHandleType.Pinned);
 
41
 
 
42
            IntPtr featureSpacePtr = pinnedFeatureSpace.AddrOfPinnedObject();
 
43
 
 
44
            uint snum = VowpalWabbitInterface.HashSpace(vw, "s");
 
45
            featureSpace[0].name = (byte)'s';
 
46
            sfeatures[0].weight_index = VowpalWabbitInterface.HashFeature(vw, "p^the_man", snum);
 
47
            sfeatures[0].x = 1;
 
48
            // add the character "delta" to test unicode
 
49
            // do it as a string to test the marshaling is doing pinning correctly.
 
50
            const string s = "w^thew^man\u0394";
 
51
            sfeatures[1].weight_index = VowpalWabbitInterface.HashFeature(vw, s, snum);
 
52
            sfeatures[1].x = 1;
 
53
            sfeatures[2].weight_index = VowpalWabbitInterface.HashFeature(vw, "w^man", snum);
 
54
            sfeatures[2].x = 1;
 
55
            featureSpace[0].len = 3;
 
56
 
 
57
            uint tnum = VowpalWabbitInterface.HashSpace(vw, "t");
 
58
            featureSpace[1].name = (byte)'t';
 
59
            tfeatures[0].weight_index = VowpalWabbitInterface.HashFeature(vw, "p^un_homme", tnum);
 
60
            tfeatures[0].x = 1;
 
61
            tfeatures[1].weight_index = VowpalWabbitInterface.HashFeature(vw, "w^un", tnum);
 
62
            tfeatures[1].x = 1;
 
63
            tfeatures[2].weight_index = VowpalWabbitInterface.HashFeature(vw, "w^homme", tnum);
 
64
            tfeatures[2].x = 1;
 
65
            featureSpace[1].len = 3;
 
66
 
 
67
            IntPtr importedExample = VowpalWabbitInterface.ImportExample(vw, featureSpacePtr, featureSpace.Length);
 
68
 
 
69
            VowpalWabbitInterface.AddLabel(importedExample, 1);
 
70
 
 
71
            score = VowpalWabbitInterface.Learn(vw, importedExample);
 
72
 
 
73
            Console.Error.WriteLine("p2 = {0}", score);
 
74
 
 
75
            VowpalWabbitInterface.Finish(vw);
 
76
 
 
77
            // clean up the memory we allocated
 
78
            pinnedsFeatures.Free();
 
79
            pinnedtFeatures.Free();
 
80
            pinnedFeatureSpace.Free();
 
81
        }
 
82
 
 
83
        private static void RunParserTest()
 
84
        {
 
85
            IntPtr vw = VowpalWabbitInterface.Initialize("-q st -d 0002.dat -f out");
 
86
 
 
87
            VowpalWabbitInterface.StartParser(vw, false);
 
88
 
 
89
            int count = 0;
 
90
            IntPtr example = IntPtr.Zero;
 
91
            while (IntPtr.Zero != (example = VowpalWabbitInterface.GetExample(vw)))
 
92
            {
 
93
                example = VowpalWabbitInterface.GetExample(vw);
 
94
                count++;
 
95
                int featureSpaceLen = 0;
 
96
                IntPtr featureSpacePtr = VowpalWabbitInterface.ExportExample(vw, example, ref featureSpaceLen);
 
97
 
 
98
                VowpalWabbitInterface.FEATURE_SPACE[] featureSpace = new VowpalWabbitInterface.FEATURE_SPACE[featureSpaceLen];
 
99
                int featureSpace_size = Marshal.SizeOf(typeof(VowpalWabbitInterface.FEATURE_SPACE));
 
100
 
 
101
                for (int i = 0; i < featureSpaceLen; i++)
 
102
                {
 
103
                    IntPtr curfeatureSpacePos = new IntPtr(featureSpacePtr.ToInt32() + i * featureSpace_size);
 
104
                    featureSpace[i] = (VowpalWabbitInterface.FEATURE_SPACE)Marshal.PtrToStructure(curfeatureSpacePos, typeof(VowpalWabbitInterface.FEATURE_SPACE));
 
105
 
 
106
                    VowpalWabbitInterface.FEATURE[] feature = new VowpalWabbitInterface.FEATURE[featureSpace[i].len];
 
107
                    int feature_size = Marshal.SizeOf(typeof(VowpalWabbitInterface.FEATURE));
 
108
                    for (int j = 0; j < featureSpace[i].len; j++)
 
109
                    {
 
110
                        IntPtr curfeaturePos = new IntPtr((featureSpace[i].features.ToInt32() + j * feature_size));
 
111
                        feature[j] = (VowpalWabbitInterface.FEATURE)Marshal.PtrToStructure(curfeaturePos, typeof(VowpalWabbitInterface.FEATURE));
 
112
                    }
 
113
                }
 
114
                VowpalWabbitInterface.ReleaseFeatureSpace(featureSpacePtr, featureSpaceLen);
 
115
 
 
116
 
 
117
                float score = VowpalWabbitInterface.Learn(vw, example);
 
118
                VowpalWabbitInterface.FinishExample(vw, example);
 
119
            }
 
120
 
 
121
            VowpalWabbitInterface.EndParser(vw);
 
122
 
 
123
            VowpalWabbitInterface.Finish(vw);
 
124
        }
 
125
 
 
126
 
 
127
        private static void RunParserTest2()
 
128
        {
 
129
            long maxEx = 10; //long.MaxValue
 
130
 
 
131
            //            IntPtr vw = VowpalWabbitInterface.Initialize("-q st -d 0002.dat -f out");
 
132
            IntPtr vw = VowpalWabbitInterface.Initialize("-q st -d 0002.dat -b 18 --hash strings -f out --ring_size 74748 --examples 10");
 
133
 
 
134
            VowpalWabbitInterface.StartParser(vw, false);
 
135
 
 
136
            int count = 0;
 
137
            IntPtr example = IntPtr.Zero;
 
138
            while (count < maxEx)
 
139
            {
 
140
                example = VowpalWabbitInterface.GetExample(vw);
 
141
                if (IntPtr.Zero == example)
 
142
                    break;
 
143
 
 
144
                count++;
 
145
 
 
146
 
 
147
                //int featureSpaceLen = 0;
 
148
                //IntPtr featureSpacePtr = VowpalWabbitInterface.ExportExample(vw, example, ref featureSpaceLen);
 
149
 
 
150
                //VowpalWabbitInterface.FEATURE_SPACE[] featureSpace = new VowpalWabbitInterface.FEATURE_SPACE[featureSpaceLen];
 
151
                //int featureSpace_size = Marshal.SizeOf(typeof(VowpalWabbitInterface.FEATURE_SPACE));
 
152
 
 
153
                //for (int i = 0; i < featureSpaceLen; i++)
 
154
                //{
 
155
                //    IntPtr curfeatureSpacePos = new IntPtr(featureSpacePtr.ToInt32() + i * featureSpace_size);
 
156
                //    featureSpace[i] = (VowpalWabbitInterface.FEATURE_SPACE)Marshal.PtrToStructure(curfeatureSpacePos, typeof(VowpalWabbitInterface.FEATURE_SPACE));
 
157
 
 
158
                //    VowpalWabbitInterface.FEATURE[] feature = new VowpalWabbitInterface.FEATURE[featureSpace[i].len];
 
159
                //    int feature_size = Marshal.SizeOf(typeof(VowpalWabbitInterface.FEATURE));
 
160
                //    for (int j = 0; j < featureSpace[i].len; j++)
 
161
                //    {
 
162
                //        IntPtr curfeaturePos = new IntPtr((featureSpace[i].features.ToInt32() + j * feature_size));
 
163
                //        feature[j] = (VowpalWabbitInterface.FEATURE)Marshal.PtrToStructure(curfeaturePos, typeof(VowpalWabbitInterface.FEATURE));
 
164
                //    }
 
165
                //}
 
166
                //VowpalWabbitInterface.ReleaseFeatureSpace(featureSpacePtr, featureSpaceLen);
 
167
 
 
168
 
 
169
                //float score = VowpalWabbitInterface.Learn(vw, example);
 
170
                //VowpalWabbitInterface.FinishExample(vw, example);
 
171
            }
 
172
 
 
173
            VowpalWabbitInterface.EndParser(vw);
 
174
 
 
175
            VowpalWabbitInterface.Finish(vw);
 
176
        }
 
177
 
 
178
    }
 
179
}