-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathPooledPredictionEnginePolicy.cs
39 lines (32 loc) · 1.17 KB
/
PooledPredictionEnginePolicy.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
using Microsoft.Extensions.ObjectPool;
using Microsoft.ML;
namespace Scalable.Model.Engine
{
public class PooledPredictionEnginePolicy<TData, TPrediction> : IPooledObjectPolicy<PredictionEngine<TData, TPrediction>>
where TData : class
where TPrediction : class, new()
{
private readonly MLContext _mlContext;
private readonly ITransformer _model;
public PooledPredictionEnginePolicy(MLContext mlContext, ITransformer model)
{
_mlContext = mlContext;
_model = model;
}
public PredictionEngine<TData, TPrediction> Create()
{
// Measuring CreatePredictionengine() time
var watch = System.Diagnostics.Stopwatch.StartNew();
var predictionEngine = _mlContext.Model.CreatePredictionEngine<TData, TPrediction>(_model);
watch.Stop();
long elapsedMs = watch.ElapsedMilliseconds;
return predictionEngine;
}
public bool Return(PredictionEngine<TData, TPrediction> obj)
{
if (obj == null)
return false;
return true;
}
}
}