-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Tensor extensions #4260
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
Merged
Merged
Tensor extensions #4260
Changes from all commits
Commits
Show all changes
21 commits
Select commit
Hold shift + click to select a range
c962327
Add TensorTypeExtensions
KsenijaS c1da00b
Upgrade performance by replacing ToArray method
KsenijaS 271947f
Fix failing tests
KsenijaS 5bf10d4
Remove unused code
KsenijaS edb4963
Remove duplicate file TensorTypeExtensions
KsenijaS 0b2c97e
Remove comented code
KsenijaS 8014a1e
Add exceptions
KsenijaS a9c8119
Add copyright header
KsenijaS 34bc241
Rename ToSpan to CopyTo
KsenijaS 3c3ebd8
Use Utils.EnsureSize method
KsenijaS 5029298
Use CopyTo method to copy the values to span
KsenijaS 97f1109
Fix Destination too short error
KsenijaS b2f3c78
Call CopyTo inside ToArray method to ensure code reuse
KsenijaS 00e18b8
No need to assign values span to dst
KsenijaS 3b7f984
Use checked keyword around cast to len
KsenijaS 5b1cee7
Set keepOld to false instead of true in Utils.EnsureSize
KsenijaS 606dad6
Add assert to ensure that imageArray and featurizedImage are the same…
KsenijaS 016079b
Cast tensor.size to int instead of long
KsenijaS fc5b6bc
Remove FetchData since it's no longer used
KsenijaS ecbfc65
Add checked keyword around cast
KsenijaS bc2fc7f
Merge https://github.com/dotnet/machinelearning into tensor_extensions
KsenijaS File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,51 @@ | ||
| // Licensed to the .NET Foundation under one or more agreements. | ||
| // The .NET Foundation licenses this file to you under the MIT license. | ||
| // See the LICENSE file in the project root for more information. | ||
|
|
||
| using System; | ||
| using Microsoft.ML.Internal.Utilities; | ||
| using NumSharp.Backends; | ||
| using NumSharp.Backends.Unmanaged; | ||
| using NumSharp.Utilities; | ||
| using Tensorflow; | ||
|
|
||
| namespace Microsoft.ML.Transforms | ||
| { | ||
| [BestFriend] | ||
| internal static class TensorTypeExtensions | ||
| { | ||
| public static void ToScalar<T>(this Tensor tensor, ref T dst) where T : unmanaged | ||
| { | ||
| if (typeof(T).as_dtype() != tensor.dtype) | ||
| throw new NotSupportedException(); | ||
|
|
||
| unsafe | ||
| { | ||
| dst = *(T*)tensor.buffer; | ||
| } | ||
|
|
||
| } | ||
|
|
||
| public static void CopyTo<T>(this Tensor tensor, Span<T> values) where T: unmanaged | ||
| { | ||
| if (typeof(T).as_dtype() != tensor.dtype) | ||
| throw new NotSupportedException(); | ||
|
|
||
| unsafe | ||
| { | ||
| var len = checked((int)tensor.size); | ||
| var src = (T*)tensor.buffer; | ||
| var span = new Span<T>(src, len); | ||
| span.CopyTo(values); | ||
| } | ||
| } | ||
|
|
||
| public static void ToArray<T>(this Tensor tensor, ref T[] array) where T : unmanaged | ||
| { | ||
| Utils.EnsureSize(ref array, (int)tensor.size, (int)tensor.size, false); | ||
| var span = new Span<T>(array); | ||
|
|
||
| CopyTo(tensor, span); | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.