Skip to content

expressions.py


pycmtensor.expressions

The code snippet is a part of the PyCMTensor expressions module. It defines a base class for parsing and manipulating Aesara tensor expressions. The class provides methods for parsing a tensor expression to remove parentheses and tensor operators, and returns a clean list of keywords found in the expression. It also defines a base class for expression objects, which includes overloaded operators for tensor operations such as addition, subtraction, multiplication, division, and comparison.

ExpressionParser(expression=None)

Bases: object

Base class for parsing and manipulating Aesara tensor expressions.

Parameters:

Name Type Description Default
expression TensorVariable

The tensor expression to parse. Defaults to None.

None

parse(expression) staticmethod

Parses a tensor expression to remove parentheses and tensor operators.

Parameters:

Name Type Description Default
expression TensorVariable

The symbolic tensor object to parse.

required

Returns:

Name Type Description
list

The clean list of keywords found in the expression.

TensorExpressions()

Base class for expression objects

Param(name, value=0.0, lb=None, ub=None, status=0)

Bases: TensorExpressions

Constructor for model param object

Parameters:

Name Type Description Default
name str

name of parameter

required
value float

the default value of the parameter

0.0
lb float

value lower bound

None
ub float

value upper bound

None
status int

if 1, do not estimate this parameter

0

Attributes:

Name Type Description
init_value float

the inital value set at object creation

shape list

the shape of the Param

Raises:

Type Description
ValueError

If lb is greater than ub

Note

init_value is an immutable property

name property

Returns the name of the object

init_value property

Returns the initial value of the parameter

status property

Returns the status of the parameter

shape property

Returns the shape of the initial value of the parameter

__call__()

Returns the shared value of the parameter

__repr__()

Returns a string representation of the parameter

get_value()

Returns the numpy representation of the parameter value

set_value(value)

Sets the value of the shared variable

reset_value()

Resets the value of the shared variable to the initial value

Beta(name, value=0.0, lb=None, ub=None, status=0)

Bases: Param

Constructor for Beta parameter

Parameters:

Name Type Description Default
name str

The name of the Beta parameter.

required
value float

The default value of the Beta parameter.

0.0
lb float

The lower bound of the Beta parameter.

None
ub float

The upper bound of the Beta parameter.

None
status int

The status of the Beta parameter.

0

Example

Specifying a Beta parameter:

b_time = Beta("b_time", value=0., lb=None, ub=0., status=0)

To obtain the raw array value from the Beta parameter:

b_time = Beta("b_time", value=-1., lb=None, ub=0., status=0)
b_time.get_value()

output:

array(-1.)

RandomDraws(name, draw_type, n_draws)

Bases: TensorExpressions

Constructor for the RandomDraws class.

Parameters:

Name Type Description Default
name str

The name of the RandomDraw object.

required
draw_type str

The distribution of the draw. Can be "normal", "lognormal", "gumbel", "exponential", "gamma", or "poisson".

required
n_draws int

The number of draws, which determines the size of the shared tensor.

required

Raises:

Type Description
NotImplementedError

If an unsupported draw_type is provided.

Returns:

Type Description

None. The method initializes the object and creates a shared variable.

name property

returns the name of the random draw tensor variable

Bias(name, size, value=None)

Bases: Param

Class object for neural net bias vector

Parameters:

Name Type Description Default
name str

The name of the parameter.

required
size Union[tuple, list]

The size of the array in 1 dimension.

required
value ndarray

The initial values of the parameter. If None is given, it defaults to 0.

None

Weight(name, size, value=None, init_type=None)

Bases: Param

Class object for neural net weight matrix

Parameters:

Name Type Description Default
name str

name of the parameter

required
size Union[tuple, list]

size of the array

required
value ndarray

initial values of the parameter. Defaults to random.uniform(-0.1, 0.1, size)

None
init_type str

initialization type, see notes

None
Note

Initialization types are one of the following:

  • "zeros": a 2-D array of zeros

  • "he": initialization method for neural networks that takes into account the non-linearity of activation functions, e.g. ReLU or Softplus [^1]

  • "glorot": initialization method that maintains the variance for symmetric activation functions, e.g. sigm, tanh [^2]

[^1] He, K., Zhang, X., Ren, S. and Sun, J., 2015. Delving deep into rectifiers: Surpassing human-level performance on imagenet classification. In Proceedings of the IEEE international conference on computer vision (pp. 1026-1034). [^2] Glorot, X. and Bengio, Y., 2010, March. Understanding the difficulty of training deep feedforward neural networks. In Proceedings of the thirteenth international conference on artificial intelligence and statistics (pp. 249-256). JMLR Workshop and Conference Proceedings.

Example

Specifying a weight array:

code