class Bottle::Tensor(T)

Defined in:

tensor/tensor.cr

Constructors

Class Method Summary

Instance Method Summary

Instance methods inherited from class Bottle::BaseArray(T)

[](mask : Tensor(Bool))
[](*args)
[]
, []=(idx : Array, assign : BaseArray(T))
[]=(*args : *U) forall U
[]=
, accumulate_along_axis(axis, &) accumulate_along_axis, aref_set(*args, value : Tensor(T))
aref_set(*args, value : Number)
aref_set
, as_strided(shape, strides, writeable = false) as_strided, astype(dtype : U.class) forall U astype, base base, basetype basetype, bc?(axis : Int32) bc?, broadcast_to(newshape) broadcast_to, broadcastable(other : BaseArray) broadcastable, buffer buffer, bytesize bytesize, check_type check_type, diag_view diag_view, dtype dtype, dup(order : Char | Nil = nil) forall U dup, dup_view dup_view, flags flags, flat_iter flat_iter, index_iter index_iter, is_contiguous is_contiguous, is_fortran_contiguous is_fortran_contiguous, nbytes nbytes, ndims ndims, ravel ravel, reduce_along_axis(axis, &) reduce_along_axis, reshape(newshape : Array(Int32)) reshape, shape shape, shape=(shape : Array(Int32)) shape=, size size, slice(idx : Array(Int32 | Range(Int32, Int32))) slice, strides strides, strides=(strides : Array(Int32)) strides=, to_s(io) to_s, transpose(order : Array(Int32) = [] of Int32) transpose, unsafe_iter unsafe_iter, value value, view(dtype : U.class) forall U view

Constructor methods inherited from class Bottle::BaseArray(T)

new(buffer : Pointer(T), shape : Array(Int32), strides : Array(Int32), flags : Bottle::Internal::ArrayFlags, base : Pointer(T) | Nil, update_flags = true)
new(_shape : Array(Int32), order : ArrayFlags = ArrayFlags::Contiguous, ptr : Pointer(T) | Nil = nil)
new(shape : Array(Int32), order : ArrayFlags = ArrayFlags::Contiguous, &block : Int32 -> T)
new(nrows : Int32, ncols : Int32, &block : Int32, Int32 -> T)
new

Class methods inherited from class Bottle::BaseArray(T)

from_array(array : Array) from_array

Constructor Detail

def self.new(buffer : Pointer(T), shape, strides, flags, base, update_flags = true) #

Internal method to create tensors from low level libraries. This does no validation on inputs and is very unsafe unless called by the library.

Should not be used by the external API.


[View source]
def self.new(_shape : Array(Int32), order : ArrayFlags = ArrayFlags::Contiguous, ptr : Pointer(T) | Nil = nil) #

[View source]
def self.new(shape : Array(Int32), order : ArrayFlags = ArrayFlags::Contiguous, &block : Int32 -> T) #

Yields a BaseArray from a provided shape and a block. The block only provides the absolute index, not an index dependent on the shape, so if a user wants to handle an arbitrary shape inside the block they need to do that themselves.

t = BaseArray.new([2, 2, 3]) { |i| i / 2 }
t # =>
Base([[[ 0,  1],
       [ 2,  3]],

      [[ 4,  5],
       [ 6,  7]],

      [[ 8,  9],
       [10, 11]]])

[View source]
def self.new(nrows : Int32, ncols : Int32, &block : Int32, Int32 -> T) #

Yields a Tensor from a provided number of rows and columns. This can quickly create matrices, useful for several Tensor creattion methods such as the underlying implementation of eye, and diag.

This method does provide i and j variables for the passed block, so no offset calculations need to be done by the user.

t = Tensor.new(3, 3) { |i, j| i == j ? 1 : 0 }
t # =>
Tensor([[1, 0, 0],
        [0, 1, 0],
        [0, 0, 1]])

[View source]

Class Method Detail

def self.from_array(_shape : Array(Int32), _data : Array) #

[View source]
def self.random(r : Range(U, U), _shape : Array(Int32)) forall U #

A flexible method to create Tensor's of arbitrary shapes filled with random values of arbitrary types. Since Ranges can contain any dtype, the type of tensor is inferred from the passed range, and a new Tensor is sampled using the provided shape.

t = Tensor.random(0...10, [2, 2])
t # =>
Tensor([[5, 9],
        [3, 9]])

[View source]

Instance Method Detail

def %(other) #

Elementwise modulus of a Tensor} to another equally sized Tensor} or scalar


[View source]
def &(other) #

Elementwise bitwise and of a Tensor} to another equally sized Tensor} or scalar


[View source]
def *(other) #

Elementwise multiplication of a Tensor} to another equally sized Tensor} or scalar


[View source]
def **(other) #

[View source]
def +(other) #

Elementwise addition of a Tensor} to another equally sized Tensor} or scalar


[View source]
def -(other) #

Elementwise subtraction of a Tensor} to another equally sized Tensor} or scalar


[View source]
def /(other) #

Elementwise division of a Tensor} to another equally sized Tensor} or scalar


[View source]
def <(other) #

Elementwise less than of a Tensor} to another equally sized Tensor} or scalar


[View source]
def <<(other) #

Elementwise left shift of a Tensor} to another equally sized Tensor} or scalar


[View source]
def <=(other) #

Elementwise less equals of a Tensor} to another equally sized Tensor} or scalar


[View source]
def ==(other) #

Elementwise equals of a Tensor} to another equally sized Tensor} or scalar


[View source]
def >(other) #

Elementwise greater than of a Tensor} to another equally sized Tensor} or scalar


[View source]
def >=(other) #

Elementwise greater equal than of a Tensor} to another equally sized Tensor} or scalar


[View source]
def >>(other) #

Elementwise right shift of a Tensor} to another equally sized Tensor} or scalar


[View source]
def ^(other) #

Elementwise bitwise xor of a Tensor} to another equally sized Tensor} or scalar


[View source]
def |(other) #

Elementwise bitwise or of a Tensor} to another equally sized Tensor} or scalar


[View source]
def basetype(t : U.class) forall U #

[View source]
def basetype #

Compile time checking of data types of a Tensor to ensure mixing data types is not allowed, not are bad data types allowed into the Tensor


[View source]
def cumsum(axis : Int32) #

[View source]
def imag #

[View source]
def matrix_iter #

[View source]
def max(axis : Int32) #

Computes the maximum value of a Tensor

v = Tensor.new [1, 2, 3, 4]
max(v) # => 4

[View source]
def max #

Computes the maximum value of a Tensor

v = Tensor.new [1, 2, 3, 4]
max(v) # => 4

[View source]
def mean(axis : Int32) #

Computes the average of all Tensor values

v = Tensor.new [1, 2, 3, 4]
mean(v) # => 2.5

[View source]
def mean #

Computes the average of all Tensor values

v = Tensor.new [1, 2, 3, 4]
mean(v) # => 2.5

[View source]
def median #

Computes the median value of a Tensor

v = Tensor.new [1, 2, 3, 4]
median(v) # => 2.5

[View source]
def min(axis : Int32) #

Computes the minimum value of a Tensor

v = Tensor.new [1, 2, 3, 4]
min(v) # => 1

[View source]
def min #

Computes the minimum value of a Tensor

v = Tensor.new [1, 2, 3, 4]
min(v) # => 1

[View source]
def ptp(axis : Int32) #

Computes the "peak to peak" of a Tensor (max - min)

v = Tensor.new [1, 2, 3, 4]
v.ptp # => 3

[View source]
def ptp #

Computes the "peak to peak" of a Tensor (max - min)

v = Tensor.new [1, 2, 3, 4]
v.ptp # => 3

[View source]
def real #

[View source]
def std #

Computes the standard deviation of a Tensor

v = Tensor.new [1, 2, 3, 4]
std(v) # => 1.118

[View source]
def sum(axis : Int32) #

Computes the total sum of a Tensor

v = Tensor.new [1, 2, 3, 4]
sum(v) # => 10

[View source]
def sum #

Computes the total sum of a Tensor

v = Tensor.new [1, 2, 3, 4]
sum(v) # => 10

[View source]
def to_s(io) #

Creates a string representation of a Tensor. The implementation of this is a bit of a mess, but I am happy with the results currently, it could however be cleaned up to handle long floating point values more precisely.


[View source]