-
Notifications
You must be signed in to change notification settings - Fork 22
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
Duplicate trial numbers are assigned on parallel execution. #130
Labels
Comments
c-bata
changed the title
Trial numbers are not unique on parallel execution.
Duplicate trial numbers are assigned on parallel execution.
Jul 7, 2020
Optuna resolved this issue by using |
Beforepackage main
import (
"context"
"flag"
"log"
"math"
"github.com/c-bata/goptuna"
"github.com/c-bata/goptuna/rdb"
"github.com/c-bata/goptuna/tpe"
"github.com/jinzhu/gorm"
_ "github.com/jinzhu/gorm/dialects/mysql"
_ "github.com/jinzhu/gorm/dialects/sqlite"
"golang.org/x/sync/errgroup"
)
func objective(trial goptuna.Trial) (float64, error) {
x1, _ := trial.SuggestFloat("x1", -10, 10)
x2, _ := trial.SuggestFloat("x2", -10, 10)
return math.Pow(x1-2, 2) + math.Pow(x2+5, 2), nil
}
func main() {
flag.Parse()
if len(flag.Args()) == 0 {
log.Fatal("please pass dialect and dsn")
}
dialect := flag.Arg(0)
dsn := flag.Arg(1)
db, err := gorm.Open(dialect, dsn)
if err != nil {
log.Fatal("failed to open db:", err)
}
rdb.RunAutoMigrate(db)
storage := rdb.NewStorage(db)
defer db.Close()
study, err := goptuna.CreateStudy(
"rdb",
goptuna.StudyOptionStorage(storage),
goptuna.StudyOptionSampler(tpe.NewSampler()),
goptuna.StudyOptionDirection(goptuna.StudyDirectionMinimize),
goptuna.StudyOptionLoadIfExists(true),
)
if err != nil {
log.Fatal("failed to create study", err)
}
eg, ctx := errgroup.WithContext(context.Background())
study.WithContext(ctx)
for i := 0; i < 10; i++ {
eg.Go(func() error {
return study.Optimize(objective, 20)
})
}
if err := eg.Wait(); err != nil {
log.Fatal("Optimize error", err)
}
trials, err := study.GetTrials()
if err != nil {
log.Fatal("failed to get trials", err)
}
for i := range trials {
log.Printf("trial id=%d: number=%d\n", trials[i].ID, trials[i].Number)
}
v, err := study.GetBestValue()
if err != nil {
log.Fatal("failed to get best value", err)
}
params, err := study.GetBestParams()
if err != nil {
log.Fatal("failed to get best params:", err)
}
log.Printf("Best evaluation=%f (x1=%f, x2=%f)",
v, params["x1"].(float64), params["x2"].(float64))
}
After #168package main
import (
"context"
"flag"
"log"
"math"
"github.com/c-bata/goptuna"
"github.com/c-bata/goptuna/rdb.v2"
"github.com/c-bata/goptuna/tpe"
"golang.org/x/sync/errgroup"
"gorm.io/driver/mysql"
"gorm.io/driver/sqlite"
"gorm.io/gorm"
)
func objective(trial goptuna.Trial) (float64, error) {
x1, _ := trial.SuggestFloat("x1", -10, 10)
x2, _ := trial.SuggestFloat("x2", -10, 10)
return math.Pow(x1-2, 2) + math.Pow(x2+5, 2), nil
}
func main() {
flag.Parse()
if len(flag.Args()) == 0 {
log.Fatal("please pass dialect and dsn")
}
dialect := flag.Arg(0)
dsn := flag.Arg(1)
var db *gorm.DB
var err error
if dialect == "sqlite3" {
db, err = gorm.Open(sqlite.Open(dsn), &gorm.Config{})
} else if dialect == "mysql" {
db, err = gorm.Open(mysql.Open(dsn), &gorm.Config{})
} else {
log.Fatal("unsupported dialect")
}
if err != nil {
log.Fatal("failed to open db:", err)
}
err = rdb.RunAutoMigrate(db)
if err != nil {
log.Fatal("failed to run auto migrate:", err)
}
study, err := goptuna.CreateStudy(
"rdb",
goptuna.StudyOptionStorage(rdb.NewStorage(db)),
goptuna.StudyOptionSampler(tpe.NewSampler()),
goptuna.StudyOptionDirection(goptuna.StudyDirectionMinimize),
goptuna.StudyOptionLoadIfExists(true),
)
if err != nil {
log.Fatal("failed to create study", err)
}
eg, ctx := errgroup.WithContext(context.Background())
study.WithContext(ctx)
for i := 0; i < 10; i++ {
eg.Go(func() error {
return study.Optimize(objective, 20)
})
}
if err := eg.Wait(); err != nil {
log.Fatal("Optimize error", err)
}
trials, err := study.GetTrials()
if err != nil {
log.Fatal("failed to get trials", err)
}
for i := range trials {
log.Printf("trial id=%d: number=%d\n", trials[i].ID, trials[i].Number)
}
v, err := study.GetBestValue()
if err != nil {
log.Fatal("failed to get best value", err)
}
params, err := study.GetBestParams()
if err != nil {
log.Fatal("failed to get best params:", err)
}
log.Printf("Best evaluation=%f (x1=%f, x2=%f)",
v, params["x1"].(float64), params["x2"].(float64))
}
|
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Refs: optuna/optuna#1488
The same problem is exist on Goptuna.
The text was updated successfully, but these errors were encountered: