2
2
// The .NET Foundation licenses this file to you under the MIT license.
3
3
// See the LICENSE file in the project root for more information.
4
4
5
- using Float = System . Single ;
6
-
7
5
using Microsoft . ML . Runtime ;
8
6
using Microsoft . ML . Runtime . CommandLine ;
9
- using Microsoft . ML . Runtime . Data ;
7
+ using Microsoft . ML . Runtime . EntryPoints ;
10
8
using Microsoft . ML . Runtime . Internal . Utilities ;
11
9
using Microsoft . ML . Runtime . Model ;
10
+ using Microsoft . ML . Transforms ;
12
11
13
12
[ assembly: LoadableClass ( typeof ( GaussianFourierSampler ) , typeof ( GaussianFourierSampler . Arguments ) , typeof ( SignatureFourierDistributionSampler ) ,
14
13
"Gaussian Kernel" , GaussianFourierSampler . LoadName , "Gaussian" ) ]
25
24
"Laplacian Fourier Sampler Executor" , "LaplacianSamplerExecutor" , LaplacianFourierSampler . LoaderSignature ) ]
26
25
27
26
// REVIEW: Roll all of this in with the RffTransform.
28
- namespace Microsoft . ML . Runtime . Data
27
+ namespace Microsoft . ML . Transforms
29
28
{
30
29
/// <summary>
31
30
/// Signature for an IFourierDistributionSampler constructor.
32
31
/// </summary>
33
- public delegate void SignatureFourierDistributionSampler ( Float avgDist ) ;
32
+ public delegate void SignatureFourierDistributionSampler ( float avgDist ) ;
34
33
35
34
public interface IFourierDistributionSampler : ICanSaveModel
36
35
{
37
- Float Next ( IRandom rand ) ;
36
+ float Next ( IRandom rand ) ;
37
+ }
38
+
39
+ [ TlcModule . ComponentKind ( "FourierDistributionSampler" ) ]
40
+ public interface IFourierDistributionSamplerFactory : IComponentFactory < float , IFourierDistributionSampler >
41
+ {
38
42
}
39
43
40
44
public sealed class GaussianFourierSampler : IFourierDistributionSampler
41
45
{
42
46
private readonly IHost _host ;
43
47
44
- public class Arguments
48
+ public class Arguments : IFourierDistributionSamplerFactory
45
49
{
46
50
[ Argument ( ArgumentType . AtMostOnce , HelpText = "gamma in the kernel definition: exp(-gamma*||x-y||^2 / r^2). r is an estimate of the average intra-example distance" , ShortName = "g" ) ]
47
- public Float Gamma = 1 ;
51
+ public float Gamma = 1 ;
52
+
53
+ public IFourierDistributionSampler CreateComponent ( IHostEnvironment env , float avgDist ) => new GaussianFourierSampler ( env , this , avgDist ) ;
48
54
}
49
55
50
56
public const string LoaderSignature = "RandGaussFourierExec" ;
@@ -61,9 +67,9 @@ private static VersionInfo GetVersionInfo()
61
67
62
68
public const string LoadName = "GaussianRandom" ;
63
69
64
- private readonly Float _gamma ;
70
+ private readonly float _gamma ;
65
71
66
- public GaussianFourierSampler ( IHostEnvironment env , Arguments args , Float avgDist )
72
+ public GaussianFourierSampler ( IHostEnvironment env , Arguments args , float avgDist )
67
73
{
68
74
Contracts . CheckValue ( env , nameof ( env ) ) ;
69
75
_host = env . Register ( LoadName ) ;
@@ -91,7 +97,7 @@ private GaussianFourierSampler(IHostEnvironment env, ModelLoadContext ctx)
91
97
// Float: gamma
92
98
93
99
int cbFloat = ctx . Reader . ReadInt32 ( ) ;
94
- _host . CheckDecode ( cbFloat == sizeof ( Float ) ) ;
100
+ _host . CheckDecode ( cbFloat == sizeof ( float ) ) ;
95
101
96
102
_gamma = ctx . Reader . ReadFloat ( ) ;
97
103
_host . CheckDecode ( FloatUtils . IsFinite ( _gamma ) ) ;
@@ -105,23 +111,25 @@ public void Save(ModelSaveContext ctx)
105
111
// int: sizeof(Float)
106
112
// Float: gamma
107
113
108
- ctx . Writer . Write ( sizeof ( Float ) ) ;
114
+ ctx . Writer . Write ( sizeof ( float ) ) ;
109
115
_host . Assert ( FloatUtils . IsFinite ( _gamma ) ) ;
110
116
ctx . Writer . Write ( _gamma ) ;
111
117
}
112
118
113
- public Float Next ( IRandom rand )
119
+ public float Next ( IRandom rand )
114
120
{
115
- return ( Float ) Stats . SampleFromGaussian ( rand ) * MathUtils . Sqrt ( 2 * _gamma ) ;
121
+ return ( float ) Stats . SampleFromGaussian ( rand ) * MathUtils . Sqrt ( 2 * _gamma ) ;
116
122
}
117
123
}
118
124
119
125
public sealed class LaplacianFourierSampler : IFourierDistributionSampler
120
126
{
121
- public class Arguments
127
+ public class Arguments : IFourierDistributionSamplerFactory
122
128
{
123
129
[ Argument ( ArgumentType . AtMostOnce , HelpText = "a in the term exp(-a|x| / r). r is an estimate of the average intra-example L1 distance" ) ]
124
- public Float A = 1 ;
130
+ public float A = 1 ;
131
+
132
+ public IFourierDistributionSampler CreateComponent ( IHostEnvironment env , float avgDist ) => new LaplacianFourierSampler ( env , this , avgDist ) ;
125
133
}
126
134
127
135
private static VersionInfo GetVersionInfo ( )
@@ -139,9 +147,9 @@ private static VersionInfo GetVersionInfo()
139
147
public const string RegistrationName = "LaplacianRandom" ;
140
148
141
149
private readonly IHost _host ;
142
- private readonly Float _a ;
150
+ private readonly float _a ;
143
151
144
- public LaplacianFourierSampler ( IHostEnvironment env , Arguments args , Float avgDist )
152
+ public LaplacianFourierSampler ( IHostEnvironment env , Arguments args , float avgDist )
145
153
{
146
154
Contracts . CheckValue ( env , nameof ( env ) ) ;
147
155
_host = env . Register ( RegistrationName ) ;
@@ -170,7 +178,7 @@ private LaplacianFourierSampler(IHostEnvironment env, ModelLoadContext ctx)
170
178
// Float: a
171
179
172
180
int cbFloat = ctx . Reader . ReadInt32 ( ) ;
173
- _host . CheckDecode ( cbFloat == sizeof ( Float ) ) ;
181
+ _host . CheckDecode ( cbFloat == sizeof ( float ) ) ;
174
182
175
183
_a = ctx . Reader . ReadFloat ( ) ;
176
184
_host . CheckDecode ( FloatUtils . IsFinite ( _a ) ) ;
@@ -184,12 +192,12 @@ public void Save(ModelSaveContext ctx)
184
192
// int: sizeof(Float)
185
193
// Float: a
186
194
187
- ctx . Writer . Write ( sizeof ( Float ) ) ;
195
+ ctx . Writer . Write ( sizeof ( float ) ) ;
188
196
_host . Assert ( FloatUtils . IsFinite ( _a ) ) ;
189
197
ctx . Writer . Write ( _a ) ;
190
198
}
191
199
192
- public Float Next ( IRandom rand )
200
+ public float Next ( IRandom rand )
193
201
{
194
202
return _a * Stats . SampleFromCauchy ( rand ) ;
195
203
}
0 commit comments