Skip to content

Instantly share code, notes, and snippets.

@GoingMyWay
Created December 23, 2020 02:59
Show Gist options
  • Save GoingMyWay/45103ad1c4d3d71d5c90e983fc7a2281 to your computer and use it in GitHub Desktop.
Save GoingMyWay/45103ad1c4d3d71d5c90e983fc7a2281 to your computer and use it in GitHub Desktop.
Median quantile finder
import numpy as np
import torch as th
import torch.nn as nn
import matplotlib.pyplot as plt
from torch.optim import Adam
arr = np.arange(100)
class QR(nn.Module):
def __init__(self):
super().__init__()
self.x = nn.Parameter(data=th.from_numpy(np.array([1], dtype=np.float32)))
qr = QR()
opt = Adam(params=qr.parameters(), lr=0.1)
estimated_median, real_median, losses = [], [], []
for i in range(2000):
loss = th.mean(th.abs(th.from_numpy(arr)-qr.x))
opt.zero_grad()
loss.backward()
opt.step()
losses.append(loss.item())
estimated_median.append(qr.x.clone().detach())
real_median.append(np.median(arr))
print(f"step: {i}, loss:{loss.item()}, estimated median: {qr.x.clone().detach()}, real meadian: {np.median(arr)}")
plt.plot(range(len(estimated_median)), estimated_median, label='estimated_median')
plt.plot(range(len(estimated_median)), real_median, label='median')
plt.plot(range(len(estimated_median)), losses, label='loss')
plt.legend()
plt.show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment