click

package
v0.4.6 Latest Latest
Warning

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

Go to latest
Published: Jun 16, 2022 License: Apache-2.0 Imports: 26 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AUC

func AUC(posPrediction, negPrediction []float32) float32

func Accuracy

func Accuracy(posPrediction, negPrediction []float32) float32

func LoadLibFMFile

func LoadLibFMFile(path string) (features [][]int32, values [][]float32, targets base.Array[float32], maxLabel int32, err error)

LoadLibFMFile loads libFM format file.

func MarshalIndex

func MarshalIndex(w io.Writer, index UnifiedIndex) error

MarshalIndex marshal index into byte stream.

func MarshalModel

func MarshalModel(w io.Writer, m FactorizationMachine) error

func Precision

func Precision(posPrediction, negPrediction []float32) float32

func Recall

func Recall(posPrediction, _ []float32) float32

Types

type BaseFactorizationMachine

type BaseFactorizationMachine struct {
	model.BaseModel
	Index UnifiedIndex
}

func (*BaseFactorizationMachine) Init

func (b *BaseFactorizationMachine) Init(trainSet *Dataset)

type Dataset

type Dataset struct {
	Index UnifiedIndex

	UserFeatures [][]int32 // features of users
	ItemFeatures [][]int32 // features of items

	Users       base.Array[int32]
	Items       base.Array[int32]
	CtxFeatures [][]int32
	CtxValues   [][]float32
	NormValues  base.Array[float32]
	Target      base.Array[float32]

	PositiveCount int
	NegativeCount int
}

Dataset for click-through-rate models.

func LoadDataFromBuiltIn

func LoadDataFromBuiltIn(name string) (train, test *Dataset, err error)

LoadDataFromBuiltIn loads built-in dataset.

func (*Dataset) Bytes

func (dataset *Dataset) Bytes() int

func (*Dataset) Count

func (dataset *Dataset) Count() int

Count returns the number of samples.

func (*Dataset) Get

func (dataset *Dataset) Get(i int) ([]int32, []float32, float32)

Get returns the i-th sample.

func (*Dataset) ItemCount

func (dataset *Dataset) ItemCount() int

ItemCount returns the number of items.

func (*Dataset) Split

func (dataset *Dataset) Split(ratio float32, seed int64) (*Dataset, *Dataset)

Split a dataset to training set and test set.

func (*Dataset) UserCount

func (dataset *Dataset) UserCount() int

UserCount returns the number of users.

type FM

type FM struct {
	BaseFactorizationMachine
	// Model parameters
	V         [][]float32
	W         []float32
	B         float32
	MinTarget float32
	MaxTarget float32
	Task      FMTask
	// contains filtered or unexported fields
}

func NewFM

func NewFM(task FMTask, params model.Params) *FM

func (*FM) Bytes

func (fm *FM) Bytes() int

func (*FM) Clear

func (fm *FM) Clear()

func (*FM) Fit

func (fm *FM) Fit(trainSet, testSet *Dataset, config *FitConfig) Score

func (*FM) GetParamsGrid

func (fm *FM) GetParamsGrid() model.ParamsGrid

func (*FM) Init

func (fm *FM) Init(trainSet *Dataset)

func (*FM) InternalPredict

func (fm *FM) InternalPredict(features []int32, values []float32) float32

func (*FM) Invalid

func (fm *FM) Invalid() bool

func (*FM) Marshal

func (fm *FM) Marshal(w io.Writer) error

Marshal model into byte stream.

func (*FM) Predict

func (fm *FM) Predict(userId, itemId string, userLabels, itemLabels []string) float32

func (*FM) SetParams

func (fm *FM) SetParams(params model.Params)

func (*FM) Unmarshal

func (fm *FM) Unmarshal(r io.Reader) error

Unmarshal model from byte stream.

type FMTask

type FMTask uint8
const (
	FMClassification FMTask = 'c'
	FMRegression     FMTask = 'r'
)

type FactorizationMachine

type FactorizationMachine interface {
	model.Model
	Predict(userId, itemId string, userLabels, itemLabels []string) float32
	InternalPredict(x []int32, values []float32) float32
	Fit(trainSet *Dataset, testSet *Dataset, config *FitConfig) Score
	Marshal(w io.Writer) error
	Bytes() int
}

func Clone

Clone a model with deep copy.

func UnmarshalModel

func UnmarshalModel(r io.Reader) (FactorizationMachine, error)

type FitConfig

type FitConfig struct {
	Jobs    int
	Verbose int
	Tracker model.Tracker
}

func NewFitConfig

func NewFitConfig() *FitConfig

func (*FitConfig) LoadDefaultIfNil

func (config *FitConfig) LoadDefaultIfNil() *FitConfig

func (*FitConfig) SetJobs

func (config *FitConfig) SetJobs(nJobs int) *FitConfig

func (*FitConfig) SetTracker

func (config *FitConfig) SetTracker(tracker model.Tracker) *FitConfig

func (*FitConfig) SetVerbose

func (config *FitConfig) SetVerbose(verbose int) *FitConfig

type ModelSearcher

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

ModelSearcher is a thread-safe click model searcher.

func NewModelSearcher

func NewModelSearcher(nEpoch, nTrials, nJobs int) *ModelSearcher

NewModelSearcher creates a thread-safe personal ranking model searcher.

func (*ModelSearcher) Fit

func (searcher *ModelSearcher) Fit(trainSet, valSet *Dataset, tracker model.Tracker, runner model.Runner) error

func (*ModelSearcher) GetBestModel

func (searcher *ModelSearcher) GetBestModel() (FactorizationMachine, Score)

GetBestModel returns the best click model with its score.

type ParamsSearchResult

type ParamsSearchResult struct {
	BestScore  Score
	BestModel  FactorizationMachine
	BestParams model.Params
	BestIndex  int
	Scores     []Score
	Params     []model.Params
}

ParamsSearchResult contains the return of grid search.

func GridSearchCV

func GridSearchCV(estimator FactorizationMachine, trainSet *Dataset, testSet *Dataset, paramGrid model.ParamsGrid,
	_ int64, fitConfig *FitConfig, runner model.Runner) ParamsSearchResult

GridSearchCV finds the best parameters for a model.

func RandomSearchCV

func RandomSearchCV(estimator FactorizationMachine, trainSet *Dataset, testSet *Dataset, paramGrid model.ParamsGrid,
	numTrials int, seed int64, fitConfig *FitConfig, runner model.Runner) ParamsSearchResult

RandomSearchCV searches hyper-parameters by random.

type Score

type Score struct {
	Task      FMTask
	RMSE      float32
	Precision float32
	Recall    float32
	Accuracy  float32
	AUC       float32
}

func EvaluateClassification

func EvaluateClassification(estimator FactorizationMachine, testSet *Dataset) Score

EvaluateClassification evaluates factorization machines in classification task.

func EvaluateRegression

func EvaluateRegression(estimator FactorizationMachine, testSet *Dataset) Score

EvaluateRegression evaluates factorization machines in regression task.

func (Score) BetterThan

func (score Score) BetterThan(s Score) bool

func (Score) GetValue

func (score Score) GetValue() float32

func (Score) ZapFields

func (score Score) ZapFields() []zap.Field

type SnapshotManger

type SnapshotManger struct {
	BestWeights []interface{}
	BestScore   Score
}

SnapshotManger manages the best snapshot.

func (*SnapshotManger) AddSnapshot

func (sm *SnapshotManger) AddSnapshot(score Score, weights ...interface{})

AddSnapshot adds a copied snapshot.

type UnifiedDirectIndex

type UnifiedDirectIndex struct {
	N int32
}

UnifiedDirectIndex maps string to integer in literal.

func (*UnifiedDirectIndex) Bytes

func (unified *UnifiedDirectIndex) Bytes() int

func (*UnifiedDirectIndex) CountContextLabels

func (unified *UnifiedDirectIndex) CountContextLabels() int32

CountContextLabels should be used by unit testing only.

func (*UnifiedDirectIndex) CountItemLabels

func (unified *UnifiedDirectIndex) CountItemLabels() int32

CountItemLabels should be used by unit testing only.

func (*UnifiedDirectIndex) CountItems

func (unified *UnifiedDirectIndex) CountItems() int32

CountItems should be used by unit testing only.

func (*UnifiedDirectIndex) CountUserLabels

func (unified *UnifiedDirectIndex) CountUserLabels() int32

CountUserLabels should be used by unit testing only.

func (*UnifiedDirectIndex) CountUsers

func (unified *UnifiedDirectIndex) CountUsers() int32

CountUsers should be used by unit testing only.

func (*UnifiedDirectIndex) EncodeContextLabel

func (unified *UnifiedDirectIndex) EncodeContextLabel(label string) int32

EncodeContextLabel should be used by unit testing only.

func (*UnifiedDirectIndex) EncodeItem

func (unified *UnifiedDirectIndex) EncodeItem(itemId string) int32

EncodeItem should be used by unit testing only.

func (*UnifiedDirectIndex) EncodeItemLabel

func (unified *UnifiedDirectIndex) EncodeItemLabel(itemLabel string) int32

EncodeItemLabel should be used by unit testing only.

func (*UnifiedDirectIndex) EncodeUser

func (unified *UnifiedDirectIndex) EncodeUser(userId string) int32

EncodeUser should be used by unit testing only.

func (*UnifiedDirectIndex) EncodeUserLabel

func (unified *UnifiedDirectIndex) EncodeUserLabel(userLabel string) int32

EncodeUserLabel should be used by unit testing only.

func (*UnifiedDirectIndex) GetContextLabels

func (unified *UnifiedDirectIndex) GetContextLabels() []string

GetContextLabels should be used by unit testing only.

func (*UnifiedDirectIndex) GetItemLabels

func (unified *UnifiedDirectIndex) GetItemLabels() []string

GetItemLabels should be used by unit testing only.

func (*UnifiedDirectIndex) GetItems

func (unified *UnifiedDirectIndex) GetItems() []string

GetItems should be used by unit testing only.

func (*UnifiedDirectIndex) GetUserLabels

func (unified *UnifiedDirectIndex) GetUserLabels() []string

GetUserLabels should be used by unit testing only.

func (*UnifiedDirectIndex) GetUsers

func (unified *UnifiedDirectIndex) GetUsers() []string

GetUsers should be used by unit testing only.

func (*UnifiedDirectIndex) Len

func (unified *UnifiedDirectIndex) Len() int32

Len should be used by unit testing only.

func (*UnifiedDirectIndex) Marshal

func (unified *UnifiedDirectIndex) Marshal(w io.Writer) error

Marshal direct index into byte stream.

func (*UnifiedDirectIndex) Unmarshal

func (unified *UnifiedDirectIndex) Unmarshal(r io.Reader) error

Unmarshal direct index from byte stream.

type UnifiedIndex

type UnifiedIndex interface {
	Len() int32
	Bytes() int
	EncodeUser(userId string) int32
	EncodeItem(itemId string) int32
	EncodeUserLabel(userLabel string) int32
	EncodeItemLabel(itemLabel string) int32
	EncodeContextLabel(ctxLabel string) int32
	GetUsers() []string
	GetItems() []string
	GetUserLabels() []string
	GetItemLabels() []string
	GetContextLabels() []string
	CountUsers() int32
	CountItems() int32
	CountUserLabels() int32
	CountItemLabels() int32
	CountContextLabels() int32
	Marshal(w io.Writer) error
	Unmarshal(r io.Reader) error
}

UnifiedIndex maps users, items and labels into a unified encoding space.

func NewUnifiedDirectIndex

func NewUnifiedDirectIndex(n int32) UnifiedIndex

NewUnifiedDirectIndex creates a UnifiedDirectIndex.

func UnmarshalIndex

func UnmarshalIndex(r io.Reader) (UnifiedIndex, error)

UnmarshalIndex unmarshal index from byte stream.

type UnifiedMapIndex

type UnifiedMapIndex struct {
	UserIndex      base.Index
	ItemIndex      base.Index
	UserLabelIndex base.Index
	ItemLabelIndex base.Index
	CtxLabelIndex  base.Index
}

UnifiedMapIndex is the id -> index mapper for factorization machines. The division of id is: | user | item | user label | item label | context label |

func (*UnifiedMapIndex) Bytes

func (unified *UnifiedMapIndex) Bytes() int

func (*UnifiedMapIndex) CountContextLabels

func (unified *UnifiedMapIndex) CountContextLabels() int32

CountContextLabels returns the number of context labels.

func (*UnifiedMapIndex) CountItemLabels

func (unified *UnifiedMapIndex) CountItemLabels() int32

CountItemLabels returns the number of item labels.

func (*UnifiedMapIndex) CountItems

func (unified *UnifiedMapIndex) CountItems() int32

CountItems returns the number of items.

func (*UnifiedMapIndex) CountUserLabels

func (unified *UnifiedMapIndex) CountUserLabels() int32

CountUserLabels returns the number of user labels.

func (*UnifiedMapIndex) CountUsers

func (unified *UnifiedMapIndex) CountUsers() int32

CountUsers returns the number of users.

func (*UnifiedMapIndex) EncodeContextLabel

func (unified *UnifiedMapIndex) EncodeContextLabel(label string) int32

EncodeContextLabel converts a context label to a integer in the encoding space.

func (*UnifiedMapIndex) EncodeItem

func (unified *UnifiedMapIndex) EncodeItem(itemId string) int32

EncodeItem converts a item id to a integer in the encoding space.

func (*UnifiedMapIndex) EncodeItemLabel

func (unified *UnifiedMapIndex) EncodeItemLabel(itemLabel string) int32

EncodeItemLabel converts a item label to a integer in the encoding space.

func (*UnifiedMapIndex) EncodeUser

func (unified *UnifiedMapIndex) EncodeUser(userId string) int32

EncodeUser converts a user id to a integer in the encoding space.

func (*UnifiedMapIndex) EncodeUserLabel

func (unified *UnifiedMapIndex) EncodeUserLabel(userLabel string) int32

EncodeUserLabel converts a user label to a integer in the encoding space.

func (*UnifiedMapIndex) GetContextLabels

func (unified *UnifiedMapIndex) GetContextLabels() []string

GetContextLabels returns all context labels.

func (*UnifiedMapIndex) GetItemLabels

func (unified *UnifiedMapIndex) GetItemLabels() []string

GetItemLabels returns all item labels.

func (*UnifiedMapIndex) GetItems

func (unified *UnifiedMapIndex) GetItems() []string

GetItems returns all items.

func (*UnifiedMapIndex) GetUserLabels

func (unified *UnifiedMapIndex) GetUserLabels() []string

GetUserLabels returns all user labels.

func (*UnifiedMapIndex) GetUsers

func (unified *UnifiedMapIndex) GetUsers() []string

GetUsers returns all users.

func (*UnifiedMapIndex) Len

func (unified *UnifiedMapIndex) Len() int32

Len returns the size of unified index.

func (*UnifiedMapIndex) Marshal

func (unified *UnifiedMapIndex) Marshal(w io.Writer) error

Marshal map index into byte stream.

func (*UnifiedMapIndex) Unmarshal

func (unified *UnifiedMapIndex) Unmarshal(r io.Reader) error

Unmarshal map index from byte stream.

type UnifiedMapIndexBuilder

type UnifiedMapIndexBuilder struct {
	UserIndex      base.Index
	ItemIndex      base.Index
	UserLabelIndex base.Index
	ItemLabelIndex base.Index
	CtxLabelIndex  base.Index
}

UnifiedMapIndexBuilder is the builder for UnifiedMapIndex.

func NewUnifiedMapIndexBuilder

func NewUnifiedMapIndexBuilder() *UnifiedMapIndexBuilder

NewUnifiedMapIndexBuilder creates a UnifiedMapIndexBuilder.

func (*UnifiedMapIndexBuilder) AddCtxLabel

func (builder *UnifiedMapIndexBuilder) AddCtxLabel(ctxLabel string)

AddCtxLabel adds a context label the unified index.

func (*UnifiedMapIndexBuilder) AddItem

func (builder *UnifiedMapIndexBuilder) AddItem(itemId string)

AddItem adds a item into the unified index.

func (*UnifiedMapIndexBuilder) AddItemLabel

func (builder *UnifiedMapIndexBuilder) AddItemLabel(itemLabel string)

AddItemLabel adds a item label into the unified index.

func (*UnifiedMapIndexBuilder) AddUser

func (builder *UnifiedMapIndexBuilder) AddUser(userId string)

AddUser adds a user into the unified index.

func (*UnifiedMapIndexBuilder) AddUserLabel

func (builder *UnifiedMapIndexBuilder) AddUserLabel(userLabel string)

AddUserLabel adds a user label into the unified index.

func (*UnifiedMapIndexBuilder) Build

func (builder *UnifiedMapIndexBuilder) Build() UnifiedIndex

Build UnifiedMapIndex from UnifiedMapIndexBuilder.

Jump to

Keyboard shortcuts

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