Skip to content

Instantly share code, notes, and snippets.

@phuocphn
phuocphn / kl_divergence.py
Last active January 14, 2021 13:08
KL Divergence (Pytorch)
'''
As with NLLLoss , the input given is expected to contain log-probabilities…
The targets are given as probabilities (i.e. without taking the logarithm).
https://discuss.pytorch.org/t/kldivloss-returns-negative-value/62148
https://discuss.pytorch.org/t/kl-divergence-produces-negative-values/16791/16
https://discuss.pytorch.org/t/kullback-leibler-divergence-loss-function-giving-negative-values/763/16
'''
import torch
import torch
import torch.nn as nn
from lsq import Conv2dLSQ, LinearLSQ, ActLSQ
__all__ = ['ResNet', 'resnet18', 'resnet34', 'resnet50', 'resnet101',
'resnet152', 'resnext50_32x4d', 'resnext101_32x8d',
'wide_resnet50_2', 'wide_resnet101_2']
def conv3x3(in_planes, out_planes, stride=1, groups=1, dilation=1):
"""3x3 convolution with padding"""
import torch
import torch.nn as nn
import torch.optim as optim
from torch.autograd import Variable
import torch.nn.functional as F
import matplotlib.pyplot as plt
import numpy as np
import torch
import numpy as np
torch.backends.cudnn.deterministic = True
torch.backends.cudnn.benchmark = False
np.random.seed(0)
torch.manual_seed(0)
import torch
import numpy as np
torch.backends.cudnn.deterministic = True
torch.backends.cudnn.benchmark = False
np.random.seed(0)
torch.manual_seed(0)
@phuocphn
phuocphn / lq-net-bitsplit.py
Last active June 3, 2020 04:32
Representing an integer q by a K-bit binary encoding as the inner product between a basis vector and the binary coding vector
import torch
import numpy as np
torch.backends.cudnn.deterministic = True
torch.backends.cudnn.benchmark = False
np.random.seed(0)
torch.manual_seed(0)
nbit = 3
import math
import numpy as np
import torch.nn.functional as F
import torch
import torch.nn as nn
torch.backends.cudnn.deterministic = True
torch.backends.cudnn.benchmark = False
@phuocphn
phuocphn / lsq-quantize.py
Created June 1, 2020 03:37
(Testcases) Learned Step Size Quantization
import torch as t
import numpy as np
class GradScale(t.nn.Module):
def forward(self, x, scale):
y = x
y_grad = x / scale
return (y - y_grad).detach() + y_grad
@phuocphn
phuocphn / distributed_relu_pytorch.py
Created February 29, 2020 12:37
distributed_relu_pytorch.py
import os
import tempfile
import torch
import torch.distributed as dist
import torch.nn as nn
import torch.optim as optim
import torch.multiprocessing as mp
import numpy as np
import random
from torch.nn.parallel import DistributedDataParallel as DDP
@phuocphn
phuocphn / py3_AES.py
Last active March 10, 2019 06:31
Encryption using pycrypto, AES, and PKCS5 padding
'''
Author: komuw/pycrypto_AES.py
Security issues:
+ https://stackoverflow.com/questions/2641720/for-aes-cbc-encryption-whats-the-importance-of-the-iv
+ https://defuse.ca/cbcmodeiv.htm
+ https://passingcuriosity.com/2009/aes-encryption-in-python-with-m2crypto/
'''
from Crypto.Cipher import AES