Variable

class webdnn.graph.variable.Variable(shape, order)[source]

Data container class which is input to and output from operators.

Variable supports basic unary and binary operations.

x1 = Variable((2, 3), OrderNC)
x2 = Variable((3, 2), OrderCN)
h = x1 + x2
y = abs(h)

# variables are connected as follows:
#
#   x1 -+
#       +-[ElementwiseAdd]-> h -[Abs]-> y
#   x2 -+
change_order(order: webdnn.graph.order.Order) → webdnn.graph.variable.Variable[source]

change_order_statement(order)

Change variable order.

When number of dimension will be increased, axes whose size is one are created. Conversely when number of dimension will be decreased, the size of axes which will be removed must be one.

Parameters:order – new order
combine_axes(shape, axes, axis)[source]

Combine some axes into one axis. Combined axes must be adjacent

x = Variable([2, 3, 4, 5], OrderNHWC)

y = x.combine_axes([Axis.W, Axis.H], Axis.W)
# # same as follow code. Note that in_order is OrderNWHC, not OrderNHWC, because "axes" parameter is [W, H].
# y, = Reshape(None, in_order=OrderNWHC, out_order=OrderNWC, out_shape=[2, 12, 5])(x)

print(y.shape, y.order)
>>> "[2, 12, 5]", "[N, H, C]"
Parameters:
  • axes (sequence of Axis) – Axes which are combined. All axes must be contained in original variable.
  • axis (Axis) – Axis created from axes. If new axis is specified, which is inserted at last.
Returns:

(Variable) reshaped variable

expand_dims(shape, axis, index)[source]

Insert new axis whose size is 1. This is alias of follow codes.

new_axes = list(v.order.axes)
new_axes.insert(index, axis)
Reshape(None, in_order=v.order,
              out_order=Order(new_axes),
              out_shape=[1 if a == axis else self.shape_dict[a] for a in new_axes])(v)[0]
Parameters:
  • axis (Axis) – inserted axis
  • index (int, optional) – insert position, As default, inserted at last.
Returns:

(Variable) expanded variable

input_to

operators which this variable is input to

name

name of this variable

ndim

number of dimension

order

data order

output_from

operator which this variable is output from

reinterpret_axes(order: webdnn.graph.order.Order) → webdnn.graph.variable.Variable[source]

reshape(shape, order) Reinterpret axes. This is alias of ReinterpretAxes(None, v.order, order)(v)[0].

Parameters:order (Order) – new order
Returns:(Variable) new variable which has same shape of original variable, but its order is order.
replace(new_variable)[source]

Replace this variable in graph by specified new variable.

All operators connected with this variable are disconnected and re-connected to new variable with same name.

Parameters:
  • new_variable (Variable) – new variable
  • with_assert (bool) – If True, it is checked whether shape and order is same as variable which will be removed
reshape(shape, order)[source]

Reshape into specified order and shape. This is alias of follow codes.

Reshape(None, in_order=v.order,
              out_order=order,
              out_shape=shape)(v)[0]
Parameters:
  • shape (tuple of int) – shape
  • order (Order) – order
Returns:

(Variable) new variable which has specified order and shape

reshape_like(other: webdnn.graph.variable.Variable) → webdnn.graph.variable.Variable[source]

reshape(shape, order) Reshape into same order and shape as other. This is alias of follow codes.

Reshape(None, in_order=v.order,
              out_order=other.order,
              out_shape=other.shape)(v)[0]
Parameters:other (Variable) – variable
Returns:(Variable) new variable which has same order and shape as other
shape

variable’s shape

shape_dict

dictionary of axis and shape size pairs

size

number of elements

squeeze(axis: typing.Union[webdnn.graph.axis.Axis, typing.Sequence[webdnn.graph.axis.Axis]] = None) → webdnn.graph.variable.Variable[source]

expand_dims(shape, axis, index) Remove axis whose size is 1. This is alias of follow codes.

new_axes = list(v.order.axes)
new_axes.remove(axis)
Reshape(None, in_order=v.order,
              out_order=Order(new_axes),
              out_shape=[self.shape_dict[a] for a in new_axes])(v)[0]
Parameters:axis (Axis or sequence of Axis, optional) – removed axis. As default, all axes whose size are 1 is removed.
Returns:(Variable) squeezed variable
stride

stride size for each dimension

stride_dict

dictionary of axis and stride size pairs

transpose(shape, order)[source]

Transpose into specified order. This is alias of Transpose(None)(v)[0].change_order_statement(order)

Parameters:order (Order) – order
Returns:(Variable) new variable which has specified order
transpose_like(other: webdnn.graph.variable.Variable) → webdnn.graph.variable.Variable[source]

reshape(shape, order) Transpose into same order as other. This is alias of Transpose(None)(v)[0].change_order_statement(other.order)

Parameters:other (Variable) – variable
Returns:(Variable) new variable which has same order as other