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 torchvision import models | |
modules=list(models.resnet18(weights='IMAGENET1K_V1').children())[:-1] | |
model = nn.Sequential(*modules) | |
x = torch.rand(size=[1, 3, 640, 480]) | |
emb = model(x) |
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 grad_norm(model: nn.Module): | |
total_norm = 0 | |
for p in model.parameters(): | |
param_norm = p.grad.detach().data.norm(2) | |
total_norm += param_norm.item() ** 2 | |
return total_norm ** 0.5 |
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 mixup(X : torch.tensor, | |
Y: torch.tensor, | |
alpha: int=8, | |
device: str='cpu'): | |
""" | |
it performs mixup between the first half of the batch with the second one | |
params | |
--- | |
X : images tensor [bs, c, h, w] |
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
column = ... | |
cond1, cond2 = ..., ... | |
agg_func = 'mean' | |
if agg_func == 'mean': | |
new_value = df[(cond1) & (cond2)][column].mean() | |
else: | |
new_value = df[(cond1) & (cond2)][column].mode()[0] | |
df.loc[(cond1) & (cond2), column] = df.loc[(cond1) & (cond2)][column].fillna(new_value) |
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
X_train = ... # (n,2) numpy array dataset | |
# Create a meshgrid | |
x_min, x_max = X_train[:, 0].min() - 1, X_train[:, 0].max() + 1 | |
y_min, y_max = X_train[:, 1].min() - 1, X_train[:, 1].max() + 1 | |
xx, yy = np.meshgrid(np.arange(x_min, x_max, 0.1), | |
np.arange(y_min, y_max, 0.1)) | |
# Make predictions on the meshgrid |
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
pd.options.display.float_format = '{:,.1f}%'.format | |
print((df.isna().sum()/df.count()).sort_values()) |
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
N_before = len(df) | |
COLUMNS_TO_PROCESS = ['sqft_living', 'sqft_basement', 'bedrooms'] | |
TRESHOLD_Q3 = 0.95 | |
TRESHOLD_Q1 = 0.05 | |
for column_name in COLUMNS_TO_PROCESS: | |
Q3 = df[column_name].quantile(TRESHOLD_Q3) | |
Q1 = df[column_name].quantile(TRESHOLD_Q1) | |
print(f"{column_name} [Q1, Q3] : [{Q1}, {Q3}]") | |
IQR = Q3 - Q1 |
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 PIL import Image | |
import requests | |
from io import BytesIO | |
url = 'https://upload.wikimedia.org/wikipedia/en/7/7d/Lenna_%28test_image%29.png' | |
response = requests.get(url) | |
img = Image.open(BytesIO(response.content)) |
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 pathlib import Path | |
DIR = Path.cwd() # work directory | |
PATH_TEST_DIR = Path(DIR, 'data') | |
PATH_TEST_FILE = Path( DIR, 'file.csv') |
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 | |
import torch.nn as nn | |
import torch.optim as optim | |
from torch.utils.data import DataLoader | |
from torchvision.datasets import MNIST | |
from torchvision.transforms import ToTensor | |
from tqdm import tqdm | |
# Set device (GPU or CPU) | |
device = torch.device("cuda" if torch.cuda.is_available() else "cpu") |