nimxla/tensor

Search:
Group by:
Source   Edit  

The tensor module contains a simple ndimensional array type which is stored in host memory. e.g.

Example:

import nimxla/tensor
import math

var t = toTensor[float32](1..6).reshape(2, 3)
for i in 0 ..< t.dims[0]:
  t[i, 0] = sqrt(t[i, 0])
echo t

Types

Tensor[T] = object
  dims*: seq[int]
  
A Tensor is a fixed size array where the data is laid out contiguously. Copying a tensor is a shallow copy - i.e. it is a view on the same data. Source   Edit  

Procs

proc `$`[T: ElemType](t: Tensor[T]): string
Pretty print tensor type, shape and summary of data. Source   Edit  
proc `==`[T: ElemType](t1, t2: Tensor[T]): bool
Checks have same shape and values are equal Source   Edit  
proc `[]`[T: ElemType](t: Tensor[T]; ix: varargs[int]): T
Get the element at the position given by the array of indces or empty array for a scalar. Negative indices may be used to offset from end of the dimension. Will raise an exception if index is out of range. Source   Edit  
proc `[]=`[T: ElemType](t: var Tensor; ix: varargs[int]; value: T)
Update the element at the position given by the array of indices similar to []. Source   Edit  
proc append[T: ElemType](t, t2: Tensor[T]): Tensor[T]
Append the data from t2 to the end of t. This will allocate and return a new tensor. New shape is [t.d0+t.d2.d0, ...] where any dimensions following the first must be the same. Source   Edit  
proc approxEqual[T: ElemType](t1, t2: Tensor[T]; eps = 0.000001): bool
Checks have same shape and abs(diff) < eps for each element Source   Edit  
proc at[T: ElemType](t: Tensor[T]; ix: Natural): Tensor[T]
Get tensor indexed by leading dimension - e.g. if shape of t is [2, 3, 4] then t.at(1) => [3, 4]. Note that this returns a view of the data from tensor t. Use clone on the result if you need a copy. Source   Edit  
proc clone[T: ElemType](t: Tensor[T]): Tensor[T]
Return a copy of the tensor Source   Edit  
proc convert[T: ElemType](t: Tensor[T]; typ: typedesc[ElemType]): Tensor[typ]
Makes a copy of the tensor with elements converted to typ. Source   Edit  
proc fill[T: ElemType](dims: openArray[int]; value: T): Tensor[T]
Create a new tensor withe the given type and dimensions and set every element to value. Source   Edit  
proc format[T: ElemType](t: Tensor[T]): string
Pretty print summary of tensor data. Source   Edit  
proc len(t: Tensor): int
Number of elements in the tensor. Source   Edit  
proc newTensor[T: ElemType](dims: varargs[int]): Tensor[T]
Create a new tensor with the given type and dimnsions. Data is not initialized. Source   Edit  
proc rawPtr[T: ElemType](t: Tensor[T]): ptr T
Pointer to start of data buffer. Source   Edit  
proc readTensor[T: ElemType](filename: string): Tensor[T]
Read tensor attributes and data from a file as per using the stream read function. Source   Edit  
proc readTensor[T: ElemType](s: Stream): Tensor[T]
Read tensor attributes and data from a stream. This will parse data written by write and should also handle other data as long as it is in NPY v1 or v2 format and the data type is supported. Will raise an IOError exception if the data cannot be read. Source   Edit  
proc reshape[T: ElemType](t: Tensor[T]; dims: varargs[int]): Tensor[T]
Returns a view of the same tensor with the shape changed. Number of elements in the tensor must be unchanged or will raise an exception. If one of the dimensions is -1 then this value is inferred from the total number of elements. Source   Edit  
proc setPrintOpts(minWidth = 8; precision = 6; floatMode = ffDefault;
                  threshold = 1000; edgeitems = 4) {....raises: [], tags: [],
    forbids: [].}
Set the formatting options for printing the tensor data with the $ operator similar to numpy.set_printoptions. See the strutils package for details of the floatMode option. Source   Edit  
proc shape[T: ElemType](t: Tensor[T]): Shape
Get the data type and dimensions of the tensor in XLA format. Source   Edit  
proc toSeq[T: ElemType](t: Tensor[T]): seq[T]
Copy the data associated with the tensor to a sequence of length t.len. Source   Edit  
proc toTensor(values: openArray[int]): Tensor[int64] {....raises: [], tags: [],
    forbids: [].}
Create a new 1D int64 tensor from an array of int values Source   Edit  
proc toTensor[T: ElemType](slice: HSlice[int, int]): Tensor[T]
Create a new 1D tensor of given type with elements set to the values from iterating over the slice. Source   Edit  
proc toTensor[T: ElemType](value: T): Tensor[T]
Convert a scalar value of type T to a 0 dimensional tensor, Source   Edit  
proc toTensor[T: ElemType](values: openArray[T]): Tensor[T]
Create a new 1D tensor of given type and size by copying data from the provided array. Source   Edit  
proc write[T: ElemType](t: Tensor[T]; filename: string)
Write tensor attributes and data to a file in Numpy NPY v1 format. Source   Edit  
proc write[T: ElemType](t: Tensor[T]; s: Stream)
Write tensor attributes and data to stream in Numpy NPY v1 format Source   Edit  
proc zeros[T: ElemType](dims: varargs[int]): Tensor[T]
Create a new tensor with the given type and dimensions and elements set to zero. Source   Edit  

Macros

macro `@@`(value: untyped): untyped

Generate a tensor literal from a scalar or array e.g.

 @@42f32               -> 0 dimensional float32 tensor
 @@[1.0, 2, 3]         -> 1 dimensional float64 tensor
 @@[[1, 2], [3, 4]]    -> 2 dimensional int64 tensor

Source   Edit