Last active
May 6, 2017 06:44
-
-
Save musyoku/0875ed9e0a43de038d9b7357cd22f968 to your computer and use it in GitHub Desktop.
chainer.links.linearの精度
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 | |
| from __future__ import division | |
| from __future__ import print_function | |
| import numpy as np | |
| import chainer.links as L | |
| from chainer import cuda | |
| def test_linear(): | |
| np.set_printoptions(precision=32) | |
| xp = cuda.cupy | |
| xp = np | |
| xp.random.seed(0) | |
| in_size = 10 | |
| out_size = 4 | |
| batchsize = 10 | |
| layer = L.Linear(in_size, out_size) | |
| if xp is cuda.cupy: | |
| layer.to_gpu() | |
| data = xp.random.normal(size=(batchsize, in_size)).astype(xp.float32) | |
| num_to_take = 1 # xp = npの時にここを1にすると誤差が出る | |
| out1 = layer(data).data | |
| out2 = layer(data[:num_to_take]).data | |
| print("error:", xp.sum((out1[:num_to_take] - out2) ** 2)) | |
| if __name__ == "__main__": | |
| test_linear() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment