Shortcuts

Tensors

PyTorch describes a tensor as a multi-dimensional matrix containing elements of a single data type. The simplest way to create a tensor is to use a k value, e.g.

q)t:tensor 0 1 2 3f

q)tensor t
0 1 2 3f

Setting properties

PyTorch defines some properties of a tensor as construction axes or attributes. The two main attributes are device, e.g. cpu or gpu, and data type. Other attributes or options determine layout and whether gradients are recorded for operations on the tensor. There are additional settings to determine if memory is pinned and optional memory formats where channels in 2-dimensional and 3-dimensional images (4-d & 5-d tensors) are stored with channels as the last dimension.

In the k interface, these attributes are represented as symbols:

  • device: `cpu or `cuda, which accepts an optional device index, e.g. `cuda:0 (see: devices)

  • dtype: one of `bool`byte`char`short`int`long`half`float`double`cfloat`cdouble (see: types)

  • layout: `strided or `sparse (see: PyTorch layout)

  • grad: either `grad, if gradients need to be computed for the tensor, or `nograd, the default setting.

  • pin: either `pinned or `unpinned (see: page-locked memory)

  • memory: either `preserve, `contiguous, `channel2d or `channel3d (see: channels last format)

These symbols can be specified in any order to set the properties of a tensor, e.g. `int or `cuda`float`grad. The options() function will display the defaults usually in effect if no argument given. Early versions of PyTorch allowed default attributes to be reset, but current versions only allow the default data type to be changed.

options() k dictionary
options(ptr) k dictionary
Dictionary of default attributes for tensor creation (empty arg) or values of the attributes for given tensor

The dtype() function will get/set the default data type or return the data type of a previously allocated tensor.

dtype() sym
dtype(sym) null
dtype(ptr) sym
With an empty argument, dtype() returns the default data type, with a sym data type, it sets the default data type and with a tensor pointer, the function returns the tensor’s datatype.

Note

Sparse tensors, complex tensors, pinned memory and the newer memory formats are less widely used and still a work in progress in PyTorch. Most options settings involve data type, device and gradient.

q)options()
device  | cpu
dtype   | float
layout  | strided
gradient| nograd
pin     | unpinned
memory  | contiguous

q)t:tensor 1 2 3
q)options t
device  | cpu
dtype   | long
layout  | strided
gradient| nograd
pin     | unpinned
memory  | contiguous

q)dtype t       / query data type of t
`long

q)dtype`double  / reset default data type from float -> double

q)free t
q)t:tensor()    / create an empty tensor with default data type
q)dtype t
`double

q)options()
device  | cpu
dtype   | double
..

q)free t
q)options t:tensor(1 2 3;`half`cuda`grad)   / create a tensor on gpu with half-precision
device  | cuda:0
dtype   | half
layout  | strided
gradient| grad
pin     | unpinned
memory  | contiguous

Creating from a k value

The api function tensor is used to create tensors from k values and retrieve the values back into a k session. The k value can be a scalar, simple list or higher dimension array. The k value must have the same data type throughout and the same size at each dimension.

tensor(ptr) value
Return a k value from an api-pointer to a previously allocated tensor
tensor(value) tensor pointer
tensor(value;options) tensor pointer
Create a tensor from k value.
Parameters:
  • value (scalar,list,array) – the k value to populate the tensor. If no options given, the data type for the tensor will be mapped from the data type of the k value.

  • options (sym) – one or more symbols for device, data type and other tensor attributes.

Returns:

An api-pointer to the allocated tensor

Examples

q)t:tensor 2 3 4#til 24

q)size t
2 3 4

q)dtype t
`long

q)device t
`cpu

q)free t
q)t:tensor(2 3 4#til 24;`cuda`double)

q)device t
`cuda:0

q)dtype t
`double

q)last tensor t
12 13 14 15
16 17 18 19
20 21 22 23

Using an output tensor

Instead of specifying creation options as the final argument in the tensor call, a previously allocated tensor can be used. The tensor’s existing attributes will be used but its values will be replaced.

tensor(value;out-tensor) null
Read k value and store in previously created tensor
Parameters:
  • value (scalar,list,array) – the k value to populate the tensor.

  • out-tensor (ptr) – a previously allocated api-pointer to a tensor which will contain the new values.

Returns:

null

q)options r:tensor()   / initialize empty tensor, retrieve attributes
device  | cpu
dtype   | float
layout  | strided
gradient| nograd
pin     | unpinned
memory  | contiguous


q)tensor(1 2 3;r)

q)tensor r
1 2 3e

q)free r                  / free tensor r, redefine on gpu as 4-byte int
q)r:tensor((); `cuda`int)
q)options r
device  | cuda:0
dtype   | int
layout  | strided
gradient| nograd
pin     | unpinned
memory  | contiguous

q)tensor(1 2 3 4;r)

q)tensor r
1 2 3 4i

q)device r
`cuda:0

Conversion errors

The k value given must be the same data type throughout and have the same size at each depth. There also needs to be a defined mapping between the k type and the PyTorch type (see data types ). Some examples where these conditions are not met:

q)t:tensor(1 2;3 4.0)
'type mismatch at depth 1, long list vs double list
  [0]  t:tensor(1 2;3 4.0)
      ^

q)t:tensor(1 2;3 4 5)
'dimension mismatch at depth 1, 2 vs 3
  [0]  t:tensor(1 2;3 4 5)
      ^

q)t:tensor `a`b`c
'no torch type found for k: symbol list
  [0]  t:tensor `a`b`c
      ^

q)t:tensor ([]1 2)
'no torch type found for k: table
  [0]  t:tensor ([]1 2)
         ^

Undefined tensors

A tensor pointer can be created which does not point to any underlying memory. It has no device or data type, but may be used as a placeholder or return value. In the k api, the value used to create an undefined tensor is generic null, (::). The information function defined() will return false if a given tensor pointer has no data defined for it.

q)t:tensor[]   /create undefined tensor
q)defined t
0b
q)free t       /the tensor must still be free'd

q)t:tensor(::)
q)defined t
0b

q)(::)~tensor t  /unary null is returned
1b

q)options t  / all sym options return null
device  |
dtype   |
layout  |
gradient|
pin     |
memory  |

An undefined tensor is different from an empty tensor, which is considered defined with a device and data type.

q)e:tensor()

q)defined e
1b

q)tensor e
`real$()     /4-byte float is the default data type if not defined at creation

q)options e
device  | cpu
dtype   | float
layout  | strided
gradient| nograd
pin     | unpinned
memory  | contiguous

Retrieving tensor values

The tensor function can also be used to retrieve values from a previously created tensor into a k array.

tensor(ptr) value
tensor(ptr;ind) value
tensor(ptr;dim;ind) value
tensor(ptr;flag;dim;ind) value
Return a k value from an api-pointer to a previously allocated tensor
Parameters:
  • ptr – a previously allocated api-pointer to a tensor.

  • flag (bool) – an optional flag for complex tensors only, true to return real & imaginary parts along first dimension, false along last dimension.

  • dim (long) – an optional dimension for the subsequent index.

  • ind (long) – an optional index/indices to retrieve tensor[ind] if no preceding dimension, else tensor[;;ind] if dim=2, etc..

q)t:tensor 2 3 4#til 24

q)tensor(t;1)
12 13 14 15
16 17 18 19
20 21 22 23

q)tensor(t;-1;3)   / pytorch uses -1 for last dimension, -2 for second to last, ..
3  7  11
15 19 23

Note

The dimension used for retrieving a particular index may be specfied with negative integers, i.e. -1 for final dimension, -2 for next to final dimension. A single index may also be specified as a negative number, -1 for last, -2 for second to last. However, a list of indices can only use 0 - n-1 where n is the size of the default or specified dimension.

q)t:tensor 3 4#til 12

q)tensor(t;-1)   /default dimension is zero
8 9 10 11

q)tensor(t;1;2 3)
2  3
6  7
10 11

q)tensor(t;-1;2 3)
2  3
6  7
10 11

q)tensor(t;-2;1 2)
4 5 6  7
8 9 10 11

q)tensor(t;-1;0 -1)
'INDICES element is out of DATA bounds, id=-1 axis_dim=4
  [0]  tensor(t;-1;0 -1)
       ^

Tensor creation modes

In addition to supplying k values to initialise tensors, the following methods create tensors following a particular distribution, sequence, etc. The k interface function accepts arguments somewhat similar to the PyTorch function/methods listed here.

  • arange: returns a tensor with a sequence of integers (replaces deprecated function: range)

  • empty: returns a tensor of given size with uninitialized values

  • eye: returns an identity matrix

  • full: returns a tensor filled with a single value

  • linspace: returns a tensor with values linearly spaced in some interval

  • logspace: returns a tensor with values logarithmically spaced in some interval

  • ones: returns a tensor filled with ones

  • rand: returns a tensor with values drawn from a uniform distribution on [0, 1)

  • randint: returns a tensor with integers randomly drawn from an interval

  • randn: returns a tensor with values drawn from a unit normal distribution

  • randperm: returns a tensor with a random permutation of integers in some interval

  • zeros: returns a tensor filled with zeros

  • complex: creates a complex tensor from real and imaginary values

  • sparse: creates a sparse tensor from indices and values

Tensors are created in the k interface using the above methods by supplying a mode symbol as the first argument to the same tensor api function.

q)t:tensor(`zeros; 2 3; `int)
q)tensor t
0 0 0
0 0 0

Creating by size: zeros, ones, empty

Return tensor filled with zeros, ones, and uninitialized (empty).

tensor(mode;size) tensor pointer
tensor(mode;size;options) tensor pointer
Create a tensor given mode, size and optional attribute(s).
Parameters:
  • mode (sym) – one of `zeros, `ones, `empty

  • size (long) – scalar/list specifiying size of array

  • options (sym) – one or more symbols for device, data type and other tensor attributes.

Returns:

An api-pointer to the allocated tensor

Alternate form using an input tensor to supply size, i.e. size will be derived from the input tensor, similar to PyTorch creation function torch.ones_like.

tensor(mode;in-tensor) tensor pointer
tensor(mode;in-tensor;options) tensor pointer
Create a tensor given mode and input tensor whose size will be used to create new tensor, along with optional tensor attribute(s).
Parameters:
  • mode (sym) – one of `zeros, `ones, `empty

  • in-tensor (ptr) – an api-pointer to a previously allocated tensor – its size will determine size of newly created tensor. Device, data type and layout also default to those of the input tensor but can be overwritten by explicit options given in last argument.

  • options (sym) – one or more symbols for device, data type and other tensor attributes.

Returns:

An api-pointer to the allocated tensor.

Alternate form using an output tensor instead of options that control data type, device, etc.

tensor(mode;size;out-tensor) null
Parameters:
  • mode (sym) – one of `zeros, `ones, `empty.

  • size (long) – scalar/list specifiying size of array.

  • out-tensor (ptr) – an api-pointer to a previously allocated output tensor.

Returns:

null return, resets values according to size given and attributes of the output tensor.

q)tensor t:tensor(`zeros;3 2)
0 0
0 0
0 0

q)tensor(`ones;5;t)  / use t as an output tensor
q)tensor t
1 1 1 1 1e

q)tensor(`empty;100;t) / t is filled with unitialized values
q)tensor t
1 1 1 1 1 0 4.332332e-37 0 2.791531e+20 1.693048e+22 7.501883e+28 2.733884e+2..

Tensor with single value

Creating tensor with single value: full.

tensor(mode;size;value) tensor pointer
tensor(mode;size;value;options) tensor pointer
Create a tensor given mode = `full, size, fill value and optional attribute(s).
Parameters:
  • mode (sym) – set to `full

  • size (long) – scalar/list specifiying size of array

  • value (scalar) – scalar fill value, real or double k type. Also possible to specify non floating point scalar, but options must include required tensor data type.

  • options (sym) – one or more symbols for device, data type and other tensor attributes.

Returns:

An api-pointer to the allocated tensor

Alternate form using an input tensor for size:

tensor(mode;in-tensor;value) tensor pointer
tensor(mode;in-tensor;value;options) tensor pointer
Create a tensor given mode of `full and input tensor whose size will be used to create new tensor, along with fill value and optional tensor attribute(s). Similar to PyTorch creation function torch.full_like.

Alternate form using an output tensor instead of options that control data type, device, etc.

tensor(mode;size;value;out-tensor) null
q)t:tensor(`full; 2 5; 3.0)

q)tensor t
3 3 3 3 3
3 3 3 3 3

q)b:tensor(`full;t;1b)  / create boolean tensor, use t's size

q)tensor b
11111b
11111b

q)tensor(`full;7;4.5;b)  / use b's properties, fill with 4.5 -> boolean

q)tensor b
1111111b

Random tensors

Return a tensor filled with random numbers from a uniform distribution on [0, 1) (rand) or unit normal (randn).

Parameters and function calls are as above for mode of `zeros, `ones and `empty.

tensor(mode;size) tensor pointer
tensor(mode;size;options) tensor pointer
Create a tensor given mode, size and optional attribute(s).
Parameters:
  • mode (sym) – one of `rand or `randn.

  • size (long) – scalar/list specifiying size of array.

  • options (sym) – one or more symbols for device, data type and other tensor attributes.

Returns:

An api-pointer to the allocated tensor.

Alternate form using an input tensor to supply size, i.e. size will be derived from the input tensor,

tensor(mode;in-tensor) tensor pointer
tensor(mode;in-tensor;options) tensor pointer
Create a tensor given mode and input tensor whose size will be used to create new tensor, along with optional tensor attribute(s).
Parameters:
  • mode (sym) – `rand or `randn.

  • in-tensor (ptr) – an api-pointer to a previously allocated tensor – its size will determine size of newly created tensor. Device, data type and layout also default to those of the input tensor but can be overwritten by explicit options given in last argument.

  • options (sym) – one or more symbols for device, data type and other tensor attributes.

Returns:

An api-pointer to the allocated tensor.

Alternate form using an output tensor instead of options that control data type, device, etc.

tensor(mode;size;out-tensor) null
Parameters:
  • mode (sym) – one of `rand or `randn.

  • size (long) – scalar/list specifiying size of array.

  • out-tensor (ptr) – an api-pointer to a previously allocated output tensor.

Returns:

null return, resets values according to size given and attributes of the output tensor.

q)tensor t:tensor(`rand;10)
0.05592483 0.7734587 0.1025799 0.6335379 0.3350263 0.5218872 0.8726696 0.9215..

q)free t
q)(avg;dev)@\:tensor t:tensor(`randn;10000000;`double)
-0.0002174295 0.9999617

Random integers

Create a tensor filled with random integers between given range: randint. Called by specifying low, high and size, or high and size (low defaults to zero), as well as other combinations with input and output tensors.

tensor(mode;high;size) tensor pointer
tensor(mode;low;high;size) tensor pointer
tensor(mode;low;high;size;options) tensor pointer
Create a tensor given mode, range and size, along with optional tensor attributes.
Parameters:
  • mode (sym) – `randint.

  • low (long) – lowest intger to be drawn from the distribution, set to zero if not given.

  • high (long) – one above the highest intger to be drawn from the distribution.

  • size (long) – scalar/list specifiying size of array.

  • options (sym) – one or more symbols for device, data type and other tensor attributes.

Returns:

An api-pointer to the allocated tensor.

An alternate form where an input tensor is supplied to provide the size of the created tensor. Tensor creation options will default to those of the input tensor unless explicitly supplied in the final argument:

tensor(mode;in-tensor;high) tensor pointer
tensor(mode;in-tensor;low;high) tensor pointer
tensor(mode;in-tensor;low;high;options) tensor pointer
Parameters:

in-tensor (ptr) – an api-pointer to a previously allocated tensor – its size will determine size of newly created tensor. Device, data type and layout also default to those of the input tensor but can be overwritten by explicit options given in last argument.

Returns:

An api-pointer to the allocated tensor.

The function call can also use a final argument of a previously allocated tensor as an output tensor:

tensor(mode;high;size;out-tensor) tensor pointer
tensor(mode;low;high;size;out-tensor) tensor pointer
Parameters:

out-tensor (ptr) – an api-pointer to a previously allocated output tensor.

Returns:

null return, resets values according to size given and attributes of the output tensor.

q)free t
q)t:tensor(`randint; -5; 6; 2 5; `float`cuda)
q)tensor t
4 0  -2 2 0
4 -5 2  3 3

q)tensor(`randint; 100; 3 9; t)
q)tensor t
85 55 87 0  1  81 36 97 22
98 20 66 12 0  95 39 66 12
21 82 59 39 64 91 54 59 91

q)dtype t
`float
q)device t
`cuda:0
q)size t
3 9

q)tensor(`randint; 100; 1000000; t)
q)avg tensor t
49.48276
q)size t
,1000000

Random permutations

Returns random permutations of integers from 0 to n-1 given n.

tensor(mode;n) tensor pointer
tensor(mode;n;options) tensor pointer
Parameters:
  • mode (sym) – `randperm.

  • n (long) – return random permutation of integers from 0-n-1 given n.

  • options (sym) – one or more symbols for device, data type and other tensor attributes.

Returns:

An api-pointer to the allocated tensor.

The function call can also use a final argument of a previously allocated tensor as an output tensor:

tensor(mode;n;out-tensor) tensor pointer
Parameters:
  • mode (sym) – `randperm.

  • n (long) – return random permutation of integers from 0-n-1 given n.

  • out-tensor (ptr) – an api-pointer to a previously allocated output tensor.

Returns:

null return, resets values according to size given and attributes of the output tensor.

q)t:tensor(`randperm;10)
q)tensor t
1 2 5 8 7 9 4 3 6 0

q)free t
q)t:tensor(`randperm;10;`pinned`double)   / pinned memory, double data type
q)tensor t
6 0 9 4 1 3 5 2 8 7f

q)tensor(`randperm;5;t)                   / use t as output tensor
q)tensor t
2 3 1 4 0f

Evenly spaced tensors

Creation modes arange and the deprecated range) return a 1-dimensional tensor of size (end-start)/step size, with start defaulting to zero and step size to 1.

tensor(mode;end) tensor pointer
tensor(mode;start;end) tensor pointer
tensor(mode;start;end;step) tensor pointer
tensor(mode;start;end;step;options) tensor pointer
Parameters:
  • mode (sym) – `arange or `range.

  • start (long) – starting value for the set of points, default is 0 for mode of `arange, must be given for `range.

  • end (long) – ending value for the set of points, mode=```arange`` returns points up to but not including end, mode of `range returns points including end.

  • step (long) – step size or gap between each pair of adjacent points, default is 1.

  • options (sym) – one or more symbols for device, data type and other tensor attributes.

Returns:

An api-pointer to the allocated tensor.

The function call can also use a final argument of a previously allocated tensor as an output tensor:

tensor(mode;start;end;step;out-tensor) tensor pointer
Parameters:

out-tensor (ptr) – an api-pointer to a previously allocated output tensor.

Returns:

null return, resets values according to size given and attributes of the output tensor.

q)tensor a:tensor(`arange;5)
0 1 2 3 4

q)tensor r:tensor(`range;0;5)
0 1 2 3 4 5e

q)t:tensor(`arange;0;10;2)
q)tensor t
0 2 4 6 8

q)free t
q)tensor t:tensor(`arange;.1;.8;.1)
0.1 0.2 0.3 0.4 0.5 0.6 0.7e

Creation modes PyTorch function linspace and logspace create 1-dimensional tensors evenly spaced from start to end, inclusive with linear step size or log scale of (end - start)/(steps-1).

tensor(mode;start;end;steps) tensor pointer
tensor(mode;start;end;steps;base) tensor pointer
tensor(mode;start;end;steps;base;options) tensor pointer
Parameters:
  • mode (sym) – `linspace or `logspace.

  • start (long) – starting value for the set of points.

  • end (long) – ending value for the set of points.

  • steps (long) – size of the created tensor running from start to end.

  • base (double) – optional base of the log function, default=``10.0``, only for mode=```logspace``

  • options (sym) – one or more symbols for device, data type and other tensor attributes.

Returns:

An api-pointer to the allocated tensor.

The function call can also use a final argument of a previously allocated tensor as an output tensor:

tensor(mode;start;end;steps;base;out-tensor) tensor pointer
Parameters:

out-tensor (ptr) – an api-pointer to a previously allocated output tensor.

Returns:

null return, resets values according to size given and attributes of the output tensor.

q)t:tensor(`linspace;0;9;10)
q)tensor t
0 1 2 3 4 5 6 7 8 9e

q)free t
q)t:tensor(`logspace;1;2;10)
q)tensor t
10 12.9155 16.68101 21.54435 27.82559 35.93814 46.41589 59.94843 77.42637 100e

q)tensor(`logspace;1;2;10;2.0;t)
q)tensor t
2 2.16012 2.333058 2.519842 2.72158 2.939469 3.174802 3.428976 3.703499 4e
q)2 xlog tensor t
1 1.111111 1.222222 1.333333 1.444444 1.555556 1.666667 1.777778 1.888889 2

Identity matrix

Function eye in PyTorch returns a 2-dimensional tensor with ones on the diagonal and zeros elsewhere.

tensor(mode;n) tensor pointer
tensor(mode;n;m) tensor pointer
tensor(mode;n;m;options) tensor pointer
Parameters:
  • mode (sym) – `eye.

  • n (long) – number of rows in the matrix.

  • m (long) – optional number of columns in the matrix, default is number of rows equal to columns.

  • options (sym) – one or more symbols for device, data type and other tensor attributes.

Returns:

An api-pointer to the allocated matrix.

The function call can also use a final argument of a previously allocated tensor as an output tensor:

tensor(mode;n;out-tensor) tensor pointer
tensor(mode;n;m;out-tensor) tensor pointer
Parameters:

out-tensor (ptr) – an api-pointer to a previously allocated output tensor.

Returns:

null return, resets values according to rows or rows and columns given and attributes of the output tensor.

q)t:tensor(`eye;3)
q)tensor t
1 0 0
0 1 0
0 0 1

q)free t
q)t:tensor(`eye;3;5;`bool`cuda)
q)tensor t
10000b
01000b
00100b

Complex tensor

A tensor of complex numbers can be created by supplying the real and imaginary parts, along with optional attributes. See section on complex tensors for other methods and details on complex tensors. The tensor creation method below is meant to match PyTorch’s torch.complex function.

tensor(mode;real;imag) tensor pointer
tensor(mode;real;imag;options) tensor pointer
Parameters:
  • mode (sym) – `complex.

  • real (numeric) – real part of the complex tensor as a k value.

  • imag (numeric) – imaginary part of the complex tensor as a k value (same size as the real part).

  • options (sym) – one or more symbols for device, data type and other tensor attributes.

Returns:

An api-pointer to the allocated complex tensor.

q)t:tensor(`complex;1 2 3;-1 2 0;`cdouble`cuda)
q)options t
device  | cpu
dtype   | cdouble
layout  | strided
gradient| nograd
pin     | unpinned
memory  | contiguous

q)tensor(t;1b)  / retrieve complex tensor as (real;imag)
1  2 3
-1 2 0

q)tensor[t]~tensor(t;1b) /default behaviour is flag set true
1b

q)tensor(t;0b)  / retrieve as real,'imag
1 -1
2 2
3 0

An alternate form of the above function call uses a single k value to create the complex tensor, with real and imaginary values across the first or last dimension of the given array.

tensor(mode;value) tensor pointer
tensor(mode;value;flag) tensor pointer
tensor(mode;value;options) tensor pointer
tensor(mode;value;flag;options) tensor pointer
Parameters:
  • mode (sym) – `complex.

  • value (numeric) – real & imaginary part of the complex tensor as a k value.

  • flag (bool) – a flag set true to indicate real and imaginary values are across the first dimension, else last dimension. If no flag given, the overall session flag for complex first dimension will be used.

  • options (sym) – one or more symbols for device, data type and other tensor attributes.

Returns:

An api-pointer to the allocated complex tensor.

q)setting`complexfirst
1b

q)x:(1 2 3; -1 0 2)

q)t:tensor(`complex; x)   / default setting: real and imaginary across 1st dim

q)tensor t                / retrieval also uses default setting
1  2 3
-1 0 2

q)tensor(t;0b)           / retrieve and arrange across last dimension
1 -1
2 0
3 2

q)use[t]tensor(`complex; x; 0b)
'complex: single input array must have a last dimension of size 2 (real,'imaginary), given size of [2, 3]
  [0]  use[t]tensor(`complex; x; 0b)
             ^

q)use[t]tensor(`complex;flip x; 0b)

Sparse tensor

A sparse tensor can be created by supplying indices and values, along with optional attributes. See section on sparse tensors for other methods and details on sparse tensors. The tensor creation method below is meant to match PyTorch’s torch.sparse_coo_tensor function.

tensor(mode;ind;val) tensor pointer
tensor(mode;ind;val;options) tensor pointer
tensor(mode;ind;val;size) tensor pointer
tensor(mode;ind;val;size;options) tensor pointer
Parameters:
  • mode (sym) – `sparse.

  • ind (long) – 2-d array of indices, each row corresponds to sparse dimension, each column for the non-zero values.

  • val (numeric) – scalar of list of k values corresponding to the given indices.

  • size (long) – scalar or list indicating the full size of the tensor; will be inferred as minimum size to hold all non-zero indices.

  • options (sym) – one or more symbols for device, data type and other tensor attributes.

Returns:

An api-pointer to the allocated sparse tensor.

q)show 0 -1 1 rotate'(v:1 2 3),\:0 0  / sparse matrix with values along diagonal
1 0 0
0 2 0
0 0 3

q)show i:flip 3 3 vs/:0 4 8
0 1 2
0 1 2

q)s:tensor(`sparse; i; v; `double)

q)tensor s
1 0 0
0 2 0
0 0 3

q)size s
3 3

q)indices s
0 1 2
0 1 2

q)values s
1 2 3f

q)use[s]tensor(`sparse; i; v; 3 5; `double)  / size beyond given indices
q)size s
3 5

q)tensor s
1 0 0 0 0
0 2 0 0 0
0 0 3 0 0

Docs

Access documentation for k api to PyTorch

View Docs

Examples

Examples using the k api to PyTorch

Examples

Github

C++ library source code and q/k examples

Github