core

Module contents

ABC of neural network.

class poornn.core.Layer(input_shape, output_shape, itype, dtype=None, otype=None, tags=None)

Bases: object

A single layer in Neural Network.

Parameters:
  • input_shape (tuple) – input shape of this layer.
  • output_shape (tuple) – output_shape of this layer.
  • itype (str) – input data type.
  • dtype (str, default=:data:itype) – variable data type.
  • otype (str, default=?) – output data type, if not provided, it will be set to itype, unless its ‘analytical’ tags is 2.
  • tags (dict, default=:data:poornn.core.DEFAULT_TAGS) – tags used to describe this layer, refer poornn.core.TAG_LIST for detail. It change tags based on template poornn.core.DEFAULT_TAGS.
input_shape

tuple – input shape of this layer.

output_shape

tuple – output_shape of this layer.

itype

str – input data type.

dtype

str – variable data type.

otype

str – output data type.

tags

dict – tags used to describe this layer, refer poornn.core.TAG_LIST for detail.

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.

get_variables()

Get current variables.

Returns:1darray,
num_variables

number of variables.

set_runtime_vars(var_dict={})

Set runtime variables for layers.

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

Change current variables.

Parameters:variables (1darray) –
class poornn.core.Function(input_shape, output_shape, itype, dtype=None, otype=None, tags=None)

Bases: poornn.core.Layer

Function layer with no variables.

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.

get_variables()

Get variables, return empty (1d but with length - 0) array.

num_variables

number of variables, which is fixed to 0.

set_runtime_vars(var_dict={})

Set runtime variables for layers.

Parameters:var_dict (dict) – the runtime variables dict.
set_variables(*args, **kwargs)

passed.

class poornn.core.ParamFunction(input_shape, output_shape, itype, params, var_mask, **kwargs)

Bases: poornn.core.Layer

Function layer with params as variables and var_mask as variable mask.

Parameters:
  • params (1darray) – variables used in this functions.
  • var_mask (1darray<bool>, default=(True,True,..)) – mask for params, a param is regarded as a constant if its mask is False.
params

1darray – variables used in this functions.

var_mask

1darray<bool> – mask for params, a param is regarded as a constant if its mask is False.

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.core.Monitor(input_shape, output_shape, itype, dtype=None, otype=None, tags=None)

Bases: poornn.core.Function

A special layer used to monitor a flow, it operate on but do not change the flow.

get_variables()

Get variables, return empty (1d but with length - 0) array.

monitor_backward(xy, dy, **kwargs)

Monitor function used in backward,

Parameters:
  • xy (ndarray) – (input, output(same as input)) data.
  • dy (ndarray) – gradient.
monitor_forward(x)

Monitor function used in forward,

Parameters:x (ndarray) – forward data.
num_variables

number of variables, which is fixed to 0.

set_runtime_vars(var_dict={})

Set runtime variables for layers.

Parameters:var_dict (dict) – the runtime variables dict.
set_variables(*args, **kwargs)

passed.

poornn.core.EXP_OVERFLOW = 12

exp(x>EXP_OVERFLOW) should be taken special care of in order avoid overflow.

poornn.core.EMPTY_VAR = array([], dtype=float32)

Empty variable, 1d array of dtype ‘float32’ and length 0.

exception poornn.core.AnalyticityError

Bases: exceptions.Exception

Behavior conflict with the analytical type of a layer.

__init__

x.__init__(...) initializes x; see help(type(x)) for signature

poornn.core.DEFAULT_TAGS = {'analytical': 1, 'is_inplace': False, 'runtimes': []}

A layer without tags attributes will take this set of tags.

  • no runtime variables,
  • changes for flow are not inplace (otherwise it will destroy integrity of flow history).
  • analytical (for complex numbers, holomophic).
poornn.core.TAG_LIST = ['runtimes', 'is_inplace', 'analytical']

List of tags

  • ‘runtimes’ (list<str>, default=[]):
    runtime variables that should be supplied during each run.
  • ‘is_inplace’ (bool, default=False):
    True if the output is made by changing input inplace.
  • ‘analytical’ (int):
    the analyticaity of a layer. A table of legal values,
    • 1, yes (default)
    • 2, yes for float, no for complex, complex output for real output.
    • 3, yes for float, no for complex, complex output for complex input.
    • 4, no