Skip to content

Instantly share code, notes, and snippets.

View alper111's full-sized avatar
🥤

Alper Ahmetoglu alper111

🥤
View GitHub Profile
@alper111
alper111 / settings.json
Created December 1, 2023 11:18
My vscode settings.json
{
"editor.minimap.enabled": false,
"editor.fontFamily": "SF Mono",
"editor.bracketPairColorization.enabled": false,
"editor.acceptSuggestionOnEnter": "off",
"editor.inlineSuggest.enabled": true,
"files.autoSave": "onFocusChange",
"files.associations": {
"*.pro": "prolog",
"*.pl": "perl"
@alper111
alper111 / gumbelsigmoid_vs_sigmoid.py
Last active November 23, 2023 18:04
This snippet compares the sigmoid function's response and derivative with the Gumbel-sigmoid's.
import torch
import matplotlib.pyplot as plt
def sample_gumbel_diff(*shape):
eps = 1e-20
u1 = torch.rand(shape)
u2 = torch.rand(shape)
diff = torch.log(torch.log(u2+eps)/torch.log(u1+eps)+eps)
return diff
@alper111
alper111 / save_grid.py
Created March 23, 2022 08:00
PyTorch torchvision.utils.save_grid method with colored pad_value.
from PIL import Image
def save_with_color(x, filename, n_per_row, pad_value=[0., 0., 0.], padding=2):
N, C, H, W = x.shape
n_per_col = N // n_per_row
if N > n_per_row * n_per_col:
n_per_col += 1
canvas = torch.empty(H*n_per_col+(n_per_col-1)*padding,
W*n_per_row+(n_per_row-1)*padding,
3,
@alper111
alper111 / p2dist.py
Last active October 21, 2019 13:48
PyTorch and NumPy implementation of pairwise distance function (p2dist) for tensors with dimensions greater or equal to 2. Distances are calculated w.r.t. last dimension.
import torch
import numpy as np
def p2dist_pytorch(x, y):
y_dim = len(y.shape)
return torch.pow(x, 2).sum(dim=-1).view(x.shape[:-1]+(1,)) - \
2 * torch.matmul(x, y.permute(list(range(y_dim-2))+[y_dim-1, y_dim-2])) + \
torch.pow(y, 2).sum(dim=-1).view(y.shape[:-2]+(1,y.shape[-2]))
def p2dist_numpy(x, y):
@alper111
alper111 / SoftNode.py
Created September 20, 2019 11:49
PyTorch implementation of soft decision tree node. This recursive version is more readable however the tensorized version is faster.
import torch
class SoftNode(torch.nn.Module):
def __init__(self, in_features, out_features, depth, projection="constant"):
super(SoftNode, self).__init__()
self.projection = projection
if depth > 0:
self.left = SoftNode(in_features, out_features, depth-1, projection=projection)
self.right = SoftNode(in_features, out_features, depth-1, projection=projection)
self.gating = torch.nn.Linear(in_features, 1)
@alper111
alper111 / vgg_perceptual_loss.py
Last active April 10, 2024 02:21
PyTorch implementation of VGG perceptual loss
import torch
import torchvision
class VGGPerceptualLoss(torch.nn.Module):
def __init__(self, resize=True):
super(VGGPerceptualLoss, self).__init__()
blocks = []
blocks.append(torchvision.models.vgg16(pretrained=True).features[:4].eval())
blocks.append(torchvision.models.vgg16(pretrained=True).features[4:9].eval())
blocks.append(torchvision.models.vgg16(pretrained=True).features[9:16].eval())
@alper111
alper111 / lap_pyramid_loss.py
Last active April 19, 2024 18:32
PyTorch implementation of Laplacian pyramid loss
# MIT License
#
# Copyright (c) 2024 Alper Ahmetoglu
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
@alper111
alper111 / SoftTree.py
Last active September 20, 2019 11:46
PyTorch implementation of soft decision tree. All gating calculations are done at one step in order to utilize from GPU. The recursive definition might be faster for non-GPU machines.
import torch
class SoftTree(torch.nn.Module):
def __init__(self, in_features, out_features, depth, projection='constant', dropout=0.0):
super(SoftTree, self).__init__()
self.proj = projection
self.depth = depth
self.in_features = in_features
self.out_features = out_features