c# - ML.Net 适用于 Xamarin.Android,但不适用于 Xamarin.iOS : System. Reflection.Emit

标签 c# .net xamarin ml.net

我有一个运行 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 上工作。但这对于本例不起作用,但是有类似的方法可以解决这个问题吗?

最佳答案

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

如果您自己执行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.

关于c# - ML.Net 适用于 Xamarin.Android,但不适用于 Xamarin.iOS : System. Reflection.Emit,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60023555/

相关文章:

c# - 致命信号 6 (SIGABRT),tid 13929 (.AppTest.Project1) 中的代码 -6 (SI_TKILL),pid 13929 (.AppTest.Project1)

xamarin - 存档 Xamarin Android 应用程序会出现错误 XABLD7019

c# - 如何使用 Accord.NET 构建 RBF 内核 SVM 分类器,包括超参数优化调整?

c# - 插件模式 - IoC/DI 与否?

.net - 在代码中执行结果集分组,而不是在数据库级别

c# - 列出 FTP 目录中的前 N ​​个文件

android - 将 Xamarin 组件添加到共享代码

C# String.Trim() 不从 MailMessage.Subject 中删除字符

c# - 无法在 MonoDevelop 中打开 ASP.NET MVC 3 解决方案的测试项目

c# - 如何从列表中删除冗余顶点