This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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): |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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') |