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. Useexpand_dims
instead.Assignment (
a[0] = 1.
) does not work. Usesetitem
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 tocompute_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