cf

package
v0.5.1 Latest Latest
Warning

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

Go to latest
Published: Dec 20, 2025 License: Apache-2.0 Imports: 27 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Evaluate

func Evaluate(estimator MatrixFactorization, testSet, trainSet dataset.CFSplit, topK, numCandidates, nJobs int, scorers ...Metric) []float32

Evaluate evaluates a model in top-n tasks.

func GetModelName

func GetModelName(m Model) string

func HR

func HR(targetSet mapset.Set[int32], rankList []int32) float32

HR means Hit Ratio.

func MAP

func MAP(targetSet mapset.Set[int32], rankList []int32) float32

MAP means Mean Average Precision. mAP: http://sdsawtelle.github.io/blog/output/mean-average-precision-MAP-for-recommender-systems.html

func MRR

func MRR(targetSet mapset.Set[int32], rankList []int32) float32

MRR means Mean Reciprocal Rank.

The mean reciprocal rank is a statistic measure for evaluating any process that produces a list of possible responses to a sample of queries, ordered by probability of correctness. The reciprocal rank of a query response is the multiplicative inverse of the rank of the first correct answer: 1 for first place, ​1⁄2 for second place, ​1⁄3 for third place and so on. The mean reciprocal rank is the average of the reciprocal ranks of results for a sample of queries Q:

MRR = \frac{1}{Q} \sum^{|Q|}_{i=1} \frac{1}{rank_i}

func MarshalModel

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

func NDCG

func NDCG(targetSet mapset.Set[int32], rankList []int32) float32

NDCG means Normalized Discounted Cumulative Gain.

func Precision

func Precision(targetSet mapset.Set[int32], rankList []int32) float32

Precision is the fraction of relevant ItemFeedback among the recommended ItemFeedback.

\frac{|relevant documents| \cap |retrieved documents|} {|{retrieved documents}|}

func Rank

func Rank(model MatrixFactorization, userId int32, candidates []int32, topN int) []int32

func Recall

func Recall(targetSet mapset.Set[int32], rankList []int32) float32

Recall is the fraction of relevant ItemFeedback that have been recommended over the total amount of relevant ItemFeedback.

\frac{|relevant documents| \cap |retrieved documents|} {|{relevant documents}|}

Types

type ALS

type ALS struct {
	BaseMatrixFactorization
	// contains filtered or unexported fields
}

func NewALS

func NewALS(params model.Params) *ALS

NewALS creates a eALS model.

func (*ALS) Fit

func (als *ALS) Fit(ctx context.Context, trainSet, valSet dataset.CFSplit, config *FitConfig) Score

Fit the ALS model. Its task complexity is O(ccd.nEpochs).

func (*ALS) Init

func (als *ALS) Init(trainSet dataset.CFSplit)

func (*ALS) Marshal

func (als *ALS) Marshal(w io.Writer) error

Marshal model into byte stream.

func (*ALS) SetParams

func (als *ALS) SetParams(params model.Params)

SetParams sets hyper-parameters for the ALS model.

func (*ALS) SuggestParams

func (als *ALS) SuggestParams(trial goptuna.Trial) model.Params

func (*ALS) Unmarshal

func (als *ALS) Unmarshal(r io.Reader) error

Unmarshal model from byte stream.

type BPR

type BPR struct {
	BaseMatrixFactorization
	// contains filtered or unexported fields
}

BPR means Bayesian Personal Ranking, is a pairwise learning algorithm for matrix factorization model with implicit feedback. The pairwise ranking between item i and j for user u is estimated by:

p(i >_u j) = \sigma( p_u^T (q_i - q_j) )

Hyper-parameters:

 Reg 		- The regularization parameter of the cost function that is
			  optimized. Default is 0.01.
 Lr 		- The learning rate of SGD. Default is 0.05.
 nFactors	- The number of latent factors. Default is 10.
 NEpochs	- The number of iteration of the SGD procedure. Default is 100.
 InitMean	- The mean of initial random latent factors. Default is 0.
 InitStdDev	- The standard deviation of initial random latent factors. Default is 0.001.

func NewBPR

func NewBPR(params model.Params) *BPR

NewBPR creates a BPR model.

func (*BPR) Fit

func (bpr *BPR) Fit(ctx context.Context, trainSet, valSet dataset.CFSplit, config *FitConfig) Score

Fit the BPR model. Its task complexity is O(bpr.nEpochs).

func (*BPR) Init

func (bpr *BPR) Init(trainSet dataset.CFSplit)

func (*BPR) Marshal

func (bpr *BPR) Marshal(w io.Writer) error

Marshal model into byte stream.

func (*BPR) SetParams

func (bpr *BPR) SetParams(params model.Params)

SetParams sets hyper-parameters of the BPR model.

func (*BPR) SuggestParams

func (bpr *BPR) SuggestParams(trial goptuna.Trial) model.Params

func (*BPR) Unmarshal

func (bpr *BPR) Unmarshal(r io.Reader) error

Unmarshal model from byte stream.

type BaseMatrixFactorization

type BaseMatrixFactorization struct {
	model.BaseModel
	UserIndex       *dataset.FreqDict
	ItemIndex       *dataset.FreqDict
	UserPredictable *bitset.BitSet
	ItemPredictable *bitset.BitSet
	// Model parameters
	UserFactor [][]float32 // p_u
	ItemFactor [][]float32 // q_i
}

func (*BaseMatrixFactorization) Clear

func (baseModel *BaseMatrixFactorization) Clear()

func (*BaseMatrixFactorization) GetItemFactor

func (baseModel *BaseMatrixFactorization) GetItemFactor(itemIndex int32) []float32

GetItemFactor returns the latent factor of an item.

func (*BaseMatrixFactorization) GetItemIndex

func (baseModel *BaseMatrixFactorization) GetItemIndex() *dataset.FreqDict

func (*BaseMatrixFactorization) GetUserFactor

func (baseModel *BaseMatrixFactorization) GetUserFactor(userIndex int32) []float32

GetUserFactor returns the latent factor of a user.

func (*BaseMatrixFactorization) GetUserIndex

func (baseModel *BaseMatrixFactorization) GetUserIndex() *dataset.FreqDict

func (*BaseMatrixFactorization) Init

func (baseModel *BaseMatrixFactorization) Init(trainSet dataset.CFSplit)

func (*BaseMatrixFactorization) Invalid

func (baseModel *BaseMatrixFactorization) Invalid() bool

func (*BaseMatrixFactorization) IsItemPredictable

func (baseModel *BaseMatrixFactorization) IsItemPredictable(itemIndex int32) bool

IsItemPredictable returns false if item has no feedback and its embedding vector never be trained.

func (*BaseMatrixFactorization) IsUserPredictable

func (baseModel *BaseMatrixFactorization) IsUserPredictable(userIndex int32) bool

IsUserPredictable returns false if user has no feedback and its embedding vector never be trained.

func (*BaseMatrixFactorization) Marshal

func (baseModel *BaseMatrixFactorization) Marshal(w io.Writer) error

Marshal model into byte stream.

func (*BaseMatrixFactorization) Predict

func (baseModel *BaseMatrixFactorization) Predict(userId, itemId string) float32

func (*BaseMatrixFactorization) Unmarshal

func (baseModel *BaseMatrixFactorization) Unmarshal(r io.Reader) error

Unmarshal model from byte stream.

type FitConfig

type FitConfig struct {
	Jobs       int
	Verbose    int
	Candidates int
	TopK       int
	Patience   int
}

func NewFitConfig

func NewFitConfig() *FitConfig

func (*FitConfig) SetJobs

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

func (*FitConfig) SetPatience

func (config *FitConfig) SetPatience(patience int) *FitConfig

func (*FitConfig) SetVerbose

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

type MatrixFactorization

type MatrixFactorization interface {
	Model
	// Predict the rating given by a user (userId) to a item (itemId).
	Predict(userId, itemId string) float32

	// GetUserIndex returns user index.
	GetUserIndex() *dataset.FreqDict
	// GetItemIndex returns item index.
	GetItemIndex() *dataset.FreqDict
	// IsUserPredictable returns false if user has no feedback and its embedding vector never be trained.
	IsUserPredictable(userIndex int32) bool
	// IsItemPredictable returns false if item has no feedback and its embedding vector never be trained.
	IsItemPredictable(itemIndex int32) bool
	// Marshal model into byte stream.
	Marshal(w io.Writer) error
	// Unmarshal model from byte stream.
	Unmarshal(r io.Reader) error
	// contains filtered or unexported methods
}

func UnmarshalModel

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

type Metric

type Metric func(targetSet mapset.Set[int32], rankList []int32) float32

Metric is used by evaluators in personalized ranking tasks.

type Model

type Model interface {
	model.Model
	// Fit a model with a train set and parameters.
	Fit(ctx context.Context, trainSet, validateSet dataset.CFSplit, config *FitConfig) Score
	// GetItemIndex returns item index.
	GetItemIndex() *dataset.FreqDict
	// Marshal model into byte stream.
	Marshal(w io.Writer) error
	// Unmarshal model from byte stream.
	Unmarshal(r io.Reader) error
	// GetUserFactor returns latent factor of a user.
	GetUserFactor(userIndex int32) []float32
	// GetItemFactor returns latent factor of an item.
	GetItemFactor(itemIndex int32) []float32
}

type ModelCreator

type ModelCreator func() MatrixFactorization

type ModelSearch

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

func NewModelSearch

func NewModelSearch(models map[string]ModelCreator, trainSet, valSet dataset.CFSplit, config *FitConfig) *ModelSearch

func (*ModelSearch) Objective

func (ms *ModelSearch) Objective(trial goptuna.Trial) (float64, error)

func (*ModelSearch) Result

func (ms *ModelSearch) Result() meta.Model[Score]

func (*ModelSearch) WithContext

func (ms *ModelSearch) WithContext(ctx context.Context) *ModelSearch

func (*ModelSearch) WithSpan

func (ms *ModelSearch) WithSpan(span *monitor.Span) *ModelSearch

type Score

type Score struct {
	NDCG      float32
	Precision float32
	Recall    float32
}

Jump to

Keyboard shortcuts

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