Skip to content

Instantly share code, notes, and snippets.

View zhpmatrix's full-sized avatar
🎯
Focusing

张大千 zhpmatrix

🎯
Focusing
View GitHub Profile
@zhpmatrix
zhpmatrix / check_convex.py
Created December 23, 2019 03:07 — forked from mblondel/check_convex.py
A small script to get numerical evidence that a function is convex
# Authors: Mathieu Blondel, Vlad Niculae
# License: BSD 3 clause
import numpy as np
def _gen_pairs(gen, max_iter, max_inner, random_state, verbose):
rng = np.random.RandomState(random_state)
# if tuple, interpret as randn
@zhpmatrix
zhpmatrix / top_k_viterbi.py
Last active November 22, 2019 12:24 — forked from PetrochukM/top_k_viterbi.py
Implemented a Top K Viterbi Decoder algorithm in PyTorch. Useful for Conditional Random Fields (CRFs)-based probabilistic graphical modelling. Learn more here: https://nlp.stanford.edu/joberant/esslli_2016/kbest-ict.pdf
import torch
# Credits to AllenNLP for the base implementation and base tests:
# https://github.com/allenai/allennlp/blob/master/allennlp/nn/util.py#L174
# Modified AllenNLP `viterbi_decode` to support `top_k` sequences efficiently.
def viterbi_decode(tag_sequence: torch.Tensor, transition_matrix: torch.Tensor, top_k: int=5):
"""
Perform Viterbi decoding in log space over a sequence given a transition matrix
specifying pairwise (transition) potentials between tags and a matrix of shape
import tensorflow as tf
from numpy import *
from random import randint
max_length = 2
batch_size = 3
label = array([[[0,0] for _ in range(max_length)] for _ in range(batch_size)])
for i in range(batch_size):
for j in range(max_length):
import tensorflow as tf
from numpy import *
from random import randint
max_length = 3
batch_size = 5
targets = array([[1 for _ in range(max_length)] for _ in range(batch_size)])
logits = array([[[randint(0,10)/10,randint(0,10)/10] for _ in range(max_length)] for _ in range(batch_size)])
sequence_length = array([randint(1,max_length) for _ in range(batch_size)])
@zhpmatrix
zhpmatrix / nms_fast.m
Created May 22, 2018 08:42 — forked from quantombone/nms_fast.m
blazing fast nms (non-maximum suppression)
function top = nms(boxes, overlap)
% top = nms_fast(boxes, overlap)
% Non-maximum suppression. (FAST VERSION)
% Greedily select high-scoring detections and skip detections
% that are significantly covered by a previously selected
% detection.
% NOTE: This is adapted from Pedro Felzenszwalb's version (nms.m),
% but an inner loop has been eliminated to significantly speed it
% up in the case of a large number of boxes
% Tomasz Malisiewicz (tomasz@cmu.edu)
from keras.models import Sequential
from keras.layers import Dense
x, y = ...
x_val, y_val = ...
# 1-dimensional MSE linear regression in Keras
model = Sequential()
model.add(Dense(1, input_dim=x.shape[1]))
model.compile(optimizer='rmsprop', loss='mse')