ML.Net 正在处理 Xamarin.Android 但不处理 Xamarin.iOS:System.Reflection.Emit

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

我有一个 .NETStandard 2.1 解决方案 运行 一个 Xamarin.Forms 解决方案。在尝试进行一些 ML 工作(预测异常)时,我仅在 Xamarin.iOS.

中收到运行时异常

System.PlatformNotSupportedException 带有消息 "Operation is not supported on this platform." 这是我的代码:

// 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 docs了解到不支持System.Reflection.Emit。所以我的问题是,有什么方法可以让这项工作成功吗?

我知道 Jonathan Peppers 的 Xamarin 书,mentions 使用 "NO_LCG" 到 Define Symbols 字段来消除相同的错误并使 Ninject 在 [=37 上工作=].虽然这对这种情况不起作用,但是否有类似的方法可以解决它?

I couldn't figure out where you got that from!

如果您自己执行 mtouch,您会发现许多选项在标准 Microsoft 指南和 "experimental" 功能中没有详细记录,这是双重事实:

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.