This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]) | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
_lambda = 0.0005 | |
l2_reg = torch.tensor(0.) | |
for param in model.parameters(): | |
l2_reg += torch.norm(param) | |
loss += lamb * l2_reg |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.], |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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): |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
import random | |
import numpy as np | |
import torch | |
def seed_everything(seed=42): | |
os.environ['PYTHONHASHSEED'] = str(seed) | |
random.seed(seed) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)] |
OlderNewer