Skip to content

Instantly share code, notes, and snippets.

View yulkang's full-sized avatar

Yul Kang yulkang

View GitHub Profile
@yulkang
yulkang / rotateVectors.py
Created December 13, 2022 04:29 — forked from fasiha/rotateVectors.py
I frequently have to re-implement projecting vectors onto other vectors, and rotating vectors about other vectors. So here we go. Public domain.
import unittest
import numpy as np
import numpy.linalg as linalg
def makeUnit(x):
"""Normalize entire input to norm 1. Not what you want for 2D arrays!"""
return x / linalg.norm(x)
@yulkang
yulkang / bootstrap.py
Created April 25, 2022 16:49 — forked from johnb30/bootstrap.py
Bootstrapped two-sample t-test in Python
from __future__ import division
import numpy as np
import pandas as pd
import random
def sample(data):
sample = [random.choice(data) for _ in xrange(len(data))]
return sample
def bootstrap_t_test(treatment, control, nboot = 1000, direction = "less"):
@yulkang
yulkang / is_keyboard_interrupt.py
Last active March 28, 2020 11:47
Test whether the exception is Ctrl + C in Pycharm's Python console
"""
You can use PyCharm's Python console and use Ctrl + C,
if you catch the exception that PyCharm raises when Ctrl + C is pressed.
I wrote a short function below called `is_keyboard_interrupt`
that tells you whether the exception is KeyboardInterrupt,
including PyCharm's.
If it is not, simply re-raise it.
I paste a simplified version of the code below.
When it is run:
@yulkang
yulkang / print_progress.py
Created March 18, 2020 12:52
Writing output on the same line multiple times in the console
# On PyCharm Debugger console, \r needs to come before the text.
# Otherwise, the text may not appear at all, or appear inconsistently.
# tested on PyCharm 2019.3, Python 3.6
# Modification of https://stackoverflow.com/a/517523/2565317
import time
print('Start.')
for i in range(100):
time.sleep(0.02)
@yulkang
yulkang / .gitignore
Last active November 12, 2023 03:29 — forked from gngdb/example_usage.py
Wrap PyTorch functions for scipy's optimize.minimize: https://docs.scipy.org/doc/scipy/reference/generated/scipy.optimize.minimize.html
*.pyc
@yulkang
yulkang / block_diag.py
Last active September 18, 2022 12:19
Block diagonal matrix in PyTorch - vectorized
"""A part of the pylabyk library: numpytorch.py at https://github.com/yulkang/pylabyk"""
import torch
def block_diag(m):
"""
Make a block diagonal matrix along dim=-3
EXAMPLE:
block_diag(torch.ones(4,3,2))
should give a 12 x 8 matrix with blocks of 3 x 2 ones.
Prepend batch dimensions if needed.
You can also give a list of matrices.
@yulkang
yulkang / kron.py
Last active February 7, 2024 13:49
Kronecker Product in PyTorch - with batch dimensions broadcast
"""A part of the pylabyk library: numpytorch.py at https://github.com/yulkang/pylabyk"""
import torch
def kron(a, b):
"""
Kronecker product of matrices a and b with leading batch dimensions.
Batch dimensions are broadcast. The number of them mush
:type a: torch.Tensor
:type b: torch.Tensor
:rtype: torch.Tensor
"""
@yulkang
yulkang / torch_jacobian.py
Created June 12, 2019 16:02 — forked from sbarratt/torch_jacobian.py
Get the jacobian of a vector-valued function that takes batch inputs, in pytorch.
def get_jacobian(net, x, noutputs):
x = x.squeeze()
n = x.size()[0]
x = x.repeat(noutputs, 1)
x.requires_grad_(True)
y = net(x)
y.backward(torch.eye(noutputs))
return x.grad.data