Source code for jacinle.utils.value_scheduler

#! /usr/bin/env python3
# -*- coding: utf-8 -*-
# File   : value_scheduler.py
# Author : Jiayuan Mao
# Email  : maojiayuan@gmail.com
# Date   : 07/18/2018
#
# This file is part of Jacinle.
# Distributed under terms of the MIT license.

__all__ = ['ValueScheduler', 'MonotonicSchedulerBase', 'LinearScheduler']


[docs] class ValueScheduler(object): """Essentially, define y = f(x), where x is a discrete, bounded variable."""
[docs] def get(self, x): raise NotImplementedError()
[docs] class ConstantScheduler(ValueScheduler):
[docs] def __init__(self, value): self.value = value
[docs] def get(self, x): return self.value
[docs] class MonotonicSchedulerBase(ValueScheduler):
[docs] def __init__(self, begin, begin_value, end, end_value): super().__init__() self.begin = begin self.begin_value = begin_value self.end = end self.end_value = end_value
[docs] def get(self, x): raise NotImplementedError()
[docs] class LinearScheduler(MonotonicSchedulerBase):
[docs] def get(self, x): if x < self.begin: return self.begin_value elif x > self.end: return self.end_value return self.begin_value + (self.end_value - self.begin_value) / (self.end - self.begin) * (x - self.begin)