Skip to main content
  • Home
  • Development
  • Documentation
  • Donate
  • Operational login
  • Browse the archive

swh logo
SoftwareHeritage
Software
Heritage
Archive
Features
  • Search

  • Downloads

  • Save code now

  • Add forge now

  • Help

swh:1:snp:eee76444da62e238a10272cb71070ca8823b3f3d
  • Code
  • Branches (1)
  • Releases (0)
    • Branches
    • Releases
    • HEAD
    • refs/heads/main
    No releases to show
  • 6250ce0
  • /
  • nerf
  • /
  • schedule.py
Raw File Download

To reference or cite the objects present in the Software Heritage archive, permalinks based on SoftWare Hash IDentifiers (SWHIDs) must be used.
Select below a type of object currently browsed in order to display its associated SWHID and permalink.

  • content
  • directory
  • revision
  • snapshot
content badge
swh:1:cnt:aaadfc5737bb75addef87ac238afdbcd76766952
directory badge
swh:1:dir:2aec7f959a197d6347e16cd0cda954702fe7482a
revision badge
swh:1:rev:da207d03e7994d9c5a097126dcd509abedc26bc0
snapshot badge
swh:1:snp:eee76444da62e238a10272cb71070ca8823b3f3d

This interface enables to generate software citations, provided that the root directory of browsed objects contains a citation.cff or codemeta.json file.
Select below a type of object currently browsed in order to generate citations for them.

  • content
  • directory
  • revision
  • snapshot
(requires biblatex-software package)
Generating citation ...
(requires biblatex-software package)
Generating citation ...
(requires biblatex-software package)
Generating citation ...
(requires biblatex-software package)
Generating citation ...
Tip revision: da207d03e7994d9c5a097126dcd509abedc26bc0 authored by zachzhang07 on 21 November 2024, 08:07:14 UTC
Update readme.md
Tip revision: da207d0
schedule.py
# coding=utf-8
# Copyright 2023 The Google Research 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.

"""Hyperparameter schedules."""

import torch
import numpy as np


def log_lerp(t, v0, v1):
    """Interpolate log-linearly from `v0` (t=0) to `v1` (t=1)."""
    if v0 <= 0 or v1 <= 0:
        raise ValueError(f'Interpolants {v0} and {v1} must be positive.')
    lv0 = np.log(v0)
    lv1 = np.log(v1)
    return np.exp(np.clip(t, 0, 1) * (lv1 - lv0) + lv0)


class Schedule:
    pass


class ConstSchedule(Schedule):
    """Fixes the hyperparameter to a constant value: no schedule is used."""

    def __init__(self, val):
        self.val = val

    def __call__(self, step):
        return self.val

    def __repr__(self):
        return f'ConstSchedule: {self.val}'


class DictSchedule(Schedule):
    """Dictionary maps iterations to hyperparameter values."""

    def __init__(self, schedule):
        self.schedule = schedule

    def __call__(self, step):
        return [
            self.schedule[t] for t in sorted(self.schedule.keys()) if step >= t
        ][-1]

    def __repr__(self):
        return f'DictSchedule: {self.schedule}'


class LogLerpSchedule(Schedule):
    """Log-linearly interpolates a hyperparameter."""

    def __init__(self, start, end, v0, v1, zero_before_start=False):
        self.start = start
        self.end = end
        self.v0 = v0
        self.v1 = v1
        self.zero_before_start = zero_before_start

    def __call__(self, step):
        def h(_step):
            t = (_step - self.start) / (self.end - self.start)
            return log_lerp(t, self.v0, self.v1)

        if self.zero_before_start and step < self.start:
            return 0.0
        else:
            return h(step)

    def __repr__(self):
        return (
            f'LogLerpSchedule: start: {self.start}, end: {self.end}, v0: {self.v0},'
            f' v1: {self.v1}'
        )

back to top

Software Heritage — Copyright (C) 2015–2026, The Software Heritage developers. License: GNU AGPLv3+.
The source code of Software Heritage itself is available on our development forge.
The source code files archived by Software Heritage are available under their own copyright and licenses.
Terms of use: Archive access, API— Content policy— Contact— JavaScript license information— Web API