An executing problem about the ML_NET Code At [samples/machine-learning/tutorials /TaxiFarePrediction/]
还没有人认领这个 Issue。
评估
- 难度
- 3/5
- 预计耗时
- 1-2 天
- 新手友好度
- 25/100
- Issue 类型
- 缺陷
- 描述清晰度
- 需要澄清
- 活跃度
- 停滞
- 技术栈
- csharp
调研方向
从 CreateAndRunPrediction.cs 开始,尤其关注 CreatePredictionEngine<TaxiTrip, TaxiTripFarePrediction> 调用,并在独立的 TaxiFarePrediction.Console 和 TaxiFarePrediction.Shared 项目中重现该异常。将该设置与所有代码文件都位于控制台项目中的正常工作安排进行比较;完成的标准是分离的项目结构能够在没有 PlatformNotSupportedException 的情况下运行。
由索引模型根据 Issue 内容生成。
描述
System information
- OS version/distro: Windows 10
- .NET Version (eg., dotnet --info): .NET8.0
STEP1
I created project [ TaxiFarePrediction.Console ] with net8.0 ;
it has codefile GlobalUsings.cs
global using Microsoft.ML;
global using Microsoft.ML.Data;
created code file Program.cs
TaxiFarePrediction.CreateAndRunPrediction.Execute();
STEP2
Then I created a shared project [ TaxiFarePrediction.Shared ]
created code file CreateAndRunPrediction.cs which is almostly same as the codefile : https://github.com/dotnet/samples/tree/main/machine-learning/tutorials/TaxiFarePrediction/Program.cs
CreateAndRunPrediction.cs :
namespace TaxiFarePrediction
{
public class CreateAndRunPrediction
{
// <Snippet2>
static readonly string _trainDataPath = Path.Combine(Environment.CurrentDirectory, "Data", "taxi-fare-train.csv");
static readonly string _testDataPath = Path.Combine(Environment.CurrentDirectory, "Data", "taxi-fare-test.csv");
static readonly string _modelPath = Path.Combine(Environment.CurrentDirectory, "Data", "Model.zip");
// </Snippet2>
public static void Execute( )
{
Console.WriteLine(Environment.CurrentDirectory);
// <Snippet3>
MLContext mlContext = new MLContext(seed: 0);
// </Snippet3>
// <Snippet5>
var model = Train(mlContext, _trainDataPath);
// </Snippet5>
// <Snippet14>
Evaluate(mlContext, model);
// </Snippet14>
// <Snippet20>
TestSinglePrediction(mlContext, model);
// </Snippet20>
}
public static ITransformer Train(MLContext mlContext, string dataPath)
{
// <Snippet6>
IDataView dataView = mlContext.Data.LoadFromTextFile<TaxiTrip>(dataPath, hasHeader: true, separatorChar: ',');
// </Snippet6>
// <Snippet7>
var pipeline = mlContext.Transforms.CopyColumns(outputColumnName: "Label", inputColumnName: "FareAmount")
// </Snippet7>
// <Snippet8>
.Append(mlContext.Transforms.Categorical.OneHotEncoding(outputColumnName: "VendorIdEncoded", inputColumnName: "VendorId"))
.Append(mlContext.Transforms.Categorical.OneHotEncoding(outputColumnName: "RateCodeEncoded", inputColumnName: "RateCode"))
.Append(mlContext.Transforms.Categorical.OneHotEncoding(outputColumnName: "PaymentTypeEncoded", inputColumnName: "PaymentType"))
// </Snippet8>
// <Snippet9>
.Append(mlContext.Transforms.Concatenate("Features", "VendorIdEncoded", "RateCodeEncoded", "PassengerCount", "TripDistance", "PaymentTypeEncoded"))
// </Snippet9>
// <Snippet10>
.Append(mlContext.Regression.Trainers.FastTree());
// </Snippet10>
Console.WriteLine("=============== Create and Train the Model ===============");
// <Snippet11>
var model = pipeline.Fit(dataView);
// </Snippet11>
Console.WriteLine("=============== End of training ===============");
Console.WriteLine();
// <Snippet12>
return model;
// </Snippet12>
}
private static void Evaluate(MLContext mlContext, ITransformer model)
{
// <Snippet15>
IDataView dataView = mlContext.Data.LoadFromTextFile<TaxiTrip>(_testDataPath, hasHeader: true, separatorChar: ',');
// </Snippet15>
// <Snippet16>
var predictions = model.Transform(dataView);
// </Snippet16>
// <Snippet17>
var metrics = mlContext.Regression.Evaluate(predictions, "Label", "Score");
// </Snippet17>
Console.WriteLine();
Console.WriteLine($"*************************************************");
Console.WriteLine($"* Model quality metrics evaluation ");
Console.WriteLine($"*------------------------------------------------");
// <Snippet18>
Console.WriteLine($"* RSquared Score: {metrics.RSquared:0.##}");
// </Snippet18>
// <Snippet19>
Console.WriteLine($"* Root Mean Squared Error: {metrics.RootMeanSquaredError:#.##}");
// </Snippet19>
Console.WriteLine($"*************************************************");
}
private static void TestSinglePrediction(MLContext mlContext, ITransformer model)
{
//Prediction test
// Create prediction function and make prediction.
// <Snippet22>
var predictionFunction = mlContext.Model.CreatePredictionEngine<TaxiTrip, TaxiTripFarePrediction>(model);
// </Snippet22>
//Sample:
//vendor_id,rate_code,passenger_count,trip_time_in_secs,trip_distance,payment_type,fare_amount
//VTS,1,1,1140,3.75,CRD,15.5
// <Snippet23>
var taxiTripSample = new TaxiTrip()
{
VendorId = "VTS",
RateCode = "1",
PassengerCount = 1,
TripTime = 1140,
TripDistance = 3.75f,
PaymentType = "CRD",
FareAmount = 0 // To predict. Actual/Observed = 15.5
};
// </Snippet23>
// <Snippet24>
var prediction = predictionFunction.Predict(taxiTripSample);
// </Snippet24>
// <Snippet25>
Console.WriteLine($"**********************************************************************");
Console.WriteLine($"Predicted fare: {prediction.FareAmount:0.####}, actual fare: 15.5");
Console.WriteLine($"**********************************************************************");
// </Snippet25>
}
}
}
TaxiTrip.cs
namespace TaxiFarePrediction;
public class TaxiTrip
{
[LoadColumn(0)]
public string VendorId;
[LoadColumn(1)]
public string RateCode;
[LoadColumn(2)]
public float PassengerCount;
[LoadColumn(3)]
public float TripTime;
[LoadColumn(4)]
public float TripDistance;
[LoadColumn(5)]
public string PaymentType;
[LoadColumn(6)]
public float FareAmount;
}
public class TaxiTripFarePrediction
{
[ColumnName("Score")]
public float FareAmount;
}
STEP3
project TaxiFarePrediction.Console reference the project TaxiFarePrediction.Shared
STEP4 error happen
When I built and executed the project TaxiFarePrediction.Console , a problem happend at the code :
var predictionFunction = mlContext.Model.CreatePredictionEngine<TaxiTrip, TaxiTripFarePrediction>(model);
AND the error infomation shows:
System.Reflection.TargetInvocationException:“Exception has been thrown by the target of an invocation.”
PlatformNotSupportedException: Dynamic code generation is not supported on this platform.
System.Reflection.Emit.AssemblyBuilder.ThrowDynamicCodeNotSupported()
System.Reflection.Emit.AssemblyBuilder.EnsureDynamicCodeSupported()
System.Reflection.Emit.DynamicMethod.Init(string, System.Reflection.MethodAttributes, System.Reflection.CallingConventions, System.Type, System.Type[], System.Type, System.Reflection.Module, bool, bool)
System.Reflection.Emit.DynamicMethod.DynamicMethod(string, System.Type, System.Type[], System.Type, bool)
Microsoft.ML.ApiUtils.GeneratePeek<TOwn, TRow, TValue>(System.Reflection.FieldInfo, System.Reflection.Emit.OpCode)
System.Reflection.MethodBaseInvoker.InvokeDirectByRefWithFewArgs(object, System.Span<object>, System.Reflection.BindingFlags)
otherwise, if I put all code files into the project TaxiFarePrediction.Console , the code would be smooth ;
SO, what's the problem about PlatformNotSupportedException , maybe my project structrue would cause some problem ?
- 主要语言
- C#
- 星标
- 3.7k
- 派生
- 5.1k
- 平均合并
- 19 小时 47 分钟
- 30 天内合并 PR
- 2
环境准备
- 没有 Dockerfile 或 Docker Compose 文件
- 有 Pull Request 模板
- 阅读贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
dotnet/samples 的其他 Issue
-
dotnet-target-version
难度 2/5 1-3 小时 新手友好度 77/100
-
dotnet-target-version
难度 2/5 1-3 小时 新手友好度 76/100
-
dotnet-target-version
难度 2/5 1-3 小时 新手友好度 78/100
-
Race condition in orleans/ShoppingCart/Grains/InventoryGrain.cs#L48可能已有人在做 @TimonPasslick 于 160 天前认领。 未关闭
难度 2/5 1-3 小时 新手友好度 72/100
-
dotnet-target-version
难度 2/5 1-3 小时 新手友好度 72/100
相似的 Issue
-
Audit by ear: boss warnings, other not-enough messages, ailments and barriers in a chapter 1 battle未关闭enhancement needs-game ready-for-agent
难度 2/5 1-3 小时 新手友好度 72/100
Sour-Dev-Home/metaphor-accessibility#1273 ·
维护者通常 1 天内回复
-
难度 2/5 1-3 小时 新手友好度 68/100
joinrpg/joinrpg-net#6336 ·
维护者通常 1 天内回复
-
难度 2/5 1-3 小时 新手友好度 76/100
muxinc/mux-csharp#88 ·
-
In-car displays show 0 km/h and P while the AV drives可能已有人在做 @bazilinskyy 今天认领。 未关闭
难度 2/5 1-3 小时 新手友好度 66/100
bazilinskyy/coupled-sim#366 ·
维护者通常 1 天内回复
-
难度 2/5 1-3 小时 新手友好度 76/100
维护者通常 3 天内回复