analogvnn.nn.module.Model#

Module Contents#

Classes#

Model

Base class for analog neural network models.

class analogvnn.nn.module.Model.Model(tensorboard_log_dir=None, device=is_cpu_cuda.device)[source]#

Bases: analogvnn.nn.module.Layer.Layer, analogvnn.backward.BackwardModule.BackwardModule

Base class for analog neural network models.

Variables:
  • _compiled (bool) – True if the model is compiled.

  • tensorboard (TensorboardModelLog) – The tensorboard logger of the model.

  • graphs (ModelGraph) – The graph of the model.

  • forward_graph (ForwardGraph) – The forward graph of the model.

  • backward_graph (BackwardGraph) – The backward graph of the model.

  • optimizer (optim.Optimizer) – The optimizer of the model.

  • loss_function (Optional[TENSOR_CALLABLE]) – The loss function of the model.

  • accuracy_function (Optional[TENSOR_CALLABLE]) – The accuracy function of the model.

  • device (torch.device) – The device of the model.

property use_autograd_graph[source]#

Is the autograd graph used for the model.

Returns:

If True, the autograd graph is used to calculate the gradients.

Return type:

bool

__constants__ = ['device'][source]#
_compiled: bool[source]#
tensorboard: Optional[analogvnn.utils.TensorboardModelLog.TensorboardModelLog][source]#
graphs: analogvnn.graph.ModelGraph.ModelGraph[source]#
forward_graph: analogvnn.graph.ForwardGraph.ForwardGraph[source]#
backward_graph: analogvnn.graph.BackwardGraph.BackwardGraph[source]#
optimizer: Optional[torch.optim.Optimizer][source]#
loss_function: Optional[analogvnn.utils.common_types.TENSOR_CALLABLE][source]#
accuracy_function: Optional[analogvnn.utils.common_types.TENSOR_CALLABLE][source]#
device: torch.device[source]#
__call__(*args, **kwargs)[source]#

Call the model.

Parameters:
  • *args – The arguments of the model.

  • **kwargs – The keyword arguments of the model.

Returns:

The output of the model.

Return type:

TENSORS

Raises:

RuntimeError – if the model is not compiled.

named_registered_children(memo: Optional[Set[torch.nn.Module]] = None) Iterator[Tuple[str, torch.nn.Module]][source]#

Returns an iterator over registered modules under self.

Parameters:

memo – a memo to store the set of modules already added to the result

Yields:

(str, nn.Module) – Tuple of name and module

Note

Duplicate modules are returned only once. In the following example, l will be returned only once.

compile(device: Optional[torch.device] = None, layer_data: bool = True)[source]#

Compile the model.

Parameters:
  • device (torch.device) – The device to run the model on.

  • layer_data (bool) – If True, the layer data is logged.

Returns:

The compiled model.

Return type:

Model

forward(*inputs: torch.Tensor) analogvnn.utils.common_types.TENSORS[source]#

Forward pass of the model.

Parameters:

*inputs (Tensor) – The inputs of the model.

Returns:

The output of the model.

Return type:

TENSORS

backward(*inputs: torch.Tensor) analogvnn.utils.common_types.TENSORS[source]#

Backward pass of the model.

Parameters:

*inputs (Tensor) – The inputs of the model.

Returns:

The output of the model.

Return type:

TENSORS

loss(output: torch.Tensor, target: torch.Tensor) Tuple[torch.Tensor, torch.Tensor][source]#

Calculate the loss of the model.

Parameters:
  • output (Tensor) – The output of the model.

  • target (Tensor) – The target of the model.

Returns:

The loss and the accuracy of the model.

Return type:

Tuple[Tensor, Tensor]

Raises:

ValueError – if loss_function is None.

train_on(train_loader: torch.utils.data.DataLoader, epoch: int = None, *args, **kwargs) Tuple[float, float][source]#

Train the model on the train_loader.

Parameters:
  • train_loader (DataLoader) – The train loader of the model.

  • epoch (int) – The epoch of the model.

  • *args – The arguments of the train function.

  • **kwargs – The keyword arguments of the train function.

Returns:

The loss and the accuracy of the model.

Return type:

Tuple[float, float]

Raises:

RuntimeError – if model is not compiled.

test_on(test_loader: torch.utils.data.DataLoader, epoch: int = None, *args, **kwargs) Tuple[float, float][source]#

Test the model on the test_loader.

Parameters:
  • test_loader (DataLoader) – The test loader of the model.

  • epoch (int) – The epoch of the model.

  • *args – The arguments of the test function.

  • **kwargs – The keyword arguments of the test function.

Returns:

The loss and the accuracy of the model.

Return type:

Tuple[float, float]

Raises:

RuntimeError – if model is not compiled.

fit(train_loader: torch.utils.data.DataLoader, test_loader: torch.utils.data.DataLoader, epoch: int = None) Tuple[float, float, float, float][source]#

Fit the model on the train_loader and test the model on the test_loader.

Parameters:
  • train_loader (DataLoader) – The train loader of the model.

  • test_loader (DataLoader) – The test loader of the model.

  • epoch (int) – The epoch of the model.

Returns:

The train loss, the train accuracy, the test loss and the test accuracy of the model.

Return type:

Tuple[float, float, float, float]

create_tensorboard(log_dir: str) analogvnn.utils.TensorboardModelLog.TensorboardModelLog[source]#

Create a tensorboard.

Parameters:

log_dir (str) – The log directory of the tensorboard.

Raises:

ImportError – if tensorboard (https://www.tensorflow.org/) is not installed.

subscribe_tensorboard(tensorboard: analogvnn.utils.TensorboardModelLog.TensorboardModelLog)[source]#

Subscribe the model to the tensorboard.

Parameters:

tensorboard (TensorboardModelLog) – The tensorboard of the model.

Returns:

self.

Return type:

Model