-
Notifications
You must be signed in to change notification settings - Fork 871
implement Attentional Factorization Machines #1134
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
defbfe2
Add benchmark
zhenghaoz d3b42a3
Remove dup output
zhenghaoz 5f61d07
Remove Spawn
zhenghaoz 2469f63
Rename NewAFM
zhenghaoz b30b6c5
Fix tests
zhenghaoz 6c6c9b9
Remove unused LLM evaluation code and clean up imports
zhenghaoz 5e873a2
Fix NPE
zhenghaoz 3af5309
Change default
zhenghaoz bee1da8
Save
zhenghaoz 0320e15
Fix NPE
zhenghaoz 925712d
FIx tests
zhenghaoz 9301f25
FIx codecov
zhenghaoz 974ff11
Fix tests
zhenghaoz 10eeeb9
Fix jobs
zhenghaoz 00e1a42
移除手动触发事件通道,改为使用同步事件通道
zhenghaoz File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,176 @@ | ||
| // Copyright 2026 gorse Project Authors | ||
| // | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
|
|
||
| package main | ||
|
|
||
| import ( | ||
| "context" | ||
| "fmt" | ||
| "log" | ||
| "os" | ||
| "runtime" | ||
| "sort" | ||
|
|
||
| "github.com/gorse-io/gorse/config" | ||
| "github.com/gorse-io/gorse/dataset" | ||
| "github.com/gorse-io/gorse/master" | ||
| "github.com/gorse-io/gorse/model/ctr" | ||
| "github.com/gorse-io/gorse/storage" | ||
| "github.com/gorse-io/gorse/storage/data" | ||
| "github.com/samber/lo" | ||
| "github.com/spf13/cobra" | ||
| "modernc.org/sortutil" | ||
| ) | ||
|
|
||
| var rootCmd = &cobra.Command{ | ||
| Use: "gorse-benchmark", | ||
| Short: "Gorse Benchmarking Tool", | ||
| } | ||
|
|
||
| var llmCmd = &cobra.Command{ | ||
| Use: "llm", | ||
| Short: "Benchmark LLM models", | ||
| Run: func(cmd *cobra.Command, args []string) { | ||
| // Load configuration | ||
| configPath, _ := cmd.Flags().GetString("config") | ||
| cfg, err := config.LoadConfig(configPath) | ||
| if err != nil { | ||
| log.Fatalf("failed to load config: %v", err) | ||
| } | ||
| // Load dataset | ||
| m := master.NewMaster(cfg, os.TempDir(), false) | ||
| m.DataClient, err = data.Open(m.Config.Database.DataStore, m.Config.Database.DataTablePrefix, | ||
| storage.WithIsolationLevel(m.Config.Database.MySQL.IsolationLevel)) | ||
| if err != nil { | ||
| log.Fatalf("failed to open data client: %v", err) | ||
| } | ||
| evaluator := master.NewOnlineEvaluator( | ||
| m.Config.Recommend.DataSource.PositiveFeedbackTypes, | ||
| m.Config.Recommend.DataSource.ReadFeedbackTypes) | ||
| dataset, _, err := m.LoadDataFromDatabase(context.Background(), m.DataClient, | ||
| m.Config.Recommend.DataSource.PositiveFeedbackTypes, | ||
| m.Config.Recommend.DataSource.ReadFeedbackTypes, | ||
| m.Config.Recommend.DataSource.ItemTTL, | ||
| m.Config.Recommend.DataSource.PositiveFeedbackTTL, | ||
| evaluator, | ||
| nil) | ||
| if err != nil { | ||
| log.Fatalf("failed to load dataset: %v", err) | ||
| } | ||
| fmt.Println("Dataset loaded:") | ||
| fmt.Printf(" Users: %d\n", dataset.CountUsers()) | ||
| fmt.Printf(" Items: %d\n", dataset.CountItems()) | ||
| fmt.Printf(" Positive Feedbacks: %d\n", dataset.CountPositive()) | ||
| fmt.Printf(" Negative Feedbacks: %d\n", dataset.CountNegative()) | ||
| // Split dataset | ||
| train, test := dataset.Split(0.2, 42) | ||
| EvaluateFM(train, test) | ||
| // EvaluateLLM(cfg, train, test, aux.GetItems()) | ||
| }, | ||
| } | ||
|
|
||
| func EvaluateFM(train, test dataset.CTRSplit) float32 { | ||
| fmt.Println("Training FM...") | ||
| ml := ctr.NewAFM(nil) | ||
| ml.Fit(context.Background(), train, test, | ||
| ctr.NewFitConfig(). | ||
| SetVerbose(10). | ||
| SetJobs(runtime.NumCPU()). | ||
| SetPatience(10)) | ||
|
|
||
| userTrain := make(map[int32]int, train.CountUsers()) | ||
| for i := 0; i < train.Count(); i++ { | ||
| indices, _, _, target := train.Get(i) | ||
| userId := indices[0] | ||
| if target > 0 { | ||
| userTrain[userId]++ | ||
| } | ||
| } | ||
|
|
||
| var posFeatures, negFeatures []lo.Tuple2[[]int32, []float32] | ||
| var posEmbeddings, negEmbeddings [][][]float32 | ||
| var posUsers, negUsers []int32 | ||
| for i := 0; i < test.Count(); i++ { | ||
| indices, values, embeddings, target := test.Get(i) | ||
| userId := indices[0] | ||
| if target > 0 { | ||
| posFeatures = append(posFeatures, lo.Tuple2[[]int32, []float32]{A: indices, B: values}) | ||
| posEmbeddings = append(posEmbeddings, embeddings) | ||
| posUsers = append(posUsers, userId) | ||
| } else { | ||
| negFeatures = append(negFeatures, lo.Tuple2[[]int32, []float32]{A: indices, B: values}) | ||
| negEmbeddings = append(negEmbeddings, embeddings) | ||
| negUsers = append(negUsers, userId) | ||
| } | ||
| } | ||
| posPrediction := ml.BatchInternalPredict(posFeatures, posEmbeddings, runtime.NumCPU()) | ||
| negPrediction := ml.BatchInternalPredict(negFeatures, negEmbeddings, runtime.NumCPU()) | ||
|
|
||
| userPosPrediction := make(map[int32][]float32) | ||
| userNegPrediction := make(map[int32][]float32) | ||
| for i, p := range posPrediction { | ||
| userPosPrediction[posUsers[i]] = append(userPosPrediction[posUsers[i]], p) | ||
| } | ||
| for i, p := range negPrediction { | ||
| userNegPrediction[negUsers[i]] = append(userNegPrediction[negUsers[i]], p) | ||
| } | ||
| var sumAUC float32 | ||
| var validUsers float32 | ||
| for user, pos := range userPosPrediction { | ||
| if userTrain[user] > 100 || userTrain[user] == 0 { | ||
| continue | ||
| } | ||
| if neg, ok := userNegPrediction[user]; ok { | ||
| sumAUC += AUC(pos, neg) * float32(len(pos)) | ||
| validUsers += float32(len(pos)) | ||
| } | ||
| } | ||
| if validUsers == 0 { | ||
| return 0 | ||
| } | ||
| score := sumAUC / validUsers | ||
|
|
||
| fmt.Println("FM GAUC:", score) | ||
| return score | ||
| } | ||
|
|
||
| func AUC(posPrediction, negPrediction []float32) float32 { | ||
| sort.Sort(sortutil.Float32Slice(posPrediction)) | ||
| sort.Sort(sortutil.Float32Slice(negPrediction)) | ||
| var sum float32 | ||
| var nPos int | ||
| for pPos := range posPrediction { | ||
| // find the negative sample with the greatest prediction less than current positive sample | ||
| for nPos < len(negPrediction) && negPrediction[nPos] < posPrediction[pPos] { | ||
| nPos++ | ||
| } | ||
| // add the number of negative samples have less prediction than current positive sample | ||
| sum += float32(nPos) | ||
| } | ||
| if len(posPrediction)*len(negPrediction) == 0 { | ||
| return 0 | ||
| } | ||
| return sum / float32(len(posPrediction)*len(negPrediction)) | ||
| } | ||
|
|
||
| func init() { | ||
| rootCmd.PersistentFlags().StringP("config", "c", "", "Path to configuration file") | ||
| rootCmd.AddCommand(llmCmd) | ||
| } | ||
|
|
||
| func main() { | ||
| if err := rootCmd.Execute(); err != nil { | ||
| log.Fatal(err) | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -6,3 +6,4 @@ coverage: | |
|
|
||
| ignore: | ||
| - "protocol/*.pb.go" | ||
| - "cmd/**" | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.