Skip to content

Instantly share code, notes, and snippets.

View tvogels's full-sized avatar

Thijs Vogels tvogels

  • Microsoft Research
  • Amsterdam
View GitHub Profile
@tvogels
tvogels / variance_reduction_techniques.py
Created March 31, 2021 07:59
Variance reduction SGD algorithms
#%%
import contextlib
import os
import random
import sys
from copy import deepcopy
import numpy as np
import pandas as pd
import seaborn as sns
@tvogels
tvogels / capture.py
Last active August 26, 2021 16:39
capture module inputs in PyTorch
import torch
from contextlib import contextmanager
_capture_inputs_dict = {}
_capture_inputs_active = False
@contextmanager
def capture_inputs():
@tvogels
tvogels / probability.py
Created September 13, 2021 16:24
with probability block
import random
import sys
class probability:
"""Based on https://stackoverflow.com/questions/12594148/skipping-execution-of-with-block"""
def __init__(self, probability):
assert probability >= 0 and probability <= 1
self.probability = probability
@tvogels
tvogels / profiler.py
Created December 22, 2021 18:48
PyTorch timer / profiler
import time
import contextlib
import queue
import threading
from collections import defaultdict
from typing import List, NamedTuple
import pandas as pd
import torch
@tvogels
tvogels / domtree.py
Created March 10, 2022 20:41
Python syntax for generating HTML, XML, or SVG programatically
from typing import Any, Dict, List, Union
def main():
# shortcuts
d = Dom()
svg, circle, rect, g = d.svg, d.circle, d.rect, d.g
print(
svg(
@tvogels
tvogels / sketch.py
Created May 10, 2022 06:39
Read a Sketch file in Python
import tempfile
from zipfile import ZipFile
import hashlib
import json
zipf = ZipFile("slides.sketch")
with tempfile.TemporaryDirectory() as d:
for file in zipf.infolist():
if file.filename.startswith("pages/"):