-
-
Notifications
You must be signed in to change notification settings - Fork 56
Open
Description
Whilst working with the tensor.Iterator functionality I found some strange behavior, when using iterator in a for loop like follows:
it = t.Iterator() // t is a tensor.Dense
for i, err := it.Start(); err == nil; i, err = it.Next() {
fmt.Printf("i = %v, coord = %v\n", i, it.Coord())
}- When using the iterator as stated above, the 'first' element of the tensor is always last in the iteration. This is not really a big issue if you want to iterate over all elements and order does not matter, but it is weird nonetheless. As an example consider the tensor [1, 2, 3, 4] with shape (2, 2). It will visit the elements in order:
i = 0, coord = [0 1] i = 1, coord = [1 0] i = 2, coord = [1 1] i = 3, coord = [0 0] <------------ should be first - When iterating over a vector or a scalar, the indices are off by one. Again same tensor, but as a vector (shape (4,)):
The same thing happens with 'scalar-like' tensors, like: [1] with shape (1, 1, 1):
i = 0, coord = [1] i = 1, coord = [2] i = 2, coord = [3] i = 3, coord = [4] <----------- gives index error when usedInterestingly, when the tensor is a vector/scalari = 0, coord = [1 0 0] <--------- also index error, should be [0 0 0] always for scalarsViewof a tensor that is not a vector/scalar (i.e. obtained by slicing), the issue does not happen.
What I found to work properly is to use the following for loop instead, but I don't think this is the intended way of iterating.
it = t.Iterator()
for it.Reset(); !it.Done(); it.Next() {
...
}Metadata
Metadata
Assignees
Labels
No labels