-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
Copy pathCollectionDataSourceTests.cs
209 lines (172 loc) · 6.76 KB
/
CollectionDataSourceTests.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using Microsoft.ML.Data;
using Microsoft.ML.Runtime;
using Microsoft.ML.Runtime.Api;
using Microsoft.ML.Runtime.Data;
using Microsoft.ML.TestFramework;
using Microsoft.ML.Trainers;
using Microsoft.ML.Transforms;
using System.Collections.Generic;
using System.Linq;
using Xunit;
using Xunit.Abstractions;
namespace Microsoft.ML.EntryPoints.Tests
{
public class CollectionDataSourceTests : BaseTestClass
{
public CollectionDataSourceTests(ITestOutputHelper output)
: base(output)
{
}
[Fact]
public void CheckConstructor()
{
Assert.NotNull(CollectionDataSource.Create(new List<Input>() { new Input { Number1 = 1, String1 = "1" } }));
Assert.NotNull(CollectionDataSource.Create(new Input[1] { new Input { Number1 = 1, String1 = "1" } }));
Assert.NotNull(CollectionDataSource.Create(new Input[1] { new Input { Number1 = 1, String1 = "1" } }.AsEnumerable()));
bool thrown = false;
try
{
CollectionDataSource.Create(new List<Input>());
}
catch
{
thrown = true;
}
Assert.True(thrown);
thrown = false;
try
{
CollectionDataSource.Create(new Input[0]);
}
catch
{
thrown = true;
}
Assert.True(thrown);
}
[Fact]
public void CanSuccessfullyApplyATransform()
{
var collection = CollectionDataSource.Create(new List<Input>() { new Input { Number1 = 1, String1 = "1" } });
using (var environment = new TlcEnvironment())
{
Experiment experiment = environment.CreateExperiment();
ILearningPipelineDataStep output = (ILearningPipelineDataStep)collection.ApplyStep(null, experiment);
Assert.NotNull(output.Data);
Assert.NotNull(output.Data.VarName);
Assert.Null(output.Model);
}
}
[Fact]
public void CanSuccessfullyEnumerated()
{
var collection = CollectionDataSource.Create(new List<Input>() {
new Input { Number1 = 1, String1 = "1" },
new Input { Number1 = 2, String1 = "2" },
new Input { Number1 = 3, String1 = "3" }
});
using (var environment = new TlcEnvironment())
{
Experiment experiment = environment.CreateExperiment();
ILearningPipelineDataStep output = collection.ApplyStep(null, experiment) as ILearningPipelineDataStep;
experiment.Compile();
collection.SetInput(environment, experiment);
experiment.Run();
IDataView data = experiment.GetOutput(output.Data);
Assert.NotNull(data);
using (var cursor = data.GetRowCursor((a => true)))
{
var IDGetter = cursor.GetGetter<float>(0);
var TextGetter = cursor.GetGetter<DvText>(1);
Assert.True(cursor.MoveNext());
float ID = 0;
IDGetter(ref ID);
Assert.Equal(1, ID);
DvText Text = new DvText();
TextGetter(ref Text);
Assert.Equal("1", Text.ToString());
Assert.True(cursor.MoveNext());
ID = 0;
IDGetter(ref ID);
Assert.Equal(2, ID);
Text = new DvText();
TextGetter(ref Text);
Assert.Equal("2", Text.ToString());
Assert.True(cursor.MoveNext());
ID = 0;
IDGetter(ref ID);
Assert.Equal(3, ID);
Text = new DvText();
TextGetter(ref Text);
Assert.Equal("3", Text.ToString());
Assert.False(cursor.MoveNext());
}
}
}
[Fact]
public void CanTrain()
{
var pipeline = new LearningPipeline();
var data = new List<IrisData>() {
new IrisData { SepalLength = 1f, SepalWidth = 1f, PetalLength=0.3f, PetalWidth=5.1f, Label=1},
new IrisData { SepalLength = 1f, SepalWidth = 1f, PetalLength=0.3f, PetalWidth=5.1f, Label=1},
new IrisData { SepalLength = 1.2f, SepalWidth = 0.5f, PetalLength=0.3f, PetalWidth=5.1f, Label=0}
};
var collection = CollectionDataSource.Create(data);
pipeline.Add(collection);
pipeline.Add(new ColumnConcatenator(outputColumn: "Features",
"SepalLength", "SepalWidth", "PetalLength", "PetalWidth"));
pipeline.Add(new StochasticDualCoordinateAscentClassifier());
PredictionModel<IrisData, IrisPrediction> model = pipeline.Train<IrisData, IrisPrediction>();
IrisPrediction prediction = model.Predict(new IrisData()
{
SepalLength = 3.3f,
SepalWidth = 1.6f,
PetalLength = 0.2f,
PetalWidth = 5.1f,
});
pipeline = new LearningPipeline();
collection = CollectionDataSource.Create(data.AsEnumerable());
pipeline.Add(collection);
pipeline.Add(new ColumnConcatenator(outputColumn: "Features",
"SepalLength", "SepalWidth", "PetalLength", "PetalWidth"));
pipeline.Add(new StochasticDualCoordinateAscentClassifier());
model = pipeline.Train<IrisData, IrisPrediction>();
prediction = model.Predict(new IrisData()
{
SepalLength = 3.3f,
SepalWidth = 1.6f,
PetalLength = 0.2f,
PetalWidth = 5.1f,
});
}
public class Input
{
[Column("0")]
public float Number1;
[Column("1")]
public string String1;
}
public class IrisData
{
[Column("0")]
public float Label;
[Column("1")]
public float SepalLength;
[Column("2")]
public float SepalWidth;
[Column("3")]
public float PetalLength;
[Column("4")]
public float PetalWidth;
}
public class IrisPrediction
{
[ColumnName("Score")]
public float[] PredictedLabels;
}
}
}