syft.generic.tensor

Module Contents

class syft.generic.tensor.AbstractTensor(id: int = None, owner: sy.workers.AbstractWorker = None, tags: List[str] = None, description: str = None, child=None)

Bases: syft.generic.object.AbstractObject

wrap(self, register=True, type=None, **kwargs)

Wraps the class inside an empty object of class type.

Because PyTorch/TF do not (yet) support functionality for creating arbitrary Tensor types (via subclassing torch.Tensor), in order for our new tensor types (such as PointerTensor) to be usable by the rest of PyTorch/TF (such as PyTorch’s layers and loss functions), we need to wrap all of our new tensor types inside of a native PyTorch type.

This function adds a .wrap() function to all of our tensor types (by adding it to AbstractTensor), such that (on any custom tensor my_tensor), my_tensor.wrap() will return a tensor that is compatible with the rest of the PyTorch/TensorFlow API.

Returns

A wrapper tensor of class type, or whatever is specified as default by the current syft.framework.Tensor.

on(self, tensor: AbstractTensor, wrap: bool = True)

Add a syft(log) tensor on top of the tensor.

Parameters
  • tensor – the tensor to extend

  • wrap – if true, add the syft tensor between the wrapper

  • the rest of the chain. If false, just add it at the top (and) –

Returns

a syft/torch tensor

copy(self)
clone(self)

Clone should keep ids unchanged, contrary to copy

refresh(self)

Forward to Additive Shared Tensor the call to refresh shares

property shape(self)
__len__(self)

Alias .shape[0] with len(), helpful for pointers

property grad(self)
syft.generic.tensor.initialize_tensor(hook, obj, owner=None, id=None, init_args=tuple(), init_kwargs={})

Initializes the tensor.

Parameters
  • hook – A reference to TorchHook class.

  • cls – An object to keep track of id, owner and whether it is a native tensor or a wrapper over pytorch.

  • is_tensor – A boolean parameter (default False) to indicate whether it is torch tensor or not.

  • owner – The owner of the tensor being initialised, leave it blank to if you have already provided a reference to TorchHook class.

  • id – The id of tensor, a random id will be generated if there is no id specified.