Skip to content

Instantly share code, notes, and snippets.

View odashi's full-sized avatar
🏠
Working from home

Yusuke Oda odashi

🏠
Working from home
View GitHub Profile
@odashi
odashi / jax_dataclass.py
Last active February 19, 2021 23:47
Augmented dataclass for JAX pytree.
import dataclasses as dc
from jax import tree_util as jt
def register_jax_dataclass(cls):
"""Registers a dataclass as a JAX pytree."""
if not dc.is_dataclass(cls):
raise TypeError('%s is not a dataclass.' % cls)
keys = [field.name for field in dc.fields(cls)]
@odashi
odashi / imake.zsh
Last active February 24, 2019 09:12
Performs a command when the content of a directory changed.
#!/bin/zsh
autoload colors; colors
if [ $# != 2 ]; then
echo "usage: $0 <root-dir> <command>"
exit 1
fi
ROOTDIR=$1
@odashi
odashi / cudnn_convolution_forward.cu
Created January 8, 2018 15:40
Example usage of cuDNN convolution forward functions.
#include <iomanip>
#include <iostream>
#include <cstdlib>
#include <vector>
#include <cuda.h>
#include <cudnn.h>
#define CUDA_CALL(f) { \
cudaError_t err = (f); \
@odashi
odashi / primitiv_xor.cc
Last active December 26, 2017 03:10
primitiv examples for Qiita (C++11/Python3)
// 実行方法:
// g++ -std=c++11 xor.cc -lprimitiv && ./a.out
#include <cstdio>
#include <iostream>
#include <primitiv/primitiv.h>
using namespace primitiv;
namespace D = primitiv::devices;
namespace F = primitiv::functions;
@odashi
odashi / gameoflife.cc
Last active June 6, 2017 21:27
Game of life on X11
#include <chrono>
#include <cstdlib>
#include <iostream>
#include <random>
#include <thread>
#include <X11/Xlib.h>
#include <X11/Xutil.h>
using namespace std;
@odashi
odashi / gameoflife.py
Last active June 1, 2017 15:53
The console game of life
#!/usr/bin/env python3
import curses
from random import random
from time import sleep
def main(scr):
curses.init_pair(1, curses.COLOR_BLACK, curses.COLOR_BLACK)
curses.init_pair(2, curses.COLOR_BLACK, curses.COLOR_BLACK)
curses.init_pair(3, curses.COLOR_WHITE, curses.COLOR_WHITE)
@odashi
odashi / apply_byte_pair_encoding.py
Last active August 15, 2017 12:23
Byte-pair encoding tools
#!/usr/bin/env python3
import sys
from argparse import ArgumentParser
from collections import defaultdict
def parse_args():
p = ArgumentParser('Converts word to integer using byte-pair encoding.')
p.add_argument(
'--input',
@odashi
odashi / generators.py
Created March 23, 2016 13:08
Frequently-used batch generators for my NLP study.
import builtins
import random
def word_list(filename):
with open(filename) as fp:
for l in fp:
yield l.split()
def batch(generator, size):
batch = []
@odashi
odashi / mert.py
Last active May 1, 2016 14:17
Minimum error-rate training for statistical machine translation
#!/usr/bin/python3
import math
import random
import sys
from argparse import ArgumentParser
from collections import defaultdict
from util.functions import trace
def parse_args():
@odashi
odashi / bleu.py
Last active September 20, 2019 06:46
BLEU calculator
# usage (single sentence):
# ref = ['This', 'is', 'a', 'pen', '.']
# hyp = ['There', 'is', 'a', 'pen', '.']
# stats = get_bleu_stats(ref, hyp)
# bleu = calculate_bleu(stats) # => 0.668740
#
# usage (multiple sentences):
# stats = defaultdict(int)
# for ref, hyp in zip(refs, hyps):
# for k, v in get_bleu_stats(ref, hyp).items():