Skip to content

Instantly share code, notes, and snippets.

View evanthebouncy's full-sized avatar
💯
每人都发小红花

evanthebouncy

💯
每人都发小红花
View GitHub Profile
@evanthebouncy
evanthebouncy / ancestor.smt
Created October 19, 2020 19:53
simple ancestor relation encoding in z3 smt solver
(declare-sort Person)
(declare-fun ancestor (Person Person) Bool)
;; anti symmetry
(assert (forall ((x Person) (y Person))
(=> (ancestor x y) (not (ancestor y x)))))
;; transitivity
(assert (forall ((x Person) (y Person) (z Person))
(=> (and (ancestor x y) (ancestor y z))
@evanthebouncy
evanthebouncy / get_close.py
Created September 23, 2020 01:11
get closeby words from w2v for small word set
import numpy as np
# open whichever w2v file you want
fd = open("glove.6B/glove.6B.50d.txt").readlines()
# return a list of keys (words) and the w2v matrix Nxd
def to_numpy(lines):
keys = []
ary = []
for l in lines:
@evanthebouncy
evanthebouncy / bandit.py
Created September 3, 2020 08:54
find best arms
import random
import numpy as np
# there are 10 casinos
# each casino_i initially has 0 arms, however
# each casino_i is equipted with a slot-machine maker
# assume the slot machine maker is Unif(a_i, opt_i)
# where a_i < opt_i < 1
# you can take 2 kinds of actions:
def normalise(mat, axis):
if axis == 0:
row_sums = mat.sum(axis=1)
new_matrix = mat / row_sums[:, np.newaxis]
return new_matrix
if axis == 1:
col_sums = mat.sum(axis=0)
new_matrix = mat / col_sums[np.newaxis, :]
return new_matrix
@evanthebouncy
evanthebouncy / maze.py
Created September 30, 2019 15:02
make a maze out of mnist stuff
import numpy as np
from numpy import array
from scipy.misc import imresize
import copy
from scipy.ndimage.filters import gaussian_filter
import random
from keras.datasets import mnist
import matplotlib.pyplot as plt
@evanthebouncy
evanthebouncy / lgrind.sty
Created August 11, 2019 20:10
odd style file
%%
%% This is file `lgrind.sty',
%% generated with the docstrip utility.
%%
%% The original source files were:
%%
%% lgrind.dtx (with options: `package')
%%
%% LGrind is used to format source code of different programming
%% languages for LaTeX.
from PIL import Image, ImageDraw
import numpy as np
L = 1028
img = Image.new('RGBA', (L, L), (255, 0, 0, 0))
center = (L / 2, L / 2)
def scale_w_by_d(d):
return int(d / (L / 2) * 32)
@evanthebouncy
evanthebouncy / auto_enc_real.py
Created July 3, 2019 21:57
trying to auto-encode a real number
import torch
import torch.nn as nn
from torch.autograd import Variable
import numpy as np
import torch.nn.functional as F
import random
from tqdm import tqdm
if torch.cuda.is_available():
def to_torch(x, dtype, req = False):
@evanthebouncy
evanthebouncy / beam.py
Created May 21, 2019 03:54
beam search
import random
import math
import numpy as np
# a random mock-up environment with a single state of a float
# the goal is to get the float as close to 0 as possible with 2 possible moves
# x <- x + 1
# x <- cos(x)
class Env:
def __init__(self):
def train_dagger(env, teacher, student):
init_state = env.reset()
s_a_agg = []
for i in range(100):
# learning
print ("learning . . . ", i)
trace = get_rollout(env, init_state, student)
state_sample = [x[0] for x in trace]