提问人:Luis Agudo 提问时间:11/15/2023 更新时间:11/15/2023 访问量:12
ML.Net 加载模型时格式无效
ML.Net Invalid format when loading model
问:
我正在尝试实现一个 ML.Net 系统来预测价格,但在加载存储的模型时出现一个奇怪的错误。
这是我的培训代码:
private async Task TrainModel()
{
var historicPrices = await _priceRepository.GetPricesByQueryAsync(x => !x.Processed);
var mlContext = new MLContext(seed: 0);
var newData = mlContext.Data.LoadFromEnumerable(historicPrices.Select(p => new PriceForML
{
Value = (float)p.Value
}).ToList());
var trainer = mlContext.Forecasting.ForecastBySsa(
outputColumnName: nameof(ForecastResult.Forecast),
inputColumnName: nameof(PriceForML.Value),
windowSize: 60,
seriesLength: 60 * 60 * 24,
trainSize: 60 * 60 * 24,
horizon: 5,
confidenceLevel: 0.95f,
confidenceLowerBoundColumn: "ConfidenceLowerBound",
confidenceUpperBoundColumn: "ConfidenceUpperBound");
ITransformer trainedModel = trainer.Fit(newData);
mlContext.Model.Save(trainedModel, newData.Schema, $"MLModels/MinuteModel.zip");
}
这些是保存数据的模型:
public class PriceForML
{
public float Value { get; set; }
}
public class ForecastResult
{
public float[] Forecast { get; set; }
}
这是预测生成器的代码:
private async Task MakePredictionsMinuteAsync()
{
var lastPrice = await _priceRepository.GetLatestPriceByCoinIdAsync(coin.Id);
if (File.Exists($"MLModels/MinuteModel.zip") && lastPrice != null)
{
var mlContext = new MLContext(seed: 0);
ITransformer forecaster;
try
{
await using (var file = File.OpenRead($"MLModels/MinuteModel.zip"))
{
forecaster = mlContext.Model.Load(file, out DataViewSchema schema);
}
}
catch (Exception e)
{
_logger.LogError(e.Message);
continue;
}
var predictions = forecaster.CreateTimeSeriesEngine<PriceForML, ForecastResult>(mlContext).Predict(new PriceForML() { Value = (float)lastPrice.Value });
if(predictions.Forecast == null) continue;
var predictionTime = DateTime.UtcNow;
var predictionsToSave = new List<Prediction>();
for (int i = 1; i < predictions.Forecast.Length; i++)
{
var predictionDT = predictionTime.AddMinutes(i);
var prediction = new Prediction
{
CoinId = coin.Id,
PredictedValue = (decimal)predictions.Forecast[i],
PredictionDateTime = new DateTime(predictionDT.Year, predictionDT.Month, predictionDT.Day, predictionDT.Hour, predictionDT.Minute, 0),
Interval = IntervalTypes.Minute,
CreatedDate = DateTime.UtcNow
};
predictionsToSave.Add(prediction);
}
await _predictionRepository.SetPredictionsAsync(predictionsToSave);
}
}
我用 60 个条目训练了模型,它“正确”生成了 .zip。但是当我尝试创建预测时,它到达了线
forecaster = mlContext.Model.Load(file, out DataViewSchema schema);
它引发以下异常:
2023-11-15 16:15:23.6143|0|ERROR|Microsoft.Extensions.Hosting.Internal.Host|BackgroundService failed System.InvalidOperationException: Error during class instantiation
---> System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation.
---> System.InvalidOperationException: Error during class instantiation
---> System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation.
---> System.FormatException: One of the identified items was in an invalid format.
at Microsoft.ML.Transforms.TimeSeries.AdaptiveSingularSpectrumSequenceModelerInternal..ctor(IHostEnvironment env, ModelLoadContext ctx)
--- End of inner exception stack trace ---
at System.RuntimeMethodHandle.InvokeMethod(Object target, Span`1& arguments, Signature sig, Boolean constructor, Boolean wrapExceptions)
at System.Reflection.RuntimeConstructorInfo.Invoke(BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
at Microsoft.ML.Runtime.ComponentCatalog.LoadableClassInfo.CreateInstanceCore(Object[] ctorArgs)
--- End of inner exception stack trace ---
at Microsoft.ML.Runtime.ComponentCatalog.LoadableClassInfo.CreateInstanceCore(Object[] ctorArgs)
at Microsoft.ML.Runtime.ComponentCatalog.TryCreateInstance[TRes](IHostEnvironment env, Type signatureType, TRes& result, String name, String options, Object[] extra)
at Microsoft.ML.Runtime.ComponentCatalog.TryCreateInstance[TRes,TSig](IHostEnvironment env, TRes& result, String name, String options, Object[] extra)
at Microsoft.ML.ModelLoadContext.TryLoadModelCore[TRes,TSig](IHostEnvironment env, TRes& result, Object[] extra)
at Microsoft.ML.ModelLoadContext.TryLoadModel[TRes,TSig](IHostEnvironment env, TRes& result, RepositoryReader rep, Entry ent, String dir, Object[] extra)
at Microsoft.ML.ModelLoadContext.LoadModel[TRes,TSig](IHostEnvironment env, TRes& result, RepositoryReader rep, Entry ent, String dir, Object[] extra)
at Microsoft.ML.ModelLoadContext.LoadModelOrNull[TRes,TSig](IHostEnvironment env, TRes& result, RepositoryReader rep, String dir, Object[] extra)
at Microsoft.ML.ModelLoadContext.LoadModel[TRes,TSig](IHostEnvironment env, TRes& result, String name, Object[] extra)
at Microsoft.ML.Transforms.TimeSeries.SsaForecastingBaseWrapper.SsaForecastingBase..ctor(IHostEnvironment env, ModelLoadContext ctx, String name)
at Microsoft.ML.Transforms.TimeSeries.SsaForecastingTransformer.Create(IHostEnvironment env, ModelLoadContext ctx)
--- End of inner exception stack trace ---
at System.RuntimeMethodHandle.InvokeMethod(Object target, Span`1& arguments, Signature sig, Boolean constructor, Boolean wrapExceptions)
at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
at Microsoft.ML.Runtime.ComponentCatalog.LoadableClassInfo.CreateInstanceCore(Object[] ctorArgs)
--- End of inner exception stack trace ---
at Microsoft.ML.Runtime.ComponentCatalog.LoadableClassInfo.CreateInstanceCore(Object[] ctorArgs)
at Microsoft.ML.Runtime.ComponentCatalog.TryCreateInstance[TRes](IHostEnvironment env, Type signatureType, TRes& result, String name, String options, Object[] extra)
at Microsoft.ML.Runtime.ComponentCatalog.TryCreateInstance[TRes,TSig](IHostEnvironment env, TRes& result, String name, String options, Object[] extra)
at Microsoft.ML.ModelLoadContext.TryLoadModelCore[TRes,TSig](IHostEnvironment env, TRes& result, Object[] extra)
at Microsoft.ML.ModelLoadContext.TryLoadModel[TRes,TSig](IHostEnvironment env, TRes& result, RepositoryReader rep, Entry ent, String dir, Object[] extra)
at Microsoft.ML.ModelLoadContext.LoadModel[TRes,TSig](IHostEnvironment env, TRes& result, RepositoryReader rep, Entry ent, String dir, Object[] extra)
at Microsoft.ML.ModelLoadContext.LoadModelOrNull[TRes,TSig](IHostEnvironment env, TRes& result, RepositoryReader rep, String dir, Object[] extra)
at Microsoft.ML.ModelLoadContext.LoadModel[TRes,TSig](IHostEnvironment env, TRes& result, RepositoryReader rep, String dir, Object[] extra)
at Microsoft.ML.ModelOperationsCatalog.Load(Stream stream, DataViewSchema& inputSchema)
at Midas.API.Services.PitiasService.MakePredictionsMinuteAsync() in C:\Users\c-lsegrelles\source\repos\Midas\Midas.API\Services\PitiasService.cs:line 460
at Midas.API.Services.PitiasService.MakePredictionsMinuteAsync() in C:\Users\c-lsegrelles\source\repos\Midas\Midas.API\Services\PitiasService.cs:line 461
at Midas.API.Services.PitiasService.MakePredictionsAsync() in C:\Users\c-lsegrelles\source\repos\Midas\Midas.API\Services\PitiasService.cs:line 58
at Midas.API.Services.OrchestratorService.TrainModelAndMakePredictions() in C:\Users\c-lsegrelles\source\repos\Midas\Midas.API\Services\OrchestratorService.cs:line 70
at Midas.API.Services.OrchestratorService.ExecuteAsync(CancellationToken stoppingToken) in C:\Users\c-lsegrelles\source\repos\Midas\Midas.API\Services\OrchestratorService.cs:line 40
at Microsoft.Extensions.Hosting.Internal.Host.TryExecuteBackgroundServiceAsync(BackgroundService backgroundService)
有没有人知道哪个“项目”是例外所说的那个(“其中一个已识别的项目格式无效。或者我怎样才能知道发生了什么?
非常感谢您的帮助。
答: 暂无答案
评论