Skip to content

Instantly share code, notes, and snippets.

View delta2323's full-sized avatar

Kenta Oono delta2323

View GitHub Profile
import chainer
import chainer.links as L
import chainer.functions as F
import numpy as np
class CNN(chainer.Chain):
def __init__(self):
super(CNN, self).__init__()
with self.init_scope():
@delta2323
delta2323 / chainer_double_backprop.py
Last active June 5, 2019 11:58
Double back propagation with Chainer (v3.0.0RC1), PyTorch (0.2.0_4), and TensorFlow (1.3.0-dev20170822)
import chainer
from chainer import Variable
import numpy as np
def f(x):
y = x * x * x / 3
gx, = chainer.grad([y], [x], enable_double_backprop=True)
z = gx - 2 * x + 1 # z = (x - 1) ** 2
return z
class ConvolutionalAutoEncoder(chainer.Chain):
def __init__(self):
super(ConvolutionalAutoEncoder).__init__(
c1=L.Convolution2D(...),
c2=L.Convolution2D(...),
dc1=L.Deconvolution2D(...),
dc2=L.Deconvolution2D(...),
)
def convolve(self, x):
@delta2323
delta2323 / dataset_in_chainer.md
Last active April 23, 2019 13:58
How to create your own dataset and use it in Chainer

The goal of this document is to explain the basic mechanism of the dataset in Chainer and how to prepare customized dataset that can work with Trainer.

See the official document for the full detail of the specifications.

Intarface of dataset

In order to make the dataset be able to work with the Trainer, it must have two methods:

  • __getitem__, which is used for indexing like dataset[i] or slicing like dataset[i:j]
  • __len__ , which enables us to feed it to len()
@delta2323
delta2323 / tied_weight_ae.py
Last active October 17, 2017 18:50
Chainer example of Tied-weight Autoencoder (Autoencoder with sharing weights)
import chainer
import chainer.functions as F
from chainer import initializers as I
from chainer import reporter
from chainer import training
from chainer.training import extensions as E
import numpy
import scipy.misc
@delta2323
delta2323 / main.py
Created January 15, 2017 23:40
Illegal memory access error in Chainer
from chainer import functions as F
import cupy
y = cupy.random.uniform(0, 1, (10, 5)).astype(cupy.float32)
t = cupy.empty((10,), dtype=cupy.int32)
p = F.softmax_cross_entropy(y, t)
p.data.get()
(pyenv-3.5.1) KANDEL /home/delta/dev/chainer-test/cupy% [16:11:58]
%(master)[]git --no-pager show -s --format=%H
353ea32b10738fb1490e39a0864e4613fcf918bf
(pyenv-3.5.1) KANDEL /home/delta/dev/chainer-test/cupy% [16:12:01]
%(master)[]pip --no-cache-dir install -e .
Obtaining file:///home/delta/dev/chainer-test/cupy
Requirement already satisfied (use --upgrade to upgrade): filelock in /home/delta/.pyenv/versions/pyenv-3.5.1/lib/python3.5/site-packages (from cupy==1.0.0)
Requirement already satisfied (use --upgrade to upgrade): nose in /home/delta/.pyenv/versions/pyenv-3.5.1/lib/python3.5/site-packages (from cupy==1.0.0)
Requirement already satisfied (use --upgrade to upgrade): numpy>=1.9.0 in /home/delta/.pyenv/versions/pyenv-3.5.1/lib/python3.5/site-packages (from cupy==1.0.0)
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
import chainer
from chainer import links as L
from chainer import utils
import warnings
import time
import numpy as np
import cupy as cp
warnings.simplefilter('ignore')
import chainer
import chainer.links as L
import chainer.functions as F
import numpy as np
class Wrapper(chainer.Chain):
pass