[Ray] stop argument 적정하게 사용되는 지 확인해보기

2020. 9. 19. 20:12분석 Python/Ray

728x90

early stopping을 tune에서 제공하고 있지만, 실제로 사용하려고 하니 뭔가 잘 사용되지 않아서 테스트를 해보고 있다.

일단 기존 문서에 나와있는 training_iteration 단위가 내가 생각하는 epoch이라는 개념과는 조금 다른 것 같다.

 

training_iteration (int): The index of this training iteration, e.g. call to train(). This is incremented after step() is called.

위와 같이 정의되어 있는데, 즉 이말은 step을 호출할 때마 하는 것이지 내부에서 학습 중간에 멈추는 것이랑은 별개인 것 같아서 아래와 같이 실험해봤다.

training_iteration은 알아서 내부적으로 생성되는 property이다.

from ray import tune

def objective(x, a, b):
    return a * (x ** 0.5) + b

class Trainable(tune.Trainable):
    def setup(self, config):
        # config (dict): A dict of hyperparameters
        self.x = 0
        self.epoch = 0
        self.a = config["a"]
        self.b = config["b"]

    def step(self):  # This is called iteratively.
        score = objective(self.x, self.a, self.b)
        self.x += 1
        self.epoch +=1
        return {"epoch" : self.epoch ,"score": score}

analysis = tune.run(
    Trainable,
    stop={"training_iteration": 50},
    config={
        "a": 2,
        "b": 4
    })

print('best config: ', analysis.get_best_config(metric="score", mode="max"))

 

from ray import tune

def objective(x, a, b):
    return a * (x ** 0.5) + b

class Trainable(tune.Trainable):
    def setup(self, config):
        # config (dict): A dict of hyperparameters
        self.x = 0
        self.epoch = 0
        self.a = config["a"]
        self.b = config["b"]

    def step(self):  # This is called iteratively.
        score = objective(self.x, self.a, self.b)
        self.x += 1
        self.epoch +=1
        return {"epoch" : self.epoch ,"score": score}

analysis = tune.run(
    Trainable,
    stop={"epoch": 50},
    config={
        "a": 2,
        "b": 4
    })

print('best config: ', analysis.get_best_config(metric="score", mode="max"))

728x90