簡體   English   中英

ML.Net 在 Xamarin.Android 上工作但不在 Xamarin.iOS 上工作:System.Reflection.Emit

[英]ML.Net working on Xamarin.Android but not on Xamarin.iOS: System.Reflection.Emit

我有一個運行 Xamarin.Forms 解決方案的 .NETStandard 2.1 解決方案。 在嘗試執行一些 ML 工作(預測異常)時,我僅在 Xamarin.iOS 中遇到運行時異常。

System.PlatformNotSupportedException帶有“此平台不支持操作”的消息。 這是我的代碼:

// Dataset for ML
var amounts = new int[] { 100, 150, 200, 300, 250, 3000, 100, 250, 300, 250 };
var withdrawals = amounts.Select(amount => new Withdrawal { Amount = amount }).ToList();

// Instantiate ML context
var mlContext = new Microsoft.ML.MLContext();

// Create you algorithm
var estimator = mlContext.Transforms.DetectIidSpike( // "using ML;" needed for this statement
    outputColumnName: nameof(Prediction.Output),
    inputColumnName: nameof(Withdrawal.Amount),
    confidence: 99,
    pvalueHistoryLength: amounts.Length/2);

// Link data to algorithm
var amountsData = mlContext.Data.LoadFromEnumerable(withdrawals); // <=THE LINE THROWING THE EXCEPTION
var transformedAmountsData = estimator.Fit(amountsData).Transform(amountsData);

// Create output
var predictions = mlContext.Data.CreateEnumerable<Prediction>(transformedAmountsData, reuseRowObject:false).ToList();

foreach (var prediction in predictions)
{
    var isAnomaly = prediction.Output[0];
    var originalValue = prediction.Output[1];
    var confidenceLevel = prediction.Output[2];
    Console.WriteLine($"{originalValue} {confidenceLevel} {isAnomaly}");
}

這是兩個模型對象:

class Withdrawal
{
    public float Amount { get; set; }
}

class Prediction
{
    [Microsoft.ML.Data.VectorType]
    public double[] Output { get; set; }
}

這是堆棧跟蹤:

at System.Reflection.Emit.DynamicMethod..ctor (System.String name, System.Type returnType, System.Type[] parameterTypes, System.Type owner, System.Boolean skipVisibility) [0x00006] in /Library/Frameworks/Xamarin.iOS.framework/Versions/Current/src/Xamarin.iOS/mcs/class/corlib/System.Reflection.Emit/DynamicMethod.notsupported.cs:64 \\n at Microsoft.ML.ApiUtils.GeneratePeek[TOwn,TRow,TValue] (System.Reflection.PropertyInfo propertyInfo, System.Reflection.Emit.OpCode assignmentOpCode) [0x00040] in <ac1708cf77ce4a63b733a786896eec8e>:0 \\n at (wrapper managed-to-native) System.Reflection.RuntimeMethodInfo.InternalInvoke(System.Reflection.RuntimeMethodInfo,object,object[],System.Exception&)\\n at System.Reflection.RuntimeMethodInfo.Invoke (System.Object obj, System.Reflection.BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object[] parameters, System.Globalization.CultureInfo culture) [0x0006a] in /Library/Frameworks/Xamarin.iOS.framework/Versions/Current/src/Xamarin.iOS/mcs/class/corlib/System.Reflection/RuntimeMethodInfo.cs:395

我從Xamarin.iOS官方文檔中了解到 System.Reflection.Emit 不受支持。 所以我的問題是有什么方法可以使這項工作?

我知道 Jonathan Peppers 的 Xamarin 書, 提到在定義符號字段中使用“NO_LCG”來消除相同的錯誤並使 Ninject 在 Xamarin.iOS 上工作。 雖然這對這種情況不起作用,但是有沒有類似的東西可以解決它?

我不知道你從哪里得到的!

如果您自己執行mtouch ,您會發現許多在標准 Microsoft 指南和“實驗性”功能中沒有很好記錄的選項,這是雙重正確的:

mtouch --help

口譯員:

  --interpreter[=VALUE]  Enable the *experimental* interpreter. Optionally
                           takes a comma-separated list of assemblies to
                           interpret (if prefixed with a minus sign, the
                           assembly will be AOT-compiled instead). 'all'
                           can be used to specify all assemblies. This
                           argument can be specified multiple times.

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM