Skip to content

build diagonal batched tensor with unknown dimensions #1416

Answered by daniellga
japm48 asked this question in Q&A
Discussion options

You must be logged in to vote

I don't know if I understood correctly what it does, but can you check if this works? It probably lacks a lot of checks but the basic stuff is there I think. I also didn't manage to use exact_chunks with Zip and had to use zip instead... It looks like it doesn't work if matrix and diag don't have the same shape.

fn transform_dim(tuple: &[usize], m: usize) -> Vec<usize> {
    let n = tuple.len();
    tuple
        .iter()
        .enumerate()
        .map(|(i, &val)| if i < n - m { 1 } else { val })
        .collect()
}

#[cfg(test)]
mod tests {
    use ndarray::{ArrayBase, IxDyn};

    use crate::transform_dim;

    #[test]
    fn test_diag1() {
        use ndarray::Array;

        let di…

Replies: 1 comment 1 reply

Comment options

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

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