network

package
v0.0.0-...-9e2d205 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 24, 2023 License: Apache-2.0 Imports: 9 Imported by: 0

Documentation

Overview

implements neural networks and he neural networks interfaces

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type HENetwork

type HENetwork struct {
	Weights []cipherUtils.BlocksOperand
	Bias    []cipherUtils.BlocksOperand

	Multiplier *cipherUtils.Multiplier
	Adder      *cipherUtils.Adder
	Activator  *cipherUtils.Activator

	Bootstrapper   cipherUtils.IBootstrapper
	Bootstrappable bool
	MinLevel       int
	BtpCapacity    int

	NumOfLayers int

	Box cipherUtils.CkksBox
	// contains filtered or unexported fields
}

Network for HE, either in clear or encrypted The implemented network should evaluate layers as: Activation(Input * Weight + bias)

func (*HENetwork) CheckLvlAtLayer

func (n *HENetwork) CheckLvlAtLayer(level, minLevel, layer int, forAct, afterMul bool) bool

true if he version needs bootstrapping at this layer with level = level

func (*HENetwork) Eval

Runs inference

func (*HENetwork) EvalDebug

func (n *HENetwork) EvalDebug(Xenc cipherUtils.BlocksOperand, Xclear *mat.Dense, network NetworkI, L1thresh float64) (*cipherUtils.EncInput, *mat.Dense, time.Duration)

Eval but with debug statements for checking the HE pipeline step by step

func (*HENetwork) GetBox

func (n *HENetwork) GetBox() cipherUtils.CkksBox

func (*HENetwork) GetRotations

func (n *HENetwork) GetRotations(params ckks.Parameters, btpParams *bootstrapping.Parameters) []int

Generate the rotations needed to evaluate the network and updates the network box with the rotation keys

func (*HENetwork) IsInit

func (n *HENetwork) IsInit() bool

func (*HENetwork) LevelsToComplete

func (n *HENetwork) LevelsToComplete(currLayer int, afterMul bool) int

computes how many levels are needed to complete the pipeline in he version

func (*HENetwork) SetBox

func (n *HENetwork) SetBox(box cipherUtils.CkksBox)

type HENetworkI

type HENetworkI interface {
	//Rotations needed for inference. Also updates the box in the network with rotation keys
	GetRotations(params ckks.Parameters, btpParams *bootstrapping.Parameters) []int

	GetBox() cipherUtils.CkksBox
	SetBox(Box cipherUtils.CkksBox)
	//Evaluates batch. Treats each layer l as: Act[l](X * weight[l] + bias[l])
	Eval(X cipherUtils.BlocksOperand) (*cipherUtils.EncInput, time.Duration)
	//Evaluates batch with debug statements. Needs the batch in clear and the network in cleartext.
	//Additionally needs the max L1 norm of the difference allowed for each intermediate result, elementwise.
	//If difference is > L1Thresh, it panics
	EvalDebug(Xenc cipherUtils.BlocksOperand, Xclear *mat.Dense, network NetworkI, L1thresh float64) (*cipherUtils.EncInput, *mat.Dense, time.Duration)
	//Checks if ciphertext at current level, for layer layer, needs bootstrap (true) or not
	//afterMul: if already multiplied by weight
	//forAct: if before activation
	CheckLvlAtLayer(level, minLevel, layer int, forAct, afterMul bool) bool
	//Returns number of levels requested to complete pipeline from current layer
	//afterMul: if already multiplied by weight
	LevelsToComplete(currLayer int, afterMul bool) int
	//True if network is initialized correctly
	IsInit() bool
}

func NewHENetwork

func NewHENetwork(network NetworkI, splits *cipherUtils.Split, encrypted, bootstrappable bool, maxLevel, minLevel, btpCapacity int, Bootstrapper cipherUtils.IBootstrapper, poolsize int, Box cipherUtils.CkksBox) HENetworkI

Creates a new network for he inference Needs splits of input and weights and loaded network from json

type Initiator

type Initiator func(args ...interface{}) []utils.ChebyPolyApprox

initiator for activation functions. Must return a list of polynomials which approximate the activation function at each layer note that Identity activations functions are supported only at the end of the network (otherwise adjacent linear layers can be collapsed)

type Network

type Network struct {
	// contains filtered or unexported fields
}

Network loaded from json. Implements INetwork. Abstract type

func (*Network) CheckLvlAtLayer

func (n *Network) CheckLvlAtLayer(level, minLevel, layer int, forAct, afterMul bool) bool

True if bootstrap is needed

func (*Network) GetActivations

func (n *Network) GetActivations() []utils.ChebyPolyApprox

func (*Network) GetDimentions

func (n *Network) GetDimentions() ([]int, []int)

Returns array of rows and cols of weights

func (*Network) GetNumOfActivations

func (n *Network) GetNumOfActivations() int

func (*Network) GetNumOfLayers

func (n *Network) GetNumOfLayers() int

func (*Network) GetParams

func (n *Network) GetParams() ([]*mat.Dense, []*mat.Dense)

Gets weights and biases

func (*Network) GetParamsRescaled

func (n *Network) GetParamsRescaled() ([]*mat.Dense, []*mat.Dense)

Gets weight and bias rescaled before activation (e.g for activation in Chebychev base)

func (*Network) IsInit

func (n *Network) IsInit() bool

func (*Network) LevelsToComplete

func (n *Network) LevelsToComplete(currLayer int, afterMul bool) int

Returns the levels needed to complete the pipeline from current layers, before or after the weigth multplication

func (*Network) NewHE

func (n *Network) NewHE(splits *cipherUtils.Split, encrypted, bootstrappable bool, maxLevel, minLevel, btpCapacity int, Bootstrapper cipherUtils.IBootstrapper, poolsize int, Box cipherUtils.CkksBox) HENetworkI

func (*Network) SetActivations

func (n *Network) SetActivations(activations []utils.ChebyPolyApprox)

func (*Network) SetBatch

func (n *Network) SetBatch(batchSize int)

func (*Network) SetLayers

func (n *Network) SetLayers(layers []utils.Layer)

type NetworkI

type NetworkI interface {
	SetBatch(batchSize int)
	SetLayers(layers []utils.Layer)
	SetActivations(activations []utils.ChebyPolyApprox)
	//returns weights and biases
	GetParams() ([]*mat.Dense, []*mat.Dense)
	//returns weight and biases rescaled for approximated activation
	GetParamsRescaled() ([]*mat.Dense, []*mat.Dense)
	GetActivations() []utils.ChebyPolyApprox
	GetNumOfLayers() int
	GetNumOfActivations() int
	//gets rows and cols of weights as linear layers
	GetDimentions() ([]int, []int)
	//true if network is initialized, i.e if batch is defined as well as layers and activations
	IsInit() bool
	//true if he version needs bootstrapping at this layer with level = level
	CheckLvlAtLayer(level, minLevel, layer int, forAct, afterMul bool) bool
	//computes how many levels are needed to complete the pipeline in he version
	LevelsToComplete(currLayer int, afterMul bool) int
	//Creates the HE version of this network
	NewHE(splits *cipherUtils.Split, encrypted, bootstrappable bool, maxLevel, minLevel, btpCapacity int, Bootstrapper cipherUtils.IBootstrapper, poolsize int, Box cipherUtils.CkksBox) HENetworkI
}

Network loaded from json

type NetworkJ

type NetworkJ struct {
	Layers    []utils.Layer `json:"layers,omitempty"`
	NumLayers int           `json:"numLayers,omitempty"`
}

NetworkJ wrapper for json struct

type NetworkLoader

type NetworkLoader interface {
	Load(path string, initActivations Initiator) NetworkI
}

Custom Network Loader. Exposes the method Load to load model from file User should make sure that this method initiates also activation functions with a user-defined init method: this means that Load should return an initiliazed network, with the activations field populated by SetActivations. It is user responsability to provide and invoke an initiator method within Load The json structure should be compatible with Network

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL