torch icon indicating copy to clipboard operation
torch copied to clipboard

`$movedim()` parameters source and destination are 0-indexed even if `torch_movedim()` ones are 1-indexed

Open cregouby opened this issue 3 years ago • 0 comments

Current behavour

$movedim() use 0-indexed dimensions for its parameters source and destination. This is not expected, create a lot of confusion is the {torch} perfect world where everything is 1-indexed:

t <- torch_randn(c(3,2,1))
t$movedim(0,1)
#> torch_tensor
#> (1,.,.) = 
#>  -0.4625
#>   1.0214
#>   1.1491
#> 
#> (2,.,.) = 
#>  -1.1606
#>  -0.5315
#>   0.4522
#> [ CPUFloatType{2,3,1} ]

For information, torch_movedim()source and destination are correctly 1-indexed

Expected behavor

$movedim() source and destination should be 1-indexed dimensions.

ReprEx

library(torch)
library(testthat)
t <- torch_randn(c(3,2,1))

# this is the expected behavior
expect_equal(t$movedim(1,2)$shape, torch_movedim(t, 1, 2)$shape)
#> Error: t$movedim(1, 2)$shape not equal to torch_movedim(t, 1, 2)$shape.
#> 3/3 mismatches (average diff: 1.33)
#> [1] 3 - 2 ==  1
#> [2] 1 - 3 == -2
#> [3] 2 - 1 ==  1

# the following is unexpected
expect_equal(t$movedim(0,1)$shape, torch_movedim(t, 1, 2)$shape)

Created on 2022-07-23 by the reprex package (v0.3.0)

cregouby avatar Jul 23 '22 15:07 cregouby