gorgonia/tensor

Slice bug "drop any dimension with size 1"

Closed this issue · 3 comments

@matdodgson the area to fix is this:

	newAP.lock()
	} else {

		// drop any dimension with size 1, except the last dimension
		for d := 0; d < dims; d++ {
			if newShape[d] == 1 /*&& d != t.dims-1  && dims > 2*/ {
				newShape = append(newShape[:d], newShape[d+1:]...)
				newStrides = append(newStrides[:d], newStrides[d+1:]...)
				d--
				dims--
			}
		}

		//fix up strides
		if newShape.IsColVec() {
			stride0 := newStrides[0]
			ReturnInts(newStrides)
			newStrides = BorrowInts(1)
			newStrides[0] = stride0
		}

		newAP = MakeAP(newShape, newStrides)
		newAP.o = order
	}

in AP.go.

The equivalent in Numpy is this:

a = np.arange(4).reshape(2,2)
a = a.reshape(1,1,2,2)
c = a[0, :, :, :]
c.shape # (1, 2, 2)
c.strides # (32, 16, 8) - the Go equivalent would be  []int{4, 2,1}

cebe4a0 fixes this