Module tripleblind.network
The network object represents a neural network.
Networks structures can be build using the NetworkBuilder
.
These networks
must be trained, then the resulting model Asset can be used to perform
inference.
Typical usage is something like this:
builder = tb.NetworkBuilder().
builder.add_conv2d_layer(1, 32, 3, 1)
builder.add_relu()
# ... continue adding layers
training_model = tb.create_network("new network", builder)
# train the network (would require more training parameters)
result = training_model.train(dataset=[dataset_train0, dataset_train1], ...)
# use the trained model
trained_model = tb.Model.cast(result.asset)
output = trained_model.infer(dataset="airplane.jpg)
if output:
print(output.table.dataframe.values.flatten())
See NetworkBuilder
for more information.
Functions
def create_network(name: str, builder: NetworkBuilder, description: Optional[str] = None, is_discoverable: bool = True, persist: bool = False, session: Optional[Session] = None, is_federated_learning_model: bool = False)
-
Create an empty network in the Router index (usually for later training)
Args
name
:str
- Name of the new network to create
builder
:NetworkBuilder
- [description]
description
:Optional[str]
, optional- Longer description of the network.
is_discoverable
:bool
, optional- Should network be discoverable by others?
persist
:bool
- By default a uniquely named temporary network asset is created, but the asset can be persisted by setting this parameter to True.
session
:Session
, optional- A connection session. If not specified, the default session is used.
is_federated_learning_model
:bool
- model and protocol are intended to be used with federated learning.
Raises
Exception
- Raised if unable to create the network.
Returns
ModelTrainerAsset
- New empty network (algorithm) asset
def create_vertical_network(name: str, server: NetworkBuilder, clients: List[NetworkBuilder], description: Optional[str] = None, is_discoverable: bool = True, session: Optional[Session] = None, is_psi: bool = False, persist: bool = False)
-
Create an empty vertically partitioned network in the Router index (usually for later training)
Args
name
:str
- Name of the new network to create
server
:NetworkBuilder
- Network definition for the server unifying clients
clients
:List[NetworkBuilder]
- Network definitions for the individual clients
description
:Optional[str]
, optional- Longer description of the network.
is_discoverable
:bool
, optional- Should network be discoverable by others?
session
:Session
, optional- A connection session. If not specified, the default session is used.
is_psi
:bool
- True if will be used with PSI. Defaults to False.
persist
:bool
- By default a uniquely named temporary network asset is created, but the asset can be persisted by setting this parameter to True.
Returns
Asset
- New empty network (algorithm) asset
def create_word_embedding(name: str, embedding_path: Union[Path, str], vocab_path: Union[Path, str] = None, description: Optional[str] = None, is_discoverable: bool = False, session: Optional[Session] = None) -> Asset
-
Create an empty network on the user's Access Point (usually for later training)
Args
name
:str
- Name of the new network to create
file_handle
:str
orPath
- Path to model
vocab_path
:str
orPath
- Path to a file with the vocabulary.
description
:Optional[str]
, optional- Longer description of the network.
is_discoverable
:bool
, optional- Should network be discoverable by others?
session
:Session
, optional- A connection session. If not specified, the default session is used.
Raises
Exception
- Raised if unable to create the network.
Returns
Asset
- Model asset
def load_model(filepath)
def upload_trained_network(name: str, file_handle: Union[Path, str], description: Optional[str] = None, is_discoverable: bool = True, session: Optional[Session] = None, allow_overwrite: bool = False) -> Asset
-
Create an empty network on the user's Access Point (usually for later training)
Args
name
:str
- Name of the new network to create
file_handle
:str
orPath
- Path to model
description
:Optional[str]
, optional- Longer description of the network.
is_discoverable
:bool
, optional- Should network be discoverable by others?
session
:Session
, optional- A connection session. If not specified, the default session is used.
allow_overwrite
:bool
, optional- If False an exception will be thrown if the asset name already exists. If True, an existing asset will be overwritten.
Raises
Exception
- Raised if unable to create the network.
Returns
Asset
- Model asset
Classes
class ModelEnsemble (server_model, *clients_models)
-
Base class for all neural network modules.
Your models should also subclass this class.
Modules can also contain other Modules, allowing to nest them in a tree structure. You can assign the submodules as regular attributes::
import torch.nn as nn import torch.nn.functional as F class Model(nn.Module): def __init__(self): super().__init__() self.conv1 = nn.Conv2d(1, 20, 5) self.conv2 = nn.Conv2d(20, 20, 5) def forward(self, x): x = F.relu(self.conv1(x)) return F.relu(self.conv2(x))
Submodules assigned in this way will be registered, and will have their parameters converted too when you call :meth:
to
, etc.Note
As per the example above, an
__init__()
call to the parent class must be made before assignment on the child.:ivar training: Boolean represents whether this module is in training or evaluation mode. :vartype training: bool
Initialize internal Module state, shared by both nn.Module and ScriptModule.
Ancestors
- torch.nn.modules.module.Module
Methods
def forward(self, *input_x) -> Callable[..., Any]
-
Define the computation performed at every call.
Should be overridden by all subclasses.
Note
Although the recipe for forward pass needs to be defined within this function, one should call the :class:
Module
instance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.