【发布时间】:2020-02-02 05:46:57
【问题描述】:
我有一个运行 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 docs 了解到,不支持 System.Reflection.Emit。所以我的问题是,有什么方法可以让我完成这项工作吗?
我知道 Jonathan Peppers 撰写的 Xamarin 书籍mentions 在“定义符号”字段中使用“NO_LCG”来消除相同的错误并使 Ninject 在 Xamarin.iOS 上工作。虽然这不适用于这种情况,但是有类似的方法可以解决吗?
【问题讨论】:
-
您是否尝试过 Xamarin.iOS 的
--interpretermtouch 选项以获得 Emit 支持? -
哦,太好了!我记得读过那篇文章devblogs.microsoft.com/xamarin/…,但没有意识到为什么我需要 Reflection.Emit 支持!刚刚测试过,它可以在模拟器和设备上正常工作!然而,它将简单的“欢迎使用 Xamarin Forms”应用程序大小增加到 234 mb,这似乎是因为“启用解释器时没有剥离 IL”。无论如何,我也许可以减小尺寸?
-
您可以使用
--interpreter=assemblyname,....仅允许解释器仅处理这些程序集,即仅将 IL 包含在这些程序集中。 -
@SushiHangover 很棒的建议,我不知道你从哪里得到的!在堆栈跟踪之后,我使用了
--interpreter=Microsoft.ML.ApiUtils,它继续工作,但几乎没有减少应用程序的大小。我还将链接器行为设置为全部链接并禁用所有调试和调试信息,它也几乎没有什么不同。