pyeddl.tensor — Tensor

pyeddl.tensor.DEV_CPU = 0

A constant representing the CPU device

pyeddl.tensor.DEV_GPU = 1000

A constant representing the GPU device

class pyeddl.tensor.Tensor(shape, dev=0)

Create an uninitialized tensor.

Parameters
  • shape – shape of the tensor to create

  • dev – device to use: DEV_CPU or DEV_GPU

Returns

Tensor

=== Creation Methods ===

static fromarray(array, dev=0)

Create a tensor from a NumPy array.

The tensor will be initialized with values, shape, etc. from the array.

The data type is automatically converted to float32, with one exception: due to the way pybind11 overloads are picked, if the input array is 1D, it must be of type float32. See http://github.com/deephealthproject/pyeddl/issues/10.

Parameters
Returns

Tensor

static zeros(shape, dev=0)

Create a tensor of the specified shape and fill it with zeros.

Parameters
  • shape – shape of the tensor to create

  • dev – device to use: DEV_CPU or DEV_GPU

Returns

Tensor

static ones(shape, dev=0)

Create a tensor of the specified shape and fill it with ones.

Parameters
  • shape – shape of the tensor to create

  • dev – device to use: DEV_CPU or DEV_GPU

Returns

Tensor

static full(shape, value, dev=0)

Create a tensor of the specified shape and fill it with value.

Parameters
  • shape – shape of the tensor to create

  • dev – device to use: DEV_CPU or DEV_GPU

  • value – value to fill the tensor with

Returns

Tensor

static arange(start, end, step, dev=0)

Create a 1D tensor with evenly spaced values within a given interval.

Parameters
  • start – start of interval

  • end – end of interval

  • step – spacing between values

  • dev – device to use: DEV_CPU or DEV_GPU

Returns

Tensor

static range(start, end, step, dev=0)

Create a 1D tensor with evenly spaced values within a given interval.

Parameters
  • start – start of interval

  • end – end of interval

  • step – spacing between values

  • dev – device to use: DEV_CPU or DEV_GPU

Returns

Tensor

static linspace(start, end, steps, dev=0)

Create a 1D tensor with evenly spaced values within a given interval.

Parameters
  • start – starting value

  • end – end value

  • steps – number of samples to generate

  • dev – device to use: DEV_CPU or DEV_GPU

Returns

Tensor

static logspace(start, end, steps, base, dev=0)

Create a 1D tensor with evenly spaced values on a log scale.

Parameters
  • startbase ** start is the starting value

  • endbase ** end is the final value of the sequence

  • steps – number of samples to generate

  • base – the base of the log space

  • dev – device to use: DEV_CPU or DEV_GPU

Returns

Tensor

static eye(size, offset=0, dev=0)

Create a size x size tensor with ones on the diagonal and zeros elsewhere.

Parameters
  • size – size of the (square) tensor to create

  • dev – device to use: DEV_CPU or DEV_GPU

Returns

Tensor

static randu(shape, dev=0)

Create a tensor with uniformly distributed random values.

Parameters
  • shape – shape of the tensor to create

  • dev – device to use: DEV_CPU or DEV_GPU

Returns

Tensor

static randn(shape, dev=0)

Create a tensor with normally distributed random values.

Parameters
  • shape – shape of the tensor to create

  • dev – device to use: DEV_CPU or DEV_GPU

Returns

Tensor

=== Data Copying Methods ===

toCPU()

Clone the tensor to the CPU.

Returns

None

toGPU()

Clone the tensor to the GPU.

Returns

None

clone()

Return a clone of the tensor (same device).

Returns

Tensor

select(indices)

Perform NumPy-like slicing on the tensor.

Parameters

indices – list of strings representing the indices to be selected. These indices must follow a NumPy-like syntax. For instance: ["1:3", "2"].

Returns

Tensor

set_select(indices, A)

Sets tensor elements to the values of tensor A using the selected indices.

Parameters

indices – list of strings representing the indices to be set. These indices must follow a NumPy-like syntax. For instance: ["1:3", "2"].

Returns

None

static copy(A, B)

Copy data from tensor A to tensor B.

Parameters
  • A – a tensor

  • B – a tensor

Returns

None

=== Serialization Methods ===

static load(fname, format='')

Load a tensor from a file.

Parameters
  • fname – name of the file to load the tensor from

  • format – file format (e.g., “bin”, “jpg”)

Returns

Tensor

save(fname, format='')

Save the tensor to a file.

Parameters
  • fname – name of the file to save the tensor to

  • format – file format (e.g., “bin”, “jpg”)

Returns

None

=== Math Methods ===

abs_()

Compute the element-wise absolute value of the tensor.

Modifies the tensor.

Returns

None

abs()

Compute the element-wise absolute value of the tensor.

Returns a new tensor.

Returns

Tensor

acos_()

Compute the element-wise inverse cosine of the tensor.

Modifies the tensor.

Returns

None

acos()

Compute the element-wise inverse cosine of the tensor.

Returns a new tensor.

Returns

Tensor

add_(other)

Adds other to the tensor.

Modifies the tensor.

Parameters

other – a tensor or scalar

Returns

None

add(other)

Adds other to the tensor.

Returns a new tensor.

Parameters

other – a tensor or scalar

Returns

Tensor

asin_()

Compute the element-wise inverse sine of the tensor.

Modifies the tensor.

Returns

None

asin()

Compute the element-wise inverse sine of the tensor.

Returns a new tensor.

Returns

Tensor

atan_()

Compute the element-wise inverse tangent of the tensor.

Modifies the tensor.

Returns

None

atan()

Compute the element-wise inverse tangent of the tensor.

Returns a new tensor.

Returns

Tensor

ceil_()

Compute the element-wise ceiling (smallest integer i such that i >= x) of the tensor.

Modifies the tensor.

Returns

None

ceil()

Compute the element-wise ceiling (smallest integer i such that i >= x) of the tensor.

Returns a new tensor.

Returns

Tensor

clamp_(min, max)

Limit the tensor’s values between min and max.

Modifies the tensor.

Parameters
  • min – minimum value

  • max – maximum value

Returns

None

clamp(min, max)

Limit the tensor’s values between min and max.

Returns a new tensor.

Parameters
  • min – minimum value

  • max – maximum value

Returns

Tensor

clampmax_(max)

Limit the tensor’s values to a maximum value.

Modifies the tensor.

Parameters

max – maximum value

Returns

None

clampmax(max)

Limit the tensor’s values to a maximum value.

Returns a new tensor.

Parameters

max – maximum value

Returns

Tensor

clampmin_(min)

Limit the tensor’s values to a minimum value.

Modifies the tensor.

Parameters

min – minimum value

Returns

None

clampmin(min)

Limit the tensor’s values to a minimum value.

Returns a new tensor.

Parameters

min – minimum value

Returns

Tensor

cos_()

Compute the element-wise cosine of the tensor.

Modifies the tensor.

Returns

None

cos()

Compute the element-wise cosine of the tensor.

Returns a new tensor.

Returns

Tensor

cosh_()

Compute the element-wise hyperbolic cosine of the tensor.

Modifies the tensor.

Returns

None

cosh()

Compute the element-wise hyperbolic cosine of the tensor.

Returns a new tensor.

Returns

Tensor

diag_(k=0)

Select diagonal elements.

Modifies the tensor (elements other than those in the selected diagonal are set to zero).

Parameters

k – offset (0 for the main diagonal, positive for the nth diagonal above the main one, negative for the nth diagonal below the main one)

Returns

None

diag(k=0)

Select diagonal elements.

Returns a new tensor which is the same as this one, except that elements other than those in the selected diagonal are set to zero.

Parameters

k – offset (0 for the main diagonal, positive for the nth diagonal above the main one, negative for the nth diagonal below the main one)

Returns

Tensor

div_(other)

Divides the tensor by other.

Modifies the tensor.

Parameters

other – a tensor or scalar

Returns

None

div(other)

Divides the tensor by other.

Returns a new tensor.

Parameters

other – a tensor or scalar

Returns

Tensor

exp_()

Compute the element-wise exponential of the tensor.

Modifies the tensor.

Returns

None

exp()

Compute the element-wise exponential of the tensor.

Returns a new tensor.

Returns

Tensor

floor_()

Compute the element-wise floor (largest integer i such that i <= x) of the tensor.

Modifies the tensor.

Returns

None

floor()

Compute the element-wise floor (largest integer i such that i <= x) of the tensor.

Returns a new tensor.

Returns

Tensor

log_()

Compute the element-wise natural logarithm of the tensor.

Modifies the tensor.

Returns

None

log()

Compute the element-wise natural logarithm of the tensor.

Returns a new tensor.

Returns

Tensor

log2_()

Compute the element-wise base-2 logarithm of the tensor.

Modifies the tensor.

Returns

None

log2()

Compute the element-wise base-2 logarithm of the tensor.

Returns a new tensor.

Returns

Tensor

log10_()

Compute the element-wise base-10 logarithm of the tensor.

Modifies the tensor.

Returns

None

log10()

Compute the element-wise base-10 logarithm of the tensor.

Returns a new tensor.

Returns

Tensor

logn_(n)

Compute the element-wise base-n logarithm of the tensor.

Modifies the tensor.

Parameters

n – logarithm base

Returns

None

logn(n)

Compute the element-wise base-n logarithm of the tensor.

Returns a new tensor.

Parameters

n – logarithm base

Returns

Tensor

max()

Return the maximum value of the tensor.

Returns

scalar

min()

Return the minimum value of the tensor.

Returns

scalar

mod_(v)

Compute the element-wise reminder of the division of the tensor by v .

Modifies the tensor.

Parameters

v – a scalar

Returns

None

mod(v)

Compute the element-wise reminder of the division of the tensor by v .

Returns a new tensor.

Parameters

v – a scalar

Returns

Tensor

mult_(other)

Multiplies the tensor by other, element-wise.

Modifies the tensor.

Parameters

other – a tensor or scalar

Returns

None

mult(other)

Multiplies the tensor by other, element-wise.

Returns a new tensor.

Parameters

other – a tensor or scalar

Returns

Tensor

static mult2D(A, B)

Computes the matrix product of A and B.

Parameters
  • A – a tensor

  • B – a tensor

Returns

Tensor

neg_()

Negate all elements in the tensor.

Modifies the tensor.

Returns

None

neg()

Negate all elements in the tensor.

Returns a new tensor.

Returns

Tensor

normalize_(min, max)

Normalize tensor values to the [min, max] range.

v' = r * (v - A_min) + min; r = (max - min) / (A_max - A_min)

Modifies the tensor.

Returns

None

normalize(min, max)

Normalize tensor values to the [min, max] range.

v' = r * (v - A_min) + min; r = (max - min) / (A_max - A_min)

Returns a new tensor.

Returns

Tensor

reciprocal_()

Compute the element-wise reciprocal of the tensor.

Modifies the tensor.

Returns

None

reciprocal()

Compute the element-wise reciprocal of the tensor.

Returns a new tensor.

Returns

Tensor

round_()

Round tensor values to the nearest integer.

Modifies the tensor.

Returns

None

round()

Round tensor values to the nearest integer.

Returns a new tensor.

Returns

Tensor

rsqrt_()

Compute the element-wise reciprocal of the square root of the tensor.

Modifies the tensor.

Returns

None

rsqrt()

Compute the element-wise reciprocal of the square root of the input tensor.

Returns a new tensor.

Returns

Tensor

sigmoid_()

Compute the element-wise sigmoid of the tensor.

Modifies the tensor.

Parameters

A – a tensor

Returns

None

sigmoid()

Compute the element-wise sigmoid of the tensor.

Returns a new tensor.

Returns

Tensor

sign_()

Compute the element-wise sign (-1 if x < 0, 0 if x == 0, 1 if x > 0) of the tensor.

Modifies the tensor.

Returns

None

sign()

Compute the element-wise sign (-1 if x < 0, 0 if x == 0, 1 if x > 0) of the tensor.

Returns a new tensor.

Returns

Tensor

sin_()

Compute the element-wise sine of the tensor.

Modifies the tensor.

Returns

None

sin()

Compute the element-wise sine of the tensor.

Returns a new tensor.

Returns

Tensor

sinh_()

Compute the element-wise hyperbolic sine of the tensor.

Modifies the tensor.

Returns

None

sinh()

Compute the element-wise hyperbolic sine of the tensor.

Returns a new tensor.

Returns

Tensor

sqr_()

Compute the element-wise square of the tensor.

Modifies the tensor.

Returns

None

sqr()

Compute the element-wise square of the tensor.

Returns a new tensor.

Returns

Tensor

sqrt_()

Compute the element-wise square root of the tensor.

Modifies the tensor.

Returns

None

sqrt()

Compute the element-wise square root of the tensor.

Returns a new tensor.

Returns

Tensor

sub_(other)

Subtracts other from the tensor.

Modifies the tensor.

Parameters

other – a tensor or scalar

Returns

None

sub(other)

Subtracts other from the tensor.

Returns a new tensor.

Parameters

other – a tensor or scalar

Returns

Tensor

tan_()

Compute the element-wise tangent of the tensor.

Modifies the tensor.

Returns

None

tan()

Compute the element-wise tangent of the tensor.

Returns a new tensor.

Returns

Tensor

tanh_()

Compute the element-wise hyperbolic tangent of the tensor.

Modifies the tensor.

Returns

None

tanh()

Compute the element-wise hyperbolic tangent of the tensor.

Returns a new tensor.

Returns

Tensor

trace(k=0)

Sum diagonal elements.

Parameters

k – offset (0 for the main diagonal, positive for the nth diagonal above the main one, negative for the nth diagonal below the main one)

Returns

float

trunc_()

Truncate (discard the fractional part) the tensor, element-wise.

Modifies the tensor.

Returns

None

trunc()

Truncate (discard the fractional part) the tensor, element-wise.

Returns a new tensor.

Returns

Tensor

=== Indexing, Slicing, Joining, Mutating ===

static concat(A, axis=0, output=None)

Join a sequence of tensors along the specified axis.

Parameters
  • A – list of tensors with the same shape

  • axis – axis along which to join

  • output – output tensor

Returns

Tensor

static stack(A, axis=0, output=None)

Repeat tensor elements along the specified dimension.

Parameters
  • A – input tensor

  • axis – axis along which to repeat the values

  • output – output tensor

Returns

Tensor

static repeat(A, repeats, axis=0, output=None, derivative=False)

Repeat the elements of a tensor along the specified dimension.

Parameters
  • A – input tensor

  • repeats – number of repetitions

  • axis – axis along which to repeat values

  • output – output tensor

  • derivative – apply derivative for output = repeat(A)

Returns

Tensor

static tile(A, repeats)

Construct an array by repeating A for the given number of times.

Parameters
  • A – input tensor

  • repeats – number of repetitions of A

Returns

Tensor

static broadcast(A, B, output=None)

Return a new tensor A to be broadcasted into B

Parameters
  • A – input tensor

  • B – input tensor

  • output – output tensor

Returns

Tensor

=== Transformations ===

scale(new_shape, mode=WrappingMode.Constant, cval=0.0, coordinate_transformation_mode=TransformationMode.Asymmetric, keep_size=False)

Scale the tensor. The array is scaled using spline interpolation.

Parameters
  • new_shape – list with the target size

  • mode – a WrappingMode

  • cval – Value to fill past edges of input if mode is WrappingMode.Constant

  • coordinate_transformation_mode – a TransformationMode

  • keep_size – whether to keep the original size

Returns

a new tensor

=== Other Methods ===

fill_(v)

Fill the tensor with the specified value.

Parameters

v – a scalar value

Returns

None

reshape_(new_shape)

Change the tensor’s shape.

Parameters

new_shape – the new shape (list of integers)

Returns

None

getdata()

Get the tensor’s data as a NumPy array.

Returns

a NumPy array

print()

Print the tensor’s values.

Returns

None

info()

Print info on the tensor (shape, strides, …).

Returns

None

getShape()

Return the tensor’s shape.

Returns

the tensor’s shape (a list of integers)

static onehot(in: pyeddl._core.Tensor, vocs: int) pyeddl._core.Tensor
static hardware_supported()

Get a list of hardware accelerators for which EDDL has been compiled.

Returns

list of supported accelerators

static is_hardware_supported(hardware)

Check if a specific hardware is supported.

Returns

True if the hardware is supported