Skip to content

Instantly share code, notes, and snippets.

View koreyou's full-sized avatar

Yuta Koreeda koreyou

View GitHub Profile
@koreyou
koreyou / sequence_mask.py
Created August 26, 2017 09:48
[Chainer] Mask sequence with length
def sequence_mask(x, length, value=0.):
xp = cuda.get_array_module(length.data)
# create permutation on (length.ndim + 1) dimension and expand dims until it has shame rank as x
perms = xp.arange(x.shape[length.ndim]).reshape(
[1] * length.ndim + [-1] + [1] * (x.ndim - length.ndim -1))
length = length.reshape([1] * (length.ndim - 1) + [-1] + [1] * (x.ndim - length.ndim))
pad = xp.ones_like(x) * value
mask = xp.broadcast_to(perms, x.shape) < length
return F.where(mask, x, pad)
import chainer
import chainer.functions as F
import chainer.links as L
from chainer import training
from chainer.training import extensions
# Network definition
class MLP(chainer.Chain):
def __init__(self, n_out):
super(MLP, self).__init__()
@koreyou
koreyou / chainer_reporting_demo.ipynb
Created June 26, 2017 14:29
Demostration of how we can use chainer's reporing mechanism to monitor variables' metrics
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
###### Editor debris ########
*.org
*~
¥#*
# spyder
.spyderproject/
# pycharm
# Written by Yuta Koreeda
# CC-BY
import numpy as np
class ExtremeLearningMachine(object):
def __init__(self, n_unit, activation=None):
self._activation = self._sig if activation is None else activation
self._n_unit = n_unit