Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change ML.NET to work with .NET Framework 4.6.1 #1075

Merged
merged 2 commits into from
Sep 28, 2018

Conversation

yaeldekel
Copy link

Fixes #1072 .

@@ -72,7 +72,7 @@ public EstimatorChain<TNewTrans> Append<TNewTrans>(IEstimator<TNewTrans> estimat
where TNewTrans : class, ITransformer
{
Contracts.CheckValue(estimator, nameof(estimator));
return new EstimatorChain<TNewTrans>(_estimators.Append(estimator).ToArray(), _scopes.Append(scope).ToArray());
return new EstimatorChain<TNewTrans>(_estimators.Concat(new[] { estimator }).ToArray(), _scopes.Concat(new[] { scope }).ToArray());
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure how concerned we are about perf in these methods, but another way would be to make our own AppendElement method:

public static T[] AppendElement(T[] array, T element)
{
    T[] result = new T[array.Length + 1];
    Array.Copy(array, result);
    result[array.Length] = element;
    return result;
}

Another option would be to use ImmutableCollections.... I don't think you'd have to pay a penalty for copying in that case. But I'm not sure we'd want to take a new dependency, if we aren't using ImmutableCollections already.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @eerhardt -- we are using ImmutableCollections, and I am beginning to use it in my code. I really like it a lot, I think we ought to use it more.

@@ -342,7 +342,7 @@ public CoefficientStatistics[] GetCoefficientStatistics(LinearBinaryPredictor pa
return null;

var order = GetUnorderedCoefficientStatistics(parent, schema).OrderByDescending(stat => stat.ZScore).Take(paramCountCap - 1);
return order.Prepend(new CoefficientStatistics("(Bias)", bias, stdError, zScore, pValue)).ToArray();
return order.Prepend(new[] { new CoefficientStatistics("(Bias)", bias, stdError, zScore, pValue) }).ToArray();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Which order.Prepend method is this?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, we have our own shim here: src\Microsoft.ML.Core\Data\MetadataUtils.cs

        /// <summary>
        /// Prepends a params array to an enumerable. Useful when implementing GetMetadataTypes.
        /// </summary>
        public static IEnumerable<T> Prepend<T>(this IEnumerable<T> tail, params T[] head)
        {
            return head.Concat(tail);
        }

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We have an overload of this method that takes a params T[], so this still works with older versions of .NET. I thought it would be better to call the same overload independent of the framework, so I changed it.


In reply to: 221026867 [](ancestors = 221026867)

Copy link
Member

@eerhardt eerhardt left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just a minor comment if we are concerned about perf, which I don't think we are in that method since it shouldn't be called often.

@@ -342,7 +342,7 @@ public CoefficientStatistics[] GetCoefficientStatistics(LinearBinaryPredictor pa
return null;

var order = GetUnorderedCoefficientStatistics(parent, schema).OrderByDescending(stat => stat.ZScore).Take(paramCountCap - 1);
return order.Prepend(new CoefficientStatistics("(Bias)", bias, stdError, zScore, pValue)).ToArray();
return order.Prepend(new[] { new CoefficientStatistics("(Bias)", bias, stdError, zScore, pValue) }).ToArray();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, we have our own shim here: src\Microsoft.ML.Core\Data\MetadataUtils.cs

        /// <summary>
        /// Prepends a params array to an enumerable. Useful when implementing GetMetadataTypes.
        /// </summary>
        public static IEnumerable<T> Prepend<T>(this IEnumerable<T> tail, params T[] head)
        {
            return head.Concat(tail);
        }

@justinormont
Copy link
Contributor

Closing/opening to re-check CI tests

Copy link
Contributor

@shauheen shauheen left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

:shipit:

Copy link
Member

@eerhardt eerhardt left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @yaeldekel

@yaeldekel yaeldekel merged commit 0e7f8c9 into dotnet:master Sep 28, 2018
@yaeldekel yaeldekel deleted the systemlinqappend branch September 28, 2018 15:01
@@ -75,7 +76,7 @@ private static MethodInfo[] InitValueTupleCreateMethods()
var methods = typeof(ValueTuple).GetMethods()
.Where(m => m.Name == methodName && m.ContainsGenericParameters)
.OrderBy(m => m.GetGenericArguments().Length).Take(7)
.Append(typeof(AnalyzeUtil).GetMethod(nameof(UnstructedCreate))).ToArray();
.ToArray().AppendElement(typeof(AnalyzeUtil).GetMethod(nameof(UnstructedCreate)));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It feels wrong to allocate an array and immediate throw it away to allocate one 1 element longer.

@@ -146,7 +146,7 @@ public TransformerChain<TNewLast> Append<TNewLast>(TNewLast transformer, Transfo
where TNewLast : class, ITransformer
{
Contracts.CheckValue(transformer, nameof(transformer));
return new TransformerChain<TNewLast>(_transformers.Append(transformer).ToArray(), _scopes.Append(scope).ToArray());
return new TransformerChain<TNewLast>(_transformers.AppendElement(transformer), _scopes.AppendElement(scope));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like this type only has enumerable constructor, so you end up with 3 arrays for each param. One that is size N, and two that are size N+1. If you keep it as an enumerable, you can reduce this. It can be further reduced if you have a constructor that takes an array with reference semantics.

It's also somewhat odd that TransformerChain has copy semantics for constructor params, but EstimatorChain has reference semantics (and thus doesn't have this array copy problem).

@ghost ghost locked as resolved and limited conversation to collaborators Mar 28, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

8 participants