Created
May 16, 2017 08:42
-
-
Save musyoku/184137f91843ec7ce599135519b69568 to your computer and use it in GitHub Desktop.
Chainer v2 beta
This file contains hidden or 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
| # encoding: utf-8 | |
| import numpy as np | |
| import chainer | |
| import chainer.links as L | |
| import chainer.functions as F | |
| from chainer import cuda, optimizers | |
| from chainer.optimizer import GradientClipping, WeightDecay | |
| class Model(chainer.Chain): | |
| def __init__(self): | |
| super(Model, self).__init__( | |
| l1=L.Linear(None, 28 ** 2), | |
| l2=L.Linear(None, 100), | |
| l3=L.Linear(None, 10), | |
| bn1=L.BatchNormalization(28 ** 2), | |
| bn2=L.BatchNormalization(100), | |
| ) | |
| def __call__(self, x): | |
| out = F.relu(self.l1(x)) | |
| out = self.bn1(out) | |
| out = F.relu(self.l2(out)) | |
| out = self.bn2(out) | |
| out = self.l3(out) | |
| return out | |
| def setup_optimizer(self): | |
| opt = optimizers.Adam(alpha=0.01, beta1=0.9) | |
| opt.setup(self) | |
| opt.add_hook(GradientClipping(10.)) | |
| rule = self.l1.W.update_rule | |
| rule.alpha = 1e-1 | |
| rule.add_hook(WeightDecay(2e-3)) | |
| rule = self.l2.W.update_rule | |
| rule.alpha = 1e-2 | |
| rule.add_hook(WeightDecay(2e-4)) | |
| rule = self.l3.W.update_rule | |
| rule.alpha = 1e-3 | |
| rule.add_hook(WeightDecay(2e-5)) | |
| return opt | |
| def main(): | |
| xp = cuda.cupy | |
| xp = np | |
| xp.random.seed(0) | |
| in_size = 28 ** 2 | |
| out_size = 10 | |
| batchsize = 256 | |
| model = Model() | |
| optimizer = model.setup_optimizer() | |
| if xp is cuda.cupy: | |
| layer.to_gpu() | |
| data = xp.random.normal(size=(batchsize, in_size)).astype(xp.float32) | |
| target = xp.random.randint(0, out_size, size=(batchsize,)).astype(xp.int32) | |
| with chainer.using_config("train", True): | |
| for epoch in xrange(10): | |
| out = model(data) | |
| loss = F.softmax_cross_entropy(out, target) | |
| optimizer.update(lossfun=lambda: loss) | |
| print(loss.data) | |
| with chainer.using_config("train", False): | |
| out = model(data) | |
| acc = F.accuracy(out, target) | |
| print(acc.data) | |
| if __name__ == "__main__": | |
| main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment