chickadee » nanograd » slice-tensor

slice-tensor tensor start lengthprocedure

Extracts a slice of a tensor along the first dimension. Gradients flow back correctly to the original tensor positions.

tensor
input tensor with shape (n, ...)
start
starting index (0-based)
length
number of elements to extract
Returns
tensor with shape (length, ...)
;; Slice a batch of data
(define batch-data (make-tensor32 (make-f32vector 100) '(10 10)))
(define mini-batch (slice-tensor batch-data 2 5))  ; Shape: (5, 10)

;; Gradients flow back to original positions
(backward! (sum-tensor mini-batch))
(tensor-grad batch-data)  ; Only indices 2-6 have non-zero gradients