An executing problem about the ML_NET Code At [samples/machine-learning/tutorials /TaxiFarePrediction/]

オープン
#6,974 コメント 1 件 リアクション 0 件 担当者 0 名 GitHub で見る

まだ誰も着手していません。

評価

難易度
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分
マージ済み PR(30日)
2

環境構築

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

dotnet/samples のほかの issue

dotnet/samples の issue をすべて見る

似ている issue

C# の issue をもっと見る

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。