utils

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: 10 Imported by: 0

Documentation

Overview

various utils, including methods for matrices in plaintext, model definitions and activation functions

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Predict

func Predict(Y []int, labels int, result [][]float64) (int, float64, []int)

Returns number of correct values, accuracy and predicted values

func ReLU

func ReLU(x float64) float64

func SetupDirectory

func SetupDirectory()

func SiLU

func SiLU(x float64) float64

func Sigmoid

func Sigmoid(x float64) float64

func SoftReLu

func SoftReLu(x float64) float64

func ThrowErr

func ThrowErr(err error)

Types

type ApproxParam

type ApproxParam struct {
	A   float64 `json:"a"`
	B   float64 `json:"b"`
	Deg int
}

Stores interval and degree of approximation in chebychev basis. Deg is set via SetDegOfParam

type ApproxParams

type ApproxParams struct {
	Params []ApproxParam `json:"intervals"`
}

Approximation parameters for each layers

func SetDegOfParam

func SetDegOfParam(Params ApproxParams) ApproxParams

decides the degree of approximation for each Param

type Bias

type Bias struct {
	B   []float64 `json:"b"`
	Len int       `json:"len"`
}

Define layer type for the various models

type ChebyPolyApprox

type ChebyPolyApprox struct {
	PolyApprox
	A, B      float64
	Degree    int
	Poly      *ckks.Polynomial
	ChebyBase bool
	F         func(x float64) float64
}

Polynomial approximation from ckks Approximate

func InitActivationCheby

func InitActivationCheby(act string, a, b float64, deg int) *ChebyPolyApprox

Initiliazes activation layer with function to approximate

func InitReLU

func InitReLU(deg int) *ChebyPolyApprox

Initialize ReLU with coeffs not in cheby form from Matlab -> used by cryptonet

func (*ChebyPolyApprox) ActivatePlain

func (activation *ChebyPolyApprox) ActivatePlain(X *mat.Dense)

applies the activation function elementwise. Needs rescaling first

func (*ChebyPolyApprox) LevelsOfAct

func (approx *ChebyPolyApprox) LevelsOfAct() int

computes how many levels are consumed by activation func

func (*ChebyPolyApprox) Rescale

func (approx *ChebyPolyApprox) Rescale(w, b *mat.Dense) (*mat.Dense, *mat.Dense)

rescale weights for polynomial activation

type Kernel

type Kernel struct {
	W    []float64 `json:"w"`
	Rows int       `json:"rows"`
	Cols int       `json:"cols"`
}

Matrix M s.t X @ M = conv(X, layer).flatten() where X is a row-flattened data sample Clearly it can be generalized to a simple dense layer

type Layer

type Layer struct {
	Weight Kernel `json:"weight"`
	Bias   Bias   `json:"bias"`
}

A Kernel (convolution in Toeplitz form or dense) and A Bias

func (*Layer) Build

func (l *Layer) Build(batchsize int) (*mat.Dense, *mat.Dense)

Returns weight and bias of layer

func (*Layer) BuildBias

func (l *Layer) BuildBias(batchsize int) *mat.Dense

func (*Layer) BuildWeight

func (l *Layer) BuildWeight() *mat.Dense

type PolyApprox

type PolyApprox interface {
	LevelsOfAct() int
	Rescale(w, b *mat.Dense) (*mat.Dense, *mat.Dense)
}

type Stats

type Stats struct {
	Iters    int
	Batch    int
	Corrects []int
	Accuracy []float64
	Time     []int64 // Add slice to store time for each evaluation
}

func NewStats

func NewStats(batch int) Stats

func (*Stats) Accumulate

func (s *Stats) Accumulate(other Stats)

func (*Stats) PrintResult

func (s *Stats) PrintResult(filePath string) error

Jump to

Keyboard shortcuts

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