qualia2 package

Subpackages

Submodules

qualia2.autograd module

class qualia2.autograd.Abs(output_shape, *args, **kwargs)[source]

Bases: qualia2.autograd.Function

calc_grad(dx)[source]

calculates gradients for backpropagation

static forward(a)[source]

calculates forward propagation

class qualia2.autograd.Add(output_shape, *args, **kwargs)[source]

Bases: qualia2.autograd.Function

Adds arrays elementwise.

calc_grad(dx)[source]

calculates gradients for backpropagation

static forward(*args)[source]

calculates forward propagation

class qualia2.autograd.Clamp(output_shape, *args, **kwargs)[source]

Bases: qualia2.autograd.Function

calc_grad(dx)[source]

calculates gradients for backpropagation

static forward(x, low, high)[source]

calculates forward propagation

class qualia2.autograd.Div(output_shape, *args, **kwargs)[source]

Bases: qualia2.autograd.Function

Elementwise true division

calc_grad(dx)[source]

calculates gradients for backpropagation

static forward(a, b)[source]

calculates forward propagation

class qualia2.autograd.Expand_dims(output_shape, *args, **kwargs)[source]

Bases: qualia2.autograd.Function

calc_grad(dx)[source]

calculates gradients for backpropagation

static forward(a, axis)[source]

calculates forward propagation

class qualia2.autograd.Function(output_shape, *args, **kwargs)[source]

Bases: object

All function should inherit this class.

forward and calc_grad methods should be overwritten.

Attributes:

output_shape (tuple(int)): output shape of a function

var (tuple(Tensor)): Tensor(s) that was feeded

kwargs (dict): some useful data for backward calculation

backward(*args)[source]

executes backpropagation

calc_grad(*args)[source]

calculates gradients for backpropagation

static forward(*args, **kwargs)[source]

calculates forward propagation

static handle_broadcast(arg, trg)[source]
classmethod prepare(output_shape, *args, **kwargs)[source]
class qualia2.autograd.Gather(output_shape, *args, **kwargs)[source]

Bases: qualia2.autograd.Function

Gathers values along an axis specified by dim.

calc_grad(dx)[source]

calculates gradients for backpropagation

static forward(a, dim, idx)[source]

calculates forward propagation

class qualia2.autograd.Matmul(output_shape, *args, **kwargs)[source]

Bases: qualia2.autograd.Function

calc_grad(dx)[source]

calculates gradients for backpropagation

static forward(a, b)[source]

calculates forward propagation

class qualia2.autograd.Mul(output_shape, *args, **kwargs)[source]

Bases: qualia2.autograd.Function

Multiplies two arrays elementwise.

calc_grad(dx)[source]

calculates gradients for backpropagation

static forward(a, b)[source]

calculates forward propagation

class qualia2.autograd.Neg(output_shape, *args, **kwargs)[source]

Bases: qualia2.autograd.Function

Takes numerical negative elementwise.

calc_grad(dx)[source]

calculates gradients for backpropagation

static forward(a)[source]

calculates forward propagation

class qualia2.autograd.Pow(output_shape, *args, **kwargs)[source]

Bases: qualia2.autograd.Function

Computes a ** b elementwise.

calc_grad(dx)[source]

calculates gradients for backpropagation

static forward(a, b)[source]

calculates forward propagation

class qualia2.autograd.Reshape(output_shape, *args, **kwargs)[source]

Bases: qualia2.autograd.Function

calc_grad(dx)[source]

calculates gradients for backpropagation

static forward(a, shape)[source]

calculates forward propagation

class qualia2.autograd.Slice(output_shape, *args, **kwargs)[source]

Bases: qualia2.autograd.Function

calc_grad(dx)[source]

calculates gradients for backpropagation

static forward(a, slice)[source]

calculates forward propagation

class qualia2.autograd.Squeeze(output_shape, *args, **kwargs)[source]

Bases: qualia2.autograd.Function

calc_grad(dx)[source]

calculates gradients for backpropagation

static forward(a, axis=None)[source]

calculates forward propagation

class qualia2.autograd.Sub(output_shape, *args, **kwargs)[source]

Bases: qualia2.autograd.Function

Subtracts arguments elementwise.

calc_grad(dx)[source]

calculates gradients for backpropagation

static forward(*args)[source]

calculates forward propagation

class qualia2.autograd.Tensor(data, requires_grad=True, dtype='float64')[source]

Bases: object

Wrapper class to execute automatic differentiation

Args:

data (Union[ndarray,int,float]): tensor to compute the automatic differentiation

requires_grad (bool): Whether to store grads. If False is set, grad of the Tensor will be zeros. Default: True

dtype (str): data type of the tensor Default: ‘float64’

Attributes:

data (ndarray): Stores data of the Tensor

grad (ndarray): Stores gradients of the Tensor

creator (Function): Stores the creator of the Tensor, which will be called at the backpropagation.

child (list): temp list of child, which will be created in a forward pass

requires_grad (bool): Whether to store grads. If False is set, grad of the Tensor will be zeros.

shape (tuple): Stores the shape of Tensor’s data

ndim (int): Stores the number of Tensor’s data dimentions

Examples:

>>> # The following example will compute the dy/dx
>>> # Create Tensor objects 
>>> x = qualia2.array(5)
>>> # Write an equation 
>>> y = x**2 - 2*x + 1
>>> print(y)
>>> # Calclate gradiant 
>>> y.backward()
>>> # Print `dy/dx`
>>> print(x.grad)
property T

transpose of the Tensor

Returns:

(Tensor): transpose of the tensor

asnumpy()[source]

aquire Tensor data as numpy ndarray

Returns:

(ndarray): numpy array

backward(*args)[source]

calculates all the gradients in the graph

Args:

*args (ndarray): seed of the reverse accumulation AD; optional

clamp(low, high)[source]

clamp the data

Args:

low (float): lower limit of the data.

high (float): upper limit of the data.

Returns:

(Tensor): clamped Tensor

copy(data)[source]
detach()[source]

returns a new Tensor, detached from the current graph.

Returns:

(Tensor): a new Tensor, detached from the current graph.

expand_dims(axis)[source]
fill(val)[source]

initialize the Tensor data with a constant value

Args:

val (float|int): a value to fill the Tensor data

gather(dim, idx)[source]
gradasnumpy()[source]

aquire Tensor grad as numpy ndarray

Returns:

(ndarray): numpy array

handle_const(obj)[source]

handles the constant object such as int or float

Args:

obj (Union[Tensor,int,float]): constant

Returns:

(Tensor)

normal(mean=0, std=1)[source]

initialize the Tensor data with normal distribution

Args:

mean (float): mean of the normal distribution.

std (float): std of the normal distribution.

ones()[source]

initialize the Tensor data with ones

register_hook(hook)[source]
reshape(*args)[source]
set_creator(obj)[source]

sets the creator of the Tensor

Args:

obj (Function): the function that created the Tensor

squeeze(axis=None)[source]
transpose(*args)[source]
uniform(low=0, high=1)[source]

initialize the Tensor data with uniform distribution

Args:

low (float): lower limit of the uniform distribution.

high (float): upper limit of the uniform distribution.

unsqueeze(axis)[source]
zeros()[source]

initialize the Tensor data with zeros

class qualia2.autograd.Transpose(output_shape, *args, **kwargs)[source]

Bases: qualia2.autograd.Function

calc_grad(dx)[source]

calculates gradients for backpropagation

static forward(a, axes)[source]

calculates forward propagation

qualia2.core module

qualia2.core.to_cpu(obj)[source]
qualia2.core.to_gpu(obj)[source]

qualia2.util module

class qualia2.util.Trainer(batch, path=None)[source]

Bases: object

Trainer base class

after_episode(epoch, model, loss)[source]
after_train()[source]
before_episode(dataloader, model)[source]
before_train(dataloader, model, filename)[source]
plot(filename=None)[source]
set_data_transformer(fn)[source]
set_label_transformer(fn)[source]
test(model, dataloader, batch, filename=None)[source]
train(model, dataloader, optim, criterion, epochs=200, filename=None)[source]

trainer helps the training process of supervised learning Args:

model (Module): model to train dataloader (DataLoader): dataloader to use optim (Optimizer): optimizer to use criterion (Function): loss function to use epochs (int): number of epochs filename (string): specify the filename as well as the loading path without the file extension. (ex) path/to/filename

train_routine(model, dataloader, optim, criterion, epochs=200)[source]
qualia2.util.check_function(fn, *args, x=None, domain=(-1000.0, 1000.0), **kwargs)[source]
qualia2.util.download_progress(count, block_size, total_size)[source]
qualia2.util.numerical_grad(fn, tensor, *args, **kwargs)[source]
qualia2.util.progressbar(progress, process, text_before='', text_after='')[source]

Module contents

qualia2.arange(*args, dtype='float64')[source]
qualia2.array(obj, dtype='float64')[source]
qualia2.copy(tensor)[source]
qualia2.empty(shape, dtype='float64')[source]
qualia2.empty_like(obj, dtype='float64')[source]
qualia2.linspace(start, stop, num=50, endpoint=True, retstep=False, dtype='float64')[source]
qualia2.load(filename)[source]

Loads parameters saved in qla format.

Args:

filename (str): specify the filename as well as the path to the file without the file extension. (ex) path/to/filename

qualia2.normal(mean=0, std=1, shape=None, dtype='float64')[source]
qualia2.normal_like(obj, mean=0, std=1, dtype='float64')[source]
qualia2.numel(obj)[source]
qualia2.ones(shape, dtype='int64')[source]
qualia2.ones_like(obj, dtype='int64')[source]
qualia2.rand(*args, dtype='float64')[source]
qualia2.rand_like(obj, dtype='float64')[source]
qualia2.randint(low, high=None, shape=None, dtype='float64')[source]
qualia2.randint_like(obj, low, high=None, dtype='float64')[source]
qualia2.randn(*args, dtype='float64')[source]
qualia2.randn_like(obj, dtype='float64')[source]
qualia2.save(state_dict, filename, protocol=-1)[source]

Saves parameters to qla format.

Args:

state_dict (dict): state dict to save filename (str): specify the filename as well as the saving path without the file extension. (ex) path/to/filename protocol (int): pickle protocol

qualia2.seed(seed=None)[source]
qualia2.uniform(low=0.0, high=1.0, shape=None, dtype='float64')[source]
qualia2.uniform_like(obj, low=0.0, high=1.0, dtype='float64')[source]
qualia2.zeros(shape, dtype='int64')[source]
qualia2.zeros_like(obj, dtype='int64')[source]