Skip to content

Instantly share code, notes, and snippets.

View 1pha's full-sized avatar
💦
I may be slow to respond.

Daehyun Cho 1pha

💦
I may be slow to respond.
View GitHub Profile
@1pha
1pha / RMSE_loss_pytorch.py
Last active December 29, 2020 03:14
RMSE loss for pytorch (from pytorch discussion)
class RMSELoss(nn.Module):
def __init__(self):
super().__init__()
self.mse = nn.MSELoss()
def forward(self, yhat, y):
return torch.sqrt(self.mse(yhat, y))
from sklearn.model_selection import KFold
kfold = KFold(n_splits=10)
for i, (trn_idx, val_idx) in enumerate(kfold.split(X_train)):
print("Working on {}th Fold".format(i))
train_multi_ds = TensorDataset(X_train[trn_idx], y_multi_train[trn_idx])
val_multi_ds = TensorDataset(X_train[val_idx], y_multi_train[val_idx])
@1pha
1pha / L2_Regularization.py
Created December 9, 2020 07:37
L2 Regularization in PyTorch
_lambda = 0.0005
l2_reg = torch.tensor(0.)
for param in model.parameters():
l2_reg += torch.norm(param)
loss += lamb * l2_reg
class NoamOpt:
"Optim wrapper that implements rate."
def __init__(self, model_size, factor, warmup, optimizer):
self.optimizer = optimizer
self._step = 0
self.warmup = warmup
self.factor = factor
self.model_size = model_size
self._rate = 0
@1pha
1pha / item_characteristic_curve.py
Last active February 2, 2021 04:26
3 Parameter item characteristic curve from Item response theory (IRT), written with python
from scipy.stats import norm
import numpy as np
import matplotlib.pyplot as plt
a, b, c = 1, 0, 0
D = 1.702
def normal_ogive(theta, a=1, b=0, c=0):
return c + (1 - c) * norm.cdf(a*(theta - b))
@1pha
1pha / torch_max_dimension.py
Created February 23, 2021 12:11
Discovering how PyTorch takes 'dimension' in many cases, this case max
import torch
sample_tensor = torch.Tensor([
[1, 2, 3, 4, 5, 6],
[7, 8, 9, 10, 11, 12],
[13, 14, 15, 16, 17, 18],
])
sample_tensor = sample_tensor.view(3, 6//2, -1)
#tensor([[[ 1., 2.],
# [ 3., 4.],
@1pha
1pha / kill_desktop_ini.py
Created April 7, 2021 01:44
Killing desktop.ini with python function. I'm sick of deleting these .ini files through Windows explorer and made this..
import os
def kill_desktop_ini(root_dir):
for (root, dirs, files) in os.walk(root_dir):
if len(files) > 0:
for file_name in files:
if file_name.endswith('.ini'):
os.remove(f'{root}/{file_name}')
@1pha
1pha / depth_firth_search.py
Last active April 15, 2021 09:21
DFS algorithm for graph search theories.
def recursive_dfs(v, discovered=[]):
discovered.append(v)
for w in graph[v]: # for the graph of adjacent matrix
if w not in discovered:
discovered = recursive_dfs(w, discovered)
return discovered # will return the nodes searched in order
def iterative_dfs(start_v):
@1pha
1pha / seed_everything.py
Created May 25, 2021 07:28
Source code for setting every seed same.
import os
import random
import numpy as np
import torch
def seed_everything(seed=42):
os.environ['PYTHONHASHSEED'] = str(seed)
random.seed(seed)
@1pha
1pha / torch_collate_fn.py
Created May 28, 2021 05:10
Understanding collate_fn
import torch
from torch.utils.data import Dataset, DataLoader
import numpy as np
class MyDataset(Dataset):
def __init__(self):
x = np.random.randint(10, size=[1000, 3]) # 1000 3-dim samples
self.x = [x[i].tolist() for i in range(1000)]
y = np.random.randint(low=0, high=2, size=(1000,))
self.y = [y[i] for i in range(1000)]