nn

package module
v0.0.0-...-5f5c69f Latest Latest
Warning

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

Go to latest
Published: Apr 28, 2020 License: Apache-2.0 Imports: 16 Imported by: 0

README

CircleCI Maintainability Test Coverage Go Report Card License


import (
	"fmt"
	"go-ml.dev/pkg/base/model"
	"go-ml.dev/pkg/dataset/mnist"
	"go-ml.dev/pkg/iokit"
	"go-ml.dev/pkg/nn"
	"go-ml.dev/pkg/nn/mx"
	"gotest.tools/assert"
	"testing"
)

var mnistConv0 = nn.Sequence(
	nn.Convolution{Channels: 24, Kernel: mx.Dim(3, 3), Activation: nn.ReLU},
	nn.MaxPool{Kernel: mx.Dim(2, 2), Stride: mx.Dim(2, 2)},
	nn.Convolution{Channels: 32, Kernel: mx.Dim(5, 5), Activation: nn.ReLU, BatchNorm: true},
	nn.MaxPool{Kernel: mx.Dim(2, 2), Stride: mx.Dim(2, 2)},
	nn.FullyConnected{Size: 32, Activation: nn.Swish, BatchNorm: true, Dropout: 0.33},
	nn.FullyConnected{Size: 10, Activation: nn.Softmax})

func Test_mnistConv0(t *testing.T) {
	modelFile := iokit.File(model.Path("mnist_test_conv0.zip"))

	report := nn.Model{
		Network:   mnistConv0,
		Optimizer: nn.Adam{Lr: .001},
		Loss:      nn.CrossEntropyLoss{},
		Input:     mx.Dim(1, 28, 28),
		Seed:      42,
		BatchSize: 32,
		//Context:   mx.GPU,
	}.Feed(model.Dataset{
		Source:   mnist.Data.RandomFlag(model.TestCol, 42, 0.2),
		Label:    model.LabelCol,
		Test:     model.TestCol,
		Features: []string{"Image"},
	}).LuckyTrain(model.Training{
		Iterations: 15,
		ModelFile:  modelFile,
		Metrics:    model.Classification{Accuracy: 0.983},
		Score:      model.ErrorScore,
	})

	fmt.Println(report.TheBest, report.Score)
	fmt.Println(report.History.Round(5))
	assert.Assert(t, model.Accuracy(report.Test) >= 0.98)

	net1 := nn.LuckyObjectify(modelFile) //.Gpu()
	lr := model.LuckyEvaluate(mnist.T10k, model.LabelCol, net1, 32, model.Classification{})
	fmt.Println(lr.Round(5))
	assert.Assert(t, model.Accuracy(lr) >= 0.98)
}

Documentation

Index

Constants

View Source
const DefaultBatchSize = 32

default batch size for general nn training

View Source
const ModelPartInfo = "network.yaml"
View Source
const ModelPartParams = "params.bin.xz"
View Source
const ModelPartSummary = "summary.txt"
View Source
const ModelPartSymbol = "symbol.bin.xz"

Variables

This section is empty.

Functions

func ChannelSoftmax

func ChannelSoftmax(a *mx.Symbol) *mx.Symbol

func Combine

func Combine(nn Block) *mx.Symbol

func DefaultModelMap

func DefaultModelMap(network *Network, features []string, predicts string) model.MemorizeMap

func HardSigmoid

func HardSigmoid(a *mx.Symbol) *mx.Symbol

func LoadSymbol

func LoadSymbol(input iokit.Input) (symbolic *mx.Symbol, inputdim mx.Dimension, err error)

func LuckyObjectify

func LuckyObjectify(source iokit.Input, collection ...string) model.GpuPredictionModel

func NextSymbolId

func NextSymbolId() int

func Objectify

func Objectify(source iokit.Input, collection ...string) (fm model.GpuPredictionModel, err error)

func ObjectifyModel

func ObjectifyModel(c map[string]iokit.Input) (pm model.PredictionModel, err error)

func ReLU

func ReLU(a *mx.Symbol) *mx.Symbol

func SaveSymbol

func SaveSymbol(inputdim mx.Dimension, sym *mx.Symbol, output iokit.Output) (err error)

func Sigmoid

func Sigmoid(a *mx.Symbol) *mx.Symbol

func Sin

func Sin(a *mx.Symbol) *mx.Symbol

func SoftReLU

func SoftReLU(a *mx.Symbol) *mx.Symbol

func SoftSign

func SoftSign(a *mx.Symbol) *mx.Symbol

func Softmax

func Softmax(a *mx.Symbol) *mx.Symbol

func Swish

func Swish(a *mx.Symbol) *mx.Symbol

func Tanh

func Tanh(a *mx.Symbol) *mx.Symbol

func Tanh25

func Tanh25(a *mx.Symbol) *mx.Symbol

func Train

func Train(e Model, dataset model.Dataset, w model.Workout, mmf ModelMapFunction) (report *model.Report, err error)

Types

type Activation

type Activation struct {
	Function  func(*mx.Symbol) *mx.Symbol
	BatchNorm bool
	Name      string
}

func (Activation) Combine

func (ly Activation) Combine(in *mx.Symbol) *mx.Symbol

type Adam

type Adam struct {
	Lr, Beta1, Beta2, Epsilon, Decay float64

	LrMap map[int]float64
}

func (Adam) Init

func (opt Adam) Init(e int) Optimizer

type AvgPool

type AvgPool struct {
	Kernel  mx.Dimension
	Stride  mx.Dimension
	Padding mx.Dimension
	Ceil    bool
	Name    string
	Round   int

	BatchNorm bool
}

func (AvgPool) Combine

func (ly AvgPool) Combine(in *mx.Symbol) *mx.Symbol

type BatchNorm

type BatchNorm struct {
	Name           string
	Mom, Epsilon   float32
	UseGlobalStats bool
}

func (BatchNorm) Combine

func (ly BatchNorm) Combine(in *mx.Symbol) *mx.Symbol

type Block

type Block interface {
	Combine(*mx.Symbol) *mx.Symbol
}

func Concat

func Concat(b ...Block) Block

func Residual

func Residual(a ...Block) Block

func Sequence

func Sequence(b ...Block) Block

func Stack

func Stack(b ...Block) Block

func TransStack

func TransStack(b ...Block) Block

type BlockConcat

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

func (*BlockConcat) Combine

func (bc *BlockConcat) Combine(s *mx.Symbol) *mx.Symbol

type BlockConnect

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

func (*BlockConnect) Combine

func (bc *BlockConnect) Combine(s *mx.Symbol) *mx.Symbol

type BlockStack

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

func (*BlockStack) Combine

func (bc *BlockStack) Combine(s *mx.Symbol) *mx.Symbol

type Const

type Const struct {
	Value float32
}

func (Const) Inite

func (x Const) Inite(a *mx.NDArray)

type Convolution

type Convolution struct {
	Channels   int
	Kernel     mx.Dimension
	Stride     mx.Dimension
	Padding    mx.Dimension
	Activation func(*mx.Symbol) *mx.Symbol
	WeightInit mx.Inite // none by default
	BiasInit   mx.Inite // &nn.Const{0} by default
	NoBias     bool
	Groups     bool
	BatchNorm  bool
	Layout     string
	Name       string
	Round      int
	TurnOff    bool
	Output     bool
	Dropout    float32
}

func (Convolution) Combine

func (ly Convolution) Combine(in *mx.Symbol) *mx.Symbol

type CrossEntropyLoss

type CrossEntropyLoss struct{ Num int }

func (CrossEntropyLoss) Loss

func (loss CrossEntropyLoss) Loss(out *mx.Symbol) *mx.Symbol

type Dropout

type Dropout struct {
	Rate float32
}

func (*Dropout) Combine

func (ly *Dropout) Combine(in *mx.Symbol) *mx.Symbol

type FeaturesMapper

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

FeaturesMapper maps features to prediction

func (*FeaturesMapper) Close

func (fm *FeaturesMapper) Close() error

Close releases all bounded resources

func (*FeaturesMapper) MapFeatures

func (fm *FeaturesMapper) MapFeatures(t *tables.Table) (r *tables.Table, err error)

MapFeature returns new table with all original columns except features adding one new column with prediction/calculation

type Flatten

type Flatten struct{}

func (Flatten) Combine

func (ly Flatten) Combine(a *mx.Symbol) *mx.Symbol

type FullyConnected

type FullyConnected struct {
	Size       int
	Activation func(*mx.Symbol) *mx.Symbol
	WeightInit mx.Inite // none by default
	BiasInit   mx.Inite // &nn.Const{0} by default
	NoBias     bool
	NoFlatten  bool
	BatchNorm  bool
	Name       string
	Output     bool
	Dropout    float32
}

func (FullyConnected) Combine

func (ly FullyConnected) Combine(in *mx.Symbol) *mx.Symbol

type L0Loss

type L0Loss struct{}

func (L0Loss) Loss

func (L0Loss) Loss(out *mx.Symbol) *mx.Symbol

type L1Loss

type L1Loss struct{ Num int }

func (L1Loss) Loss

func (loss L1Loss) Loss(out *mx.Symbol) *mx.Symbol

type L2Loss

type L2Loss struct{ Num int }

func (L2Loss) Loss

func (loss L2Loss) Loss(out *mx.Symbol) *mx.Symbol

type Lambda

type Lambda struct {
	F func(*mx.Symbol) *mx.Symbol
}

func (Lambda) Combine

func (nb Lambda) Combine(input *mx.Symbol) *mx.Symbol

type LcosLoss

type LcosLoss struct{ Num int }

func (LcosLoss) Loss

func (loss LcosLoss) Loss(out *mx.Symbol) *mx.Symbol

type LossFunc

type LossFunc func(*mx.Symbol) *mx.Symbol

func (LossFunc) Loss

func (loss LossFunc) Loss(out *mx.Symbol) *mx.Symbol

type MaxPool

type MaxPool struct {
	Kernel  mx.Dimension
	Stride  mx.Dimension
	Padding mx.Dimension
	Ceil    bool
	Name    string
	Round   int

	BatchNorm bool
}

func (MaxPool) Combine

func (ly MaxPool) Combine(in *mx.Symbol) *mx.Symbol

type Model

type Model struct {
	Network   Block
	Optimizer OptimizerConf
	Loss      mx.Loss
	Input     mx.Dimension
	Seed      int
	BatchSize int
	Predicted string
	Context   mx.Context // CPU by default
}

Model is a ANN/Gym definition to train network

func (Model) Feed

func (e Model) Feed(ds model.Dataset) model.FatModel

type ModelMapFunction

type ModelMapFunction func(network *Network, features []string, predicts string) model.MemorizeMap

type Network

type Network struct {
	*mx.Graph

	BatchSize int
	// contains filtered or unexported fields
}

func Inherit

func Inherit(context mx.Context, nn Block, inputdim mx.Dimension, params iokit.Input, batchSize int, seed int) (*Network, error)

func Load

func Load(context mx.Context, symbol, params iokit.Input, batchSize int) (*Network, error)

func New

func New(context mx.Context, nn Block, inputdim mx.Dimension, loss mx.Loss, batchSize int, seed int) *Network

func (*Network) Forward

func (network *Network) Forward(data interface{}, out []float32)

func (*Network) LoadParams

func (network *Network) LoadParams(input iokit.Input, forced ...bool) (err error)

func (*Network) Predict

func (network *Network) Predict(data interface{}) [][]float32

func (*Network) Release

func (network *Network) Release()

func (*Network) SaveParams

func (network *Network) SaveParams(output iokit.Output, only ...string) (err error)

func (*Network) SaveSymbol

func (network *Network) SaveSymbol(output iokit.Output) (err error)

func (*Network) Train

func (network *Network) Train(data interface{}, label interface{}, opt Optimizer)

func (*Network) Update

func (network *Network) Update(opt Optimizer)

type Optimizer

type Optimizer interface {
	Release()
	Update(params *mx.NDArray, grads *mx.NDArray)
}

type OptimizerConf

type OptimizerConf interface {
	Init(epoch int) Optimizer
}

type Output

type Output struct {
	Name  string
	Round int
	Axis  int
	Begin int
	End   int
}

func (Output) Combine

func (ly Output) Combine(a *mx.Symbol) *mx.Symbol

type ParamsReader

type ParamsReader struct {
	io.Closer
	// contains filtered or unexported fields
}

func NewParamsReader

func NewParamsReader(input iokit.Input) (prd *ParamsReader, err error)

func (*ParamsReader) HasMore

func (prd *ParamsReader) HasMore() bool

func (*ParamsReader) Next

func (prd *ParamsReader) Next() (n string, out []float32, err error)

type PredictionModel

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

PredictionModel is the FeaturesMapper factory

func (PredictionModel) Features

func (pm PredictionModel) Features() []string

Features model uses when maps features the same as Features in the training dataset

func (PredictionModel) FeaturesMapper

func (pm PredictionModel) FeaturesMapper(batchSize int) (fm tables.FeaturesMapper, err error)

Returns new table with all original columns except features adding one new column with prediction

func (PredictionModel) Gpu

func (pm PredictionModel) Gpu(no ...int) model.PredictionModel

Gpu changes context of prediction network to gpu enabled

func (PredictionModel) Predicted

func (pm PredictionModel) Predicted() string

Column name model adds to result table when maps features. By default it's 'Predicted'

type ResidualBlock

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

func (*ResidualBlock) Combine

func (rcb *ResidualBlock) Combine(a *mx.Symbol) *mx.Symbol

type SGD

type SGD struct {
	Lr, Mom, Decay float64

	LrMap map[int]float64
}

func (SGD) Init

func (opt SGD) Init(e int) Optimizer

type Slice

type Slice struct {
	Axis    int
	Begin   int
	End     int
	Name    string
	Output  bool
	TurnOff bool
}

func (Slice) Combine

func (ly Slice) Combine(in *mx.Symbol) *mx.Symbol

type SoftmaxCrossEntropyLoss

type SoftmaxCrossEntropyLoss struct{}

func (SoftmaxCrossEntropyLoss) Loss

type Uniform

type Uniform struct {
	Magnitude float32
}

func (Uniform) Inite

func (x Uniform) Inite(a *mx.NDArray)

type Xavier

type Xavier struct {
	Gaussian  bool
	Magnitude float32
	Factor    XavierFactor
}

func (Xavier) Inite

func (x Xavier) Inite(a *mx.NDArray)

type XavierFactor

type XavierFactor int
const (
	XavierIn  XavierFactor = 1
	XavierOut XavierFactor = 2
	XavierAvg XavierFactor = 3
)

Directories

Path Synopsis
mx

Jump to

Keyboard shortcuts

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