nets

Module contents

ABC of neural network.

class poornn.nets.ANN(layers=None, labels=None)

Bases: poornn.core.Container

Sequential Artificial Neural network.

add_layer(cls, label=None, **kwargs)

Add a new layer, comparing with self.layers.append()

  • input_shape of new layer is infered from output_shape of last layer.
  • itype of new layer is infered from otype of last layer.
Parameters:
  • cls (class) – create a layer instance, take input_shape and itype as first and second parameters.
  • label (str|None, default=None) – label to index this layer, leave None if indexing is not needed.
  • **kwargs – keyword arguments used by cls.__init__(), excluding input_shape and itype.

Note

if num_layers is 0, this function will raise an error, because it fails to infer input_shape and itype.

Returns:newly generated object.
Return type:Layer
backward(xy, dy=array(1), data_cache=None, do_shape_check=False)

Compute gradients.

Parameters:
  • xy (tuple) – input and output
  • dy (ndarray) – gradient of output defined as \(\partial J/\partial y\).
  • data_cache (dict) – a dict with collected datas.
  • do_shape_check (bool) – check shape of data flow if True.
Returns:

gradients for vairables in layers.

Return type:

list

dtype

variable data shape, which is infered from layers.

forward(x, data_cache=None, do_shape_check=False, **kwargs)

Feed input to this feed forward network.

Parameters:
  • x (ndarray) – input in ‘F’ order.
  • data_cache (dict|None, default=None) – a dict used to collect datas.
  • do_shape_check (bool) – check shape of data flow if True.

Note

data_cache should be pass to this method if you are about to call a subsequent backward() method, because backward need data_cache.

'%d-ys'%id(self) is used as the key to store run-time output of layers in this network. data_cache['%d-ys'%id(self)] is a list with contents outputs in each layers generate in this forward run.

Returns:output in each layer.
Return type:list
get_runtimes()

show runtime variables used in this Container.

get_variables()

Dump values to an array.

num_layers

number of layers.

num_variables

int – number of variables.

set_runtime_vars(var_dict)

Set runtime variables.

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

Load data from an array.

Parameters:v (1darray) – variables.
tags

tags for this Container, which is infered from self.layers.

class poornn.nets.ParallelNN(axis=0, layers=None, labels=None)

Bases: poornn.core.Container

Parallel Artificial Neural network.

Parameters:axis (int, default=0) – specify the additional axis on which outputs are packed.
axis

int – specify the additional axis on which outputs are packed.

add_layer(cls, **kwargs)

add a new layer, comparing with self.layers.append()

  • input_shape of new layer is infered from input_shape of first layer.
  • itype of new layer is infered from itype of first layer.
  • otype of new layer is infered from otype of first layer.
Parameters:
  • cls (class) – create a layer instance, take input_shape and itype as first and second parameters.
  • **kwargs – keyword arguments used by cls.__init__, excluding input_shape and itype.

Note

if self.num_layers is 0, this function will raise an error, because it fails to infer input_shape, itype and otype.

Returns:newly generated object.
Return type:Layer
backward(xy, dy=array(1), do_shape_check=False, **kwargs)

Compute gradients.

Parameters:
  • xy (tuple) – input and output
  • dy (ndarray) – gradient of output defined as \(\partial J/\partial y\).
  • do_shape_check (bool) – check shape of data flow if True.
Returns:

gradients for vairables in layers.

Return type:

list

dtype

variable data shape, which is infered from layers.

forward(x, do_shape_check=False, **kwargs)

Feed input, it will generate a new axis,and storge the outputs of layers parallel along this axis.

Parameters:
  • x (ndarray) – input in ‘F’ order.
  • do_shape_check (bool) – check shape of data flow if True.
Returns:

output,

Return type:

ndarray

get_runtimes()

show runtime variables used in this Container.

get_variables()

Dump values to an array.

num_layers

number of layers.

num_variables

int – number of variables.

set_runtime_vars(var_dict)

Set runtime variables.

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

Load data from an array.

Parameters:v (1darray) – variables.
tags

tags for this Container, which is infered from self.layers.

class poornn.nets.JointComplex(real, imag)

Bases: poornn.core.Container

Function \(f(z) = h(x) + ig(y)\), where \(h\) and \(g\) are real functions. This Container can be used to generate complex layers, but its non-holomophic (analytical type 3).

Parameters:
  • real (Layer) – layer for real part.
  • imag (Layer) – layer for imaginary part.
dtype

variable data shape, which is infered from layers.

get_runtimes()

show runtime variables used in this Container.

get_variables()

Dump values to an array.

imag

the imaginary part layer.

num_layers

number of layers.

num_variables

int – number of variables.

real

the real part layer.

set_runtime_vars(var_dict)

Set runtime variables.

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

Load data from an array.

Parameters:v (1darray) – variables.
class poornn.nets.KeepSignFunc(h, is_real=False)

Bases: poornn.core.Container

Function \(f(z) = h(|z|)\sign(z)\), where \(h\) is a real function. This Container inherit sign from input, so it must have same input and ouput dimension. It can also be used to generate complex layers, but its non-holomophic (analytical type 3).

Parameters:is_real (bool, default=False) – input is real if True.
is_real

bool – input is real if True.

dtype

variable data shape, which is infered from layers.

get_runtimes()

show runtime variables used in this Container.

get_variables()

Dump values to an array.

h

layer applied on amplitude.

num_layers

number of layers.

num_variables

int – number of variables.

set_runtime_vars(var_dict)

Set runtime variables.

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

Load data from an array.

Parameters:v (1darray) – variables.