Documentation ¶
Index ¶
- Constants
- func Evaluate(estimator MatrixFactorization, testSet, trainSet *DataSet, ...) []float32
- func GetModelName(m Model) string
- func HR(targetSet *i32set.Set, rankList []int32) float32
- func LoadDataFromBuiltIn(dataSetName string) (*DataSet, *DataSet, error)
- func MAP(targetSet *i32set.Set, rankList []int32) float32
- func MRR(targetSet *i32set.Set, rankList []int32) float32
- func MarshalModel(w io.Writer, m Model) error
- func NDCG(targetSet *i32set.Set, rankList []int32) float32
- func Precision(targetSet *i32set.Set, rankList []int32) float32
- func Rank(model MatrixFactorization, userId int32, candidates []int32, topN int) ([]int32, []float32)
- func Recall(targetSet *i32set.Set, rankList []int32) float32
- type ALS
- func (als *ALS) Clear()
- func (als *ALS) Fit(trainSet, valSet *DataSet, config *FitConfig) Score
- func (als *ALS) GetItemFactor(_ int32) []float32
- func (als *ALS) GetParamsGrid() model.ParamsGrid
- func (als *ALS) GetUserFactor(_ int32) []float32
- func (als *ALS) Init(trainSet *DataSet)
- func (als *ALS) InternalPredict(userIndex, itemIndex int32) float32
- func (als *ALS) Invalid() bool
- func (als *ALS) Marshal(w io.Writer) error
- func (als *ALS) Predict(userId, itemId string) float32
- func (als *ALS) SetParams(params model.Params)
- func (als *ALS) Unmarshal(r io.Reader) error
- type BPR
- func (bpr *BPR) Clear()
- func (bpr *BPR) Fit(trainSet, valSet *DataSet, config *FitConfig) Score
- func (bpr *BPR) GetItemFactor(itemIndex int32) []float32
- func (bpr *BPR) GetParamsGrid() model.ParamsGrid
- func (bpr *BPR) GetUserFactor(userIndex int32) []float32
- func (bpr *BPR) Init(trainSet *DataSet)
- func (bpr *BPR) InternalPredict(userIndex, itemIndex int32) float32
- func (bpr *BPR) Invalid() bool
- func (bpr *BPR) Marshal(w io.Writer) error
- func (bpr *BPR) Predict(userId, itemId string) float32
- func (bpr *BPR) SetParams(params model.Params)
- func (bpr *BPR) Unmarshal(r io.Reader) error
- type BaseMatrixFactorization
- func (baseModel *BaseMatrixFactorization) GetItemIndex() base.Index
- func (baseModel *BaseMatrixFactorization) GetUserIndex() base.Index
- func (baseModel *BaseMatrixFactorization) Init(trainSet *DataSet)
- func (baseModel *BaseMatrixFactorization) IsItemPredictable(itemIndex int32) bool
- func (baseModel *BaseMatrixFactorization) IsUserPredictable(userIndex int32) bool
- func (baseModel *BaseMatrixFactorization) Marshal(w io.Writer) error
- func (baseModel *BaseMatrixFactorization) Unmarshal(r io.Reader) error
- type CCD
- func (ccd *CCD) Clear()
- func (ccd *CCD) Fit(trainSet, valSet *DataSet, config *FitConfig) Score
- func (ccd *CCD) GetItemFactor(itemIndex int32) []float32
- func (ccd *CCD) GetParamsGrid() model.ParamsGrid
- func (ccd *CCD) GetUserFactor(userIndex int32) []float32
- func (ccd *CCD) Init(trainSet *DataSet)
- func (ccd *CCD) InternalPredict(userIndex, itemIndex int32) float32
- func (ccd *CCD) Invalid() bool
- func (ccd *CCD) Marshal(w io.Writer) error
- func (ccd *CCD) Predict(userId, itemId string) float32
- func (ccd *CCD) SetParams(params model.Params)
- func (ccd *CCD) Unmarshal(r io.Reader) error
- type DataSet
- func (dataset *DataSet) AddFeedback(userId, itemId string, insertUserItem bool)
- func (dataset *DataSet) AddItem(itemId string)
- func (dataset *DataSet) AddUser(userId string)
- func (dataset *DataSet) Count() int
- func (dataset *DataSet) GetIndex(i int) (int32, int32)
- func (dataset *DataSet) ItemCount() int
- func (dataset *DataSet) NegativeSample(excludeSet *DataSet, numCandidates int) [][]int32
- func (dataset *DataSet) SetNegatives(userId string, negatives []string)
- func (dataset *DataSet) Split(numTestUsers int, seed int64) (*DataSet, *DataSet)
- func (dataset *DataSet) UserCount() int
- type FitConfig
- type MatrixFactorization
- type Metric
- type Model
- type ModelSearcher
- type ParamsSearchResult
- type Score
- type SnapshotManger
Constants ¶
const ( CollaborativeBPR = "bpr" CollaborativeALS = "als" CollaborativeCCD = "ccd" )
Variables ¶
This section is empty.
Functions ¶
func Evaluate ¶
func Evaluate(estimator MatrixFactorization, testSet, trainSet *DataSet, topK, numCandidates, nJobs int, scorers ...Metric) []float32
Evaluate evaluates a model in top-n tasks.
func GetModelName ¶ added in v0.2.5
func LoadDataFromBuiltIn ¶
LoadDataFromBuiltIn loads a built-in Data set. Now support:
func MAP ¶
MAP means Mean Average Precision. mAP: http://sdsawtelle.github.io/blog/output/mean-average-precision-MAP-for-recommender-systems.html
func MRR ¶
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 Precision ¶
Precision is the fraction of relevant ItemFeedback among the recommended ItemFeedback.
\frac{|relevant documents| \cap |retrieved documents|} {|{retrieved documents}|}
Types ¶
type ALS ¶
type ALS struct { BaseMatrixFactorization // Model parameters UserFactor *mat.Dense // p_u ItemFactor *mat.Dense // q_i // contains filtered or unexported fields }
ALS [7] is the Weighted Regularized Matrix Factorization, which exploits unique properties of implicit feedback datasets. It treats the data as indication of positive and negative preference associated with vastly varying confidence levels. This leads to a factor model which is especially tailored for implicit feedback recommenders. Authors also proposed a scalable optimization procedure, which scales linearly with the data size. Hyper-parameters:
NFactors - The number of latent factors. Default is 10. NEpochs - The number of training epochs. Default is 50. InitMean - The mean of initial latent factors. Default is 0. InitStdDev - The standard deviation of initial latent factors. Default is 0.1. Reg - The strength of regularization.
func (*ALS) GetItemFactor ¶ added in v0.3.2
GetItemFactor returns the item latent factors.
func (*ALS) GetParamsGrid ¶
func (als *ALS) GetParamsGrid() model.ParamsGrid
func (*ALS) GetUserFactor ¶ added in v0.3.2
GetUserFactor returns the user latent factors.
func (*ALS) InternalPredict ¶
type BPR ¶
type BPR struct { BaseMatrixFactorization // Model parameters UserFactor [][]float32 // p_u ItemFactor [][]float32 // q_i // 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 (*BPR) GetItemFactor ¶ added in v0.3.2
GetItemFactor returns the latent factor of an item.
func (*BPR) GetParamsGrid ¶
func (bpr *BPR) GetParamsGrid() model.ParamsGrid
func (*BPR) GetUserFactor ¶ added in v0.3.2
GetUserFactor returns the latent factor of a user.
func (*BPR) InternalPredict ¶
type BaseMatrixFactorization ¶
type BaseMatrixFactorization struct { model.BaseModel UserIndex base.Index ItemIndex base.Index UserPredictable *bitset.BitSet ItemPredictable *bitset.BitSet }
func (*BaseMatrixFactorization) GetItemIndex ¶
func (baseModel *BaseMatrixFactorization) GetItemIndex() base.Index
func (*BaseMatrixFactorization) GetUserIndex ¶
func (baseModel *BaseMatrixFactorization) GetUserIndex() base.Index
func (*BaseMatrixFactorization) Init ¶
func (baseModel *BaseMatrixFactorization) Init(trainSet *DataSet)
func (*BaseMatrixFactorization) IsItemPredictable ¶ added in v0.3.1
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 ¶ added in v0.3.1
func (baseModel *BaseMatrixFactorization) IsUserPredictable(userIndex int32) bool
IsUserPredictable returns false if user has no feedback and its embedding vector never be trained.
type CCD ¶
type CCD struct { BaseMatrixFactorization // Model parameters UserFactor [][]float32 ItemFactor [][]float32 // contains filtered or unexported fields }
func (*CCD) GetItemFactor ¶ added in v0.3.2
GetItemFactor returns latent factor of an item.
func (*CCD) GetParamsGrid ¶
func (ccd *CCD) GetParamsGrid() model.ParamsGrid
func (*CCD) GetUserFactor ¶ added in v0.3.2
GetUserFactor returns latent factor of a user.
func (*CCD) InternalPredict ¶
type DataSet ¶
type DataSet struct { UserIndex base.Index ItemIndex base.Index FeedbackUsers base.Integers FeedbackItems base.Integers UserFeedback [][]int32 ItemFeedback [][]int32 Negatives [][]int32 ItemLabels [][]int32 UserLabels [][]int32 HiddenItems []bool ItemCategories [][]string CategorySet *strset.Set // statistics NumItemLabels int32 NumUserLabels int32 }
DataSet contains preprocessed data structures for recommendation models.
func LoadDataFromCSV ¶
LoadDataFromCSV loads Data from a CSV file. The CSV file should be:
[optional header] <userId 1> <sep> <itemId 1> <sep> <rating 1> <sep> <extras> <userId 2> <sep> <itemId 2> <sep> <rating 2> <sep> <extras> <userId 3> <sep> <itemId 3> <sep> <rating 3> <sep> <extras> ...
For example, the `u.Data` from MovieLens 100K is:
196\t242\t3\t881250949 186\t302\t3\t891717742 22\t377\t1\t878887116
func NewDirectIndexDataset ¶
func NewDirectIndexDataset() *DataSet
func (*DataSet) AddFeedback ¶
func (*DataSet) NegativeSample ¶
func (*DataSet) SetNegatives ¶
type FitConfig ¶
func NewFitConfig ¶ added in v0.2.2
func NewFitConfig() *FitConfig
func (*FitConfig) LoadDefaultIfNil ¶
func (*FitConfig) SetTracker ¶ added in v0.2.4
func (*FitConfig) SetVerbose ¶ added in v0.2.5
type MatrixFactorization ¶
type MatrixFactorization interface { Model // Predict the rating given by a user (userId) to a item (itemId). Predict(userId, itemId string) float32 // InternalPredict predicts rating given by a user index and a item index InternalPredict(userIndex, itemIndex int32) float32 // GetUserIndex returns user index. GetUserIndex() base.Index // GetItemIndex returns item index. GetItemIndex() base.Index // 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 }
func UnmarshalModel ¶ added in v0.2.7
func UnmarshalModel(r io.Reader) (MatrixFactorization, error)
type Model ¶
type Model interface { model.Model // Fit a model with a train set and parameters. Fit(trainSet *DataSet, validateSet *DataSet, config *FitConfig) Score // GetItemIndex returns item index. GetItemIndex() base.Index // 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 ModelSearcher ¶
type ModelSearcher struct {
// contains filtered or unexported fields
}
ModelSearcher is a thread-safe personal ranking model searcher.
func NewModelSearcher ¶
func NewModelSearcher(nEpoch, nTrials, nJobs int) *ModelSearcher
NewModelSearcher creates a thread-safe personal ranking model searcher.
func (*ModelSearcher) GetBestModel ¶
func (searcher *ModelSearcher) GetBestModel() (string, Model, Score)
GetBestModel returns the optimal personal ranking model.
type ParamsSearchResult ¶
type ParamsSearchResult struct { BestModel Model BestScore Score BestParams model.Params BestIndex int Scores []Score Params []model.Params }
ParamsSearchResult contains the return of grid search.
func GridSearchCV ¶
func GridSearchCV(estimator MatrixFactorization, 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 MatrixFactorization, trainSet *DataSet, testSet *DataSet, paramGrid model.ParamsGrid, numTrials int, seed int64, fitConfig *FitConfig, runner model.Runner) ParamsSearchResult
RandomSearchCV searches hyper-parameters by random.
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.
func (*SnapshotManger) AddSnapshotNoCopy ¶
func (sm *SnapshotManger) AddSnapshotNoCopy(score Score, weights ...interface{})
AddSnapshotNoCopy adds a snapshot without copy.