linears

Module contents

Linear Layer.

class poornn.linears.LinearBase(input_shape, itype, weight, bias, var_mask=(1, 1))

Bases: poornn.core.Layer

Base of Linear Layer.

Parameters:
  • weight (ndarray/matrix) – weights in a matrix.
  • bias (1darray|None) – bias of shape (fout,), zeros if None.
  • var_mask (tuple<bool>, len=2, default=(True,True)) – variable mask for weight and bias.
weight

ndarray/matrix – weights in a matrix.

bias

1darray|None – bias of shape (fout,), zeros if None.

var_mask

tuple<bool>, len=2 – variable mask for weight and bias.

backward(xy, dy, mask=(1, 1))

back propagation to get \(\frac{\partial J(w,x)}{\partial w}\) and \(\frac{\partial J(w,x)}{\partial x}\), where \(J\) and \(w\) are cost function and variables respectively.

Parameters:
  • xy (tuple<ndarray>, len=2) – input and output array.
  • dy (ndarray) – gradient of output defined as \(\partial J/\partial y\).
  • mask (tuple) – (do_wgrad, do_xgrad)
Returns:

(ndarray, ndarray), \(\partial J/\partial w\) and \(\partial J/\partial x\).

forward(x, **kwargs)

forward propagration to evaluate \(y=f(x)\).

Parameters:
  • x (ndarray) – input array.
  • runtime_vars (dict) – runtime variables.
Returns:

ndarray, output array y.

set_runtime_vars(var_dict={})

Set runtime variables for layers.

Parameters:var_dict (dict) – the runtime variables dict.
class poornn.linears.Linear(input_shape, itype, weight, bias, var_mask=(1, 1), is_unitary=False, **kwargs)

Bases: poornn.linears.LinearBase

Dense Linear Layer, \(f = x\cdot W^\dagger + b\)

Parameters:
  • is_unitary (bool, default=False) – keep unitary if True,
  • way to keep unitary during evolution will overload set_variables method. (the) –
is_unitary

bool – keep unitary if True, unitary will overload set_variables method.

be_unitary()

make weight unitary through qr decomposition.

check_unitary(tol=1e-10)

check weight is unitary or not, if not, raise an exception.

Parameters:tol (float, default=1e-10) – the tolerence.
Returns:error rate.
Return type:float
set_runtime_vars(var_dict={})

Set runtime variables for layers.

Parameters:var_dict (dict) – the runtime variables dict.
class poornn.linears.SPLinear(input_shape, itype, weight, bias, var_mask=(1, 1), **kwargs)

Bases: poornn.linears.LinearBase

Sparse Linear Layer, weight now is a sparse matrix..

set_runtime_vars(var_dict={})

Set runtime variables for layers.

Parameters:var_dict (dict) – the runtime variables dict.
class poornn.linears.Apdot(input_shape, itype, weight, bias, var_mask=(1, 1))

Bases: poornn.linears.LinearBase

Apdot swiches roles between multiply and add in linear layer.

set_runtime_vars(var_dict={})

Set runtime variables for layers.

Parameters:var_dict (dict) – the runtime variables dict.