loupe.core.Function#

class loupe.core.Function(*inputs)[source]#

Base class for representing functions that operate on array objects.

Function defines the interface for objects that operate on array objects but it doesn’t actually implement any useful functionality. See Extending Loupe for more details on how to define a new operation by subclassing Function.

Parameters:

inputs (list of array) – Array objects that the function operates on.

here.

Attributes

cache

Data that has been saved by cache_for_backward().

data

The array's data.

dtype

Data type of the array's elements.

inputs

List of array inputs the function operates on.

ndim

requires_grad

Is True if any of the function's inputs have requires_grad=True, False otherwise.

shape

Tuple of array dimensions.

Methods

backward(grad)

Defines a formula for differentiating the operation.

cache_for_backward(*data)

Save data for a future call to backward().

forward()

Performs the operation.

getdata([copy, dtype])

Return the array's data.

backward(grad)[source]#

Defines a formula for differentiating the operation.

This method should be overridden by all subclasses.

Parameters:

grad (array_like) – The gradient with respect to the function output.

property cache#

Data that has been saved by cache_for_backward().

Returns:

cache

Return type:

list

cache_for_backward(*data)[source]#

Save data for a future call to backward().

Saved data can be accessed through the cache attribute.

Parameters:

data (list_like) – List of data to cache. Cached data should probably be ndarrays, but nothing prevents the caching of any Python type.

Warning

Care must be taken that cached data is not modified in place after calling forward(). Doing so will make the cached data stale and its resulting use during a call to backward() will be invalid.

Notes

This method should be called at most once, and only from inside the forward() method.

property data#

The array’s data.

Returns:

data

Return type:

ndarray

property dtype#

Data type of the array’s elements.

forward()[source]#

Performs the operation.

This method should by overridden by all subclasses.

Returns:

out – The result of the operation.

Return type:

ndarray

getdata(copy=False, dtype=None)#

Return the array’s data.

Parameters:
  • copy (bool, optional) – Whether to force a copy of the underlying data to be returned.

  • dtype (type or numpy dtype, optional) – The dtype of the returned data.

property inputs#

List of array inputs the function operates on.

property ndim#
property requires_grad#

Is True if any of the function’s inputs have requires_grad=True, False otherwise.

property shape#

Tuple of array dimensions.