Documentation
¶
Index ¶
- func AUC(posPrediction, negPrediction []float32) float32
- func Accuracy(posPrediction, negPrediction []float32) float32
- func LoadLibFMFile(path string) (features [][]lo.Tuple2[int32, float32], targets []float32, maxLabel int32, ...)
- func MarshalModel(w io.Writer, m FactorizationMachines) error
- func Precision(posPrediction, negPrediction []float32) float32
- func Recall(posPrediction, _ []float32) float32
- type AFM
- func (fm *AFM) BatchInternalPredict(x []lo.Tuple2[[]int32, []float32], e [][][]uint16, jobs int) []float32
- func (fm *AFM) BatchPredict(inputs []lo.Tuple4[string, string, []Label, []Label], embeddings [][]Embedding, ...) []float32
- func (fm *AFM) Clear()
- func (fm *AFM) Fit(ctx context.Context, trainSet, testSet dataset.CTRSplit, config *FitConfig) Score
- func (fm *AFM) Forward(indices, values *nn.Tensor, embeddings []*nn.Tensor, jobs int) *nn.Tensor
- func (fm *AFM) Init(trainSet dataset.CTRSplit)
- func (fm *AFM) InternalPredict(_ []int32, _ []float32) float32
- func (fm *AFM) Invalid() bool
- func (fm *AFM) Marshal(w io.Writer) error
- func (fm *AFM) Parameters() []*nn.Tensor
- func (fm *AFM) Predict(_, _ string, _, _ []Label) float32
- func (fm *AFM) SetParams(params model.Params)
- func (fm *AFM) SuggestParams(trial goptuna.Trial) model.Params
- func (fm *AFM) Unmarshal(r io.Reader) error
- type AutoScaler
- type BaseFactorizationMachines
- type BatchInference
- type Dataset
- func (dataset *Dataset) Count() int
- func (dataset *Dataset) CountContextLabels() int
- func (dataset *Dataset) CountItemLabels() int
- func (dataset *Dataset) CountItems() int
- func (dataset *Dataset) CountNegative() int
- func (dataset *Dataset) CountPositive() int
- func (dataset *Dataset) CountUserLabels() int
- func (dataset *Dataset) CountUsers() int
- func (dataset *Dataset) Get(i int) ([]int32, []float32, [][]uint16, float32)
- func (dataset *Dataset) GetIndex() dataset.UnifiedIndex
- func (dataset *Dataset) GetItemEmbeddingDim() []int
- func (dataset *Dataset) GetItemEmbeddingIndex() *dataset.Index
- func (dataset *Dataset) GetTarget(i int) float32
- func (dataset *Dataset) Split(ratio float32, seed int64) (*Dataset, *Dataset)
- func (dataset *Dataset) SplitByUserTime(ratio float32) (*Dataset, *Dataset)
- type Embedding
- type FactorizationMachines
- type FitConfig
- type Label
- type MinMaxScaler
- type ModelCreator
- type ModelSearch
- type RobustScaler
- type Score
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func LoadLibFMFile ¶
func LoadLibFMFile(path string) (features [][]lo.Tuple2[int32, float32], targets []float32, maxLabel int32, err error)
LoadLibFMFile loads libFM format file.
func MarshalModel ¶
func MarshalModel(w io.Writer, m FactorizationMachines) error
Types ¶
type AFM ¶ added in v0.5.2
type AFM struct {
BaseFactorizationMachines
// parameters
B *nn.Tensor
W nn.Layer
V nn.Layer
A []nn.Layer
E []nn.Layer
// numerical feature scalers: feature_index -> AutoScaler
Scalers map[int32]*AutoScaler
// contains filtered or unexported fields
}
func (*AFM) BatchInternalPredict ¶ added in v0.5.2
func (*AFM) BatchPredict ¶ added in v0.5.2
func (*AFM) InternalPredict ¶ added in v0.5.2
func (*AFM) Parameters ¶ added in v0.5.2
func (*AFM) SuggestParams ¶ added in v0.5.2
type AutoScaler ¶ added in v0.5.7
type AutoScaler struct {
UseLog bool // true if log1p preprocessing is applied, false if RobustScaler is used
MinMax MinMaxScaler // for non-negative values (after log1p if UseLog)
Robust RobustScaler // for data with negative values
}
AutoScaler automatically selects the appropriate scaling method based on data distribution. - If data contains negative values: uses RobustScaler - If all values are non-negative: applies log1p then MinMaxScaler
func NewAutoScaler ¶ added in v0.5.7
func NewAutoScaler() *AutoScaler
NewAutoScaler creates an AutoScaler.
func (*AutoScaler) Fit ¶ added in v0.5.7
func (s *AutoScaler) Fit(values []float32)
Fit analyzes the data and selects the appropriate scaling method.
func (*AutoScaler) Marshal ¶ added in v0.5.7
func (s *AutoScaler) Marshal(w io.Writer) error
Marshal writes the scaler to a writer.
func (*AutoScaler) Transform ¶ added in v0.5.7
func (s *AutoScaler) Transform(value float32) float32
Transform scales a value using the selected method.
type BaseFactorizationMachines ¶
type BaseFactorizationMachines struct {
model.BaseModel
Index dataset.UnifiedIndex
}
func (*BaseFactorizationMachines) Init ¶
func (b *BaseFactorizationMachines) Init(trainSet dataset.CTRSplit)
type BatchInference ¶
type Dataset ¶
type Dataset struct {
Index dataset.UnifiedIndex
UserLabels [][]lo.Tuple2[int32, float32]
ItemLabels [][]lo.Tuple2[int32, float32]
ContextLabels [][]lo.Tuple2[int32, float32]
Users []int32
Items []int32
Target []float32
Timestamps []time.Time
ItemEmbeddings [][][]uint16 // Index by row id, embedding id, embedding dimension; stored as BF16 bits
ItemEmbeddingDimension []int
ItemEmbeddingIndex *dataset.Index
PositiveCount int
NegativeCount int
}
Dataset for click-through-rate models.
func LoadDataFromBuiltIn ¶
LoadDataFromBuiltIn loads built-in dataset.
func (*Dataset) CountContextLabels ¶
func (*Dataset) CountItemLabels ¶
func (*Dataset) CountItems ¶
CountItems returns the number of items.
func (*Dataset) CountNegative ¶
func (*Dataset) CountPositive ¶
func (*Dataset) CountUserLabels ¶
func (*Dataset) CountUsers ¶
CountUsers returns the number of users.
func (*Dataset) GetIndex ¶
func (dataset *Dataset) GetIndex() dataset.UnifiedIndex
func (*Dataset) GetItemEmbeddingDim ¶ added in v0.5.2
func (*Dataset) GetItemEmbeddingIndex ¶ added in v0.5.2
func (*Dataset) SplitByUserTime ¶ added in v0.5.8
SplitByUserTime splits the dataset within each user by timestamp. Earlier samples are used for training and the latest samples are used for testing. At least one sample is kept in train for every user with two or more samples.
type Embedding ¶
func ConvertEmbeddings ¶
type FactorizationMachines ¶
type FactorizationMachines interface {
model.Model
Predict(userId, itemId string, userFeatures, itemFeatures []Label) float32
InternalPredict(x []int32, values []float32) float32
Fit(ctx context.Context, trainSet, testSet dataset.CTRSplit, config *FitConfig) Score
Marshal(w io.Writer) error
}
func UnmarshalModel ¶
func UnmarshalModel(r io.Reader) (FactorizationMachines, error)
type FitConfig ¶
func NewFitConfig ¶
func NewFitConfig() *FitConfig
func (*FitConfig) LoadDefaultIfNil ¶
func (*FitConfig) SetPatience ¶
func (*FitConfig) SetVerbose ¶
type Label ¶
func ConvertLabels ¶
type MinMaxScaler ¶ added in v0.5.7
MinMaxScaler transforms a single feature by scaling to [0, 1] range. The transformation is given by:
X_scaled = (X - X.min) / (X.max - X.min)
func NewMinMaxScaler ¶ added in v0.5.7
func NewMinMaxScaler() *MinMaxScaler
NewMinMaxScaler creates a MinMaxScaler.
func (*MinMaxScaler) Fit ¶ added in v0.5.7
func (s *MinMaxScaler) Fit(values []float32)
Fit computes the minimum and maximum values from the given values.
func (*MinMaxScaler) Marshal ¶ added in v0.5.7
func (s *MinMaxScaler) Marshal(w io.Writer) error
Marshal writes the scaler to a writer.
func (*MinMaxScaler) Transform ¶ added in v0.5.7
func (s *MinMaxScaler) Transform(value float32) float32
Transform scales a value to [0, 1] range.
type ModelCreator ¶
type ModelCreator func() FactorizationMachines
type ModelSearch ¶
type ModelSearch struct {
// contains filtered or unexported fields
}
func NewModelSearch ¶
func NewModelSearch(models map[string]ModelCreator, trainSet, testSet dataset.CTRSplit, config *FitConfig) *ModelSearch
func (*ModelSearch) Objective ¶
func (ms *ModelSearch) Objective(trial goptuna.Trial) (float64, error)
func (*ModelSearch) WithContext ¶
func (ms *ModelSearch) WithContext(ctx context.Context) *ModelSearch
func (*ModelSearch) WithSpan ¶
func (ms *ModelSearch) WithSpan(span *monitor.Span) *ModelSearch
type RobustScaler ¶ added in v0.5.7
type RobustScaler struct {
Median float32
Q1 float32 // 25th percentile
Q3 float32 // 75th percentile
IQR float32 // Interquartile range (Q3 - Q1)
}
RobustScaler transforms features using statistics that are robust to outliers. It uses the median and interquartile range (IQR) for scaling:
X_scaled = (X - median) / IQR
where IQR = Q3 - Q1 (75th percentile - 25th percentile)
func NewRobustScaler ¶ added in v0.5.7
func NewRobustScaler() *RobustScaler
NewRobustScaler creates a RobustScaler.
func (*RobustScaler) Fit ¶ added in v0.5.7
func (s *RobustScaler) Fit(values []float32)
Fit computes the median and IQR from the given values.
func (*RobustScaler) Marshal ¶ added in v0.5.7
func (s *RobustScaler) Marshal(w io.Writer) error
Marshal writes the scaler to a writer.
func (*RobustScaler) Transform ¶ added in v0.5.7
func (s *RobustScaler) Transform(value float32) float32
Transform scales a value using median and IQR.
type Score ¶
func EvaluateClassification ¶
func EvaluateClassification(estimator FactorizationMachines, testSet dataset.CTRSplit, jobs int) Score
EvaluateClassification evaluates factorization machines in classification task.
func EvaluateRegression ¶
func EvaluateRegression(estimator FactorizationMachines, testSet *Dataset) Score
EvaluateRegression evaluates factorization machines in regression task.