Documentation
¶
Index ¶
- Constants
- Variables
- func DeleteStudy(name string, storage Storage) error
- func DistributionIsSingle(distribution interface{}) (bool, error)
- func DistributionToJSON(distribution interface{}) ([]byte, error)
- func IntersectionSearchSpace(study *Study) (map[string]interface{}, error)
- func JSONToDistribution(jsonBytes []byte) (interface{}, error)
- func ToExternalRepresentation(distribution interface{}, ir float64) (interface{}, error)
- type CategoricalDistribution
- type DiscreteUniformDistribution
- type Distribution
- type FrozenTrial
- type FuncObjective
- type InMemoryStorage
- func (s *InMemoryStorage) CloneTrial(studyID int, baseTrial FrozenTrial) (int, error)
- func (s *InMemoryStorage) CreateNewStudy(name string) (int, error)
- func (s *InMemoryStorage) CreateNewTrial(studyID int) (int, error)
- func (s *InMemoryStorage) DeleteStudy(studyID int) error
- func (s *InMemoryStorage) GetAllStudySummaries() ([]StudySummary, error)
- func (s *InMemoryStorage) GetAllTrials(studyID int) ([]FrozenTrial, error)
- func (s *InMemoryStorage) GetBestTrial(studyID int) (FrozenTrial, error)
- func (s *InMemoryStorage) GetStudyDirection(studyID int) (StudyDirection, error)
- func (s *InMemoryStorage) GetStudyIDFromName(name string) (int, error)
- func (s *InMemoryStorage) GetStudyIDFromTrialID(trialID int) (int, error)
- func (s *InMemoryStorage) GetStudyNameFromID(studyID int) (string, error)
- func (s *InMemoryStorage) GetStudySystemAttrs(studyID int) (map[string]string, error)
- func (s *InMemoryStorage) GetStudyUserAttrs(studyID int) (map[string]string, error)
- func (s *InMemoryStorage) GetTrial(trialID int) (FrozenTrial, error)
- func (s *InMemoryStorage) GetTrialNumberFromID(trialID int) (int, error)
- func (s *InMemoryStorage) GetTrialParam(trialID int, paramName string) (float64, error)
- func (s *InMemoryStorage) GetTrialParams(trialID int) (map[string]interface{}, error)
- func (s *InMemoryStorage) GetTrialSystemAttrs(trialID int) (map[string]string, error)
- func (s *InMemoryStorage) GetTrialUserAttrs(trialID int) (map[string]string, error)
- func (s *InMemoryStorage) SetStudyDirection(studyID int, direction StudyDirection) error
- func (s *InMemoryStorage) SetStudySystemAttr(studyID int, key string, value string) error
- func (s *InMemoryStorage) SetStudyUserAttr(studyID int, key string, value string) error
- func (s *InMemoryStorage) SetTrialIntermediateValue(trialID int, step int, value float64) error
- func (s *InMemoryStorage) SetTrialParam(trialID int, paramName string, paramValueInternal float64, ...) error
- func (s *InMemoryStorage) SetTrialState(trialID int, state TrialState) error
- func (s *InMemoryStorage) SetTrialSystemAttr(trialID int, key string, value string) error
- func (s *InMemoryStorage) SetTrialUserAttr(trialID int, key string, value string) error
- func (s *InMemoryStorage) SetTrialValue(trialID int, value float64) error
- type IntUniformDistribution
- type LogUniformDistribution
- type Logger
- type LoggerLevel
- type Pruner
- type RandomSearchSampler
- type RandomSearchSamplerOption
- type RelativeSampler
- type Sampler
- type StdLogger
- type Storage
- type Study
- func (s *Study) Direction() StudyDirection
- func (s *Study) EnqueueTrial(internalParams map[string]float64) error
- func (s *Study) GetBestParams() (map[string]interface{}, error)
- func (s *Study) GetBestValue() (float64, error)
- func (s *Study) GetSystemAttrs() (map[string]string, error)
- func (s *Study) GetTrials() ([]FrozenTrial, error)
- func (s *Study) GetUserAttrs() (map[string]string, error)
- func (s *Study) Optimize(objective FuncObjective, evaluateMax int) error
- func (s *Study) SetSystemAttr(key, value string) error
- func (s *Study) SetUserAttr(key, value string) error
- func (s *Study) WithContext(ctx context.Context)
- type StudyDirection
- type StudyOption
- func StudyOptionIgnoreError(ignore bool) StudyOption
- func StudyOptionLoadIfExists(loadIfExists bool) StudyOption
- func StudyOptionLogger(logger Logger) StudyOption
- func StudyOptionPruner(pruner Pruner) StudyOption
- func StudyOptionRelativeSampler(sampler RelativeSampler) StudyOption
- func StudyOptionSampler(sampler Sampler) StudyOption
- func StudyOptionSetDirection(direction StudyDirection) StudyOption
- func StudyOptionSetTrialNotifyChannel(notify chan FrozenTrial) StudyOption
- func StudyOptionStorage(storage Storage) StudyOption
- type StudySummary
- type Trial
- func (t *Trial) GetContext() context.Context
- func (t *Trial) GetSystemAttrs() (map[string]string, error)
- func (t *Trial) GetUserAttrs() (map[string]string, error)
- func (t *Trial) Number() (int, error)
- func (t *Trial) Report(value float64, step int) error
- func (t *Trial) SetSystemAttr(key, value string) error
- func (t *Trial) SetUserAttr(key, value string) error
- func (t *Trial) ShouldPrune(value float64) (bool, error)
- func (t *Trial) SuggestCategorical(name string, choices []string) (string, error)
- func (t *Trial) SuggestDiscreteUniform(name string, low, high, q float64) (float64, error)
- func (t *Trial) SuggestInt(name string, low, high int) (int, error)
- func (t *Trial) SuggestLogUniform(name string, low, high float64) (float64, error)
- func (t *Trial) SuggestUniform(name string, low, high float64) (float64, error)
- type TrialState
- type UniformDistribution
Examples ¶
Constants ¶
const CategoricalDistributionName = "CategoricalDistribution"
CategoricalDistributionName is the identifier name of CategoricalDistribution
const DiscreteUniformDistributionName = "DiscreteUniformDistribution"
DiscreteUniformDistributionName is the identifier name of DiscreteUniformDistribution
const InMemoryStorageStudyID = 1
InMemoryStorageStudyID is a study id for in memory storage backend.
const InMemoryStorageStudyUUID = "00000000-0000-0000-0000-000000000000"
InMemoryStorageStudyUUID is a UUID for in memory storage backend
const IntUniformDistributionName = "IntUniformDistribution"
IntUniformDistributionName is the identifier name of IntUniformDistribution
const LogUniformDistributionName = "LogUniformDistribution"
LogUniformDistributionName is the identifier name of LogUniformDistribution
const UniformDistributionName = "UniformDistribution"
UniformDistributionName is the identifier name of UniformDistribution
Variables ¶
var ( // ErrNotFound represents not found. ErrNotFound = errors.New("not found") // ErrInvalidStudyID represents invalid study id. ErrInvalidStudyID = errors.New("invalid study id") // ErrInvalidTrialID represents invalid trial id. ErrInvalidTrialID = errors.New("invalid trial id") // ErrTrialCannotBeUpdated represents trial cannot be updated. ErrTrialCannotBeUpdated = errors.New("trial cannot be updated") // ErrNoCompletedTrials represents no trials are completed yet. ErrNoCompletedTrials = errors.New("no trials are completed yet") // ErrUnknownDistribution returns the distribution is unknown. ErrUnknownDistribution = errors.New("unknown distribution") // ErrTrialPruned represents the pruned. ErrTrialPruned = errors.New("trial is pruned") )
var DefaultStudyNamePrefix = "no-name-"
DefaultStudyNamePrefix is a prefix of the default study name.
var ( // ErrUnsupportedSearchSpace represents sampler does not support a given search space. ErrUnsupportedSearchSpace = errors.New("unsupported search space") )
var StudyOptionSetLogger = StudyOptionLogger
StudyOptionSetLogger sets Logger. Deprecated: please use StudyOptionLogger instead.
Functions ¶
func DeleteStudy ¶ added in v0.2.0
DeleteStudy delete a study object.
func DistributionIsSingle ¶ added in v0.2.0
DistributionIsSingle whether the distribution contains just a single value.
func DistributionToJSON ¶ added in v0.0.2
DistributionToJSON serialize a distribution to JSON format.
func IntersectionSearchSpace ¶ added in v0.2.0
IntersectionSearchSpace return return the intersection search space of the Study.
Intersection search space contains the intersection of parameter distributions that have been suggested in the completed trials of the study so far. If there are multiple parameters that have the same name but different distributions, neither is included in the resulting search space (i.e., the parameters with dynamic value ranges are excluded).
func JSONToDistribution ¶ added in v0.0.2
JSONToDistribution deserialize a distribution in JSON format.
func ToExternalRepresentation ¶ added in v0.0.4
ToExternalRepresentation converts to external representation
Types ¶
type CategoricalDistribution ¶ added in v0.0.2
type CategoricalDistribution struct {
// Choices is a candidates of parameter values
Choices []string `json:"choices"`
}
CategoricalDistribution is a distribution for categorical parameters
func (*CategoricalDistribution) Contains ¶ added in v0.0.2
func (d *CategoricalDistribution) Contains(ir float64) bool
Contains to check a parameter value is contained in the range of this distribution.
func (*CategoricalDistribution) Single ¶ added in v0.0.2
func (d *CategoricalDistribution) Single() bool
Single to test whether the range of this distribution contains just a single value.
func (*CategoricalDistribution) ToExternalRepr ¶ added in v0.0.2
func (d *CategoricalDistribution) ToExternalRepr(ir float64) interface{}
ToExternalRepr to convert internal representation of a parameter value into external representation.
type DiscreteUniformDistribution ¶ added in v0.0.3
type DiscreteUniformDistribution struct {
// High is higher endpoint of the range of the distribution (included in the range).
High float64 `json:"high"`
// Low is lower endpoint of the range of the distribution (included in the range).
Low float64 `json:"low"`
// Q is a discretization step.
Q float64 `json:"q"`
}
DiscreteUniformDistribution is a discretized uniform distribution in the linear domain.
func (*DiscreteUniformDistribution) Contains ¶ added in v0.0.3
func (d *DiscreteUniformDistribution) Contains(ir float64) bool
Contains to check a parameter value is contained in the range of this distribution.
func (*DiscreteUniformDistribution) Single ¶ added in v0.0.3
func (d *DiscreteUniformDistribution) Single() bool
Single to test whether the range of this distribution contains just a single value.
func (*DiscreteUniformDistribution) ToExternalRepr ¶ added in v0.0.3
func (d *DiscreteUniformDistribution) ToExternalRepr(ir float64) interface{}
ToExternalRepr to convert internal representation of a parameter value into external representation.
type Distribution ¶
type Distribution interface {
// ToExternalRepr to convert internal representation of a parameter value into external representation.
ToExternalRepr(float64) interface{}
// Single to test whether the range of this distribution contains just a single value.
Single() bool
// Contains to check a parameter value is contained in the range of this distribution.
Contains(float64) bool
}
Distribution represents a parameter that can be optimized.
type FrozenTrial ¶
type FrozenTrial struct {
ID int `json:"trial_id"`
StudyID int `json:"study_id"`
Number int `json:"number"`
State TrialState `json:"state"`
Value float64 `json:"value"`
IntermediateValues map[int]float64 `json:"intermediate_values"`
DatetimeStart time.Time `json:"datetime_start"`
DatetimeComplete time.Time `json:"datetime_complete"`
InternalParams map[string]float64 `json:"internal_params"`
Params map[string]interface{} `json:"params"`
Distributions map[string]interface{} `json:"distributions"`
UserAttrs map[string]string `json:"user_attrs"`
SystemAttrs map[string]string `json:"system_attrs"`
}
FrozenTrial holds the status and results of a Trial.
func (FrozenTrial) GetLatestStep ¶ added in v0.2.0
func (t FrozenTrial) GetLatestStep() (step int, exist bool)
GetLatestStep returns the latest step in intermediate values.
type FuncObjective ¶
FuncObjective is a type of objective function
type InMemoryStorage ¶
type InMemoryStorage struct {
// contains filtered or unexported fields
}
InMemoryStorage stores data in memory of the Go process.
func NewInMemoryStorage ¶
func NewInMemoryStorage() *InMemoryStorage
NewInMemoryStorage returns new memory storage.
func (*InMemoryStorage) CloneTrial ¶ added in v0.3.0
func (s *InMemoryStorage) CloneTrial(studyID int, baseTrial FrozenTrial) (int, error)
CloneTrial creates new Trial from the given base Trial.
func (*InMemoryStorage) CreateNewStudy ¶ added in v0.2.0
func (s *InMemoryStorage) CreateNewStudy(name string) (int, error)
CreateNewStudy creates study and returns studyID.
Example ¶
package main
import (
"fmt"
"github.com/c-bata/goptuna"
)
func main() {
storage := goptuna.NewInMemoryStorage()
studyID, err := storage.CreateNewStudy("")
if err != nil {
panic(err)
}
fmt.Println(studyID)
}
Output: 1
func (*InMemoryStorage) CreateNewTrial ¶ added in v0.2.0
func (s *InMemoryStorage) CreateNewTrial(studyID int) (int, error)
CreateNewTrial creates trial and returns trialID.
func (*InMemoryStorage) DeleteStudy ¶ added in v0.2.0
func (s *InMemoryStorage) DeleteStudy(studyID int) error
DeleteStudy deletes a study.
func (*InMemoryStorage) GetAllStudySummaries ¶ added in v0.0.2
func (s *InMemoryStorage) GetAllStudySummaries() ([]StudySummary, error)
GetAllStudySummaries returns all study summaries.
func (*InMemoryStorage) GetAllTrials ¶
func (s *InMemoryStorage) GetAllTrials(studyID int) ([]FrozenTrial, error)
GetAllTrials returns the all trials.
func (*InMemoryStorage) GetBestTrial ¶
func (s *InMemoryStorage) GetBestTrial(studyID int) (FrozenTrial, error)
GetBestTrial returns the best trial.
func (*InMemoryStorage) GetStudyDirection ¶
func (s *InMemoryStorage) GetStudyDirection(studyID int) (StudyDirection, error)
GetStudyDirection returns study direction of the objective.
func (*InMemoryStorage) GetStudyIDFromName ¶ added in v0.0.2
func (s *InMemoryStorage) GetStudyIDFromName(name string) (int, error)
GetStudyIDFromName return the study id from study name.
func (*InMemoryStorage) GetStudyIDFromTrialID ¶ added in v0.0.2
func (s *InMemoryStorage) GetStudyIDFromTrialID(trialID int) (int, error)
GetStudyIDFromTrialID return the study id from trial id.
Example ¶
package main
import (
"fmt"
"github.com/c-bata/goptuna"
)
func main() {
storage := goptuna.NewInMemoryStorage()
studyID, err := storage.CreateNewStudy("")
if err != nil {
panic(err)
}
trialID, err := storage.CreateNewTrial(studyID)
if err != nil {
panic(err)
}
actual, err := storage.GetStudyIDFromTrialID(trialID)
if err != nil {
panic(err)
}
fmt.Println(actual)
}
Output: 1
func (*InMemoryStorage) GetStudyNameFromID ¶ added in v0.0.2
func (s *InMemoryStorage) GetStudyNameFromID(studyID int) (string, error)
GetStudyNameFromID return the study name from study id.
func (*InMemoryStorage) GetStudySystemAttrs ¶ added in v0.0.2
func (s *InMemoryStorage) GetStudySystemAttrs(studyID int) (map[string]string, error)
GetStudySystemAttrs to restore the attributes for the system.
func (*InMemoryStorage) GetStudyUserAttrs ¶ added in v0.0.2
func (s *InMemoryStorage) GetStudyUserAttrs(studyID int) (map[string]string, error)
GetStudyUserAttrs to restore the attributes for the user.
func (*InMemoryStorage) GetTrial ¶
func (s *InMemoryStorage) GetTrial(trialID int) (FrozenTrial, error)
GetTrial returns Trial.
func (*InMemoryStorage) GetTrialNumberFromID ¶ added in v0.0.2
func (s *InMemoryStorage) GetTrialNumberFromID(trialID int) (int, error)
GetTrialNumberFromID returns the trial's number.
func (*InMemoryStorage) GetTrialParam ¶ added in v0.0.2
func (s *InMemoryStorage) GetTrialParam(trialID int, paramName string) (float64, error)
GetTrialParam returns the internal parameter of the trial
func (*InMemoryStorage) GetTrialParams ¶ added in v0.0.2
func (s *InMemoryStorage) GetTrialParams(trialID int) (map[string]interface{}, error)
GetTrialParams returns the external parameters in the trial
func (*InMemoryStorage) GetTrialSystemAttrs ¶ added in v0.0.2
func (s *InMemoryStorage) GetTrialSystemAttrs(trialID int) (map[string]string, error)
GetTrialSystemAttrs to restore the attributes for the system.
func (*InMemoryStorage) GetTrialUserAttrs ¶ added in v0.0.2
func (s *InMemoryStorage) GetTrialUserAttrs(trialID int) (map[string]string, error)
GetTrialUserAttrs to restore the attributes for the user.
func (*InMemoryStorage) SetStudyDirection ¶
func (s *InMemoryStorage) SetStudyDirection(studyID int, direction StudyDirection) error
SetStudyDirection sets study direction of the objective.
func (*InMemoryStorage) SetStudySystemAttr ¶ added in v0.0.2
func (s *InMemoryStorage) SetStudySystemAttr(studyID int, key string, value string) error
SetStudySystemAttr to store the value for the system.
Example ¶
package main
import (
"fmt"
"github.com/c-bata/goptuna"
)
func main() {
storage := goptuna.NewInMemoryStorage()
studyID, err := storage.CreateNewStudy("")
if err != nil {
panic(err)
}
err = storage.SetStudySystemAttr(studyID, "key", "value")
if err != nil {
panic(err)
}
attrs, err := storage.GetStudySystemAttrs(studyID)
if err != nil {
panic(err)
}
for k, v := range attrs {
fmt.Println(k, v)
}
}
Output: key value
func (*InMemoryStorage) SetStudyUserAttr ¶ added in v0.0.2
func (s *InMemoryStorage) SetStudyUserAttr(studyID int, key string, value string) error
SetStudyUserAttr to store the value for the user.
Example ¶
package main
import (
"fmt"
"github.com/c-bata/goptuna"
)
func main() {
storage := goptuna.NewInMemoryStorage()
studyID, err := storage.CreateNewStudy("")
if err != nil {
panic(err)
}
err = storage.SetStudyUserAttr(studyID, "key", "value")
if err != nil {
panic(err)
}
attrs, err := storage.GetStudyUserAttrs(studyID)
if err != nil {
panic(err)
}
for k, v := range attrs {
fmt.Println(k, v)
}
}
Output: key value
func (*InMemoryStorage) SetTrialIntermediateValue ¶ added in v0.0.3
func (s *InMemoryStorage) SetTrialIntermediateValue(trialID int, step int, value float64) error
SetTrialIntermediateValue sets the intermediate value of trial. While sets the intermediate value, trial.Value is also updated.
func (*InMemoryStorage) SetTrialParam ¶
func (s *InMemoryStorage) SetTrialParam( trialID int, paramName string, paramValueInternal float64, distribution interface{}) error
SetTrialParam sets the sampled parameters of trial.
func (*InMemoryStorage) SetTrialState ¶
func (s *InMemoryStorage) SetTrialState(trialID int, state TrialState) error
SetTrialState sets the state of trial.
func (*InMemoryStorage) SetTrialSystemAttr ¶ added in v0.0.2
func (s *InMemoryStorage) SetTrialSystemAttr(trialID int, key string, value string) error
SetTrialSystemAttr to store the value for the system.
Example ¶
package main
import (
"fmt"
"github.com/c-bata/goptuna"
)
func main() {
storage := goptuna.NewInMemoryStorage()
studyID, err := storage.CreateNewStudy("")
if err != nil {
panic(err)
}
trialID, err := storage.CreateNewTrial(studyID)
if err != nil {
panic(err)
}
err = storage.SetTrialSystemAttr(trialID, "key", "value")
if err != nil {
panic(err)
}
attrs, err := storage.GetTrialSystemAttrs(trialID)
if err != nil {
panic(err)
}
for k, v := range attrs {
fmt.Println(k, v)
}
}
Output: key value
func (*InMemoryStorage) SetTrialUserAttr ¶ added in v0.0.2
func (s *InMemoryStorage) SetTrialUserAttr(trialID int, key string, value string) error
SetTrialUserAttr to store the value for the user.
Example ¶
package main
import (
"fmt"
"github.com/c-bata/goptuna"
)
func main() {
storage := goptuna.NewInMemoryStorage()
studyID, err := storage.CreateNewStudy("")
if err != nil {
panic(err)
}
trialID, err := storage.CreateNewTrial(studyID)
if err != nil {
panic(err)
}
err = storage.SetTrialUserAttr(trialID, "key", "value")
if err != nil {
panic(err)
}
attrs, err := storage.GetTrialUserAttrs(trialID)
if err != nil {
panic(err)
}
for k, v := range attrs {
fmt.Println(k, v)
}
}
Output: key value
func (*InMemoryStorage) SetTrialValue ¶
func (s *InMemoryStorage) SetTrialValue(trialID int, value float64) error
SetTrialValue sets the value of trial.
type IntUniformDistribution ¶
type IntUniformDistribution struct {
// High is higher endpoint of the range of the distribution (included in the range).
High int `json:"high"`
// Low is lower endpoint of the range of the distribution (included in the range).
Low int `json:"low"`
}
IntUniformDistribution is a uniform distribution on integers.
func (*IntUniformDistribution) Contains ¶ added in v0.0.2
func (d *IntUniformDistribution) Contains(ir float64) bool
Contains to check a parameter value is contained in the range of this distribution.
func (*IntUniformDistribution) Single ¶ added in v0.0.2
func (d *IntUniformDistribution) Single() bool
Single to test whether the range of this distribution contains just a single value.
func (*IntUniformDistribution) ToExternalRepr ¶ added in v0.0.2
func (d *IntUniformDistribution) ToExternalRepr(ir float64) interface{}
ToExternalRepr to convert internal representation of a parameter value into external representation.
type LogUniformDistribution ¶ added in v0.0.3
type LogUniformDistribution struct {
// High is higher endpoint of the range of the distribution (included in the range).
High float64 `json:"high"`
// Low is lower endpoint of the range of the distribution (included in the range).
Low float64 `json:"low"`
}
LogUniformDistribution is a uniform distribution in the log domain.
func (*LogUniformDistribution) Contains ¶ added in v0.0.3
func (d *LogUniformDistribution) Contains(ir float64) bool
Contains to check a parameter value is contained in the range of this distribution.
func (*LogUniformDistribution) Single ¶ added in v0.0.3
func (d *LogUniformDistribution) Single() bool
Single to test whether the range of this distribution contains just a single value.
func (*LogUniformDistribution) ToExternalRepr ¶ added in v0.0.3
func (d *LogUniformDistribution) ToExternalRepr(ir float64) interface{}
ToExternalRepr to convert internal representation of a parameter value into external representation.
type Logger ¶ added in v0.1.0
type Logger interface {
Debug(msg string, fields ...interface{})
Info(msg string, fields ...interface{})
Warn(msg string, fields ...interface{})
Error(msg string, fields ...interface{})
}
Logger is the interface for logging messages. If you want to log nothing, please set Logger as nil. If you want to print more verbose logs, it might StudyOptionSetTrialNotifyChannel option are useful.
type LoggerLevel ¶ added in v0.1.0
type LoggerLevel int
LoggerLevel represents Level is a logging priority. Higher levels are more important.
const ( // LoggerLevelDebug logs are typically voluminous, and are usually disabled in production. LoggerLevelDebug LoggerLevel = iota - 1 // LoggerLevelInfo is the default logging priority. LoggerLevelInfo // LoggerLevelWarn logs are more important than Info, but don't need individual human review. LoggerLevelWarn // LoggerLevelError logs are high-priority. LoggerLevelError )
type Pruner ¶ added in v0.0.3
type Pruner interface {
// Prune judge whether the trial should be pruned at the given step.
Prune(study *Study, trial FrozenTrial) (bool, error)
}
Pruner is a interface for early stopping algorithms.
type RandomSearchSampler ¶
type RandomSearchSampler struct {
// contains filtered or unexported fields
}
RandomSearchSampler for random search
func NewRandomSearchSampler ¶
func NewRandomSearchSampler(opts ...RandomSearchSamplerOption) *RandomSearchSampler
NewRandomSearchSampler implements random search algorithm.
func (*RandomSearchSampler) Sample ¶
func (s *RandomSearchSampler) Sample( study *Study, trial FrozenTrial, paramName string, paramDistribution interface{}, ) (float64, error)
Sample a parameter for a given distribution.
type RandomSearchSamplerOption ¶
type RandomSearchSamplerOption func(sampler *RandomSearchSampler)
RandomSearchSamplerOption is a type of function to set change the option.
func RandomSearchSamplerOptionSeed ¶
func RandomSearchSamplerOptionSeed(seed int64) RandomSearchSamplerOption
RandomSearchSamplerOptionSeed sets seed number.
type RelativeSampler ¶ added in v0.2.0
type RelativeSampler interface {
// SampleRelative samples multiple dimensional parameters in a given search space.
SampleRelative(*Study, FrozenTrial, map[string]interface{}) (map[string]float64, error)
}
RelativeSampler is the interface for sampling algorithms that use relationship between parameters such as Gaussian Process and CMA-ES.
This interface is called once at the beginning of each trial, i.e., right before the evaluation of the objective function.
type Sampler ¶
type Sampler interface {
// Sample a single parameter for a given distribution.
Sample(*Study, FrozenTrial, string, interface{}) (float64, error)
}
Sampler is the interface for sampling algorithms that do not use relationship between parameters such as random sampling and TPE.
Note that if study object has RelativeSampler, this interface is used only for parameters that are not sampled by RelativeSampler.
type StdLogger ¶ added in v0.1.0
type StdLogger struct {
Logger *log.Logger
Level LoggerLevel
Color bool
}
StdLogger wraps 'log' standard library.
func (*StdLogger) Debug ¶ added in v0.1.0
Debug logs a message at LoggerLevelDebug which are usually disabled in production.
func (*StdLogger) Error ¶ added in v0.1.0
Error logs a message at LoggerLevelError which are high-priority.
type Storage ¶
type Storage interface {
// Basic study manipulation
CreateNewStudy(name string) (int, error)
DeleteStudy(studyID int) error
SetStudyDirection(studyID int, direction StudyDirection) error
SetStudyUserAttr(studyID int, key string, value string) error
SetStudySystemAttr(studyID int, key string, value string) error
// Basic study access
GetStudyIDFromName(name string) (int, error)
GetStudyIDFromTrialID(trialID int) (int, error)
GetStudyNameFromID(studyID int) (string, error)
GetStudyDirection(studyID int) (StudyDirection, error)
GetStudyUserAttrs(studyID int) (map[string]string, error)
GetStudySystemAttrs(studyID int) (map[string]string, error)
GetAllStudySummaries() ([]StudySummary, error)
// Basic trial manipulation
CreateNewTrial(studyID int) (int, error)
CloneTrial(studyID int, baseTrial FrozenTrial) (int, error)
SetTrialValue(trialID int, value float64) error
SetTrialIntermediateValue(trialID int, step int, value float64) error
SetTrialParam(trialID int, paramName string, paramValueInternal float64,
distribution interface{}) error
SetTrialState(trialID int, state TrialState) error
SetTrialUserAttr(trialID int, key string, value string) error
SetTrialSystemAttr(trialID int, key string, value string) error
// Basic trial access
GetTrialNumberFromID(trialID int) (int, error)
GetTrialParam(trialID int, paramName string) (float64, error)
GetTrial(trialID int) (FrozenTrial, error)
GetAllTrials(studyID int) ([]FrozenTrial, error)
GetBestTrial(studyID int) (FrozenTrial, error)
GetTrialParams(trialID int) (map[string]interface{}, error)
GetTrialUserAttrs(trialID int) (map[string]string, error)
GetTrialSystemAttrs(trialID int) (map[string]string, error)
}
Storage interface abstract a backend database and provide library internal interfaces to read/write history of studies and trials. This interface is not supposed to be directly accessed by library users.
type Study ¶
type Study struct {
ID int
Storage Storage
Sampler Sampler
RelativeSampler RelativeSampler
Pruner Pruner
// contains filtered or unexported fields
}
Study corresponds to an optimization task, i.e., a set of trials.
func CreateStudy ¶
func CreateStudy( name string, opts ...StudyOption, ) (*Study, error)
CreateStudy creates a new Study object.
func LoadStudy ¶ added in v0.0.4
func LoadStudy( name string, opts ...StudyOption, ) (*Study, error)
LoadStudy loads an existing study.
func (*Study) Direction ¶
func (s *Study) Direction() StudyDirection
Direction returns the direction of objective function value
func (*Study) EnqueueTrial ¶ added in v0.3.0
EnqueueTrial to enqueue a trial with given parameter values. You can fix the next sampling parameters which will be evaluated in your objective function.
This is an EXPERIMENTAL API and may be changed in the future. Currently EnqueueTrial only accepts internal representations. This means that you need to encode categorical parameter to its index number. Furthermore, please caution that there is a concurrency problem in RDB storage like https://github.com/optuna/optuna/pull/1014.
Example ¶
package main
import (
"fmt"
"math"
"github.com/c-bata/goptuna"
)
func main() {
study, _ := goptuna.CreateStudy(
"example",
goptuna.StudyOptionLogger(nil),
)
objective := func(trial goptuna.Trial) (float64, error) {
x1, _ := trial.SuggestUniform("x1", -10, 10)
x2, _ := trial.SuggestUniform("x2", -10, 10)
return math.Pow(x1-2, 2) + math.Pow(x2+5, 2), nil
}
study.EnqueueTrial(map[string]float64{"x1": 2, "x2": -5})
err := study.Optimize(objective, 1)
if err != nil {
panic(err)
}
trials, err := study.GetTrials()
if err != nil {
panic(err)
}
fmt.Printf("x1: %.3f\n", trials[0].Params["x1"].(float64))
fmt.Printf("x2: %.3f\n", trials[0].Params["x2"].(float64))
}
Output: x1: 2.000 x2: -5.000
func (*Study) GetBestParams ¶
GetBestParams return parameters of the best trial
func (*Study) GetBestValue ¶
GetBestValue return the best objective value
func (*Study) GetSystemAttrs ¶ added in v0.0.4
GetSystemAttrs to store the value for the system.
func (*Study) GetTrials ¶
func (s *Study) GetTrials() ([]FrozenTrial, error)
GetTrials returns all trials in this study.
func (*Study) GetUserAttrs ¶ added in v0.0.4
GetUserAttrs to store the value for the user.
func (*Study) Optimize ¶
func (s *Study) Optimize(objective FuncObjective, evaluateMax int) error
Optimize optimizes an objective function.
Example ¶
package main
import (
"fmt"
"math"
"github.com/c-bata/goptuna"
)
func main() {
sampler := goptuna.NewRandomSearchSampler(
goptuna.RandomSearchSamplerOptionSeed(0),
)
study, _ := goptuna.CreateStudy(
"example",
goptuna.StudyOptionSampler(sampler),
goptuna.StudyOptionLogger(nil),
)
objective := func(trial goptuna.Trial) (float64, error) {
x1, _ := trial.SuggestUniform("x1", -10, 10)
x2, _ := trial.SuggestUniform("x2", -10, 10)
return math.Pow(x1-2, 2) + math.Pow(x2+5, 2), nil
}
if err := study.Optimize(objective, 10); err != nil {
panic(err)
}
value, _ := study.GetBestValue()
params, _ := study.GetBestParams()
fmt.Printf("Best trial: %.5f\n", value)
fmt.Printf("x1: %.3f\n", params["x1"].(float64))
fmt.Printf("x2: %.3f\n", params["x2"].(float64))
}
Output: Best trial: 0.03833 x1: 2.182 x2: -4.927
func (*Study) SetSystemAttr ¶ added in v0.0.4
SetSystemAttr to store the value for the system.
func (*Study) SetUserAttr ¶ added in v0.0.4
SetUserAttr to store the value for the user.
func (*Study) WithContext ¶ added in v0.0.2
WithContext sets a context and it might cancel the execution of Optimize.
type StudyDirection ¶
type StudyDirection string
StudyDirection represents the direction of the optimization
const ( // StudyDirectionMaximize maximizes objective function value StudyDirectionMaximize StudyDirection = "maximize" // StudyDirectionMinimize minimizes objective function value StudyDirectionMinimize StudyDirection = "minimize" )
type StudyOption ¶
StudyOption to pass the custom option
func StudyOptionIgnoreError ¶ added in v0.1.0
func StudyOptionIgnoreError(ignore bool) StudyOption
StudyOptionIgnoreError is an option to continue even if it receive error while running Optimize method.
func StudyOptionLoadIfExists ¶ added in v0.2.0
func StudyOptionLoadIfExists(loadIfExists bool) StudyOption
StudyOptionLoadIfExists to load the study if exists.
func StudyOptionLogger ¶ added in v0.1.0
func StudyOptionLogger(logger Logger) StudyOption
StudyOptionLogger sets Logger.
func StudyOptionPruner ¶ added in v0.0.3
func StudyOptionPruner(pruner Pruner) StudyOption
StudyOptionPruner sets the pruner object.
func StudyOptionRelativeSampler ¶ added in v0.2.0
func StudyOptionRelativeSampler(sampler RelativeSampler) StudyOption
StudyOptionRelativeSampler sets the relative sampler object.
func StudyOptionSampler ¶
func StudyOptionSampler(sampler Sampler) StudyOption
StudyOptionSampler sets the sampler object.
func StudyOptionSetDirection ¶
func StudyOptionSetDirection(direction StudyDirection) StudyOption
StudyOptionSetDirection change the direction of optimize
func StudyOptionSetTrialNotifyChannel ¶
func StudyOptionSetTrialNotifyChannel(notify chan FrozenTrial) StudyOption
StudyOptionSetTrialNotifyChannel to subscribe the finished trials.
func StudyOptionStorage ¶
func StudyOptionStorage(storage Storage) StudyOption
StudyOptionStorage sets the storage object.
type StudySummary ¶ added in v0.0.2
type StudySummary struct {
ID int `json:"study_id"`
Name string `json:"study_name"`
Direction StudyDirection `json:"direction"`
BestTrial FrozenTrial `json:"best_trial"`
UserAttrs map[string]string `json:"user_attrs"`
SystemAttrs map[string]string `json:"system_attrs"`
DatetimeStart time.Time `json:"datetime_start"`
}
StudySummary holds basic attributes and aggregated results of Study.
type Trial ¶
Trial is a process of evaluating an objective function.
This object is passed to an objective function and provides interfaces to get parameter suggestion, manage the trial's state of the trial. Note that this object is seamlessly instantiated and passed to the objective function behind; hence, in typical use cases, library users do not care about instantiation of this object.
func (*Trial) GetContext ¶ added in v0.0.4
GetContext returns a context which is registered at 'study.WithContext()'.
func (*Trial) GetSystemAttrs ¶ added in v0.0.4
GetSystemAttrs to store the value for the system.
func (*Trial) GetUserAttrs ¶ added in v0.0.4
GetUserAttrs to store the value for the user.
func (*Trial) Number ¶ added in v0.0.3
Number return trial's number which is consecutive and unique in a study.
func (*Trial) SetSystemAttr ¶ added in v0.0.4
SetSystemAttr to store the value for the system.
func (*Trial) SetUserAttr ¶ added in v0.0.4
SetUserAttr to store the value for the user.
func (*Trial) ShouldPrune ¶ added in v0.0.3
ShouldPrune judges whether the trial should be pruned. This method calls prune method of the pruner, which judges whether the trial should be pruned at the given step.
func (*Trial) SuggestCategorical ¶ added in v0.0.2
SuggestCategorical suggests an categorical parameter.
func (*Trial) SuggestDiscreteUniform ¶ added in v0.0.4
SuggestDiscreteUniform suggests a value from a discrete uniform distribution.
func (*Trial) SuggestInt ¶
SuggestInt suggests an integer parameter.
func (*Trial) SuggestLogUniform ¶ added in v0.0.4
SuggestLogUniform suggests a value from a uniform distribution in the log domain.
type TrialState ¶
type TrialState int
TrialState is a state of Trial
const ( // TrialStateRunning means Trial is running. TrialStateRunning TrialState = iota // TrialStateComplete means Trial has been finished without any error. TrialStateComplete // TrialStatePruned means Trial has been pruned. TrialStatePruned // TrialStateFail means Trial has failed due to an uncaught error. TrialStateFail // TrialStateWaiting means Trial has been stopped, but may be resuming. TrialStateWaiting )
func (TrialState) IsFinished ¶
func (i TrialState) IsFinished() bool
IsFinished returns true if trial is not running.
func (TrialState) String ¶
func (i TrialState) String() string
type UniformDistribution ¶
type UniformDistribution struct {
// High is higher endpoint of the range of the distribution (included in the range).
High float64 `json:"high"`
// Low is lower endpoint of the range of the distribution (included in the range).
Low float64 `json:"low"`
}
UniformDistribution is a uniform distribution in the linear domain.
func (*UniformDistribution) Contains ¶ added in v0.0.2
func (d *UniformDistribution) Contains(ir float64) bool
Contains to check a parameter value is contained in the range of this distribution.
func (*UniformDistribution) Single ¶ added in v0.0.2
func (d *UniformDistribution) Single() bool
Single to test whether the range of this distribution contains just a single value.
func (*UniformDistribution) ToExternalRepr ¶ added in v0.0.2
func (d *UniformDistribution) ToExternalRepr(ir float64) interface{}
ToExternalRepr to convert internal representation of a parameter value into external representation.
Source Files
¶
Directories
¶
| Path | Synopsis |
|---|---|
|
_benchmarks
|
|
|
tpe_solver
command
|
|
|
_examples
|
|
|
concurrency
command
|
|
|
signalhandling
command
|
|
|
simple_rdb
command
|
|
|
simple_tpe
command
|
|
|
trialnotify
command
|
|
|
internal
|
|