简体   繁体   English

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

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

I have a .NETStandard 2.1 solution running a Xamarin.Forms solution.我有一个运行 Xamarin.Forms 解决方案的 .NETStandard 2.1 解决方案。 While trying to do some ML work (predicting anomalies), I am getting an runtime exception ONLY in Xamarin.iOS.在尝试执行一些 ML 工作(预测异常)时,我仅在 Xamarin.iOS 中遇到运行时异常。

System.PlatformNotSupportedException with a message that the "Operation is not supported on this platform." System.PlatformNotSupportedException带有“此平台不支持操作”的消息。 Here's my code:这是我的代码:

// 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}");
}

And these are the two model objects:这是两个模型对象:

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

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

Here's the stacktrace:这是堆栈跟踪:

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

I understand from the official Xamarin.iOS docs that System.Reflection.Emit is not supported.我从Xamarin.iOS官方文档中了解到 System.Reflection.Emit 不受支持。 So my question is that is there any way that I can make this work?所以我的问题是有什么方法可以使这项工作?

I know the Xamarin book by Jonathan Peppers, mentions to use "NO_LCG" to the Define Symbols field to get rid of the same error and make Ninject work on Xamarin.iOS.我知道 Jonathan Peppers 的 Xamarin 书, 提到在定义符号字段中使用“NO_LCG”来消除相同的错误并使 Ninject 在 Xamarin.iOS 上工作。 That didn't work for this case though, but is there something similar to go around it?虽然这对这种情况不起作用,但是有没有类似的东西可以解决它?

I couldn't figure out where you got that from!我不知道你从哪里得到的!

If you execute mtouch yourself, you can find a number of options that are not well documented in the standard Microsoft guides and for "experimental" features, this is doubly true:如果您自己执行mtouch ,您会发现许多在标准 Microsoft 指南和“实验性”功能中没有很好记录的选项,这是双重正确的:

mtouch --help mtouch --help

interpreter:口译员:

  --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.

相关问题 如何使用System.Reflection.Emit解决Xamarin.iOS中的MTOUCH(MT2002)错误 - How to solve MTOUCH (MT2002) error in Xamarin.iOS with System.Reflection.Emit 无法将Xamarin.Android和Xamarin.iOS添加到PCL - Unable to add Xamarin.Android and Xamarin.iOS to PCL Xamarin.iOS 和 Xamarin.Android 的 NuGet 包缩写是什么? - What are the NuGet package abbreviations for Xamarin.iOS and Xamarin.Android? 在Xamarin.Android/Xamarin.iOS中使用Akavache - Using Akavache in Xamarin.Android/Xamarin.iOS 是否可以将Xamarin.IOS或Xamarin.Android代码用于Xamarin Windows Phone开发 - Is It possible to use the Xamarin.IOS or Xamarin.Android code for Xamarin Windows Phone development 在xamarin.android/iOS中检查相机是否正常工作 - Check camera working or not in xamarin.android/iOS 无法使用Xamarin.Android / Xamarin.iOS创建可移植类库项目 - Can't Create Portable Class Library Project with Xamarin.Android / Xamarin.iOS 如何在Xamarin.Android和Xamarin.iOS上使用dllmap? - How can I use dllmap on Xamarin.Android and Xamarin.iOS? 如何将 CustomRenderer 用于 Xamarin.iOS 和 Xamarin.ZE84E30B9390CDB6DZDB6DB2CDB878 中的单个 ContentPage - How to use CustomRenderer for a single ContentPage in Xamarin.iOS and Xamarin.Android System.Reflection.Emit将代码添加到propertyInfo - System.Reflection.Emit adding code to propertyInfo
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM