Skip to content

symbolic_data

This is a collection of symbolic data types.

The main class and a grandfather for all other classes is Symbolic Data.

Symbolic Tensor has similar API to torch.Tensor object, but Symbolic Tensor is used only to define the graph, not to perform actual computations. You should use it to register new layers in your computation graph and later to create the model. You can use methods of torch.Tensor when working with Symbolic Tensor. For example tensor.t() or tensor.T will return another Symbolic Tensor with transposition applied to the underlying data.

Symbolic Data supports slicing too, so you can do:

from pytorch_symbolic import Input, SymbolicModel

x = Input(batch_shape=(3, 4, 5))

y = x[0]
for row in x[1:]:
    y += row

model = SymbolicModel(x, y)

But be careful! Each slice operation creates a new layer, so if you do a lot of slicing, it is better enclose it in a custom module. However, being able to do it directly on Symbolic Data is convenient for prototyping.

pytorch_symbolic.Input

pytorch_symbolic.Input(shape: Tuple | List = (), batch_size: int = 1, batch_shape: Tuple | List | None = None, dtype = torch.float32, min_value: float = 0.0, max_value: float = 1.0) -> SymbolicTensor

Input to Symbolic Model. Create Symbolic Tensor as a root node in the graph.

Symbolic Tensor returned by Input has no parents while every other Symbolic Tensor has at least one.

Parameters:

Name Type Description Default
shape Tuple | List

Shape of the real data NOT including the batch dimension

()
batch_size int

Optional batch size of the Tensor

1
batch_shape Tuple | List | None

Shape of the real data including the batch dimension. Should be provided instead shape if cuda graphs will be used. If both shape and batch_shape are given, batch_shape has higher priority.

None
dtype

Dtype of the real data that will be the input of the network

torch.float32
min_value float

In rare cases, if real world data is very specific and some values cannot work with the model, this should be used to set a reasonable minimal value that the model can take as an input.

0.0
max_value float

As above, but the maximal value

1.0

Returns:

Type Description
SymbolicTensor

Root node in the graph

pytorch_symbolic.CustomInput

pytorch_symbolic.CustomInput(data: Any) -> SymbolicData

Input to Symbolic Model. Creates Symbolic Data as a root node in the graph.

This should be used when Input won't work.

Parameters:

Name Type Description Default
data Any

Speficic data that will be used during the graph tracing. It can, but doesn't need to be a torch.Tensor.

required

Returns:

Type Description
SymbolicData

Root node in the graph

pytorch_symbolic.symbolic_data.SymbolicData

pytorch_symbolic.symbolic_data.SymbolicData(value: Any, parents: Tuple[SymbolicData, ...] = (), depth: int = 0, layer: nn.Module | None = None, batch_size_known: bool = False, custom_name: str | None = None)

Grandfather of all Symbolic datatypes.

Underlying data is a normal Python object, for example a dict. You can use methods and operators of the underlying object. You can also unpack or index it, if only the underlying data allows it.

If the underlying data is torch.Tensor, it should be created as SymbolicTensor instead.

Parameters:

Name Type Description Default
value Any required
parents Tuple[SymbolicData, ...] ()
depth int 0
layer nn.Module | None None
batch_size_known bool False
custom_name str | None None

Attributes:

Name Type Description
v Any

Underlying data that is used during model tracing

layer nn.Module

A torch.nn.Module that transforms parents' values into this value. Also it's the incoming edge.

depth int

Maximum of parents' depths plus one

batch_size_known bool

In case of Input, whether batch size was provided by the user. For non-Input nodes, batch size is known iff all parents' batch sizes are known.

custom_name str

Instead of using the default name for the undelying layer, user can provide his own.

v property

v

Get the underlying value.

parents property

parents: Tuple[SymbolicData, ...]

Acces the tuple of parents of this node.

children property

children: Tuple[SymbolicData, ...]

Acces the tuple of children of this node.

apply_module

apply_module(layer: nn.Module, others: SymbolicData, custom_name: str | None = None) -> SymbolicData | Tuple[SymbolicData, ...]

Register a new layer in the graph. Layer must be nn.Module.

__iter__

__iter__()

Creates the only layer that has multiple children: UnpackLayer.

Suitable for unpacking results, even nested ones.

__len__

__len__() -> int

Length of the symbolic data.

pytorch_symbolic.symbolic_data.SymbolicCallable

Bases: SymbolicData

pytorch_symbolic.symbolic_data.SymbolicTensor

pytorch_symbolic.symbolic_data.SymbolicTensor(args, kwds)

Bases: SymbolicData

Recommended to use Symbolic datatype. It mimics and extends torch.Tensor API.

Treat it as a placeholder that will be replaced with real data after the model is created. For calculation purposes treat it as a normal torch.Tensor: add, subtract, multiply, take absolute value of, index, slice, etc.

features property

features: int

Size of the last dimension.

C property

C: int

Number of channels in Image data.

channels property

channels: int

Same as .C

H property

H: int

Height in Image data.

W property

W: int

Width in Image data.

HW property

HW: Tuple[int, int]

Tuple of (height, width) in Image data.

CHW property

CHW: Tuple[int, int, int]

Tuple of (channels, height, width) in Image data.

HWC property

HWC: Tuple[int, int, int]

Tuple of (height, width, channels) in Image data.

batch_size property

batch_size: int | None

Batch size of the data. Will be default if was not provided.

shape property

shape: Tuple[int | None, ...]

Shape of the underlying Symbolic Tensor, including batch size.

numel property

numel: int

Number of the values in underlying Symbolic Tensor. If batch size is known, it is used too.