Skip to content

How to use the slice syntax #982

Answered by NiklasGustafsson
eagle-k asked this question in Q&A
Discussion options

You must be logged in to vote

That depends on whether you're using .NET FX or .NET Core, since we don't have System.Range in the former. Assuming that you're using .NET Core:

var tensor = torch.ones(4, 4);
Console.WriteLine($"First row: {tensor[0].str()}");
Console.WriteLine($"First column: {tensor[.., 0].str()}");
Console.WriteLine($"Last column: {tensor[TensorIndex.Ellipsis, ^1].str()");
tensor[..,1] = 0;
Console.WriteLine(tensor.str());

The str() is necessary, because the default ToString() function does not print all the values, just the metadata. There's an overloaded ToString() that does the right thing, but I chose the str() extension method just for brevity's sake.

See the TorchSharp C# tutorials for more deta…

Replies: 4 comments 2 replies

Comment options

You must be logged in to vote
2 replies
@eagle-k
Comment options

@NiklasGustafsson
Comment options

Answer selected by eagle-k
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants