Skip to content

Newbie question: what is the corresponding method of mat[:, 1, :] with mat as a 3-D matrix in pyTorch? #1845

Answered by nathanielsimard
Magicloud asked this question in Q&A
Discussion options

You must be logged in to vote

I think it's a simple slice or slice_assign method call that you need. When using this function you need to specify the range included in every dimension:

let [d1, d2, d3] = mat.shape();
// to read the matrix, like `x = mat[:, 1, :]`
let x = mat.slice(0..d1, 0..1, 0..d3);

// to write to the matrix, like `mat[:, 1, :] = value` but it returns the results instead of mutating the tensor inplace.
let x = mat.slice_assign(0..d1, 0..1, 0..d3, value);

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@Magicloud
Comment options

Answer selected by Magicloud
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