# ngraph¶

This API documentation covers the public API for ngraph, organized into three main modules:

• ngraph: Contains the core ops for constructing the graph.
• ngraph.transformers: Defines methods for executing a defined graph on hardware.
• ngraph.types: Types in ngraph (e.g. Axes, Op, etc.)

## ngraph¶

Several ops are used to create different types of tensors

Method Description
ngraph.variable() Create a trainable variable.
ngraph.persistent_tensor() Tensor that persists across computations.
ngraph.placeholder() Used for input values, typically from host.
ngraph.constant() Immutable constant that can be inlined.

Assign the above tensors requires defining Axis, which can be done using the following methods:

Method Description
ngraph.axes_with_order() Return a tensor with a different axes order.
ngraph.cast_axes() Cast the axes of a tensor to new axes.
ngraph.make_axes() Create an Axes object.
ngraph.make_axis() Create an Axis.
ngraph.make_axis_role() Create an Axis role.
ngraph.make_name_scope() Create a name scope for NameableValue objects.
ngraph.name_scope() Create and use a new name scope
ngraph.with_name_scope() Use a name scope

We also provide several helper function for retrieving information from tensors.

Method Description
ngraph.batch_size() Returns the batch size
ngraph.is_constant() Returns true if tensor is constant
ngraph.is_constant_scalar() Returns true if tensor is a constant scalar
ngraph.constant_value() Returns the value of a constant tensor
ngraph.tensor_size() Returns the total size of the tensor

To compose a computational graph, we support the following operations:

Method Description
ngraph.absolute() $$\operatorname{abs}(a)$$
ngraph.negative() $$-a$$
ngraph.sign() if $$x<0$$, $$-1$$; if $$x=0$$, $$0$$; if $$x>0$$, $$1$$
ngraph.add() $$a+b$$
ngraph.reciprocal() $$1/a$$
ngraph.square() $$a^2$$
ngraph.sqrt() $$\sqrt{a}$$
ngraph.cos() $$\cos(a)$$
ngraph.sin() $$\sin(a)$$
ngraph.tanh() $$\tanh(a)$$
ngraph.sigmoid() $$1/(1+\exp(-a))$$
ngraph.exp() $$\exp(a)$$
ngraph.log() $$\log(a)$$
ngraph.safelog() $$\log(a)$$
ngraph.one_hot() Convert to one-hot
ngraph.variance() Compute variance
ngraph.stack() Stack tensors along an axis
ngraph.convolution() Convolution operation
ngraph.pad() Pad a tensor with zeros along each dimension
ngraph.pooling() Pooling operation
ngraph.squared_L2() dot x with itself

Note

Additional operations are supported that are not currently documented, and so are not included in the list above. We will continue to populate this API when the documentation is updated.

## ngraph.transformers¶

Method Description
ngraph.transformers.allocate_transformer() Allocate a transformer.
ngraph.transformers.make_transformer() Generates a transformer using the factory.
ngraph.transformers.make_transformer_factory() Creates a new factory with numpy default.
ngraph.transformers.set_transformer_factory() Sets the Transformer factory used by make_transformer.
ngraph.transformers.transformer_choices() Return the list of available transformers.
ngraph.transformers.Transformer() Produce an executable version of op-graphs.

## ngraph.types¶

Method Description
ngraph.types.AssignableTensorOp() Assign a tensor. Used by ng.placeholder, and more.
ngraph.types.Axis() An Axis labels a dimension of a tensor.
ngraph.types.AxisRole() Labels axis with roles.
ngraph.types.Axes() Axes represent multiple axis dimensions.
ngraph.types.Computation() Computations to attach to transformers.
ngraph.types.NameableValue() Objects that can derive name from the name scope.
ngraph.types.NameScope() Name scope for objects.
ngraph.types.Op() Basic class for ops.
ngraph.types.TensorOp() Base class for ops related to Tensors.

### ngraph¶

Graph construction.

ngraph.absolute(x)[source]

Returns the absolute value of x.

Parameters: x (TensorOp) – A tensor. The absolute value of x. TensorOp
ngraph.add(x, y, name=None)[source]

Returns a TensorOp for the sum of x and y.

Parameters: x (TensorOp) – The first input. y (TensorOp) – The second input. name (String, optional) – A name for the sum. x + y TensorOp
ngraph.as_op(x)[source]

Finds an Op appropriate for x.

If x is an Op, it returns x. Otherwise, constant(x) is returned.

Parameters: x – Some value. Op
ngraph.as_ops(xs)[source]

Converts an iterable of values to a tuple of Ops using as_op.

Parameters: xs – An iterable of values. A tuple of Ops.
ngraph.axes_with_order(x, axes)[source]

Return a tensor with a different axes order.

Parameters: x (TensorOp) – The tensor. axes (Axes) – A permutation of the axes of the tensor. The new tensor. TensorOp
ngraph.batch_size(x)[source]
Parameters: x – A Tensor The size of the batch axis in x.
ngraph.broadcast(x, axes)[source]

Parameters: x (TensorOp) – The tensor. axes – New axes. Tensor with additional axes. TensorOp
ngraph.cast_axes(tensor, axes)[source]

Cast the axes of a tensor to new axes.

Parameters: tensor (TensorOp) – The tensor. axes (Axes) – The new axes. The tensor with new axes. TensorOp
ngraph.constant(const, axes=None, dtype=None)[source]
Makes a constant scalar/tensor. For a tensor, constant provides the opportunity
to supply axes. Scalar/NumPytensor arguments are usually automatically converted to tensors, but constant may be used to supply axes or in the rare cases where constant is not automatically provided.
Parameters: const – The constant, a scalar or a NumPy array. axes – The axes for the constant. dtype (optional) – The dtype to use. An AssignableTensorOp for the constant.
ngraph.constant_value(value)[source]

Returns the constant value of an Op.

Parameters: value (TensorOp) – A constant op.

Returns: The constant value.

ngraph.convolution(conv_params, inputs, filters, axes, docstring=None)[source]
Parameters: conv_params – Dimensions. inputs (TensorOp) – The input tensor. filters (TensorOp) – Filter/kernel tensor. docstring (String, optional) – Documentation for the op. The result of the convolution. TensorOp
ngraph.cos(x)[source]

Returns the cos of x.

Parameters: x (TensorOp) – A tensor. The cos of x. TensorOp
ngraph.exp(x)[source]

Returns the exp of x.

Parameters: x (TensorOp) – A tensor. The exp of x. TensorOp
ngraph.is_constant(value)[source]

Test an Op to see if it is a constant.

Parameters: value – An Op

Returns: True if value is a constant.

ngraph.is_constant_scalar(value)[source]

Tests an Op to see if it is a constant scalar.

Parameters: value – An Op.

Returns: True if value is a constant scalar.

ngraph.log(x)[source]

Returns the log of x.

Parameters: x (TensorOp) – A tensor. The log of x. TensorOp
ngraph.make_axes(axes=())[source]

Makes an Axes object.

Parameters: axes – A list of Axis. An Axes. Axes
ngraph.make_axis(length=None, name=None, batch=False, recurrent=False, match_on_length=False, roles=None, docstring=None)[source]

Returns a new Axis.

Parameters: length (int, optional) – Length of the axis. name (String, optional) – Name of the axis. batch (bool, optional) – This is a batch axis. Defaults to False. recurrent (bool, optional) – This is a recurrent axis. Defaults to False. match_on_length (bool, optional) – This axis will match an axis with the same length. Defaults to False. roles (set, optional) – A set of axis roles for the axis. docstring (String, optional) – A docstring for the axis. A new Axis. Axis
ngraph.make_axis_role(name=None, docstring=None)[source]

Returns a new AxisRole.

Parameters: name (String, optional) – The name for the role. docstring (String, optional) – A docstring for the role. A new AxisRole with the given name and docstring. AxisRole
ngraph.make_name_scope(name)[source]

Creates a NameScope.

When NameableValue objects are assigned to attributes of a NameScope, the name of the object is set to the attribute.

Parameters: name – The name of this NameScope. A NameScope. Namescope
ngraph.name_scope(*args, **kwds)[source]

Create and use a new name scope

Parameters: name_scope – Reuse an existing name scope name – Create a new name scope within the current name scope The new name scope. NameScope
ngraph.negative(x)[source]

Returns the negative of x.

Parameters: x (TensorOp) – tensor. The negative of x. (TensorOp)
ngraph.one_hot(x, axis)[source]
Parameters: x – The one_hot tensor. axis – The hot axis. The op. OneHotOp
ngraph.pad(x, paddings, axes=None)[source]

Pads a tensor with zeroes along each of its dimensions.

Parameters: x – the tensor to be padded paddings – the length of the padding along each dimension. should be an array with the same length as x.axes. Each element of the array should be either an integer, in which case the padding will be symmetrical, or a tuple of the form (before, after) axes – the axes to be given to the padded tensor. If unsupplied, we create anonymous axes of the correct lengths. symbolic expression for the padded tensor TensorOp
ngraph.persistent_tensor(axes, dtype=None, initial_value=None)[source]

Persistent storage.

Storage that will retain its value from computation to computation.

Parameters: axes (Axes) – The axes of the persistent storage. dtype (optional) – The dtype of the persistent storage. initial_value (optional) – A host constant or callable. If callable, will be called to generate an initial value. The persistent storage. AssignableTensorOp
ngraph.placeholder(axes, dtype=None, initial_value=None)[source]

A persistent tensor to be initialized from the CPU.

Parameters: axes (Axes) – The axes of the placeholder. dtype (optional) – The dtype of the placeholder. initial_value (optional) – A host constant or callable. If callable, will be called to generate an initial value. The placeholder. AssignableTensorOp
ngraph.pooling(poolparams, inputs, axes, docstring=None)[source]
Parameters: poolparams – Dimensions. inputs (TensorOp) – Input to pooling. docstring (String, optional) – Dcoumentation for the computation. The pooling computation. TensorOp
ngraph.reciprocal(x)[source]

Returns the reciprocal of x.

Parameters: x (TensorOp) – A tensor. The reciprocal of x. TensorOp
ngraph.safelog(x, limit=1.9287498479639178e-22)[source]
ngraph.sigmoid(x)[source]

sigmoid(x)

:math:

rac{1}{1+exp(-x)}

Arguments:
x: A tensor
Returns:
TensorOp: sigmoid(x).
ngraph.sign(x)[source]

Returns the sign of x.

Parameters: x (TensorOp) – A tensor. The sign of x. TensorOp
ngraph.sin(x)[source]

Returns the sin of x.

Parameters: x (TensorOp) – A tensor. sin of x. TensorOp
ngraph.slice_along_axis(x, axis, idx)[source]

Returns a slice of a tensor constructed by indexing into a single axis at a single position. If the axis occurs multiple times in the dimensions of the input tensor, we select only on the first occurrence. :param x: input tensor :param axis: axis along which to slice :param idx: index to select from the axis

Returns: a slice of x y
ngraph.sqrt(x)[source]

Returns the square root of x.

Parameters: x (TensorOp) – A tensor. The square root of x. TensorOp
ngraph.square(x)[source]

Returns the square of x.

Parameters: x (TensorOp) – A tensor. The square of x. TensorOp
ngraph.squared_L2(x)[source]

Returns the dot of x and y, with the axes of x set to their dual offset.

Parameters: x (TensorOp) – The first value, axes shifted down by 1. y (TensorOp) – The second value. The result. TensorOp
ngraph.stack(x_list, axis, pos=0)[source]
Parameters: x_list – A list of identically-axed tensors to join. axis – The axis to select joined tensors. pos – The position within the axes of the x_list tensors to insert axis in the result. The joined tensors. TensorOp
ngraph.tanh(x)[source]

Returns the cos of x.

Parameters: x (TensorOp) – A tensor. The tanh of x. TensorOp
ngraph.temporary(axes, dtype=None, initial_value=None)[source]

Temporary storage.

Statically allocates storage that may be reused outside of the scope of the values.

Parameters: axes (Axes) – The axes of the storage. dtype (optional) – The dtype of the storage. initial_value (optional) – A host constant or callable. If callable, will be called to generate an initial value. The placeholder. AssignableTensorOp
ngraph.tensor_size(x, reduction_axes=None, out_axes=None)[source]

A scalar returning the total size of a tensor in elements.

Parameters: x – The tensor whose axes we are measuring. reduction_axes – if supplied, return the size of these axes instead.
ngraph.variable(axes, dtype=None, initial_value=None)[source]

A trainable tensor.

Parameters: axes (Axes) – Axes for the variable. dtype (optional) – The dtype for the tensor. initial_value – A constant or callable. If a callable, the callable will be called to provide an initial value. The variable. AssignableTensorOp
ngraph.variance(x, out_axes=None, reduction_axes=None)[source]
ngraph.with_name_scope(fun, name=None)[source]

Function annotator for introducing a name scope.

Parameters: fun – The function being annotated. name (String) – The name scope name, defaults to the function name The annotated function.

### ngraph.transformers¶

Transformer manipulation.

ngraph.transformers.allocate_transformer(name, **kargs)[source]

Allocate a named backend.

ngraph.transformers.make_transformer()[source]

Generates a Transformer using the factory in this module which defaults to NumPy

Returns: Transformer

ngraph.transformers.make_transformer_factory(name, **kargs)[source]
ngraph.transformers.set_transformer_factory(factory)[source]

Sets the Transformer factory used by make_transformer

Parameters: factory (object) – Callable object which generates a Transformer
ngraph.transformers.transformer_choices()[source]

Return the list of available transformers.

class ngraph.transformers.Transformer(fusion=None, **kwargs)[source]

Produce an executable version of op-graphs.

Computations are subsets of Ops to compute. The transformer determines storage allocation and transforms the computations and allocations into functions.

Parameters: fusion (bool) – Whether to combine sequences of operations into one operation. **kwargs – Args for related classes.
computations

set of Computation – The set of requested computations.

all_results

set of ngraph.op_graph.op_graph.Op – A root set of Ops that need to be computed.

finalized

bool – True when transformation has been performed.

initialized

bool – True when variables have been initialized/restored.

opids

dict – TODO

fusion

bool – True when fusion was enabled.

device_buffers

set – Set of handles for storage allocations.

cpu_initializations

list – Initializations to be performed from the CPU after allocation.

init_computation

Computation – The computation that performs initialization after allocation. This happens once per training session, not once per-minibatch.

allocate()[source]

Allocate storage and then initializes constants.

Will finalize if not already done.

allocate_storage()[source]

Allocate storage on the device.

computation(results, *parameters, **kwargs)[source]

Adds a computation to the transformer.

Parameters: results – Values to be computed *parameters – Values to be set as arguments to evaluate name – Name for function. Defaults to None. Dictionary from results to their values
device_buffer_reference()[source]

Make a DeviceBufferReference.

Returns: A DeviceBufferReference.

device_buffer_storage(bytes, dtype, name)[source]

Make a DeviceBuffer.

Parameters: bytes – Size of buffer. dtype – dtype of buffer. name – Name of the storage variable

returns: A DeviceBuffer.

finish_transform()[source]

Finish generating the model.

finish_transform_allocate()[source]

Called after last allocation is transformed.

initialize()[source]

Initialize storage. Will allocate if not already performed.

initialize_constant(s, dispatch_arg, *args, **kwargs)[source]
initialize_tensor_descriptions(s, dispatch_arg, *args, **kwargs)[source]

Ensures that tensor descriptions associated with op are initialized.

Parameters: (class (op) – ngraph.op_graph.op_graph.Op): Initialize the tensor description for op.
next()
ordered_initializers(ordered_ops)[source]

TODO.

Parameters: ordered_ops – TODO

Returns:

register_graph_pass(graph_pass)[source]
run_registered_graph_passes(ops)[source]
start_transform_allocate()[source]

Called just before allocation code is transformed.

transform_ordered_ops(ordered_ops)[source]

Generate code to compute ordered_ops.

Arguments: ordered_ops: Ops to compute

Returns: Handle for generated code

transformers = {'numpy': <class 'ngraph.transformers.nptransform.NumPyTransformer'>}

### ngraph.types¶

class ngraph.types.AssignableTensorOp(initial_value=None, input=False, persistent=False, **kwargs)[source]

Value comes directly from storage.

Parameters: input – The storage is used as an input from the CPU. Implies persistent. initial_value – If callable, a function that generates an Op whose tensor should be used as the initial value. Otherwise an Op that should be used as the initial value.
input

bool – The storage is used as an input.

add_initializer(init)
add_other_dep(dep)
add_schema(schema, set_generate_adjoints=True)

Adds a description of some op substructure.

When a function generates a groups of nodes, it can add a schema describing the roles of these nodes. The schema may include its own generate_adjoints.

with the version from the schema.
Returns: TODO
adjoints(*args, **kwargs)

Returns a map containing the adjoints of this op with respect to other ops.

Creates the map if it does not already exist.

Parameters: error (TensorOp, optional) – The tensor holding the error value the derivative will be computed at. Must have the same axes as dependent. Map from Op to dSelf/dOp.
all_ops(*args, **kwds)

Collects all Ops created within the context. Does not hide ops created in this context from parent contexts.

append_axis(axis)
args

All the inputs to this node.

assignable

Returns – True if the tensor can be assigned to.

axes

Returns – The axes of the tensor.

call_info(*args, **kwargs)

Creates the TensorDescriptions (of this op or its arguments) required to evaluate it.

The list is used to allocate buffers (in the transformers) and supply values to the transform method (in the transform_call_info) method.

Only TensorDescriptions of the arguments are necessary. A TensorDescription of the output is generate by calling self.tensor_description()

captured_ops(*args, **kwds)

Capture all Ops created within the context. Hides ops created in this context from parent contexts.

defs

Returns – AssignableTensorOp is not executed, so its appearance in the instruction stream does not affect liveness of its value.

device_op

Returns the op that performs the operations on the device.

Returns: self

file_info

Return file location that created the node.

Returns: String with file location that created the node.
find_schema(t)

Find a schema of particular type.

Searches added schema for one of type t.

Parameters: t – The type of schema desired. A schema of type t, or None.
forward

If not None, self has been replaced with forward.

When set, invalidates cached tensor descriptions.

Returns: None or the replacement.
forwarded

Finds the op that handles this op.

Returns: Follows forwarding to the op that shoud handle this op.
generate_add_delta(adjoints, delta)

Adds delta to the backprop contribution..

Parameters: adjoints – dy/dOp for all Ops used to compute y. delta – Backprop contribute.
generate_adjoints(adjoints, delta, *args)

With delta as the computation for the adjoint of this Op, incorporates delta into the adjoints for thr args.

Parameters: adjoints – dy/dOp for all ops involved in computing y. delta – Backprop amount for this Op. *args – The args of this Op.
graph_label

The label used for drawings of the graph.

has_axes

Returns – True if axes have been set.

insert_axis(index, axis)

Inserts an axis :param index: Index to insert at :param axis: The Axis object to insert

is_device_op

Returns – False, because this is handled by the transformer.

is_scalar
mean(reduction_axes=None, out_axes=None)

Used in Neon front end.

Returns: mean(self)

name

The name.

named(name)
next()
ordered_ops(results)

depth-first, post-order “Bottom Up” traversal of Ops in results.

Ops will only appear once in result.

Parameters: results – a list of ops which are the roots of the graph traversal list of Ops in depth-first, post-order
persistent

Returns – True if value is not shared and is retained through computation. Always true for a reference.

replace_self(rep)
saved_user_deps(*args, **kwds)

Switches the user_deps map within a context.

The user_deps of an Op are Ops that must run before the Op is used. When Ops are generated outside of the normal stream, such as initializions that run once before any computation, they must be isolated from the normal tracking of variable pre-dependencies.

Parameters: user_deps_map – The new user deps map to use. If not provided, one is created returned. (and) –
scalar_op
shape

This is required for parameter initializers in legacy neon code. It expects layers to implement a shape that it can use to pass through layers.

Returns: self.axes

shape_dict()

Retuns: shape of this tensor as a dictionary

short_name
tensor_description(*args, **kwargs)

Returns a TensorDescription describing the output of this TensorOp

Returns: TensorDescription for this op.
update_forwards()

Updates internal op references with their forwarded versions.

user_deps

return: Set of Ops the must come before this Op is used. See SetItem.

value

Returns a handle to the device tensor.

The transformer must have been initialized.

Returns: A handle to the device tensor.
variables(filter=None)

Return all trainable Ops used in computing this node.

Parameters: filter – Boolean filter of op, defaults to trainable. Set of trainable Ops.
visit_input_closure(roots, fun)

“Bottom-up” post-order traversal of root and their inputs.

Nodes will only be visited once, even if there are multiple routes to the same Node.

Parameters: roots – root set of nodes to visit fun – Function to call on each visited node None
class ngraph.types.Axis(length=None, batch=False, recurrent=False, match_on_length=False, roles=None, **kwargs)[source]

An Axis labels a dimension of a tensor. The op-graph uses the identity of Axis objects to pair and specify dimensions in symbolic expressions. This system has several advantages over using the length and position of the axis as in other frameworks:

1) Convenience. The dimensions of tensors, which may be nested deep in a computation graph, can be specified without having to calculate their lengths.

2) Safety. Axis labels are analogous to types in general-purpose programming languages, allowing objects to interact only when they are permitted to do so in advance. In symbolic computation, this prevents interference between axes that happen to have the same lengths but are logically distinct, e.g. if the number of training examples and the number of input features are both 50.

Parameters: length – The length of the axis. batch – Whether the axis is a batch axis. recurrent – Whether the axis is a recurrent axis. match_on_length – Whether to only use length (and not identity) when comparing equality against other Axis values. This is useful for anonymous Axis of constant tensors.
T
add_role(axis_role)[source]
Parameters: axis_role –

Returns:

axes
dual_level

Returns – Axis displacement for dot.

In dot, left axis of level n matches right axis of level n+1. Level n-1 is the dual space of level n.

get_dual(offset=-1)[source]

Returns a dual for an axis.

The dual of an axis in the left side of a dot product matches the axis in the right side.

Parameters: axis (Axis) – offset (int, optional) – The dual offset from axis. Defaults to -1. The dual of the axis. (Axis)
graph_label

The label used for drawings of the graph.

has_role(axis_role)[source]
Parameters: axis_role – A role to test. True if this axis has the role.
is_batch

Tests if an axis is a batch axis.

Returns: True if the axis is a batch axis. bool
is_recurrent

Tests if an axis is a recurrent axis.

Returns: True if the axis is a recurrent axis. bool
length

Returns – The length of the axis.

match_on_length

Returns – bool: True if this axis matches axes with the same length.

name

The name.

named(name)
next()
primary_axis
roles

Returns – The AxisRoles of this axis.

short_name
class ngraph.types.AxisRole(**kwargs)[source]

An AxisRole is like a type for an Axis, such as “Height” or “Channels”.

At different parts of a computation, axes of different length may be used for a role. For example, after a convolution the height axis is usually shortened, and in a convolution filter, the height axis of the filter is related to the height axis of the input and output, but not the height. By matching AxisRoles, operations such as convolution can match the axes in their arguments.

graph_label

The label used for drawings of the graph.

name

The name.

named(name)
next()
short_name
class ngraph.types.Axes(axes=None)[source]

An Axes is a tuple of Axis objects used as a label for a tensor’s dimensions.

T
append(axis)[source]

Appends an axis

Parameters: other – The Axis object to append.
batch_axes()[source]
Returns: The Axes subset that are batch axes.
static check_broadcast(*args)[source]

Checks whether axes can be broadcasted to new_axes. We require that the components of axes be laid out in the same order in new_axes.

Axes:
axes: The original axes. new_axes: The broadcasted axes.
Returns: True if axes can be broadcasted to new_axes, False otherwise.
static check_flatten(*args)[source]

Checks whther axes can safely be flattened to produce new_axes. The requirements are that the components of axes should all be present in new_axes and that they should be laid out in the same order.

Parameters: axes – The original axes. new_axes – The flattened axes. True if axes can be safely flattened to new_axes, False otherwise.
static check_unflatten(*args)[source]

Checks whether axes can safely be unflattened to produce new_axes. The requirements are that the components of axes should all be present in new_axes and that they should be laid out in the same order.

Parameters: axes – The original axes. new_axes – The unflattened axes. True if axes can be safely unflattened to new_axes, False otherwise.
static find(*args)[source]

Attempts to locate a subsequence of Axes (sub_axes) in axes.

Parameters: axes – The superset of Axes. sub_axes – Axes to search for. The index at which the subsequence sub_axes occurs in axes. int
static find_axis(axes, axis)[source]

Attempts to locate an axis in Axes.

Parameters: axes – The superset of Axes. axis – Axis to search for. The index at which the axis occurs in axes. int
flatten()[source]
full_lengths

Returns all information about the lengths of the axis objects in this Axes in the form of a nested tuple. An element of the outer tuple that is itself a tuple contains the restored lengths of axes that have been flattened in this Axis object.

Returns: A nested tuple with the axis lengths. tuple
get_dual(dual_offset=-1)[source]
has_same_axes(axes)[source]

Checks whether axes have the same set of axes as self.

Parameters: axes (Axes) – axes. True if axes has the same elements, False otherwise.
index(axis)[source]

Returns the index of an axis.

Parameters: axis – The axis to search for. The index.
insert(index, axis)[source]

Inserts an axis :param index: Index to insert at :param axis: The Axis object to insert

intersect(axes)[source]

Returns the intersection of the elements, leaving out duplicate Axes.

Parameters: axes – second axes to intersect The ordered intersection
lengths

Returns – tuple: The lengths of the outer axes.

static linear_map_axes(*args)[source]

For tensors out = dot(T, in) used in linear transformations determines the axes T must have.

Parameters: in_axes – The axes of in. out_axes – The axes of out. Axes of the weights used in the transformation.
names

Returns – tuple: The names of the outer axes.

next()
recurrent_axes()[source]
Returns: The Axes subset that are recurrent axes.
role_axes(role)[source]
Returns: The Axes subset that have the specified role
sample_axes()[source]
Returns: The Axes subset that are not batch axes.
set_shape(shape)[source]
shape_dict()[source]
Retuns:
dict: A dictionary with names of the axes as keys and lengths as values
size

TODO.

class ngraph.types.Computation(transformer, returns, *args, **kwargs)[source]

A handle for a computation function.

Parameters: (obj (transformer) – Transformer): The associated transformer. returns – If an Op, return the value of the Op, if sequence of Ops, return the sequence of values, if a set return a map, if None, return None. *args – AllocationOps marked input will be arguments to the function. **kwargs – Args for related classes.
graph_label

The label used for drawings of the graph.

name

The name.

named(name)
next()
short_name
transform()[source]

Transforms the computation so that it can be run.

class ngraph.types.NameableValue(name=None, graph_label_type=None, docstring=None, **kwargs)[source]

An object that can be named.

Parameters: graph_label_type – A label that should be used when drawing the graph. Defaults to the class name. name (str) – The name of the object. **kwargs – Parameters for related classes.
graph_label_type

A label that should be used when drawing the graph.

id

Unique id for this object.

graph_label

The label used for drawings of the graph.

name

The name.

named(name)[source]
next()
short_name
class ngraph.types.NameScope(**kwargs)[source]

A NameScope is a hierarchical namespace for objects.

When NameableValues are assigned to attributes, the value’s name is set to the attribute name.

Parameters: name – The name of this scope. **kwargs – Parameters for related classes.
graph_label

The label used for drawings of the graph.

name

The name.

named(name)
next()
short_name
class ngraph.types.Op(args=(), metadata=None, const=None, constant=False, initializers=None, persistent=True, reference=False, trainable=False, **kwargs)[source]

Any operation that can be in an AST.

Parameters: args – Values used by this node. const – The value of a constant Op, or None, constant (bool) – The Op is constant. Default False. forward – If not None, the node to use instead of this node. initializers – List of one-time initializations to run before the op. persistent (bool) – The value will be retained from computation to computation and not shared. Default False. reference (bool) – The storage is accessed via a reference. Default False. metadata – String key value dictionary for frontend metadata. trainable (bool) – The value is trainable. Default False. kwargs – Args defined in related classes.
const

The value of a constant.

constant

bool – The value is constant.

initializers

list – Additional Ops to run before this Op is run the first time.

other_deps

OrderedSet – Ops in addtion to args that must run before this op.

persistent

bool – The value will be retained from computation to computation and not shared. Always True if reference is set.

reference

bool – The storage is accessed via a reference. Implies persistent.

schemas

Information about how the Op was generated.

metadata

Dictionary with of string keys and values used for attaching arbitrary metadata to nodes.

trainable

The value is trainable.

add_initializer(init)[source]
add_other_dep(dep)[source]
add_schema(schema, set_generate_adjoints=True)[source]

Adds a description of some op substructure.

When a function generates a groups of nodes, it can add a schema describing the roles of these nodes. The schema may include its own generate_adjoints.

with the version from the schema.
Returns: TODO
adjoints(*args, **kwargs)[source]

Returns a map containing the adjoints of this op with respect to other ops.

Creates the map if it does not already exist.

Parameters: error (TensorOp, optional) – The tensor holding the error value the derivative will be computed at. Must have the same axes as dependent. Map from Op to dSelf/dOp.
static all_ops(*args, **kwds)[source]

Collects all Ops created within the context. Does not hide ops created in this context from parent contexts.

args

All the inputs to this node.

assignable

Returns – True if the tensor can be assigned to.

call_info(*args, **kwargs)[source]

Creates the TensorDescriptions (of this op or its arguments) required to evaluate it.

The list is used to allocate buffers (in the transformers) and supply values to the transform method (in the transform_call_info) method.

Only TensorDescriptions of the arguments are necessary. A TensorDescription of the output is generate by calling self.tensor_description()

static captured_ops(*args, **kwds)[source]

Capture all Ops created within the context. Hides ops created in this context from parent contexts.

defs

Returns – For liveness analysis. The storage associated with everything in the returned list is modified when the Op is executed.

device_op

Returns the op that performs the operations on the device.

Returns: self

file_info

Return file location that created the node.

Returns: String with file location that created the node.
find_schema(t)[source]

Find a schema of particular type.

Searches added schema for one of type t.

Parameters: t – The type of schema desired. A schema of type t, or None.
forward

If not None, self has been replaced with forward.

When set, invalidates cached tensor descriptions.

Returns: None or the replacement.
forwarded

Finds the op that handles this op.

Returns: Follows forwarding to the op that shoud handle this op.
graph_label

The label used for drawings of the graph.

is_device_op

Returns – True if the Op executes on the device.

is_scalar
name

The name.

named(name)
next()
static ordered_ops(results)[source]

depth-first, post-order “Bottom Up” traversal of Ops in results.

Ops will only appear once in result.

Parameters: results – a list of ops which are the roots of the graph traversal list of Ops in depth-first, post-order
persistent

Returns – True if value is not shared and is retained through computation. Always true for a reference.

replace_self(rep)[source]
static saved_user_deps(*args, **kwds)[source]

Switches the user_deps map within a context.

The user_deps of an Op are Ops that must run before the Op is used. When Ops are generated outside of the normal stream, such as initializions that run once before any computation, they must be isolated from the normal tracking of variable pre-dependencies.

Parameters: user_deps_map – The new user deps map to use. If not provided, one is created returned. (and) –
scalar_op
short_name
tensor_description()[source]
update_forwards()[source]

Updates internal op references with their forwarded versions.

user_deps

return: Set of Ops the must come before this Op is used. See SetItem.

variables(filter=None)[source]

Return all trainable Ops used in computing this node.

Parameters: filter – Boolean filter of op, defaults to trainable. Set of trainable Ops.
static visit_input_closure(roots, fun)[source]

“Bottom-up” post-order traversal of root and their inputs.

Nodes will only be visited once, even if there are multiple routes to the same Node.

Parameters: roots – root set of nodes to visit fun – Function to call on each visited node None
class ngraph.types.TensorOp(dtype=None, axes=None, scale=None, **kwargs)[source]

Super class for all Ops whose value is a Tensor.

Parameters: axes – The axes of the tensor. dtype – The element type of the tensor. scale – If specified, a scaling factor applied during updates. **kwargs – Arguments for related classes.
add_initializer(init)
add_other_dep(dep)
add_schema(schema, set_generate_adjoints=True)

Adds a description of some op substructure.

When a function generates a groups of nodes, it can add a schema describing the roles of these nodes. The schema may include its own generate_adjoints.

with the version from the schema.
Returns: TODO
adjoints(*args, **kwargs)

Returns a map containing the adjoints of this op with respect to other ops.

Creates the map if it does not already exist.

Parameters: error (TensorOp, optional) – The tensor holding the error value the derivative will be computed at. Must have the same axes as dependent. Map from Op to dSelf/dOp.
all_ops(*args, **kwds)

Collects all Ops created within the context. Does not hide ops created in this context from parent contexts.

append_axis(axis)[source]
args

All the inputs to this node.

assignable

Returns – True if the tensor can be assigned to.

axes

Returns – The axes of the tensor.

call_info(*args, **kwargs)

Creates the TensorDescriptions (of this op or its arguments) required to evaluate it.

The list is used to allocate buffers (in the transformers) and supply values to the transform method (in the transform_call_info) method.

Only TensorDescriptions of the arguments are necessary. A TensorDescription of the output is generate by calling self.tensor_description()

captured_ops(*args, **kwds)

Capture all Ops created within the context. Hides ops created in this context from parent contexts.

defs

Returns – For liveness analysis. The storage associated with everything in the returned list is modified when the Op is executed.

device_op

Returns the op that performs the operations on the device.

Returns: self

file_info

Return file location that created the node.

Returns: String with file location that created the node.
find_schema(t)

Find a schema of particular type.

Searches added schema for one of type t.

Parameters: t – The type of schema desired. A schema of type t, or None.
forward

If not None, self has been replaced with forward.

When set, invalidates cached tensor descriptions.

Returns: None or the replacement.
forwarded

Finds the op that handles this op.

Returns: Follows forwarding to the op that shoud handle this op.
generate_add_delta(adjoints, delta)[source]

Adds delta to the backprop contribution..

Parameters: adjoints – dy/dOp for all Ops used to compute y. delta – Backprop contribute.
generate_adjoints(adjoints, delta, *args)[source]

With delta as the computation for the adjoint of this Op, incorporates delta into the adjoints for thr args.

Parameters: adjoints – dy/dOp for all ops involved in computing y. delta – Backprop amount for this Op. *args – The args of this Op.
graph_label

The label used for drawings of the graph.

has_axes

Returns – True if axes have been set.

insert_axis(index, axis)[source]

Inserts an axis :param index: Index to insert at :param axis: The Axis object to insert

is_device_op

Returns – True if the Op executes on the device.

is_scalar
mean(reduction_axes=None, out_axes=None)[source]

Used in Neon front end.

Returns: mean(self)

name

The name.

named(name)
next()
ordered_ops(results)

depth-first, post-order “Bottom Up” traversal of Ops in results.

Ops will only appear once in result.

Parameters: results – a list of ops which are the roots of the graph traversal list of Ops in depth-first, post-order
persistent

Returns – True if value is not shared and is retained through computation. Always true for a reference.

replace_self(rep)
saved_user_deps(*args, **kwds)

Switches the user_deps map within a context.

The user_deps of an Op are Ops that must run before the Op is used. When Ops are generated outside of the normal stream, such as initializions that run once before any computation, they must be isolated from the normal tracking of variable pre-dependencies.

Parameters: user_deps_map – The new user deps map to use. If not provided, one is created returned. (and) –
scalar_op
shape

This is required for parameter initializers in legacy neon code. It expects layers to implement a shape that it can use to pass through layers.

Returns: self.axes

shape_dict()[source]

Retuns: shape of this tensor as a dictionary

short_name
tensor_description(*args, **kwargs)[source]

Returns a TensorDescription describing the output of this TensorOp

Returns: TensorDescription for this op.
update_forwards()

Updates internal op references with their forwarded versions.

user_deps

return: Set of Ops the must come before this Op is used. See SetItem.

value

Returns a handle to the device tensor.

The transformer must have been initialized.

Returns: A handle to the device tensor.
variables(filter=None)

Return all trainable Ops used in computing this node.

Parameters: filter – Boolean filter of op, defaults to trainable. Set of trainable Ops.
visit_input_closure(roots, fun)

“Bottom-up” post-order traversal of root and their inputs.

Nodes will only be visited once, even if there are multiple routes to the same Node.

Parameters: roots – root set of nodes to visit fun – Function to call on each visited node None
class ngraph.types.Transformer(fusion=None, **kwargs)[source]

Produce an executable version of op-graphs.

Computations are subsets of Ops to compute. The transformer determines storage allocation and transforms the computations and allocations into functions.

Parameters: fusion (bool) – Whether to combine sequences of operations into one operation. **kwargs – Args for related classes.
computations

set of Computation – The set of requested computations.

all_results

set of ngraph.op_graph.op_graph.Op – A root set of Ops that need to be computed.

finalized

bool – True when transformation has been performed.

initialized

bool – True when variables have been initialized/restored.

opids

dict – TODO

fusion

bool – True when fusion was enabled.

device_buffers

set – Set of handles for storage allocations.

cpu_initializations

list – Initializations to be performed from the CPU after allocation.

init_computation

Computation – The computation that performs initialization after allocation. This happens once per training session, not once per-minibatch.

allocate()[source]

Allocate storage and then initializes constants.

Will finalize if not already done.

allocate_storage()[source]

Allocate storage on the device.

computation(results, *parameters, **kwargs)[source]

Adds a computation to the transformer.

Parameters: results – Values to be computed *parameters – Values to be set as arguments to evaluate name – Name for function. Defaults to None. Dictionary from results to their values
device_buffer_reference()[source]

Make a DeviceBufferReference.

Returns: A DeviceBufferReference.

device_buffer_storage(bytes, dtype, name)[source]

Make a DeviceBuffer.

Parameters: bytes – Size of buffer. dtype – dtype of buffer. name – Name of the storage variable

returns: A DeviceBuffer.

finish_transform()[source]

Finish generating the model.

finish_transform_allocate()[source]

Called after last allocation is transformed.

initialize()[source]

Initialize storage. Will allocate if not already performed.

initialize_constant(s, dispatch_arg, *args, **kwargs)[source]
initialize_tensor_descriptions(s, dispatch_arg, *args, **kwargs)[source]

Ensures that tensor descriptions associated with op are initialized.

Parameters: (class (op) – ngraph.op_graph.op_graph.Op): Initialize the tensor description for op.
next()
ordered_initializers(ordered_ops)[source]

TODO.

Parameters: ordered_ops – TODO

Returns:

register_graph_pass(graph_pass)[source]
run_registered_graph_passes(ops)[source]
start_transform_allocate()[source]

Called just before allocation code is transformed.

transform_ordered_ops(ordered_ops)[source]

Generate code to compute ordered_ops.

Arguments: ordered_ops: Ops to compute

Returns: Handle for generated code

transformers = {'numpy': <class 'ngraph.transformers.nptransform.NumPyTransformer'>}