2
using System.Collections.Generic;
7
using Microsoft.Research.MachineLearning;
8
using System.Runtime.InteropServices;
14
static void Main(string[] args)
21
private static void RunFeaturesTest()
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);
29
VowpalWabbitInterface.FEATURE_SPACE[] featureSpace = new VowpalWabbitInterface.FEATURE_SPACE[2];//maximum number of index spaces
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
34
GCHandle pinnedsFeatures = GCHandle.Alloc(sfeatures, GCHandleType.Pinned);
35
GCHandle pinnedtFeatures = GCHandle.Alloc(tfeatures, GCHandleType.Pinned);
37
featureSpace[0].features = pinnedsFeatures.AddrOfPinnedObject();
38
featureSpace[1].features = pinnedtFeatures.AddrOfPinnedObject();
40
GCHandle pinnedFeatureSpace = GCHandle.Alloc(featureSpace, GCHandleType.Pinned);
42
IntPtr featureSpacePtr = pinnedFeatureSpace.AddrOfPinnedObject();
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);
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);
53
sfeatures[2].weight_index = VowpalWabbitInterface.HashFeature(vw, "w^man", snum);
55
featureSpace[0].len = 3;
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);
61
tfeatures[1].weight_index = VowpalWabbitInterface.HashFeature(vw, "w^un", tnum);
63
tfeatures[2].weight_index = VowpalWabbitInterface.HashFeature(vw, "w^homme", tnum);
65
featureSpace[1].len = 3;
67
IntPtr importedExample = VowpalWabbitInterface.ImportExample(vw, featureSpacePtr, featureSpace.Length);
69
VowpalWabbitInterface.AddLabel(importedExample, 1);
71
score = VowpalWabbitInterface.Learn(vw, importedExample);
73
Console.Error.WriteLine("p2 = {0}", score);
75
VowpalWabbitInterface.Finish(vw);
77
// clean up the memory we allocated
78
pinnedsFeatures.Free();
79
pinnedtFeatures.Free();
80
pinnedFeatureSpace.Free();
83
private static void RunParserTest()
85
IntPtr vw = VowpalWabbitInterface.Initialize("-q st -d 0002.dat -f out");
87
VowpalWabbitInterface.StartParser(vw, false);
90
IntPtr example = IntPtr.Zero;
91
while (IntPtr.Zero != (example = VowpalWabbitInterface.GetExample(vw)))
93
example = VowpalWabbitInterface.GetExample(vw);
95
int featureSpaceLen = 0;
96
IntPtr featureSpacePtr = VowpalWabbitInterface.ExportExample(vw, example, ref featureSpaceLen);
98
VowpalWabbitInterface.FEATURE_SPACE[] featureSpace = new VowpalWabbitInterface.FEATURE_SPACE[featureSpaceLen];
99
int featureSpace_size = Marshal.SizeOf(typeof(VowpalWabbitInterface.FEATURE_SPACE));
101
for (int i = 0; i < featureSpaceLen; i++)
103
IntPtr curfeatureSpacePos = new IntPtr(featureSpacePtr.ToInt32() + i * featureSpace_size);
104
featureSpace[i] = (VowpalWabbitInterface.FEATURE_SPACE)Marshal.PtrToStructure(curfeatureSpacePos, typeof(VowpalWabbitInterface.FEATURE_SPACE));
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++)
110
IntPtr curfeaturePos = new IntPtr((featureSpace[i].features.ToInt32() + j * feature_size));
111
feature[j] = (VowpalWabbitInterface.FEATURE)Marshal.PtrToStructure(curfeaturePos, typeof(VowpalWabbitInterface.FEATURE));
114
VowpalWabbitInterface.ReleaseFeatureSpace(featureSpacePtr, featureSpaceLen);
117
float score = VowpalWabbitInterface.Learn(vw, example);
118
VowpalWabbitInterface.FinishExample(vw, example);
121
VowpalWabbitInterface.EndParser(vw);
123
VowpalWabbitInterface.Finish(vw);
127
private static void RunParserTest2()
129
long maxEx = 10; //long.MaxValue
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");
134
VowpalWabbitInterface.StartParser(vw, false);
137
IntPtr example = IntPtr.Zero;
138
while (count < maxEx)
140
example = VowpalWabbitInterface.GetExample(vw);
141
if (IntPtr.Zero == example)
147
//int featureSpaceLen = 0;
148
//IntPtr featureSpacePtr = VowpalWabbitInterface.ExportExample(vw, example, ref featureSpaceLen);
150
//VowpalWabbitInterface.FEATURE_SPACE[] featureSpace = new VowpalWabbitInterface.FEATURE_SPACE[featureSpaceLen];
151
//int featureSpace_size = Marshal.SizeOf(typeof(VowpalWabbitInterface.FEATURE_SPACE));
153
//for (int i = 0; i < featureSpaceLen; i++)
155
// IntPtr curfeatureSpacePos = new IntPtr(featureSpacePtr.ToInt32() + i * featureSpace_size);
156
// featureSpace[i] = (VowpalWabbitInterface.FEATURE_SPACE)Marshal.PtrToStructure(curfeatureSpacePos, typeof(VowpalWabbitInterface.FEATURE_SPACE));
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++)
162
// IntPtr curfeaturePos = new IntPtr((featureSpace[i].features.ToInt32() + j * feature_size));
163
// feature[j] = (VowpalWabbitInterface.FEATURE)Marshal.PtrToStructure(curfeaturePos, typeof(VowpalWabbitInterface.FEATURE));
166
//VowpalWabbitInterface.ReleaseFeatureSpace(featureSpacePtr, featureSpaceLen);
169
//float score = VowpalWabbitInterface.Learn(vw, example);
170
//VowpalWabbitInterface.FinishExample(vw, example);
173
VowpalWabbitInterface.EndParser(vw);
175
VowpalWabbitInterface.Finish(vw);