Skip to content

Instantly share code, notes, and snippets.

@alwc
alwc / ctc_alignment_targets.py
Created August 26, 2020 06:45 — forked from vadimkantorov/ctc_alignment_targets.py
An implementation of CTC re-formulation via cross-entropy with pseudo-labels, following "A Novel Re-weighting Method for Connectionist Temporal Classification"
# CTC vanilla and CTC via crossentropy are equal, and their gradients as well. In this reformulation it's easier to experiment with modifications of CTC.
# References on CTC regularization:
# "A Novel Re-weighting Method for Connectionist Temporal Classification", Li et al, https://arxiv.org/abs/1904.10619
# "Focal CTC Loss for Chinese Optical Character Recognition on Unbalanced Datasets", Feng et al, https://www.hindawi.com/journals/complexity/2019/9345861/
# "Improved training for online end-to-end speech recognition systems", Kim et al, https://arxiv.org/abs/1711.02212
import torch
import torch.nn.functional as F
## generate example data
@alwc
alwc / timestamp.vim
Created May 10, 2020 17:20 — forked from jelera/timestamp.vim
It auto updates the timestamp in lines within the 20 first lines, matching "Last modified, Updated, Changed, etc."
" Append this to your vimrc file
"""""""""""""""""""""""""""""""""
" auto-update the timestamp right before saving a file
" The Timestamp format is : Sat 07 Dec 2013 12:51:00 AM CST
" Within the 20 first lines, the matched lines are :
" Last [Cc]hange(d)
" Changed
" Last [Mm]odified
" Modified
" Last [Uu]pdate(d)
@alwc
alwc / semisupervised.py
Created April 8, 2020 06:57 — forked from Dref360/semisupervised.py
Semi-supervised iterator
from typing import Optional
import numpy as np
import torch
from torch.utils.data import DataLoader, TensorDataset
from baal.active import ActiveLearningDataset
class AlternateIterator:
@alwc
alwc / brewv.sh
Created March 16, 2020 17:01 — forked from demosten/brewv.sh
Install specific version of a Homebrew formula
#!/bin/bash
#
# Install specific version of a Homebrew formula
#
# Usage: brewv.sh formula_name desired_version
#
# Notes:
# - this will unshallow your brew repo copy. It might take some time the first time
# you call this script
# - it will uninstall (instead of unlink) all your other versions of the formula.
@alwc
alwc / multiple_head.py
Created February 21, 2020 06:09 — forked from quanvuong/multiple_head.py
Multiple head network with pytorch
import torch
import torch.nn as nn
from torch.autograd import Variable
# Do this to display pytorch version.
# The version used in this gist is 0.3.0.post4.
print(torch.__version__)
# There are three steps to demonstrate multi head network
# 1. build the network
@alwc
alwc / plot_timings.py
Created February 18, 2020 07:34 — forked from david-macleod/plot_timings.py
plot_timings
def plot_timings(loader, n_batches, model_time=0.2, max_time=2.5):
fig, ax = plt.subplots()
ax.set_axisbelow(True)
ax.yaxis.grid(which="major", color='black', linewidth=1)
zero_time = time.time()
worker_ids = {}
worker_count = count()
@alwc
alwc / top-k-top-p.py
Created May 3, 2019 15:16 — forked from thomwolf/top-k-top-p.py
Sample the next token from a probability distribution using top-k and/or nucleus (top-p) sampling
def top_k_top_p_filtering(logits, top_k=0, top_p=0.0, filter_value=-float('Inf')):
""" Filter a distribution of logits using top-k and/or nucleus (top-p) filtering
Args:
logits: logits distribution shape (..., vocabulary size)
top_k >0: keep only top k tokens with highest probability (top-k filtering).
top_p >0.0: keep the top tokens with cumulative probability >= top_p (nucleus filtering).
"""
top_k = min(top_k, logits.size(-1)) # Safety check
if top_k > 0:
# Remove all tokens with a probability less than the last token of the top-k
@alwc
alwc / onnx2pytorch.py
Created April 22, 2019 15:24 — forked from qinjian623/onnx2pytorch.py
ONNX file to Pytorch model
import onnx
import struct
import torch
import torch.nn as nn
import torchvision as tv
import warnings
# enum DataType {
# UNDEFINED = 0;
@alwc
alwc / README.md
Created April 7, 2019 17:23 — forked from otmb/README.md
MLMultiArray to Double Array

Sample Data

Get cat.bin

Xcode Resources Add cat.bin

resources.png

Playground

import CoreML
@alwc
alwc / ellipse_bounding_box.ipynb
Created March 1, 2019 06:03 — forked from smidm/ellipse_bounding_box.ipynb
Compute bounding box for an ellipse.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.