Skip to content

Instantly share code, notes, and snippets.

View yuq-1s's full-sized avatar
:octocat:
Working in the lab

Yuquan Wang yuq-1s

:octocat:
Working in the lab
  • Tsinghua University
  • Beijing
View GitHub Profile
@yuq-1s
yuq-1s / cached.py
Created November 30, 2021 10:53
Utility function for caching middle results of Python
import gzip
import logging
import pickle
import time
import yaml
from tqdm import tqdm
def cached(cache_path):
def wrapper(func):
@yuq-1s
yuq-1s / income.py
Last active September 29, 2021 09:28
Comparing income of Beijing and Singapore.
import sys
import fire
class Beijing:
max_base = 28221
min_base = 5360
def __init__(self, total):
self.total = total
def wuxianyijin(self):
@yuq-1s
yuq-1s / hmm.py
Last active July 2, 2021 12:55
HMM on GPU with pytorch in 100 lines
'''
This proof-of-concept follows [PRML](https://www.microsoft.com/en-us/research/people/cmbishop/prml-book/)'s idea.
This code extends plain HMM in the way that it has different transition matrix and emission matrix on different features `xs`.
To get a normal HMM, you can set all `x` to the same.
`HMM.predict()` uses formula (13.44) in PRML, which considers the whole seen sequence of observation `y`s.
If you have no observed `y`s and only have `x`s, you can use `model.trans(x).view(T, N, self.H, self.H).softmax(dim=3)` as transition matrix to get predicted sequence.
`gamma` here represents posterior probability of hidden states.
'''
@yuq-1s
yuq-1s / treemnist.py
Created April 8, 2019 10:19
mnist with naive binary-tree-like hierarchical softmax
from __future__ import print_function
import argparse
import torch
import torch.nn as nn
import torch.nn.functional as F
import torch.optim as optim
from torchvision import datasets, transforms
import torchvision
class Net(nn.Module):
@yuq-1s
yuq-1s / heartbeat-client.py
Last active February 22, 2019 10:22
heartbeat for dynamic dns
import socket
import time
import copy
import struct
import socketserver
import select
#######################################################################
# package pyaes #
#######################################################################
@yuq-1s
yuq-1s / dptable.cc
Last active December 3, 2019 10:58
Dynamic programming like a mathematician
#include <limits>
#include <array>
#include <cassert>
#include <iostream>
#include <string>
#include <vector>
#define MO 998244353
template <typename T, size_t rank>
@yuq-1s
yuq-1s / workstation.md
Last active February 28, 2019 11:16
Build my own workstation with hybrid disk and dual boot

workstation

Goal

Dual boot Ubuntu 18.04 and Windows 10

Windows occupies an 128GB ssd for gaming Ubuntu occupies a 256GB ssd and two same 1TB hdd with raid0 grouped by [bcache][3].

@yuq-1s
yuq-1s / imle.py
Last active February 1, 2022 09:27
[Implicit Maximum Likelihood Estimation](https://arxiv.org/abs/1809.09087) in 100 lines
import mxnet as mx
from mxnet import nd, autograd
from mxnet.gluon import nn
from mxnet.gluon.contrib.nn import Identity, Concurrent
from mxnet import gluon
import logging
def d(a, b):
return (a - b).norm()
@yuq-1s
yuq-1s / djh4linux.sh
Created November 24, 2018 07:52
Compile Junhui Deng's DSA code on a linux machine
#! /bin/bash
find -name '*.cpp' | sed -i 's/typeinfo\.h/typeinfo/g'
find -name '*.h' | sed -i 's/typeinfo\.h/typeinfo/g'
find -name '*.cpp' | python patch.py
find -name '*.h' | python patch.py
imps=$(find -name '*_implementation.h')
@yuq-1s
yuq-1s / bits2float.cc
Last active October 14, 2018 09:40
Playing with floats: bits to floats and floats to bits.
#include "showbits.h"
#include <iostream>
#include <algorithm>
#include <string>
#include <locale>
#include <cassert>
typedef union {
int64_t i;