How to use the syft.tensor.TensorBase function in syft

To help you get started, we’ve selected a few syft examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github OpenMined / PySyft / syft / math.py View on Github external
tensor : TensorBase
        The first operand in the diag operation
    diagonal : Integer
        The second operand in the diag operation

    Returns
    -------
    TensorBase
        Computed tensor result for diag operation
    """
    tensor = _ensure_tensorbase(tensor)
    if tensor.encrypted is True:
        return NotImplemented
    dim = tensor.dim()
    if dim == 1:
        return TensorBase(np.diag(tensor.data, diagonal))
    elif dim == 2:
        return TensorBase(np.diagonal(tensor.data, diagonal))
    else:
        raise ValueError("Input must be 1- or 2-d tensor.")
github OpenMined / PySyft / syft / nn / linear.py View on Github external
def generate_gradient(self, input, target):
        target = TensorBase(np.array(target).astype('float64'))
        input = TensorBase(np.array(input).astype('float64'))
        pred = self.forward(input)

        target_v = target

        if(self.pubkey is not None and self.encrypted):
            target_v = self.pubkey.encrypt(target_v)

        output_grad = (pred - target_v)

        weight_grad = TensorBase(np.zeros_like(self.weights.data))

        if(self.encrypted):
            weight_grad = weight_grad.encrypt(self.pubkey)

        for i in range(len(input)):
github OpenMined / PySyft / syft / he / paillier / basic.py View on Github external
import numpy as np
from ...tensor import TensorBase


class PaillierTensor(TensorBase):

    def __init__(self, public_key, data=None, input_is_decrypted=True):
        """
        Initializes a Tensor.
        :param public_key: Public key to encrypt the data by
        :param data: Array-like data
        :param input_is_decrypted: To indicate whether `data` is encrypted
        """
        self.encrypted = True

        self.public_key = public_key
        if(type(data) == np.ndarray or type(data) == TensorBase) and input_is_decrypted:
            if type(data) == np.ndarray:
                self.data = public_key.encrypt(data, True)
            else:
                self.data = public_key.encrypt(data.data, True)
github OpenMined / PySyft / syft / math.py View on Github external
function

    Parameters
    ----------
    tensor: TensorBase
        input Tensor

    Returns
    -------
    TensorBase:
        Output Tensor;
    """
    tensor = _ensure_tensorbase(tensor)
    if tensor.encrypted:
        return NotImplemented
    return TensorBase(np.sinh(np.array(tensor.data)))
github OpenMined / PySyft / syft / math.py View on Github external
_ensure_tensorbase(tensor1)
    _ensure_tensorbase(tensor2)
    _ensure_tensorbase(mat)
    if tensor2.data.ndim != 3:
        print("dimension of tensor2 is not 3")
    elif tensor1.data.ndim != 3:
        print("dimension of tensor1 is not 3")
    elif tensor1.encrypted or tensor2.encrypted or mat.encrypted:
        return NotImplemented
    else:
        mmul = np.matmul(tensor1.data, tensor2.data)
        sum_ = 0  # sum is a built in python function
        for i, _ in enumerate(mmul):
            sum_ += mmul[i]
        out = (mat.data * beta) + (alpha * sum_)
        return TensorBase(out)
github OpenMined / PySyft / syft / math.py View on Github external
value: ,optional

    Returns
    -------
    TensorBase:
        Output Tensor
    """
    _ensure_tensorbase(tensor1)
    _ensure_tensorbase(tensor2)
    _ensure_tensorbase(mat)
    if tensor1.encrypted or tensor2.encrypted or mat.encrypted:
        return NotImplemented
    else:
        out = mat.data + ((tensor1.data * tensor2.data) * value)
        return TensorBase(out)
github OpenMined / PySyft / syft / math.py View on Github external
tanh(x) = (e^(x) - e^(-x))/(e^(x) + e^(-x))
        Parameters
    ----------
    tensor: TensorBase

        input Tensor

    Returns
    -------
    TensorBase:
        Output Tensor;
    """
    tensor = _ensure_tensorbase(tensor)
    if tensor.encrypted is True:
        return NotImplemented
    return TensorBase(np.tanh(np.array(tensor.data)))
github OpenMined / PySyft / syft / mpc / spdz / tensor.py View on Github external
def __init__(self, repo, data=None, input_is_shared=False):

        self.repo = repo
        self.encrypted = False

        if(input_is_shared):
            self.data = data
        else:

            if(type(data) == int or type(data) == float or type(data) == np.float64):
                self.data = self.repo.create_float(data)

            elif(type(data) == TensorBase):
                return SPDZMPCTensor(self.repo, data.data)

            elif(type(data) == np.ndarray):
                x = data
                sh = x.shape
                x_ = x.reshape(-1)
                out = list()
                for v in x_:
                    out.append(self.repo.create_float(v))

                self.data = np.array(out).reshape(sh)
            else:
                print("format not recognized:" + str(type(x)))
                return NotImplemented
github OpenMined / PySyft / syft / nn / linear.py View on Github external
def batch_update(self, minibatch, alpha):
        """Updates a minibatch of input and target prediction. Should be called through
        learn() for default parameters
        """
        weight_update = TensorBase(np.zeros(self.weights.data.shape))
        if (self.encrypted):
            weight_update = weight_update.encrypt(self.pubkey)
        for (x, y) in zip(*minibatch):
            weight_update += self.generate_gradient(x, y)
        self.weights -= weight_update * (alpha / len(minibatch[0]))
github OpenMined / PySyft / syft / math.py View on Github external
Parameters
    ----------
    tensor: TensorBase
        input Tensor

    Returns
    -------
    TensorBase:
        Output Tensor
    """

    tensor = _ensure_tensorbase(tensor)
    if tensor.encrypted is True:
        return NotImplemented
    return TensorBase(np.ceil(tensor.data))