Expressions

A network is defined as a giant expression object that is built using normal-looking operations, but isn’t evaluated right away:

x = constant(2.)
y = constant(2.)
z = x + y

constant(value) creates an expression whose value is value, typically a NumPy array, and x + y creates a new expression that is the sum of x and y, but doesn’t actually perform the addition yet.

Evaluating

To evaluate an expression:

>>> compute_value(z)
4.0

This also evaluates all the subexpressions of z but discards their values. To get a dictionary mapping from all subexpressions to their values:

>>> values = compute_values(z)
>>> values[z]
4.0

Arrays

The [] operator can be used to get an index or slice of an array.

  • Negative indices (a[-1]) work.

  • Extended slices (a[::2]) work.

  • Ellipses (a[...]) work.

  • numpy.newaxis does not work. Use expand_dims instead.

  • Assignment (a[0] = 1.) does not work. Use setitem instead:

    a = setitem(a, 0, 1.)
    

Note that this creates a new Expression object, and during evaluation, will copy the whole array.

Many NumPy operators and functions have equivalents in Penne that work more or less the same way. See the reference below. Note that none of the methods of numpy.ndarray are implemented in Penne, so, for example, instead of x.dot(y), write dot(x, y).

Penne has one other array operation that NumPy doesn’t have: vecdot(x, y) is equivalent to numpy.sum(x * y). It takes optional axis and keepdims arguments just like numpy.sum. This especially comes in handy when dealing with Minibatching.

Reference

penne.constant(value)[source]

A constant value.

The value can be accessed in the value field, and can be changed between calls to compute_values.

Parameters:value (NumPy array) – The value of the new expression.
penne.compute_value(x)[source]

Evaluate an expression.

Shorthand for compute_values(x)[x].

Parameters:x – expression to evaluate.
Returns:computed value.
penne.compute_values(x, initvalues={})[source]

Evaluate an expression and all its subexpressions.

Parameters:
  • x – expression to evaluate.
  • initvalues – optional dictionary from subexpressions to precomputed values; can be used to continue a previous computation.
Returns:

dictionary from subexpressions to computed values.

penne.setitem(x, item, y)[source]

A new expression with an index or slice modified.

Note that when evaluated, this makes a copy of the whole array.

Parameters:
  • x – array to be indexed
  • item (int or slice, not Expression) – index
  • y – new value

Array creation

penne.zeros(shape)[source]
penne.ones(shape)[source]
penne.full(shape, value)[source]

Elementwise operations

penne.add(x, y)[source]
penne.subtract(x, y)[source]
penne.multiply(x, y)[source]
penne.divide(x, y)[source]
penne.power(x, y)[source]
penne.log(x)[source]
penne.exp(x)[source]
penne.maximum(x, y)[source]
penne.minimum(x, y)[source]
penne.clip(x, lo, hi)[source]

Reductions

penne.asum(x, axis=None, keepdims=False)
penne.amax(x, axis=None, keepdims=False)[source]
penne.amin(x, axis=None, keepdims=False)[source]
penne.mean(x, axis=None, keepdims=False)[source]

Shape-changing operations

penne.concatenate(args, axis=0)[source]
penne.stack(args, axis=0)[source]
penne.transpose(arg, axes=None)[source]
penne.reshape(arg, shape)[source]
penne.expand_dims(arg, axis)[source]

Product-like operations

penne.dot(x, y)[source]
penne.vecdot(x, y, axis=None, keepdims=False)[source]

Equivalent to sum(x*y, axis).

Parameters:
  • axis (int or tuple of ints) – axis or axes along which to perform the sum-product.
  • keepdims (bool) – leave axis or axes in result as dimensions with size one.