├── chainer
├── backends
│ └── __init__.py
├── links
│ ├── loss
│ │ ├── __init__.py
│ │ └── crf1d.py
│ ├── model
│ │ ├── __init__.py
│ │ └── vision
│ │ │ └── __init__.py
│ ├── theano
│ │ └── __init__.py
│ ├── activation
│ │ ├── __init__.py
│ │ └── prelu.py
│ ├── connection
│ │ ├── __init__.py
│ │ └── parameter.py
│ ├── caffe
│ │ ├── protobuf3
│ │ │ └── __init__.py
│ │ └── __init__.py
│ └── normalization
│ │ └── __init__.py
├── functions
│ ├── array
│ │ ├── __init__.py
│ │ ├── flipud.py
│ │ ├── fliplr.py
│ │ ├── flatten.py
│ │ ├── cast.py
│ │ ├── swapaxes.py
│ │ ├── flip.py
│ │ ├── copy.py
│ │ └── rollaxis.py
│ ├── loss
│ │ ├── __init__.py
│ │ ├── mean_absolute_error.py
│ │ ├── squared_error.py
│ │ ├── absolute_error.py
│ │ └── mean_squared_error.py
│ ├── math
│ │ ├── __init__.py
│ │ ├── identity.py
│ │ ├── ceil.py
│ │ ├── fix.py
│ │ ├── floor.py
│ │ ├── exponential_m1.py
│ │ ├── logarithm_1p.py
│ │ ├── sign.py
│ │ ├── square.py
│ │ ├── fmod.py
│ │ ├── squared_difference.py
│ │ ├── bias.py
│ │ ├── scale.py
│ │ ├── sqrt.py
│ │ ├── erf.py
│ │ ├── erfc.py
│ │ ├── cumsum.py
│ │ └── hyperbolic.py
│ ├── noise
│ │ ├── __init__.py
│ │ └── gumbel_softmax.py
│ ├── pooling
│ │ ├── __init__.py
│ │ └── average_pooling_nd_kernel.py
│ ├── theano
│ │ └── __init__.py
│ ├── util
│ │ └── __init__.py
│ ├── activation
│ │ ├── __init__.py
│ │ └── selu.py
│ ├── connection
│ │ └── __init__.py
│ ├── evaluation
│ │ └── __init__.py
│ └── normalization
│ │ └── __init__.py
├── _version.py
├── cuda.py
├── training
│ ├── updater.py
│ ├── trigger.py
│ ├── updaters
│ │ └── __init__.py
│ ├── triggers
│ │ └── __init__.py
│ ├── __init__.py
│ ├── extensions
│ │ ├── value_observation.py
│ │ └── __init__.py
│ └── util.py
├── iterators
│ └── __init__.py
├── function_hooks
│ ├── __init__.py
│ └── cuda_profile.py
├── serializers
│ └── __init__.py
├── utils
│ ├── argument.py
│ ├── imgproc.py
│ ├── array.py
│ └── __init__.py
├── dataset
│ └── __init__.py
├── optimizers
│ ├── __init__.py
│ └── sgd.py
├── datasets
│ ├── concatenated_dataset.py
│ ├── dict_dataset.py
│ ├── _mnist_helper.py
│ ├── __init__.py
│ └── transform_dataset.py
├── initializer.py
├── testing
│ ├── __init__.py
│ ├── helper.py
│ ├── attr.py
│ ├── serializer.py
│ └── array.py
└── _environment_check.py
├── examples
├── .gitignore
├── cifar
│ ├── models
│ │ └── __init__.py
│ └── README.md
├── ptb
│ ├── .gitignore
│ └── README.md
├── imagenet
│ ├── mean.npy
│ ├── README.md
│ ├── compute_mean.py
│ └── nin.py
├── dcgan
│ ├── example_image.png
│ ├── README.md
│ ├── visualize.py
│ └── updater.py
├── modelzoo
│ ├── .gitignore
│ ├── download_mean_file.py
│ ├── README.md
│ └── download_model.py
├── mnist
│ ├── .gitignore
│ └── README.md
├── sentiment
│ ├── README.md
│ └── download.py
├── reinforcement_learning
│ └── README.md
├── word2vec
│ ├── README.md
│ └── search.py
├── vae
│ └── README.md
├── pos
│ └── README.md
└── image_captioning
│ └── download.py
├── tests
└── chainer_tests
│ ├── __init__.py
│ ├── links_tests
│ ├── __init__.py
│ ├── caffe_tests
│ │ └── __init__.py
│ ├── loss_tests
│ │ └── __init__.py
│ ├── model_tests
│ │ └── __init__.py
│ ├── theano_tests
│ │ └── __init__.py
│ ├── activation_tests
│ │ └── __init__.py
│ ├── connection_tests
│ │ ├── __init__.py
│ │ └── test_inception.py
│ └── normalization_tests
│ │ └── __init__.py
│ ├── utils_tests
│ ├── __init__.py
│ ├── test_walker_alias.py
│ ├── test_conv_nd_kernel.py
│ └── test_utils.py
│ ├── dataset_tests
│ └── __init__.py
│ ├── datasets_tests
│ ├── __init__.py
│ ├── image_dataset
│ │ ├── __init__.py
│ │ ├── img.lst
│ │ ├── labeled_img.lst
│ │ ├── chainer.png
│ │ └── chainer_grey.png
│ ├── test_transform_dataset.py
│ ├── test_tuple_dataset.py
│ ├── test_dict_dataset.py
│ └── test_concatenated_dataset.py
│ ├── functions_tests
│ ├── __init__.py
│ ├── loss_tests
│ │ └── __init__.py
│ ├── math_tests
│ │ ├── __init__.py
│ │ ├── test_square.py
│ │ ├── test_erf.py
│ │ ├── test_erfc.py
│ │ ├── test_sqrt.py
│ │ ├── test_fix.py
│ │ └── test_ceil.py
│ ├── util_tests
│ │ └── __init__.py
│ ├── array_tests
│ │ ├── __init__.py
│ │ ├── test_transpose.py
│ │ ├── test_flatten.py
│ │ └── test_reshape.py
│ ├── noise_tests
│ │ ├── __init__.py
│ │ └── test_gumbel_softmax.py
│ ├── pooling_tests
│ │ ├── __init__.py
│ │ ├── pooling_nd_helper.py
│ │ └── test_pooling_nd_kernel.py
│ ├── activation_tests
│ │ └── __init__.py
│ ├── connection_tests
│ │ └── __init__.py
│ ├── evaluation_tests
│ │ └── __init__.py
│ └── normalization_tests
│ │ └── __init__.py
│ ├── iterators_tests
│ └── __init__.py
│ ├── optimizers_tests
│ └── __init__.py
│ ├── testing_tests
│ ├── __init__.py
│ ├── test_serializer.py
│ ├── test_unary_math_function_test.py
│ ├── test_training.py
│ └── test_parameterized.py
│ ├── training_tests
│ ├── __init__.py
│ ├── extensions_tests
│ │ ├── __init__.py
│ │ ├── test_snapshot.py
│ │ └── test_plot_report.py
│ ├── triggers_tests
│ │ ├── __init__.py
│ │ └── test_minmax_trigger.py
│ └── updaters_tests
│ │ └── __init__.py
│ ├── function_hooks_tests
│ └── __init__.py
│ ├── initializer_tests
│ ├── __init__.py
│ ├── test_init.py
│ └── test_uniform.py
│ ├── serializers_tests
│ └── __init__.py
│ ├── test_initializer.py
│ ├── test_function_hook.py
│ ├── test_runnable.py
│ ├── test_init.py
│ └── test_init_docstring.py
├── docs
├── image
│ ├── googlenet.png
│ ├── polynomial.png
│ ├── ptb
│ │ ├── rnnlm.png
│ │ └── rnnlm_example.png
│ ├── chainer_red_h.png
│ ├── train_loop
│ │ ├── 5.png
│ │ └── 7.png
│ ├── trainer
│ │ ├── trainer.png
│ │ ├── mnist_loss.png
│ │ ├── mnist_graph.png
│ │ ├── mnist_output.png
│ │ └── mnist_accuracy.png
│ └── word2vec
│ │ ├── skipgram_detail.png
│ │ └── center_context_word.png
└── source
│ ├── reference
│ ├── util
│ │ ├── algorithm.rst
│ │ ├── experimental.rst
│ │ ├── conv.rst
│ │ ├── reporter.rst
│ │ └── cuda.rst
│ ├── util.rst
│ ├── variable.rst
│ ├── index.rst
│ ├── triggers.rst
│ ├── iterators.rst
│ ├── caffe.rst
│ ├── optimizers.rst
│ ├── serializers.rst
│ ├── debug.rst
│ ├── graph.rst
│ ├── initializers.rst
│ └── check.rst
│ ├── examples
│ └── index.rst
│ ├── guides
│ ├── index.rst
│ ├── trainer.rst
│ └── define_by_run.rst
│ ├── imports.rst
│ ├── index.rst
│ ├── license.rst
│ ├── _templates
│ └── autosummary
│ │ └── class.rst
│ └── _autosummary_check.py
├── readthedocs.yml
├── .github
├── CONTRIBUTING.md
├── PULL_REQUEST_TEMPLATE.md
├── ISSUE_TEMPLATE.md
└── stale.yml
├── .coveragerc
├── codecov.yml
├── docker
├── python2
│ └── Dockerfile
└── python3
│ └── Dockerfile
├── .gitignore
├── chainer_bibtex.txt
├── setup.cfg
├── LICENSE
└── .travis.yml
/chainer/backends/__init__.py:
--------------------------------------------------------------------------------
1 |
--------------------------------------------------------------------------------
/chainer/links/loss/__init__.py:
--------------------------------------------------------------------------------
1 |
--------------------------------------------------------------------------------
/chainer/links/model/__init__.py:
--------------------------------------------------------------------------------
1 |
--------------------------------------------------------------------------------
/chainer/links/theano/__init__.py:
--------------------------------------------------------------------------------
1 |
--------------------------------------------------------------------------------
/examples/.gitignore:
--------------------------------------------------------------------------------
1 | result/
2 |
--------------------------------------------------------------------------------
/examples/cifar/models/__init__.py:
--------------------------------------------------------------------------------
1 |
--------------------------------------------------------------------------------
/tests/chainer_tests/__init__.py:
--------------------------------------------------------------------------------
1 |
--------------------------------------------------------------------------------
/chainer/functions/array/__init__.py:
--------------------------------------------------------------------------------
1 |
--------------------------------------------------------------------------------
/chainer/functions/loss/__init__.py:
--------------------------------------------------------------------------------
1 |
--------------------------------------------------------------------------------
/chainer/functions/math/__init__.py:
--------------------------------------------------------------------------------
1 |
--------------------------------------------------------------------------------
/chainer/functions/noise/__init__.py:
--------------------------------------------------------------------------------
1 |
--------------------------------------------------------------------------------
/chainer/functions/pooling/__init__.py:
--------------------------------------------------------------------------------
1 |
--------------------------------------------------------------------------------
/chainer/functions/theano/__init__.py:
--------------------------------------------------------------------------------
1 |
--------------------------------------------------------------------------------
/chainer/functions/util/__init__.py:
--------------------------------------------------------------------------------
1 |
--------------------------------------------------------------------------------
/chainer/links/activation/__init__.py:
--------------------------------------------------------------------------------
1 |
--------------------------------------------------------------------------------
/chainer/links/connection/__init__.py:
--------------------------------------------------------------------------------
1 |
--------------------------------------------------------------------------------
/chainer/links/model/vision/__init__.py:
--------------------------------------------------------------------------------
1 |
--------------------------------------------------------------------------------
/chainer/functions/activation/__init__.py:
--------------------------------------------------------------------------------
1 |
--------------------------------------------------------------------------------
/chainer/functions/connection/__init__.py:
--------------------------------------------------------------------------------
1 |
--------------------------------------------------------------------------------
/chainer/functions/evaluation/__init__.py:
--------------------------------------------------------------------------------
1 |
--------------------------------------------------------------------------------
/chainer/functions/normalization/__init__.py:
--------------------------------------------------------------------------------
1 |
--------------------------------------------------------------------------------
/chainer/links/caffe/protobuf3/__init__.py:
--------------------------------------------------------------------------------
1 |
--------------------------------------------------------------------------------
/chainer/links/normalization/__init__.py:
--------------------------------------------------------------------------------
1 |
--------------------------------------------------------------------------------
/tests/chainer_tests/links_tests/__init__.py:
--------------------------------------------------------------------------------
1 |
--------------------------------------------------------------------------------
/tests/chainer_tests/utils_tests/__init__.py:
--------------------------------------------------------------------------------
1 |
--------------------------------------------------------------------------------
/chainer/_version.py:
--------------------------------------------------------------------------------
1 | __version__ = '4.0.0b4'
2 |
--------------------------------------------------------------------------------
/tests/chainer_tests/dataset_tests/__init__.py:
--------------------------------------------------------------------------------
1 |
--------------------------------------------------------------------------------
/tests/chainer_tests/datasets_tests/__init__.py:
--------------------------------------------------------------------------------
1 |
--------------------------------------------------------------------------------
/tests/chainer_tests/functions_tests/__init__.py:
--------------------------------------------------------------------------------
1 |
--------------------------------------------------------------------------------
/tests/chainer_tests/iterators_tests/__init__.py:
--------------------------------------------------------------------------------
1 |
--------------------------------------------------------------------------------
/tests/chainer_tests/optimizers_tests/__init__.py:
--------------------------------------------------------------------------------
1 |
--------------------------------------------------------------------------------
/tests/chainer_tests/testing_tests/__init__.py:
--------------------------------------------------------------------------------
1 |
--------------------------------------------------------------------------------
/tests/chainer_tests/training_tests/__init__.py:
--------------------------------------------------------------------------------
1 |
--------------------------------------------------------------------------------
/tests/chainer_tests/function_hooks_tests/__init__.py:
--------------------------------------------------------------------------------
1 |
--------------------------------------------------------------------------------
/tests/chainer_tests/initializer_tests/__init__.py:
--------------------------------------------------------------------------------
1 |
--------------------------------------------------------------------------------
/tests/chainer_tests/serializers_tests/__init__.py:
--------------------------------------------------------------------------------
1 |
--------------------------------------------------------------------------------
/tests/chainer_tests/functions_tests/loss_tests/__init__.py:
--------------------------------------------------------------------------------
1 |
--------------------------------------------------------------------------------
/tests/chainer_tests/functions_tests/math_tests/__init__.py:
--------------------------------------------------------------------------------
1 |
--------------------------------------------------------------------------------
/tests/chainer_tests/functions_tests/util_tests/__init__.py:
--------------------------------------------------------------------------------
1 |
--------------------------------------------------------------------------------
/tests/chainer_tests/links_tests/caffe_tests/__init__.py:
--------------------------------------------------------------------------------
1 |
--------------------------------------------------------------------------------
/tests/chainer_tests/links_tests/loss_tests/__init__.py:
--------------------------------------------------------------------------------
1 |
--------------------------------------------------------------------------------
/tests/chainer_tests/links_tests/model_tests/__init__.py:
--------------------------------------------------------------------------------
1 |
--------------------------------------------------------------------------------
/tests/chainer_tests/links_tests/theano_tests/__init__.py:
--------------------------------------------------------------------------------
1 |
--------------------------------------------------------------------------------
/tests/chainer_tests/datasets_tests/image_dataset/__init__.py:
--------------------------------------------------------------------------------
1 |
--------------------------------------------------------------------------------
/tests/chainer_tests/functions_tests/array_tests/__init__.py:
--------------------------------------------------------------------------------
1 |
--------------------------------------------------------------------------------
/tests/chainer_tests/functions_tests/noise_tests/__init__.py:
--------------------------------------------------------------------------------
1 |
--------------------------------------------------------------------------------
/tests/chainer_tests/functions_tests/pooling_tests/__init__.py:
--------------------------------------------------------------------------------
1 |
--------------------------------------------------------------------------------
/tests/chainer_tests/links_tests/activation_tests/__init__.py:
--------------------------------------------------------------------------------
1 |
--------------------------------------------------------------------------------
/tests/chainer_tests/links_tests/connection_tests/__init__.py:
--------------------------------------------------------------------------------
1 |
--------------------------------------------------------------------------------
/tests/chainer_tests/links_tests/normalization_tests/__init__.py:
--------------------------------------------------------------------------------
1 |
--------------------------------------------------------------------------------
/tests/chainer_tests/training_tests/extensions_tests/__init__.py:
--------------------------------------------------------------------------------
1 |
--------------------------------------------------------------------------------
/tests/chainer_tests/training_tests/triggers_tests/__init__.py:
--------------------------------------------------------------------------------
1 |
--------------------------------------------------------------------------------
/tests/chainer_tests/training_tests/updaters_tests/__init__.py:
--------------------------------------------------------------------------------
1 |
--------------------------------------------------------------------------------
/tests/chainer_tests/functions_tests/activation_tests/__init__.py:
--------------------------------------------------------------------------------
1 |
--------------------------------------------------------------------------------
/tests/chainer_tests/functions_tests/connection_tests/__init__.py:
--------------------------------------------------------------------------------
1 |
--------------------------------------------------------------------------------
/tests/chainer_tests/functions_tests/evaluation_tests/__init__.py:
--------------------------------------------------------------------------------
1 |
--------------------------------------------------------------------------------
/tests/chainer_tests/functions_tests/normalization_tests/__init__.py:
--------------------------------------------------------------------------------
1 |
--------------------------------------------------------------------------------
/examples/ptb/.gitignore:
--------------------------------------------------------------------------------
1 | ptb.test.txt
2 | ptb.train.txt
3 | ptb.valid.txt
4 | vocab.bin
5 |
--------------------------------------------------------------------------------
/tests/chainer_tests/datasets_tests/image_dataset/img.lst:
--------------------------------------------------------------------------------
1 | chainer.png
2 | chainer_grey.png
3 |
--------------------------------------------------------------------------------
/docs/image/googlenet.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/D-K-E/chainer/master/docs/image/googlenet.png
--------------------------------------------------------------------------------
/docs/image/polynomial.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/D-K-E/chainer/master/docs/image/polynomial.png
--------------------------------------------------------------------------------
/docs/image/ptb/rnnlm.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/D-K-E/chainer/master/docs/image/ptb/rnnlm.png
--------------------------------------------------------------------------------
/examples/imagenet/mean.npy:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/D-K-E/chainer/master/examples/imagenet/mean.npy
--------------------------------------------------------------------------------
/docs/image/chainer_red_h.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/D-K-E/chainer/master/docs/image/chainer_red_h.png
--------------------------------------------------------------------------------
/docs/image/train_loop/5.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/D-K-E/chainer/master/docs/image/train_loop/5.png
--------------------------------------------------------------------------------
/docs/image/train_loop/7.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/D-K-E/chainer/master/docs/image/train_loop/7.png
--------------------------------------------------------------------------------
/tests/chainer_tests/datasets_tests/image_dataset/labeled_img.lst:
--------------------------------------------------------------------------------
1 | chainer.png 0
2 | chainer_grey.png 1
3 |
--------------------------------------------------------------------------------
/docs/image/trainer/trainer.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/D-K-E/chainer/master/docs/image/trainer/trainer.png
--------------------------------------------------------------------------------
/readthedocs.yml:
--------------------------------------------------------------------------------
1 | name: chainer
2 | type: sphinx
3 | base: docs/source
4 | python:
5 | setup_py_install: true
6 |
--------------------------------------------------------------------------------
/chainer/cuda.py:
--------------------------------------------------------------------------------
1 | import sys
2 |
3 | from chainer.backends import cuda
4 |
5 |
6 | sys.modules[__name__] = cuda
7 |
--------------------------------------------------------------------------------
/docs/image/ptb/rnnlm_example.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/D-K-E/chainer/master/docs/image/ptb/rnnlm_example.png
--------------------------------------------------------------------------------
/docs/image/trainer/mnist_loss.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/D-K-E/chainer/master/docs/image/trainer/mnist_loss.png
--------------------------------------------------------------------------------
/examples/dcgan/example_image.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/D-K-E/chainer/master/examples/dcgan/example_image.png
--------------------------------------------------------------------------------
/.github/CONTRIBUTING.md:
--------------------------------------------------------------------------------
1 | Please refer to [our Contribution Guide](https://docs.chainer.org/en/stable/contribution.html).
2 |
--------------------------------------------------------------------------------
/docs/image/trainer/mnist_graph.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/D-K-E/chainer/master/docs/image/trainer/mnist_graph.png
--------------------------------------------------------------------------------
/docs/image/trainer/mnist_output.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/D-K-E/chainer/master/docs/image/trainer/mnist_output.png
--------------------------------------------------------------------------------
/docs/image/trainer/mnist_accuracy.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/D-K-E/chainer/master/docs/image/trainer/mnist_accuracy.png
--------------------------------------------------------------------------------
/docs/image/word2vec/skipgram_detail.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/D-K-E/chainer/master/docs/image/word2vec/skipgram_detail.png
--------------------------------------------------------------------------------
/docs/image/word2vec/center_context_word.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/D-K-E/chainer/master/docs/image/word2vec/center_context_word.png
--------------------------------------------------------------------------------
/chainer/links/caffe/__init__.py:
--------------------------------------------------------------------------------
1 | from chainer.links.caffe import caffe_function
2 |
3 |
4 | CaffeFunction = caffe_function.CaffeFunction
5 |
--------------------------------------------------------------------------------
/examples/modelzoo/.gitignore:
--------------------------------------------------------------------------------
1 | bvlc_alexnet.caffemodel
2 | bvlc_googlenet.caffemodel
3 | bvlc_reference_caffenet.caffemodel
4 | ilsvrc_2012_mean.npy
5 |
--------------------------------------------------------------------------------
/tests/chainer_tests/datasets_tests/image_dataset/chainer.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/D-K-E/chainer/master/tests/chainer_tests/datasets_tests/image_dataset/chainer.png
--------------------------------------------------------------------------------
/tests/chainer_tests/datasets_tests/image_dataset/chainer_grey.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/D-K-E/chainer/master/tests/chainer_tests/datasets_tests/image_dataset/chainer_grey.png
--------------------------------------------------------------------------------
/docs/source/reference/util/algorithm.rst:
--------------------------------------------------------------------------------
1 | Common algorithms
2 | -----------------
3 |
4 | .. autosummary::
5 | :toctree: generated/
6 | :nosignatures:
7 |
8 | chainer.utils.WalkerAlias
9 |
--------------------------------------------------------------------------------
/docs/source/reference/util.rst:
--------------------------------------------------------------------------------
1 | Utilities
2 | =========
3 |
4 | .. toctree::
5 | :maxdepth: 2
6 |
7 | util/conv
8 | util/cuda
9 | util/algorithm
10 | util/reporter
11 | util/experimental
12 |
--------------------------------------------------------------------------------
/docs/source/reference/util/experimental.rst:
--------------------------------------------------------------------------------
1 | Experimental feature annotation
2 | -------------------------------
3 | .. autosummary::
4 | :toctree: generated/
5 | :nosignatures:
6 |
7 | chainer.utils.experimental
8 |
--------------------------------------------------------------------------------
/examples/mnist/.gitignore:
--------------------------------------------------------------------------------
1 | graph.dot
2 | graph.wo_split.dot
3 | mlp.model
4 | mlp.state
5 | mnist.pkl
6 | t10k-images-idx3-ubyte.gz
7 | t10k-labels-idx1-ubyte.gz
8 | train-images-idx3-ubyte.gz
9 | train-labels-idx1-ubyte.gz
10 |
--------------------------------------------------------------------------------
/examples/sentiment/README.md:
--------------------------------------------------------------------------------
1 | # Recursive Nets for Sentiment Analysis
2 |
3 | This example implements the simple recursive model by Richard Socher.
4 | It requires the preprocessed dataset which is available by running `download.py`.
5 |
--------------------------------------------------------------------------------
/.coveragerc:
--------------------------------------------------------------------------------
1 | [run]
2 | include = chainer/*, examples/*
3 |
4 | # Exclude auto-generated source files.
5 | omit =
6 | chainer/links/caffe/protobuf3/caffe_pb2.py
7 |
8 | [report]
9 | exclude_lines =
10 | if __name__ == .__main__.:
11 |
--------------------------------------------------------------------------------
/docs/source/examples/index.rst:
--------------------------------------------------------------------------------
1 | ===================
2 | Neural Net Examples
3 | ===================
4 |
5 | .. toctree::
6 | :maxdepth: 1
7 |
8 | mnist
9 | train_loop
10 | cnn
11 | rnn
12 | ptb
13 | word2vec
14 |
15 |
--------------------------------------------------------------------------------
/docs/source/reference/util/conv.rst:
--------------------------------------------------------------------------------
1 | Convolution/Deconvolution utilities
2 | -----------------------------------
3 |
4 | .. autosummary::
5 | :toctree: generated/
6 | :nosignatures:
7 |
8 | chainer.utils.get_conv_outsize
9 | chainer.utils.get_deconv_outsize
10 |
--------------------------------------------------------------------------------
/chainer/training/updater.py:
--------------------------------------------------------------------------------
1 | from chainer.training._updater import Updater # NOQA
2 |
3 | # For backward compatibility
4 | from chainer.training.updaters.parallel_updater import ParallelUpdater # NOQA
5 | from chainer.training.updaters.standard_updater import StandardUpdater # NOQA
6 |
--------------------------------------------------------------------------------
/docs/source/guides/index.rst:
--------------------------------------------------------------------------------
1 | Guides
2 | ======
3 |
4 | .. toctree::
5 | :maxdepth: 1
6 |
7 | define_by_run
8 | variables
9 | links
10 | functions
11 | models
12 | optimizers
13 | trainer
14 | extensions
15 | gpu
16 | type_checks
17 | serializers
18 |
--------------------------------------------------------------------------------
/docs/source/reference/variable.rst:
--------------------------------------------------------------------------------
1 | Variable and Parameter
2 | ----------------------
3 |
4 | .. autosummary::
5 | :toctree: generated/
6 | :nosignatures:
7 |
8 | chainer.Variable
9 | chainer.as_variable
10 | chainer.Parameter
11 | chainer.variable.VariableNode
12 |
--------------------------------------------------------------------------------
/chainer/training/trigger.py:
--------------------------------------------------------------------------------
1 | from chainer.training.triggers import interval_trigger
2 | from chainer.training import util
3 |
4 |
5 | # For backward compatibility
6 | IntervalTrigger = interval_trigger.IntervalTrigger
7 | get_trigger = util.get_trigger
8 | _never_fire_trigger = util._never_fire_trigger
9 |
--------------------------------------------------------------------------------
/tests/chainer_tests/functions_tests/math_tests/test_square.py:
--------------------------------------------------------------------------------
1 | import unittest
2 |
3 | import chainer.functions as F
4 | from chainer import testing
5 |
6 |
7 | @testing.unary_math_function_unittest(F.square)
8 | class TestSquare(unittest.TestCase):
9 | pass
10 |
11 |
12 | testing.run_module(__name__, __file__)
13 |
--------------------------------------------------------------------------------
/codecov.yml:
--------------------------------------------------------------------------------
1 | comment: false
2 |
3 | coverage:
4 | status:
5 | # Disable coverage measurement for overall codebase.
6 | project: off
7 | # Enable coverage measurement for diff introduced in the pull-request,
8 | # but do not mark "X" on commit status for now.
9 | patch:
10 | default:
11 | target: '0%'
12 |
--------------------------------------------------------------------------------
/docker/python2/Dockerfile:
--------------------------------------------------------------------------------
1 | FROM nvidia/cuda:8.0-cudnn6-devel
2 |
3 | RUN apt-get update -y && \
4 | apt-get install -y --no-install-recommends \
5 | python-dev \
6 | python-pip \
7 | python-wheel \
8 | python-setuptools && \
9 | rm -rf /var/lib/apt/lists/* /var/cache/apt/archives/*
10 |
11 | RUN pip install cupy-cuda80==4.0.0b4 chainer==4.0.0b4
12 |
--------------------------------------------------------------------------------
/docker/python3/Dockerfile:
--------------------------------------------------------------------------------
1 | FROM nvidia/cuda:8.0-cudnn6-devel
2 |
3 | RUN apt-get update -y && \
4 | apt-get install -y --no-install-recommends \
5 | python3-dev \
6 | python3-pip \
7 | python3-wheel \
8 | python3-setuptools && \
9 | rm -rf /var/lib/apt/lists/* /var/cache/apt/archives/*
10 |
11 | RUN pip3 install cupy-cuda80==4.0.0b4 chainer==4.0.0b4
12 |
--------------------------------------------------------------------------------
/examples/modelzoo/download_mean_file.py:
--------------------------------------------------------------------------------
1 | #!/usr/bin/env python
2 | from __future__ import print_function
3 |
4 | import six
5 |
6 |
7 | print('Downloading ILSVRC12 mean file for NumPy...')
8 | six.moves.urllib.request.urlretrieve(
9 | 'https://github.com/BVLC/caffe/raw/master/python/caffe/imagenet/'
10 | 'ilsvrc_2012_mean.npy',
11 | 'ilsvrc_2012_mean.npy')
12 | print('Done')
13 |
--------------------------------------------------------------------------------
/examples/reinforcement_learning/README.md:
--------------------------------------------------------------------------------
1 | # Reinforcement Learning Examples
2 |
3 | These are minimal examples of writing reinforcement learning agents. You need to install [gym](https://github.com/openai/gym) before running the examples.
4 |
5 | - `dqn_cartpole.py` implements DQN and DoubleDQN and supports discrete-action tasks.
6 | - `ddpg_pendulum.py` implements DDPG and supports continuous-action tasks.
7 |
--------------------------------------------------------------------------------
/docs/source/reference/index.rst:
--------------------------------------------------------------------------------
1 | *********
2 | Reference
3 | *********
4 |
5 | .. module:: chainer
6 |
7 | .. toctree::
8 | :maxdepth: 2
9 |
10 | variable
11 | configuration
12 | training
13 | debug
14 | util
15 | check
16 | functions
17 | links
18 | optimizers
19 | serializers
20 | initializers
21 | datasets
22 | iterators
23 | triggers
24 | caffe
25 | graph
26 |
--------------------------------------------------------------------------------
/examples/ptb/README.md:
--------------------------------------------------------------------------------
1 | # Recurrent Net Language Model
2 |
3 | This is an example of a recurrent net for language modeling.
4 | The network is trained to predict the word given the preceding word sequence.
5 |
6 | This example is based on the following RNNLM implementation written in Torch7.
7 | https://github.com/tomsercu/lstm
8 |
9 | If you want to run this example on the N-th GPU, pass `--gpu=N` to the script.
10 |
--------------------------------------------------------------------------------
/examples/mnist/README.md:
--------------------------------------------------------------------------------
1 | # Multi-Layer Perceptron for MNIST Classification
2 |
3 | This is a minimal example to write a feed-forward net.
4 | The code consists of three parts: dataset preparation, network and optimizer definition and learning loop.
5 | This is a common routine to write a learning process of networks with dataset that is small enough to fit into memory.
6 |
7 | If you want to run this example on the N-th GPU, pass `--gpu=N` to the script.
8 |
--------------------------------------------------------------------------------
/.github/PULL_REQUEST_TEMPLATE.md:
--------------------------------------------------------------------------------
1 | Thank you for creating a pull request!
2 |
3 | Please double-check the following.
4 |
5 | - Read [our contribution guide](https://docs.chainer.org/en/stable/contribution.html).
6 | - Does you code conform to our coding guidelines?
7 | - Did you write sufficient test code?
8 | - Did you write sufficient documentation?
9 | - Also take a look at [our compatibility policy](https://docs.chainer.org/en/stable/compatibility.html).
10 |
--------------------------------------------------------------------------------
/chainer/functions/math/identity.py:
--------------------------------------------------------------------------------
1 | from chainer import function_node
2 |
3 |
4 | class Identity(function_node.FunctionNode):
5 |
6 | """Identity function."""
7 |
8 | def forward(self, xs):
9 | return xs
10 |
11 | def backward(self, indexes, gys):
12 | return gys
13 |
14 |
15 | def identity(*inputs):
16 | """Just returns input variables."""
17 | ret = Identity().apply(inputs)
18 | return ret[0] if len(ret) == 1 else ret
19 |
--------------------------------------------------------------------------------
/docs/source/reference/triggers.rst:
--------------------------------------------------------------------------------
1 | Trainer triggers
2 | ================
3 |
4 | .. autosummary::
5 | :toctree: generated/
6 | :nosignatures:
7 |
8 | chainer.training.triggers.BestValueTrigger
9 | chainer.training.triggers.EarlyStoppingTrigger
10 | chainer.training.triggers.IntervalTrigger
11 | chainer.training.triggers.ManualScheduleTrigger
12 | chainer.training.triggers.MaxValueTrigger
13 | chainer.training.triggers.MinValueTrigger
14 |
15 |
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | *~
2 | *.egg/
3 | *.pyc
4 | *.pyo
5 | *.cpp
6 | *.so
7 | build
8 | \#*\#
9 | .\#*
10 | .coverage
11 | .eggs/
12 | _readthedocs_build
13 | /TAGS
14 | /docs/source/reference/**/generated
15 | /tags
16 | chainer.egg-info/
17 | dist/
18 | htmlcov/
19 | .idea/
20 | .cache/
21 | docs/my.model
22 | docs/my.state
23 | docs/my_mnist.model
24 | docs/mnist_result
25 | docs/*.png
26 | docs/source/reference/core
27 | docs/source/reference/generated
28 | docs/source/reference/util
29 |
--------------------------------------------------------------------------------
/chainer/iterators/__init__.py:
--------------------------------------------------------------------------------
1 | from chainer.iterators import multiprocess_iterator # NOQA
2 | from chainer.iterators import multithread_iterator # NOQA
3 | from chainer.iterators import serial_iterator # NOQA
4 |
5 |
6 | # import class and function
7 | from chainer.iterators.multiprocess_iterator import MultiprocessIterator # NOQA
8 | from chainer.iterators.multithread_iterator import MultithreadIterator # NOQA
9 | from chainer.iterators.serial_iterator import SerialIterator # NOQA
10 |
--------------------------------------------------------------------------------
/examples/sentiment/download.py:
--------------------------------------------------------------------------------
1 | #!/usr/bin/env python
2 | import os
3 | import os.path
4 | from six.moves.urllib import request
5 | import zipfile
6 |
7 |
8 | request.urlretrieve(
9 | 'https://nlp.stanford.edu/sentiment/trainDevTestTrees_PTB.zip',
10 | 'trainDevTestTrees_PTB.zip')
11 | zf = zipfile.ZipFile('trainDevTestTrees_PTB.zip')
12 | for name in zf.namelist():
13 | (dirname, filename) = os.path.split(name)
14 | if not filename == '':
15 | zf.extract(name, '.')
16 |
--------------------------------------------------------------------------------
/examples/word2vec/README.md:
--------------------------------------------------------------------------------
1 | # Word Embedding
2 |
3 | This is an example of word embedding.
4 | We implemented Mikolov's Skip-gram model and Continuous-BoW model with Hierarchical softmax and Negative sampling.
5 |
6 | Run `train_word2vec.py` to train and get `word2vec.model` which includes embedding data.
7 | You can find top-5 nearest embedding vectors using `search.py`.
8 |
9 | This example is based on the following word embedding implementation in C++.
10 | https://code.google.com/p/word2vec/
11 |
--------------------------------------------------------------------------------
/examples/vae/README.md:
--------------------------------------------------------------------------------
1 | # Variational AutoEncoder
2 |
3 | This is a sample implementation of variational autoencoder.
4 |
5 | This method is proposed by Kingma and Welling, Auto-Encoding Variational Bayes, 2014.
6 |
7 | If you want to run this example on the N-th GPU, pass `--gpu=N` to the script.
8 |
9 | If you run this script on Linux, setting the environmental variable `MPLBACKEND` to `Agg` may be required to use `matplotlib`. For example,
10 |
11 | ```
12 | MPLBACKEND=Agg python train_vae.py
13 | ```
14 |
--------------------------------------------------------------------------------
/chainer/training/updaters/__init__.py:
--------------------------------------------------------------------------------
1 | from chainer.training.updaters import multiprocess_parallel_updater # NOQA
2 | from chainer.training.updaters import parallel_updater # NOQA
3 | from chainer.training.updaters import standard_updater # NOQA
4 |
5 | from chainer.training.updaters.multiprocess_parallel_updater import MultiprocessParallelUpdater # NOQA
6 | from chainer.training.updaters.parallel_updater import ParallelUpdater # NOQA
7 | from chainer.training.updaters.standard_updater import StandardUpdater # NOQA
8 |
--------------------------------------------------------------------------------
/chainer_bibtex.txt:
--------------------------------------------------------------------------------
1 | @inproceedings{chainer_learningsys2015,
2 | author = "Tokui, Seiya and Oono, Kenta and Hido, Shohei and Clayton, Justin",
3 | title = "Chainer: a Next-Generation Open Source Framework for Deep Learning",
4 | booktitle = "Proceedings of Workshop on Machine Learning Systems (LearningSys) in The Twenty-ninth Annual Conference on Neural Information Processing Systems (NIPS)",
5 | year = "2015",
6 | url = "http://learningsys.org/papers/LearningSys_2015_paper_33.pdf"
7 | }
8 |
--------------------------------------------------------------------------------
/docs/source/reference/util/reporter.rst:
--------------------------------------------------------------------------------
1 | .. _reporter:
2 |
3 | Reporter
4 | --------
5 |
6 | .. currentmodule:: chainer
7 |
8 | Reporter
9 | ~~~~~~~~
10 |
11 | .. autosummary::
12 | :toctree: generated/
13 | :nosignatures:
14 |
15 | chainer.Reporter
16 | chainer.get_current_reporter
17 | chainer.report
18 | chainer.report_scope
19 |
20 | Summary and DictSummary
21 | ~~~~~~~~~~~~~~~~~~~~~~~
22 |
23 | .. autosummary::
24 | :toctree: generated/
25 | :nosignatures:
26 |
27 | chainer.Summary
28 | chainer.DictSummary
29 |
--------------------------------------------------------------------------------
/docs/source/imports.rst:
--------------------------------------------------------------------------------
1 |
2 | In the example code of this tutorial, we assume for simplicity that the following symbols are already imported.
3 |
4 | .. testcode::
5 |
6 | import numpy as np
7 | import chainer
8 | from chainer import cuda, Function, gradient_check, report, training, utils, Variable
9 | from chainer import datasets, iterators, optimizers, serializers
10 | from chainer import Link, Chain, ChainList
11 | import chainer.functions as F
12 | import chainer.links as L
13 | from chainer.training import extensions
14 |
15 |
--------------------------------------------------------------------------------
/chainer/functions/math/ceil.py:
--------------------------------------------------------------------------------
1 | import chainer
2 | from chainer.backends import cuda
3 | from chainer import utils
4 |
5 |
6 | def ceil(x):
7 | """Elementwise ceil function.
8 |
9 | .. math::
10 | y_i = \\lceil x_i \\rceil
11 |
12 | Args:
13 | x (~chainer.Variable): Input variable.
14 |
15 | Returns:
16 | ~chainer.Variable: Output variable.
17 | """
18 | if isinstance(x, chainer.variable.Variable):
19 | x = x.data
20 | xp = cuda.get_array_module(x)
21 | return chainer.as_variable(utils.force_array(xp.ceil(x), x.dtype))
22 |
--------------------------------------------------------------------------------
/chainer/functions/math/fix.py:
--------------------------------------------------------------------------------
1 | import chainer
2 | from chainer.backends import cuda
3 | from chainer import utils
4 |
5 |
6 | def fix(x):
7 | """Elementwise fix function.
8 |
9 | .. math::
10 | y_i = \\lfix x_i \\rfix
11 |
12 | Args:
13 | x (~chainer.Variable): Input variable.
14 |
15 | Returns:
16 | ~chainer.Variable: Output variable.
17 | """
18 |
19 | if isinstance(x, chainer.variable.Variable):
20 | x = x.data
21 | xp = cuda.get_array_module(x)
22 | return chainer.as_variable(utils.force_array(xp.fix(x), x.dtype))
23 |
--------------------------------------------------------------------------------
/chainer/functions/math/floor.py:
--------------------------------------------------------------------------------
1 | import chainer
2 | from chainer.backends import cuda
3 | from chainer import utils
4 |
5 |
6 | def floor(x):
7 | """Elementwise floor function.
8 |
9 | .. math::
10 | y_i = \\lfloor x_i \\rfloor
11 |
12 | Args:
13 | x (~chainer.Variable): Input variable.
14 |
15 | Returns:
16 | ~chainer.Variable: Output variable.
17 | """
18 | if isinstance(x, chainer.variable.Variable):
19 | x = x.data
20 | xp = cuda.get_array_module(x)
21 | return chainer.as_variable(utils.force_array(xp.floor(x), x.dtype))
22 |
--------------------------------------------------------------------------------
/setup.cfg:
--------------------------------------------------------------------------------
1 | [flake8]
2 | exclude = .eggs,*.egg,build,caffe_pb2.py,caffe_pb3.py,docs,.git
3 |
4 | [pep8]
5 | exclude = .eggs,*.egg,build,caffe_pb2.py,caffe_pb3.py,docs,.git
6 |
7 | [tool:pytest]
8 | filterwarnings= ignore::FutureWarning
9 | error::DeprecationWarning
10 | # theano 0.8 causes DeprecationWarnings. It is fixed in 0.9
11 | ignore::DeprecationWarning:theano.configparser
12 | testpaths = tests docs
13 | python_files = test_*.py
14 | python_classes = Test
15 | python_functions = test
16 | minversion = 2.9
17 | addopts = --doctest-modules
18 |
--------------------------------------------------------------------------------
/chainer/function_hooks/__init__.py:
--------------------------------------------------------------------------------
1 | from chainer.function_hooks import cuda_profile # NOQA
2 | from chainer.function_hooks import cupy_memory_profile # NOQA
3 | from chainer.function_hooks import debug_print # NOQA
4 | from chainer.function_hooks import timer # NOQA
5 |
6 |
7 | # import class and function
8 | from chainer.function_hooks.cuda_profile import CUDAProfileHook # NOQA
9 | from chainer.function_hooks.cupy_memory_profile import CupyMemoryProfileHook # NOQA
10 | from chainer.function_hooks.debug_print import PrintHook # NOQA
11 | from chainer.function_hooks.timer import TimerHook # NOQA
12 |
--------------------------------------------------------------------------------
/chainer/serializers/__init__.py:
--------------------------------------------------------------------------------
1 | from chainer.serializers import hdf5 # NOQA
2 | from chainer.serializers import npz # NOQA
3 |
4 |
5 | from chainer.serializers.hdf5 import HDF5Deserializer # NOQA
6 | from chainer.serializers.hdf5 import HDF5Serializer # NOQA
7 | from chainer.serializers.hdf5 import load_hdf5 # NOQA
8 | from chainer.serializers.hdf5 import save_hdf5 # NOQA
9 | from chainer.serializers.npz import DictionarySerializer # NOQA
10 | from chainer.serializers.npz import load_npz # NOQA
11 | from chainer.serializers.npz import NpzDeserializer # NOQA
12 | from chainer.serializers.npz import save_npz # NOQA
13 |
--------------------------------------------------------------------------------
/examples/modelzoo/README.md:
--------------------------------------------------------------------------------
1 | # Evaluate a Caffe reference model
2 |
3 | ## Requirements
4 |
5 | - Caffe model support (Python 2.7+, Protocol Buffers)
6 | - Pillow (Pillow requires an external library that corresponds to the image format)
7 |
8 | ## Description
9 |
10 | This is an example of evaluating a Caffe reference model using ILSVRC2012 classification dataset.
11 | It requires the validation dataset in the same format as that for the imagenet example.
12 |
13 | Model files can be downloaded by `download_model.py`. AlexNet and reference CaffeNet requires a mean file, which can be downloaded by `download_mean_file.py`.
14 |
--------------------------------------------------------------------------------
/examples/pos/README.md:
--------------------------------------------------------------------------------
1 | # POS-tagging
2 |
3 | This is an example of a POS-tagging problem for natural language processing.
4 | Part of speech (POS) is a category of word, such as noun and verb.
5 | POS-tagging problem is the task of assigning part of speech to each word in a sentence.
6 | This problem can be formulated as a sequential labeling problem.
7 | So, you can apply this example to another problems such as named entity recognition.
8 |
9 | This example uses traditional linear-chain CRF model.
10 | And of course you can extends this model with RNN, LSTM, Bi-LSTM and CNN.
11 |
12 | You need to install nltk (https://www.nltk.org/) to execute the example.
13 |
--------------------------------------------------------------------------------
/examples/dcgan/README.md:
--------------------------------------------------------------------------------
1 | # DCGAN
2 |
3 | This is an example implementation of DCGAN (https://arxiv.org/abs/1511.06434) using trainer.
4 |
5 | This code uses Cifar-10 dataset by default.
6 | You can use your own dataset by specifying `--dataset` argument to the directory consisting of image files for training.
7 | The model assumes the resolution of an input image is 32x32.
8 | If you want to use another image resolution, you need to change the network architecture in net.py.
9 |
10 | Below is an example learning result using cifar-10 dataset after 200 epoch.
11 | 
12 |
13 |
--------------------------------------------------------------------------------
/chainer/utils/argument.py:
--------------------------------------------------------------------------------
1 | def check_unexpected_kwargs(kwargs, **unexpected):
2 | for key, message in unexpected.items():
3 | if key in kwargs:
4 | raise ValueError(message)
5 |
6 |
7 | def parse_kwargs(kwargs, *name_and_values):
8 | values = [kwargs.pop(name, default_value)
9 | for name, default_value in name_and_values]
10 | if kwargs:
11 | args = ', '.join(["'%s'" % arg for arg in kwargs.keys()])
12 | raise TypeError('got unexpected keyword argument(s) %s' % args)
13 | return tuple(values)
14 |
15 |
16 | def assert_kwargs_empty(kwargs):
17 | # It only checks if kwargs is empty.
18 | parse_kwargs(kwargs)
19 |
--------------------------------------------------------------------------------
/tests/chainer_tests/training_tests/extensions_tests/test_snapshot.py:
--------------------------------------------------------------------------------
1 | import unittest
2 |
3 | import mock
4 |
5 | from chainer import testing
6 | from chainer.training import extensions
7 |
8 |
9 | class TestSnapshotObject(unittest.TestCase):
10 |
11 | def test_trigger(self):
12 | target = mock.MagicMock()
13 | snapshot_object = extensions.snapshot_object(target, 'myfile.dat')
14 | self.assertEqual(snapshot_object.trigger, (1, 'epoch',))
15 |
16 |
17 | class TestSnapshot(unittest.TestCase):
18 |
19 | def test_trigger(self):
20 | snapshot = extensions.snapshot()
21 | self.assertEqual(snapshot.trigger, (1, 'epoch'))
22 |
23 |
24 | testing.run_module(__name__, __file__)
25 |
--------------------------------------------------------------------------------
/chainer/training/triggers/__init__.py:
--------------------------------------------------------------------------------
1 | from chainer.training.triggers import interval_trigger # NOQA
2 | from chainer.training.triggers import minmax_value_trigger # NOQA
3 |
4 |
5 | # import class and function
6 | from chainer.training.triggers.early_stopping_trigger import EarlyStoppingTrigger # NOQA
7 | from chainer.training.triggers.interval_trigger import IntervalTrigger # NOQA
8 | from chainer.training.triggers.manual_schedule_trigger import ManualScheduleTrigger # NOQA
9 | from chainer.training.triggers.minmax_value_trigger import BestValueTrigger # NOQA
10 | from chainer.training.triggers.minmax_value_trigger import MaxValueTrigger # NOQA
11 | from chainer.training.triggers.minmax_value_trigger import MinValueTrigger # NOQA
12 |
--------------------------------------------------------------------------------
/.github/ISSUE_TEMPLATE.md:
--------------------------------------------------------------------------------
1 | If your issue is a **request for support in using Chainer**,
2 | please post it on [Stack Overflow](https://stackoverflow.com/questions/tagged/chainer).
3 |
4 | If it is a **bug report**, **feature request** or **suggestion**,
5 | please take a look at [our contribution guide](https://docs.chainer.org/en/stable/contribution.html).
6 |
7 | Specifically, if it is a bug report, these information are very helpful:
8 |
9 | * Conditions
10 | - Chainer version
11 | - CuPy version
12 | - OS/Platform
13 | - CUDA/cuDNN version
14 | - etc.
15 | * Code to reproduce
16 | * Error messages, stack traces, or logs
17 |
18 | Thank you for your cooperation!
19 |
20 | Support is available on our [Slack Chat](https://bit.ly/join-chainer-slack).
21 |
--------------------------------------------------------------------------------
/docs/source/reference/iterators.rst:
--------------------------------------------------------------------------------
1 | .. module:: chainer.iterators
2 |
3 | .. _iterators:
4 |
5 | Iterator
6 | ========
7 |
8 | Chainer provides some iterators that implement typical strategies to create mini-batches by iterating over datasets.
9 | :class:`SerialIterator` is the simplest one, which extract mini-batches in the main thread.
10 | :class:`MultiprocessIterator` and :class:`MultithreadIterator` are a parallelized version of :class:`SerialIterator`. It maintains worker subprocesses and subthreads to load the next mini-batch in parallel.
11 |
12 |
13 | .. autosummary::
14 | :toctree: generated/
15 | :nosignatures:
16 |
17 | chainer.iterators.SerialIterator
18 | chainer.iterators.MultiprocessIterator
19 | chainer.iterators.MultithreadIterator
20 |
--------------------------------------------------------------------------------
/tests/chainer_tests/functions_tests/math_tests/test_erf.py:
--------------------------------------------------------------------------------
1 | import math
2 | import unittest
3 |
4 | import numpy
5 |
6 | from chainer.backends import cuda
7 | import chainer.functions as F
8 | from chainer import testing
9 |
10 |
11 | def _erf_cpu(x, dtype):
12 | return numpy.vectorize(math.erf, otypes=[dtype])(x)
13 |
14 |
15 | def _erf_gpu(x, dtype):
16 | return cuda.to_gpu(_erf_cpu(cuda.to_cpu(x), dtype))
17 |
18 |
19 | def _erf_expected(x, dtype):
20 | if cuda.get_array_module(x) is numpy:
21 | return _erf_cpu(x, dtype)
22 | else:
23 | return _erf_gpu(x, dtype)
24 |
25 |
26 | @testing.unary_math_function_unittest(
27 | F.erf,
28 | func_expected=_erf_expected,
29 | )
30 | class TestErf(unittest.TestCase):
31 | pass
32 |
33 |
34 | testing.run_module(__name__, __file__)
35 |
--------------------------------------------------------------------------------
/examples/imagenet/README.md:
--------------------------------------------------------------------------------
1 | # Large Scale ConvNets
2 |
3 | ## Requirements
4 |
5 | - Pillow (Pillow requires an external library that corresponds to the image format)
6 |
7 | ## Description
8 |
9 | This is an experimental example of learning from the ILSVRC2012 classification dataset.
10 | It requires the training and validation dataset of following format:
11 |
12 | * Each line contains one training example.
13 | * Each line consists of two elements separated by space(s).
14 | * The first element is a path to 256x256 RGB image.
15 | * The second element is its ground truth label from 0 to 999.
16 |
17 | The text format is equivalent to what Caffe uses for ImageDataLayer.
18 | This example currently does not include dataset preparation script.
19 |
20 | This example requires "mean file" which is computed by `compute_mean.py`.
21 |
--------------------------------------------------------------------------------
/tests/chainer_tests/test_initializer.py:
--------------------------------------------------------------------------------
1 | import unittest
2 |
3 | from chainer import initializer
4 | from chainer import testing
5 |
6 |
7 | @testing.parameterize(
8 | {'shape': (2, 1), 'expect': (1, 2)},
9 | {'shape': (2, 3, 4), 'expect': (12, 8)},
10 | {'shape': (2, 3, 4, 5), 'expect': (60, 40)})
11 | class TestGetFans(unittest.TestCase):
12 |
13 | def test_get_fans(self):
14 | actual = initializer.get_fans(self.shape)
15 | self.assertTupleEqual(self.expect, actual)
16 |
17 |
18 | @testing.parameterize(
19 | {'shape': ()},
20 | {'shape': (2,)})
21 | class TestGetFansInvalid(unittest.TestCase):
22 |
23 | def test_invalid(self):
24 | with self.assertRaises(ValueError):
25 | initializer.get_fans(self.shape)
26 |
27 |
28 | testing.run_module(__name__, __file__)
29 |
--------------------------------------------------------------------------------
/tests/chainer_tests/functions_tests/math_tests/test_erfc.py:
--------------------------------------------------------------------------------
1 | import math
2 | import unittest
3 |
4 | import numpy
5 |
6 | from chainer.backends import cuda
7 | import chainer.functions as F
8 | from chainer import testing
9 |
10 |
11 | def _erfc_cpu(x, dtype):
12 | return numpy.vectorize(math.erfc, otypes=[dtype])(x)
13 |
14 |
15 | def _erfc_gpu(x, dtype):
16 | return cuda.to_gpu(_erfc_cpu(cuda.to_cpu(x), dtype))
17 |
18 |
19 | def _erfc_expected(x, dtype):
20 | if cuda.get_array_module(x) is numpy:
21 | return _erfc_cpu(x, dtype)
22 | else:
23 | return _erfc_gpu(x, dtype)
24 |
25 |
26 | @testing.unary_math_function_unittest(
27 | F.erfc,
28 | func_expected=_erfc_expected,
29 | )
30 | class TestErfc(unittest.TestCase):
31 | pass
32 |
33 |
34 | testing.run_module(__name__, __file__)
35 |
--------------------------------------------------------------------------------
/tests/chainer_tests/test_function_hook.py:
--------------------------------------------------------------------------------
1 | import unittest
2 |
3 | import chainer
4 | from chainer import testing
5 |
6 |
7 | class TestFunctionHook(unittest.TestCase):
8 |
9 | def setUp(self):
10 | self.h = chainer.FunctionHook()
11 |
12 | def test_name(self):
13 | self.assertEqual(self.h.name, 'FunctionHook')
14 |
15 | def test_forward_preprocess(self):
16 | self.assertTrue(hasattr(self.h, 'forward_preprocess'))
17 |
18 | def test_forward_postprocess(self):
19 | self.assertTrue(hasattr(self.h, 'forward_postprocess'))
20 |
21 | def test_backward_preprocess(self):
22 | self.assertTrue(hasattr(self.h, 'backward_preprocess'))
23 |
24 | def test_backward_postprocess(self):
25 | self.assertTrue(hasattr(self.h, 'backward_postprocess'))
26 |
27 |
28 | testing.run_module(__name__, __file__)
29 |
--------------------------------------------------------------------------------
/tests/chainer_tests/test_runnable.py:
--------------------------------------------------------------------------------
1 | import os
2 | import unittest
3 |
4 | from chainer import testing
5 |
6 |
7 | class TestRunnable(unittest.TestCase):
8 |
9 | def test_runnable(self):
10 | cwd = os.path.dirname(__file__)
11 | for dirpath, dirnames, filenames in os.walk(cwd):
12 | for filename in filenames:
13 | if not filename.endswith('.py') or '__init__' in filename:
14 | continue
15 | path = os.path.join(dirpath, filename)
16 | with open(path) as f:
17 | source = f.read()
18 | self.assertIn('testing.run_module(__name__, __file__)',
19 | source,
20 | '''{0} is not runnable.
21 | Call testing.run_module at the end of the test.'''.format(path))
22 |
23 |
24 | testing.run_module(__name__, __file__)
25 |
--------------------------------------------------------------------------------
/docs/source/reference/caffe.rst:
--------------------------------------------------------------------------------
1 | Caffe Reference Model Support
2 | =============================
3 |
4 | .. module:: chainer.links.caffe
5 |
6 | `Caffe `_ is a popular framework maintained by `BVLC `_ at UC Berkeley.
7 | It is widely used by computer vision communities, and aims at fast computation and easy usage without any programming.
8 | The BVLC team provides trained reference models in their `Model Zoo `_, one of the reason why this framework gets popular.
9 |
10 | Chainer can import the reference models and emulate the network by :class:`~chainer.Link` implementations.
11 | This functionality is provided by the :class:`chainer.links.caffe.CaffeFunction` class.
12 |
13 | .. autosummary::
14 | :toctree: generated/
15 | :nosignatures:
16 |
17 | chainer.links.caffe.CaffeFunction
18 |
--------------------------------------------------------------------------------
/chainer/dataset/__init__.py:
--------------------------------------------------------------------------------
1 | from chainer.dataset import convert # NOQA
2 | from chainer.dataset import dataset_mixin # NOQA
3 | from chainer.dataset import download # NOQA
4 | from chainer.dataset import iterator # NOQA
5 |
6 |
7 | # import class and function
8 | from chainer.dataset.convert import concat_examples # NOQA
9 | from chainer.dataset.convert import ConcatWithAsyncTransfer # NOQA
10 | from chainer.dataset.convert import to_device # NOQA
11 | from chainer.dataset.dataset_mixin import DatasetMixin # NOQA
12 | from chainer.dataset.download import cache_or_load_file # NOQA
13 | from chainer.dataset.download import cached_download # NOQA
14 | from chainer.dataset.download import get_dataset_directory # NOQA
15 | from chainer.dataset.download import get_dataset_root # NOQA
16 | from chainer.dataset.download import set_dataset_root # NOQA
17 | from chainer.dataset.iterator import Iterator # NOQA
18 |
--------------------------------------------------------------------------------
/chainer/function_hooks/cuda_profile.py:
--------------------------------------------------------------------------------
1 | from chainer.backends import cuda
2 | from chainer import function_hook
3 |
4 |
5 | class CUDAProfileHook(function_hook.FunctionHook):
6 |
7 | name = 'CUDAProfileHook'
8 |
9 | def __init__(self):
10 | cuda.check_cuda_available()
11 | if not cuda.cupy.cuda.nvtx_enabled:
12 | raise RuntimeError('nvtx is required for CUDAProfileHook')
13 |
14 | def forward_preprocess(self, function, in_data):
15 | cuda.cupy.cuda.nvtx.RangePush(function.label + '.forward')
16 |
17 | def forward_postprocess(self, function, in_data):
18 | cuda.cupy.cuda.nvtx.RangePop()
19 |
20 | def backward_preprocess(self, function, in_data, out_grad):
21 | cuda.cupy.cuda.nvtx.RangePush(function.label + '.backward')
22 |
23 | def backward_postprocess(self, function, in_data, out_grad):
24 | cuda.cupy.cuda.nvtx.RangePop()
25 |
--------------------------------------------------------------------------------
/tests/chainer_tests/functions_tests/pooling_tests/pooling_nd_helper.py:
--------------------------------------------------------------------------------
1 | import itertools
2 | import six
3 |
4 | from chainer import testing
5 |
6 |
7 | def pooling_patches(dims, ksize, stride, pad, cover_all):
8 | """Return tuples of slices that indicate pooling patches."""
9 | # Left-top indexes of each pooling patch.
10 | if cover_all:
11 | xss = itertools.product(
12 | *[six.moves.range(-p, d + p - k + s, s)
13 | for (d, k, s, p) in six.moves.zip(dims, ksize, stride, pad)])
14 | else:
15 | xss = itertools.product(
16 | *[six.moves.range(-p, d + p - k + 1, s)
17 | for (d, k, s, p) in six.moves.zip(dims, ksize, stride, pad)])
18 | # Tuples of slices for pooling patches.
19 | return [tuple(slice(max(x, 0), min(x + k, d))
20 | for (x, d, k) in six.moves.zip(xs, dims, ksize))
21 | for xs in xss]
22 |
23 |
24 | testing.run_module(__name__, __file__)
25 |
--------------------------------------------------------------------------------
/tests/chainer_tests/testing_tests/test_serializer.py:
--------------------------------------------------------------------------------
1 | import unittest
2 |
3 | from chainer.serializers import hdf5
4 | from chainer import testing
5 |
6 |
7 | class Serializable(object):
8 |
9 | def __init__(self, value):
10 | self.value = value
11 |
12 | def serialize(self, serializer):
13 | self.value = serializer('value', self.value)
14 |
15 |
16 | class TestSaveAndLoad(unittest.TestCase):
17 |
18 | def setUp(self):
19 | self.src = Serializable(1)
20 | self.dst = Serializable(2)
21 |
22 | def test_save_and_load_npz(self):
23 | testing.save_and_load_npz(self.src, self.dst)
24 | self.assertEqual(self.dst.value, 1)
25 |
26 | @unittest.skipUnless(hdf5._available, 'h5py is not available')
27 | def test_save_and_load_hdf5(self):
28 | testing.save_and_load_hdf5(self.src, self.dst)
29 | self.assertEqual(self.dst.value, 1)
30 |
31 |
32 | testing.run_module(__name__, __file__)
33 |
--------------------------------------------------------------------------------
/docs/source/reference/optimizers.rst:
--------------------------------------------------------------------------------
1 | Optimizers
2 | ==========
3 |
4 | .. autosummary::
5 | :toctree: generated/
6 | :nosignatures:
7 |
8 | chainer.optimizers.AdaDelta
9 | chainer.optimizers.AdaGrad
10 | chainer.optimizers.Adam
11 | chainer.optimizers.MomentumSGD
12 | chainer.optimizers.NesterovAG
13 | chainer.optimizers.RMSprop
14 | chainer.optimizers.RMSpropGraves
15 | chainer.optimizers.SGD
16 | chainer.optimizers.SMORMS3
17 |
18 | Optimizer base classes
19 | ~~~~~~~~~~~~~~~~~~~~~~
20 |
21 | .. autosummary::
22 | :toctree: generated/
23 | :nosignatures:
24 |
25 | chainer.Optimizer
26 | chainer.UpdateRule
27 | chainer.optimizer.Hyperparameter
28 | chainer.GradientMethod
29 |
30 | Hook functions
31 | ~~~~~~~~~~~~~~
32 |
33 | .. autosummary::
34 | :toctree: generated/
35 | :nosignatures:
36 |
37 | chainer.optimizer.WeightDecay
38 | chainer.optimizer.Lasso
39 | chainer.optimizer.GradientClipping
40 | chainer.optimizer.GradientNoise
41 |
--------------------------------------------------------------------------------
/chainer/functions/math/exponential_m1.py:
--------------------------------------------------------------------------------
1 | import numpy
2 |
3 | from chainer.backends import cuda
4 | from chainer import function_node
5 | from chainer import utils
6 | from chainer.utils import type_check
7 |
8 |
9 | class Expm1(function_node.FunctionNode):
10 |
11 | @property
12 | def label(self):
13 | return 'expm1'
14 |
15 | def check_type_forward(self, in_types):
16 | type_check.expect(in_types.size() == 1)
17 | type_check.expect(in_types[0].dtype.kind == 'f')
18 |
19 | def forward_cpu(self, x):
20 | self.retain_outputs((0,))
21 | return utils.force_array(numpy.expm1(x[0])),
22 |
23 | def forward_gpu(self, x):
24 | self.retain_outputs((0,))
25 | return cuda.cupy.expm1(x[0]),
26 |
27 | def backward(self, indexes, gy):
28 | y = self.get_retained_outputs()[0]
29 | return (y + 1.0) * gy[0],
30 |
31 |
32 | def expm1(x):
33 | """Elementwise exponential minus one function."""
34 | return Expm1().apply((x,))[0]
35 |
--------------------------------------------------------------------------------
/chainer/functions/math/logarithm_1p.py:
--------------------------------------------------------------------------------
1 | import numpy
2 |
3 | from chainer.backends import cuda
4 | from chainer import function_node
5 | from chainer import utils
6 | from chainer.utils import type_check
7 |
8 |
9 | class Log1p(function_node.FunctionNode):
10 |
11 | @property
12 | def label(self):
13 | return 'log1p'
14 |
15 | def check_type_forward(self, in_types):
16 | type_check.expect(in_types.size() == 1)
17 | type_check.expect(in_types[0].dtype.kind == 'f')
18 |
19 | def forward_cpu(self, x):
20 | self.retain_inputs((0,))
21 | return utils.force_array(numpy.log1p(x[0])),
22 |
23 | def forward_gpu(self, x):
24 | self.retain_inputs((0,))
25 | return cuda.cupy.log1p(x[0]),
26 |
27 | def backward(self, indexes, gy):
28 | x = self.get_retained_inputs()
29 | return gy[0] / (x[0] + 1.0),
30 |
31 |
32 | def log1p(x):
33 | """Elementwise natural logarithm plus one function."""
34 | return Log1p().apply((x,))[0]
35 |
--------------------------------------------------------------------------------
/chainer/functions/math/sign.py:
--------------------------------------------------------------------------------
1 | import chainer
2 | from chainer.backends import cuda
3 | from chainer import utils
4 |
5 |
6 | def sign(x):
7 | """Elementwise sign function.
8 |
9 | For a given input :math:`x`, this function returns :math:`sgn(x)`
10 | defined as
11 |
12 | .. math::
13 |
14 | sgn(x) = \\left \\{ \\begin{array}{cc}
15 | -1 & {\\rm if~x < 0} \\\\
16 | 0 & {\\rm if~x = 0} \\\\
17 | 1 & {\\rm if~x > 0} \\\\
18 | \\end{array} \\right.
19 |
20 | .. note::
21 |
22 | The gradient of this function is ``None`` everywhere and therefore
23 | unchains the computational graph.
24 |
25 | Args:
26 | x (~chainer.Variable): Input variable for which the sign is computed.
27 |
28 | Returns:
29 | ~chainer.Variable: Output variable.
30 |
31 | """
32 | if isinstance(x, chainer.variable.Variable):
33 | x = x.data
34 | xp = cuda.get_array_module(x)
35 | return chainer.as_variable(utils.force_array(xp.sign(x)))
36 |
--------------------------------------------------------------------------------
/chainer/functions/array/flipud.py:
--------------------------------------------------------------------------------
1 | from chainer.backends import cuda
2 | from chainer import function_node
3 | from chainer.utils import type_check
4 |
5 |
6 | class FlipUD(function_node.FunctionNode):
7 |
8 | """Flip array in the up/down direction."""
9 |
10 | def check_type_forward(self, in_types):
11 | type_check.expect(in_types.size() == 1)
12 | x_type = in_types[0]
13 |
14 | type_check.expect(
15 | x_type.dtype.kind == 'f',
16 | x_type.ndim >= 1
17 | )
18 |
19 | def forward(self, inputs):
20 | xp = cuda.get_array_module(*inputs)
21 | return xp.flipud(inputs[0]),
22 |
23 | def backward(self, indexes, grad_outputs):
24 | return FlipUD().apply(grad_outputs)
25 |
26 |
27 | def flipud(a):
28 | """Flip array in the up/down direction.
29 |
30 | Args:
31 | xs (~chainer.Variable): Input variable.
32 |
33 | Returns:
34 | ~chainer.Variable: Output variable.
35 |
36 | """
37 | return FlipUD().apply((a,))[0]
38 |
--------------------------------------------------------------------------------
/chainer/functions/array/fliplr.py:
--------------------------------------------------------------------------------
1 | from chainer.backends import cuda
2 | from chainer import function_node
3 | from chainer.utils import type_check
4 |
5 |
6 | class FlipLR(function_node.FunctionNode):
7 |
8 | """Flip array in the left/right direction."""
9 |
10 | def check_type_forward(self, in_types):
11 | type_check.expect(in_types.size() == 1)
12 | x_type = in_types[0]
13 |
14 | type_check.expect(
15 | x_type.dtype.kind == 'f',
16 | x_type.ndim >= 2
17 | )
18 |
19 | def forward(self, inputs):
20 | xp = cuda.get_array_module(*inputs)
21 | return xp.fliplr(inputs[0]),
22 |
23 | def backward(self, indexes, grad_outputs):
24 | return FlipLR().apply(grad_outputs)
25 |
26 |
27 | def fliplr(a):
28 | """Flip array in the left/right direction.
29 |
30 | Args:
31 | xs (~chainer.Variable): Input variable.
32 |
33 | Returns:
34 | ~chainer.Variable: Output variable.
35 |
36 | """
37 | return FlipLR().apply((a,))[0]
38 |
--------------------------------------------------------------------------------
/tests/chainer_tests/functions_tests/math_tests/test_sqrt.py:
--------------------------------------------------------------------------------
1 | import unittest
2 |
3 | import numpy
4 |
5 | import chainer.functions as F
6 | from chainer import testing
7 |
8 |
9 | # sqrt
10 |
11 | def make_data(shape, dtype):
12 | x = numpy.random.uniform(0.1, 5, shape).astype(dtype)
13 | gy = numpy.random.uniform(-1, 1, shape).astype(dtype)
14 | ggx = numpy.random.uniform(-1, 1, shape).astype(dtype)
15 | return x, gy, ggx
16 |
17 |
18 | @testing.unary_math_function_unittest(
19 | F.sqrt,
20 | make_data=make_data,
21 | backward_options={'eps': 1e-3},
22 | double_backward_options={'eps': 1e-3},
23 | )
24 | class TestSqrt(unittest.TestCase):
25 | pass
26 |
27 |
28 | # rsqrt
29 |
30 | def rsqrt(x):
31 | return numpy.reciprocal(numpy.sqrt(x))
32 |
33 |
34 | class TestRsqrt(unittest.TestCase):
35 |
36 | def test_rsqrt(self):
37 | x = numpy.random.uniform(0.1, 5, (3, 2)).astype(numpy.float32)
38 | testing.assert_allclose(F.rsqrt(x).data, rsqrt(x))
39 |
40 |
41 | testing.run_module(__name__, __file__)
42 |
--------------------------------------------------------------------------------
/chainer/optimizers/__init__.py:
--------------------------------------------------------------------------------
1 | from chainer.optimizers import ada_delta # NOQA
2 | from chainer.optimizers import ada_grad # NOQA
3 | from chainer.optimizers import adam # NOQA
4 | from chainer.optimizers import momentum_sgd # NOQA
5 | from chainer.optimizers import nesterov_ag # NOQA
6 | from chainer.optimizers import rmsprop # NOQA
7 | from chainer.optimizers import rmsprop_graves # NOQA
8 | from chainer.optimizers import sgd # NOQA
9 | from chainer.optimizers import smorms3 # NOQA
10 |
11 |
12 | # import class and function
13 | from chainer.optimizers.ada_delta import AdaDelta # NOQA
14 | from chainer.optimizers.ada_grad import AdaGrad # NOQA
15 | from chainer.optimizers.adam import Adam # NOQA
16 | from chainer.optimizers.momentum_sgd import MomentumSGD # NOQA
17 | from chainer.optimizers.nesterov_ag import NesterovAG # NOQA
18 | from chainer.optimizers.rmsprop import RMSprop # NOQA
19 | from chainer.optimizers.rmsprop_graves import RMSpropGraves # NOQA
20 | from chainer.optimizers.sgd import SGD # NOQA
21 | from chainer.optimizers.smorms3 import SMORMS3 # NOQA
22 |
--------------------------------------------------------------------------------
/chainer/datasets/concatenated_dataset.py:
--------------------------------------------------------------------------------
1 | from chainer.dataset import dataset_mixin
2 |
3 |
4 | class ConcatenatedDataset(dataset_mixin.DatasetMixin):
5 |
6 | """Dataset which concatenates some base datasets.
7 |
8 | This dataset wraps some base datasets and works as a concatenated dataset.
9 | For example, if a base dataset with 10 samples and
10 | another base dataset with 20 samples are given, this dataset works as
11 | a dataset which has 30 samples.
12 |
13 | Args:
14 | datasets: The underlying datasets. Each dataset has to support
15 | :meth:`__len__` and :meth:`__getitem__`.
16 |
17 | """
18 |
19 | def __init__(self, *datasets):
20 | self._datasets = datasets
21 |
22 | def __len__(self):
23 | return sum(len(dataset) for dataset in self._datasets)
24 |
25 | def get_example(self, i):
26 | if i < 0:
27 | raise IndexError
28 | for dataset in self._datasets:
29 | if i < len(dataset):
30 | return dataset[i]
31 | i -= len(dataset)
32 | raise IndexError
33 |
--------------------------------------------------------------------------------
/tests/chainer_tests/testing_tests/test_unary_math_function_test.py:
--------------------------------------------------------------------------------
1 | import unittest
2 |
3 | from chainer import function_node
4 | from chainer import testing
5 |
6 |
7 | def dummy():
8 | pass
9 |
10 |
11 | class TestNoNumpyFunction(unittest.TestCase):
12 |
13 | def test_no_numpy_function(self):
14 | with self.assertRaises(ValueError):
15 | testing.unary_math_function_unittest(dummy) # no numpy.dummy
16 |
17 |
18 | class DummyLinear(function_node.FunctionNode):
19 |
20 | @property
21 | def label(self):
22 | return 'dummy_linear'
23 |
24 | def forward(self, x):
25 | return x[0],
26 |
27 | def backward(self, indexes, gy):
28 | return gy[0],
29 |
30 |
31 | def dummy_linear(x):
32 | return DummyLinear().apply((x,))[0]
33 |
34 |
35 | @testing.unary_math_function_unittest(dummy_linear,
36 | func_expected=lambda x, dtype: x,
37 | is_linear=True)
38 | class TestIsLinear(unittest.TestCase):
39 | pass
40 |
41 |
42 | testing.run_module(__name__, __file__)
43 |
--------------------------------------------------------------------------------
/.github/stale.yml:
--------------------------------------------------------------------------------
1 | # Configuration for probot-stale - https://github.com/probot/stale
2 |
3 | # Number of days of inactivity before an Issue or Pull Request becomes stale
4 | daysUntilStale: 90
5 | # Number of days of inactivity before a stale Issue or Pull Request is closed
6 | daysUntilClose: 30
7 | # Issues or Pull Requests with these labels will never be considered stale. Set to `[]` to disable
8 | exemptLabels:
9 | - bug
10 | # Label to use when marking as stale
11 | staleLabel: stale
12 | # Comment to post when marking as stale. Set to `false` to disable
13 | markComment: >
14 | This issue has been automatically marked as stale because it has not had
15 | recent activity. It will be closed after 30 days if no further activity occurs.
16 | Thank you for your contributions.
17 | # Comment to post when removing the stale label. Set to `false` to disable
18 | unmarkComment: false
19 | # Comment to post when closing a stale Issue or Pull Request. Set to `false` to disable
20 | closeComment: >
21 | This issue is closed as announced. Feel free to re-open it if needed.
22 | # Limit to only `issues` or `pulls`
23 | # only: issues
24 |
--------------------------------------------------------------------------------
/chainer/functions/activation/selu.py:
--------------------------------------------------------------------------------
1 | from chainer.functions.activation import elu
2 |
3 |
4 | def selu(x,
5 | alpha=1.6732632423543772848170429916717,
6 | scale=1.0507009873554804934193349852946):
7 | """Scaled Exponential Linear Unit function.
8 |
9 | For parameters :math:`\\alpha` and :math:`\\lambda`, it is expressed as
10 |
11 | .. math::
12 | f(x) = \\lambda \\left \\{ \\begin{array}{ll}
13 | x & {\\rm if}~ x \\ge 0 \\\\
14 | \\alpha (\\exp(x) - 1) & {\\rm if}~ x < 0,
15 | \\end{array} \\right.
16 |
17 | See: https://arxiv.org/abs/1706.02515
18 |
19 | Args:
20 | x (:class:`~chainer.Variable` or :class:`numpy.ndarray` or \
21 | :class:`cupy.ndarray`):
22 | Input variable. A :math:`(s_1, s_2, ..., s_N)`-shaped float array.
23 | alpha (float): Parameter :math:`\\alpha`.
24 | scale (float): Parameter :math:`\\lambda`.
25 |
26 | Returns:
27 | ~chainer.Variable: Output variable. A
28 | :math:`(s_1, s_2, ..., s_N)`-shaped float array.
29 |
30 | """
31 | return scale * elu.elu(x, alpha=alpha)
32 |
--------------------------------------------------------------------------------
/chainer/functions/array/flatten.py:
--------------------------------------------------------------------------------
1 | import chainer
2 |
3 |
4 | def flatten(x):
5 | """Flatten a given array into one dimension.
6 |
7 | Args:
8 | x (:class:`~chainer.Variable` or :class:`numpy.ndarray` or \
9 | :class:`cupy.ndarray`): Input variable.
10 |
11 | Returns:
12 | ~chainer.Variable: Output variable flatten to one dimension.
13 |
14 | .. note::
15 |
16 | When you input a scalar array (i.e. the shape is ``()``),
17 | you can also get the one dimension array whose shape is ``(1,)``.
18 |
19 | .. admonition:: Example
20 |
21 | >>> x = np.array([[1, 2], [3, 4]])
22 | >>> x.shape
23 | (2, 2)
24 | >>> y = F.flatten(x)
25 | >>> y.shape
26 | (4,)
27 | >>> y.data
28 | array([1, 2, 3, 4])
29 |
30 | >>> x = np.arange(8).reshape(2, 2, 2)
31 | >>> x.shape
32 | (2, 2, 2)
33 | >>> y = F.flatten(x)
34 | >>> y.shape
35 | (8,)
36 | >>> y.data
37 | array([0, 1, 2, 3, 4, 5, 6, 7])
38 |
39 | """
40 | return chainer.functions.reshape(x, (x.size,))
41 |
--------------------------------------------------------------------------------
/chainer/utils/imgproc.py:
--------------------------------------------------------------------------------
1 | import numpy
2 |
3 |
4 | def oversample(images, crop_dims):
5 | """Crop an image into center, corners, and mirror images."""
6 |
7 | # Dimensions and center.
8 | channels, src_h, src_w = images[0].shape
9 | cy, cx = src_h / 2.0, src_w / 2.0
10 | dst_h, dst_w = crop_dims
11 |
12 | # Make crop coordinates
13 | crops_ix = numpy.empty((5, 4), dtype=int)
14 | crops_ix[0, :2] = [0, 0]
15 | crops_ix[1, :2] = [0, src_w - dst_w]
16 | crops_ix[2, :2] = [src_h - dst_h, 0]
17 | crops_ix[3, :2] = [src_h - dst_h, src_w - dst_w]
18 | crops_ix[4, :2] = [int(cy - dst_h / 2.0), int(cx - dst_w / 2.0)]
19 | crops_ix[:, 2] = crops_ix[:, 0] + dst_h
20 | crops_ix[:, 3] = crops_ix[:, 1] + dst_w
21 |
22 | crops = numpy.empty(
23 | (10 * len(images), channels, dst_h, dst_w), dtype=images[0].dtype)
24 | ix = 0
25 | for img in images:
26 | for crop in crops_ix:
27 | crops[ix] = img[:, crop[0]:crop[2], crop[1]:crop[3]]
28 | ix += 1
29 | crops[ix:ix + 5] = crops[ix - 5:ix, :, :, ::-1]
30 | ix += 5
31 | return crops
32 |
--------------------------------------------------------------------------------
/chainer/training/__init__.py:
--------------------------------------------------------------------------------
1 | from chainer.training import extension # NOQA
2 | from chainer.training import extensions # NOQA
3 | from chainer.training import trainer # NOQA
4 | from chainer.training import trigger # NOQA
5 | from chainer.training import triggers # NOQA
6 | from chainer.training import updater # NOQA
7 | from chainer.training import updaters # NOQA
8 | from chainer.training import util # NOQA
9 |
10 |
11 | # import class and function
12 | from chainer.training.extension import Extension # NOQA
13 | from chainer.training.extension import make_extension # NOQA
14 | from chainer.training.extension import PRIORITY_EDITOR # NOQA
15 | from chainer.training.extension import PRIORITY_READER # NOQA
16 | from chainer.training.extension import PRIORITY_WRITER # NOQA
17 | from chainer.training.trainer import Trainer # NOQA
18 | from chainer.training.trigger import get_trigger # NOQA
19 | from chainer.training.trigger import IntervalTrigger # NOQA
20 | from chainer.training.updater import ParallelUpdater # NOQA
21 | from chainer.training.updater import StandardUpdater # NOQA
22 | from chainer.training.updater import Updater # NOQA
23 |
--------------------------------------------------------------------------------
/chainer/utils/array.py:
--------------------------------------------------------------------------------
1 | import warnings
2 |
3 | import numpy
4 |
5 | from chainer.backends import cuda
6 |
7 |
8 | def as_vec(x):
9 | warnings.warn(
10 | 'chainer.utils.array.as_vec is deprecated. Please refer to '
11 | 'numpy.ravel or other array backend functions to flatten ndarrays.',
12 | DeprecationWarning)
13 | if x.ndim == 1:
14 | return x
15 | return x.ravel()
16 |
17 |
18 | def as_mat(x):
19 | warnings.warn(
20 | 'chainer.utils.array.as_mat is deprecated. Please refer to '
21 | 'numpy.reshape or other array backend functions to reshape ndarrays.',
22 | DeprecationWarning)
23 | if x.ndim == 2:
24 | return x
25 | return x.reshape(len(x), -1)
26 |
27 |
28 | def empty_like(x):
29 | warnings.warn(
30 | 'chainer.utils.array.empty_like is deprecated. Please refer to '
31 | 'numpy.empty_like or other array backend functions to initialize '
32 | 'empty arrays.',
33 | DeprecationWarning)
34 | if cuda.available and isinstance(x, cuda.ndarray):
35 | return cuda.cupy.empty_like(x)
36 | else:
37 | return numpy.empty_like(x)
38 |
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | Copyright (c) 2015 Preferred Infrastructure, Inc.
2 | Copyright (c) 2015 Preferred Networks, Inc.
3 |
4 | Permission is hereby granted, free of charge, to any person obtaining a copy
5 | of this software and associated documentation files (the "Software"), to deal
6 | in the Software without restriction, including without limitation the rights
7 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 | copies of the Software, and to permit persons to whom the Software is
9 | furnished to do so, subject to the following conditions:
10 |
11 | The above copyright notice and this permission notice shall be included in
12 | all copies or substantial portions of the Software.
13 |
14 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20 | THE SOFTWARE.
--------------------------------------------------------------------------------
/chainer/functions/math/square.py:
--------------------------------------------------------------------------------
1 | from chainer.backends import cuda
2 | from chainer import function_node
3 | from chainer import utils
4 | from chainer.utils import type_check
5 |
6 |
7 | class Square(function_node.FunctionNode):
8 |
9 | @property
10 | def label(self):
11 | return 'square'
12 |
13 | def check_type_forward(self, in_types):
14 | type_check.expect(
15 | in_types.size() == 1,
16 | in_types[0].dtype.kind == 'f',
17 | )
18 |
19 | def forward(self, x):
20 | self.retain_inputs((0,))
21 | xp = cuda.get_array_module(*x)
22 | return utils.force_array(xp.square(x[0], dtype=x[0].dtype)),
23 |
24 | def backward(self, indexes, gy):
25 | x = self.get_retained_inputs()[0]
26 | gx = gy[0] * 2.0 * x
27 | return gx,
28 |
29 |
30 | def square(x):
31 | """Elementwise square function.
32 |
33 | .. math::
34 | y_i = x_i ^ 2.
35 |
36 | Args:
37 | x (chainer.Variable or :class:`numpy.ndarray` or cupy.ndarray):
38 | Input variable.
39 |
40 | Returns:
41 | ~chainer.Variable: Output variable.
42 | """
43 | return Square().apply((x,))[0]
44 |
--------------------------------------------------------------------------------
/docs/source/index.rst:
--------------------------------------------------------------------------------
1 | ==================================================
2 | Chainer -- A flexible framework of neural networks
3 | ==================================================
4 |
5 | Welcome to the `Chainer `_ documentation.
6 |
7 | * Certified 99% Python code!
8 | * Define-by-run approach for flexibility and understandable errors
9 | * Standard Numpy syntax
10 | * NVIDIA GPU acceleration, thanks to `CuPy `_
11 |
12 | .. toctree::
13 | :maxdepth: 2
14 | :caption: Chainer Documents
15 |
16 | install
17 | guides/index
18 | examples/index
19 | reference/index
20 |
21 | .. toctree::
22 | :maxdepth: 1
23 | :caption: Other
24 |
25 | compatibility
26 | contribution
27 | tips
28 | upgrade
29 | comparison
30 | license
31 |
32 | Indices and tables
33 | ==================
34 |
35 | * :ref:`genindex`
36 | * :ref:`modindex`
37 | * :ref:`search`
38 |
39 | .. toctree::
40 | :maxdepth: 1
41 | :caption: Community
42 |
43 | Slack Chat
44 | Forums
45 | Examples in Awesome Chainer
46 |
--------------------------------------------------------------------------------
/examples/imagenet/compute_mean.py:
--------------------------------------------------------------------------------
1 | #!/usr/bin/env python
2 | import argparse
3 | import sys
4 |
5 | import numpy as np
6 |
7 | import chainer
8 |
9 |
10 | def compute_mean(dataset):
11 | print('compute mean image')
12 | sum_image = 0
13 | N = len(dataset)
14 | for i, (image, _) in enumerate(dataset):
15 | sum_image += image
16 | sys.stderr.write('{} / {}\r'.format(i, N))
17 | sys.stderr.flush()
18 | sys.stderr.write('\n')
19 | return sum_image / N
20 |
21 |
22 | def main():
23 | parser = argparse.ArgumentParser(description='Compute images mean array')
24 | parser.add_argument('dataset',
25 | help='Path to training image-label list file')
26 | parser.add_argument('--root', '-R', default='.',
27 | help='Root directory path of image files')
28 | parser.add_argument('--output', '-o', default='mean.npy',
29 | help='path to output mean array')
30 | args = parser.parse_args()
31 |
32 | dataset = chainer.datasets.LabeledImageDataset(args.dataset, args.root)
33 | mean = compute_mean(dataset)
34 | np.save(args.output, mean)
35 |
36 |
37 | if __name__ == '__main__':
38 | main()
39 |
--------------------------------------------------------------------------------
/chainer/utils/__init__.py:
--------------------------------------------------------------------------------
1 | import numpy
2 |
3 | from chainer.utils import walker_alias # NOQA
4 |
5 |
6 | # import class and function
7 | from chainer.utils.conv import get_conv_outsize # NOQA
8 | from chainer.utils.conv import get_deconv_outsize # NOQA
9 | from chainer.utils.experimental import experimental # NOQA
10 | from chainer.utils.walker_alias import WalkerAlias # NOQA
11 |
12 |
13 | def force_array(x, dtype=None):
14 | # numpy returns a float value (scalar) when a return value of an operator
15 | # is a 0-dimension array.
16 | # We need to convert such a value to a 0-dimension array because `Function`
17 | # object needs to return an `numpy.ndarray`.
18 | if numpy.isscalar(x):
19 | if dtype is None:
20 | return numpy.array(x)
21 | else:
22 | return numpy.array(x, dtype)
23 | else:
24 | if dtype is None:
25 | return x
26 | else:
27 | return x.astype(dtype, copy=False)
28 |
29 |
30 | def force_type(dtype, value):
31 | if numpy.isscalar(value):
32 | return dtype.type(value)
33 | elif value.dtype != dtype:
34 | return value.astype(dtype, copy=False)
35 | else:
36 | return value
37 |
--------------------------------------------------------------------------------
/docs/source/license.rst:
--------------------------------------------------------------------------------
1 | License
2 | =======
3 |
4 | Copyright (c) 2015 Preferred Infrastructure, Inc.
5 |
6 | Copyright (c) 2015 Preferred Networks, Inc.
7 |
8 |
9 | Permission is hereby granted, free of charge, to any person obtaining a copy
10 | of this software and associated documentation files (the "Software"), to deal
11 | in the Software without restriction, including without limitation the rights
12 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13 | copies of the Software, and to permit persons to whom the Software is
14 | furnished to do so, subject to the following conditions:
15 |
16 | The above copyright notice and this permission notice shall be included in
17 | all copies or substantial portions of the Software.
18 |
19 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25 | THE SOFTWARE.
26 |
--------------------------------------------------------------------------------
/chainer/links/activation/prelu.py:
--------------------------------------------------------------------------------
1 | from chainer.functions.activation import prelu
2 | from chainer import link
3 | from chainer import variable
4 |
5 |
6 | class PReLU(link.Link):
7 |
8 | """Parametric ReLU function as a link.
9 |
10 | Args:
11 | shape (tuple of ints): Shape of the parameter array.
12 | init (float): Initial parameter value.
13 |
14 | See the paper for details: `Delving Deep into Rectifiers: Surpassing \
15 | Human-Level Performance on ImageNet Classification \
16 | `_.
17 |
18 | .. seealso:: :func:`chainer.functions.prelu`
19 |
20 | Attributes:
21 | W (~chainer.Parameter): Coefficient of parametric ReLU.
22 |
23 | """
24 |
25 | def __init__(self, shape=(), init=0.25):
26 | super(PReLU, self).__init__()
27 | with self.init_scope():
28 | self.W = variable.Parameter(init, shape)
29 |
30 | def __call__(self, x):
31 | """Applies the parametric ReLU activation function.
32 |
33 | Args:
34 | x (~chainer.Variable): Input variable.
35 |
36 | Returns:
37 | ~chainer.Variable: Output of the parametric ReLU function.
38 |
39 | """
40 | return prelu.prelu(x, self.W)
41 |
--------------------------------------------------------------------------------
/docs/source/_templates/autosummary/class.rst:
--------------------------------------------------------------------------------
1 | {{ fullname }}
2 | {{ underline }}
3 |
4 | .. currentmodule:: {{ module }}
5 |
6 | .. autoclass:: {{ objname }}
7 |
8 | ..
9 | Methods
10 |
11 | {% block methods %}
12 |
13 | .. rubric:: Methods
14 |
15 | ..
16 | Special methods
17 |
18 | {% for item in ('__call__', '__enter__', '__exit__', '__getitem__', '__setitem__', '__len__', '__next__', '__iter__', '__copy__') %}
19 | {% if item in all_methods %}
20 | .. automethod:: {{ item }}
21 | {% endif %}
22 | {%- endfor %}
23 |
24 | ..
25 | Ordinary methods
26 |
27 | {% for item in methods %}
28 | {% if item not in ('__init__',) %}
29 | .. automethod:: {{ item }}
30 | {% endif %}
31 | {%- endfor %}
32 |
33 | ..
34 | Special methods
35 |
36 | {% for item in ('__eq__', '__ne__', '__lt__', '__le__', '__gt__', '__ge__', '__nonzero__', '__bool__') %}
37 | {% if item in all_methods %}
38 | .. automethod:: {{ item }}
39 | {% endif %}
40 | {%- endfor %}
41 | {% endblock %}
42 |
43 | ..
44 | Atributes
45 |
46 | {% block attributes %} {% if attributes %}
47 |
48 | .. rubric:: Attributes
49 |
50 | {% for item in attributes %}
51 | .. autoattribute:: {{ item }}
52 | {%- endfor %}
53 | {% endif %} {% endblock %}
54 |
--------------------------------------------------------------------------------
/examples/dcgan/visualize.py:
--------------------------------------------------------------------------------
1 | #!/usr/bin/env python
2 |
3 | import os
4 |
5 | import numpy as np
6 | from PIL import Image
7 |
8 | import chainer
9 | import chainer.backends.cuda
10 | from chainer import Variable
11 |
12 |
13 | def out_generated_image(gen, dis, rows, cols, seed, dst):
14 | @chainer.training.make_extension()
15 | def make_image(trainer):
16 | np.random.seed(seed)
17 | n_images = rows * cols
18 | xp = gen.xp
19 | z = Variable(xp.asarray(gen.make_hidden(n_images)))
20 | with chainer.using_config('train', False):
21 | x = gen(z)
22 | x = chainer.backends.cuda.to_cpu(x.data)
23 | np.random.seed()
24 |
25 | x = np.asarray(np.clip(x * 255, 0.0, 255.0), dtype=np.uint8)
26 | _, _, H, W = x.shape
27 | x = x.reshape((rows, cols, 3, H, W))
28 | x = x.transpose(0, 3, 1, 4, 2)
29 | x = x.reshape((rows * H, cols * W, 3))
30 |
31 | preview_dir = '{}/preview'.format(dst)
32 | preview_path = preview_dir +\
33 | '/image{:0>8}.png'.format(trainer.updater.iteration)
34 | if not os.path.exists(preview_dir):
35 | os.makedirs(preview_dir)
36 | Image.fromarray(x).save(preview_path)
37 | return make_image
38 |
--------------------------------------------------------------------------------
/chainer/training/extensions/value_observation.py:
--------------------------------------------------------------------------------
1 | from chainer.training import extension
2 |
3 |
4 | def observe_value(observation_key, target_func):
5 | """Returns a trainer extension to continuously record a value.
6 |
7 | Args:
8 | observation_key (str): Key of observation to record.
9 | target_func (function): Function that returns the value to record.
10 | It must take one argument: :class:~chainer.training.Trainer object.
11 | Returns:
12 | The extension function.
13 | """
14 | @extension.make_extension(
15 | trigger=(1, 'epoch'), priority=extension.PRIORITY_WRITER)
16 | def _observe_value(trainer):
17 | trainer.observation[observation_key] = target_func(trainer)
18 | return _observe_value
19 |
20 |
21 | def observe_lr(optimizer_name='main', observation_key='lr'):
22 | """Returns a trainer extension to record the learning rate.
23 |
24 | Args:
25 | optimizer_name (str): Name of optimizer whose learning rate is
26 | recorded.
27 | observation_key (str): Key of observation to record.
28 |
29 | Returns:
30 | The extension function.
31 | """
32 | return observe_value(
33 | observation_key,
34 | lambda trainer: trainer.updater.get_optimizer(optimizer_name).lr)
35 |
--------------------------------------------------------------------------------
/docs/source/_autosummary_check.py:
--------------------------------------------------------------------------------
1 | import inspect
2 | import os
3 | import types
4 |
5 | import chainer.functions
6 | import chainer.links
7 |
8 |
9 | def _is_rst_exists(entity):
10 | return os.path.exists('source/reference/generated/{}.rst'.format(entity))
11 |
12 |
13 | def check(app, exception):
14 | missing_entities = []
15 |
16 | missing_entities += [
17 | name for name in _list_chainer_functions()
18 | if not _is_rst_exists(name)]
19 |
20 | missing_entities += [
21 | name for name in _list_chainer_links()
22 | if not _is_rst_exists(name)]
23 |
24 | if len(missing_entities) != 0:
25 | app.warn('\n'.join([
26 | 'Undocumented entities found.',
27 | '',
28 | ] + missing_entities))
29 |
30 |
31 | def _list_chainer_functions():
32 | # List exported functions under chainer.functions.
33 | return ['chainer.functions.{}'.format(name)
34 | for (name, func) in chainer.functions.__dict__.items()
35 | if isinstance(func, types.FunctionType)]
36 |
37 |
38 | def _list_chainer_links():
39 | # List exported classes under chainer.links.
40 | return ['chainer.links.{}'.format(name)
41 | for (name, link) in chainer.links.__dict__.items()
42 | if inspect.isclass(link)]
43 |
--------------------------------------------------------------------------------
/tests/chainer_tests/utils_tests/test_walker_alias.py:
--------------------------------------------------------------------------------
1 | import unittest
2 |
3 | import numpy
4 |
5 | from chainer.backends import cuda
6 | from chainer import testing
7 | from chainer.testing import attr
8 | from chainer import utils
9 |
10 |
11 | class TestWalkerAlias(unittest.TestCase):
12 |
13 | def setUp(self):
14 | self.ps = numpy.array([5, 3, 4, 1, 2], dtype=numpy.int32)
15 | self.sampler = utils.WalkerAlias(self.ps)
16 |
17 | def check_sample(self):
18 | counts = numpy.zeros(len(self.ps), numpy.float32)
19 | for _ in range(1000):
20 | vs = self.sampler.sample((4, 3))
21 | numpy.add.at(counts, cuda.to_cpu(vs), 1)
22 | counts /= (1000 * 12)
23 | counts *= sum(self.ps)
24 | testing.assert_allclose(self.ps, counts, atol=0.1, rtol=0.1)
25 |
26 | def test_sample_cpu(self):
27 | self.check_sample()
28 |
29 | @attr.gpu
30 | def test_sample_gpu(self):
31 | self.sampler.to_gpu()
32 | self.assertTrue(self.sampler.use_gpu)
33 | self.check_sample()
34 |
35 | @attr.gpu
36 | def test_to_cpu(self):
37 | self.sampler.to_gpu()
38 | self.sampler.to_cpu()
39 | self.assertFalse(self.sampler.use_gpu)
40 | self.check_sample()
41 |
42 |
43 | testing.run_module(__name__, __file__)
44 |
--------------------------------------------------------------------------------
/docs/source/reference/serializers.rst:
--------------------------------------------------------------------------------
1 | .. module:: chainer.serializers
2 |
3 | Serializers
4 | ===========
5 |
6 | Serialization in NumPy NPZ format
7 | ---------------------------------
8 |
9 | NumPy serializers can be used in arbitrary environments that Chainer runs with.
10 | It consists of asymmetric serializer/deserializer due to the fact that :func:`numpy.savez` does not support online serialization.
11 | Therefore, serialization requires two-step manipulation: first packing the objects into a flat dictionary, and then serializing it into npz format.
12 |
13 | .. autosummary::
14 | :toctree: generated/
15 | :nosignatures:
16 |
17 | chainer.serializers.DictionarySerializer
18 | chainer.serializers.NpzDeserializer
19 | chainer.serializers.save_npz
20 | chainer.serializers.load_npz
21 |
22 | Serialization in HDF5 format
23 | ----------------------------
24 |
25 | .. autosummary::
26 | :toctree: generated/
27 | :nosignatures:
28 |
29 | chainer.serializers.HDF5Serializer
30 | chainer.serializers.HDF5Deserializer
31 | chainer.serializers.save_hdf5
32 | chainer.serializers.load_hdf5
33 |
34 | Serializers base classes
35 | ------------------------
36 |
37 | .. module:: chainer
38 |
39 | .. autosummary::
40 | :toctree: generated/
41 | :nosignatures:
42 |
43 | chainer.Serializer
44 | chainer.AbstractSerializer
45 | chainer.Deserializer
46 |
--------------------------------------------------------------------------------
/docs/source/reference/util/cuda.rst:
--------------------------------------------------------------------------------
1 | CUDA utilities
2 | --------------
3 | .. automodule:: chainer.backends.cuda
4 |
5 | .. currentmodule:: /
6 |
7 | Devices
8 | ~~~~~~~
9 |
10 | .. autosummary::
11 | :toctree: generated/
12 | :nosignatures:
13 |
14 | chainer.backends.cuda.get_device
15 | chainer.backends.cuda.get_device_from_id
16 | chainer.backends.cuda.get_device_from_array
17 |
18 | CuPy array allocation and copy
19 | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
20 |
21 | .. autosummary::
22 | :toctree: generated/
23 | :nosignatures:
24 |
25 | chainer.backends.cuda.copy
26 | chainer.backends.cuda.to_cpu
27 | chainer.backends.cuda.to_gpu
28 |
29 | Kernel definition utilities
30 | ~~~~~~~~~~~~~~~~~~~~~~~~~~~
31 |
32 | .. autosummary::
33 | :toctree: generated/
34 | :nosignatures:
35 |
36 | chainer.backends.cuda.memoize
37 | chainer.backends.cuda.clear_memo
38 | chainer.backends.cuda.elementwise
39 | chainer.backends.cuda.reduce
40 |
41 | CPU/GPU generic code support
42 | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
43 |
44 | .. autosummary::
45 | :toctree: generated/
46 | :nosignatures:
47 |
48 | chainer.backends.cuda.get_array_module
49 |
50 | cuDNN support
51 | ~~~~~~~~~~~~~
52 |
53 | .. autosummary::
54 | :toctree: generated/
55 | :nosignatures:
56 |
57 | chainer.backends.cuda.set_max_workspace_size
58 | chainer.backends.cuda.get_max_workspace_size
59 |
--------------------------------------------------------------------------------
/tests/chainer_tests/links_tests/connection_tests/test_inception.py:
--------------------------------------------------------------------------------
1 | import unittest
2 |
3 | import numpy
4 |
5 | import chainer
6 | from chainer.backends import cuda
7 | from chainer import links
8 | from chainer import testing
9 | from chainer.testing import attr
10 |
11 |
12 | class TestInception(unittest.TestCase):
13 |
14 | in_channels = 3
15 | out1, proj3, out3, proj5, out5, proj_pool = 3, 2, 3, 2, 3, 3
16 |
17 | def setUp(self):
18 | self.x = numpy.random.uniform(
19 | -1, 1, (10, self.in_channels, 5, 5)
20 | ).astype(numpy.float32)
21 | out = self.out1 + self.out3 + self.out5 + self.proj_pool
22 | self.gy = numpy.random.uniform(
23 | -1, 1, (10, out, 5, 5)).astype(numpy.float32)
24 | self.l = links.Inception(
25 | self.in_channels, self.out1, self.proj3, self.out3,
26 | self.proj5, self.out5, self.proj_pool)
27 |
28 | def check_backward(self, x_data, y_grad):
29 | x = chainer.Variable(x_data)
30 | y = self.l(x)
31 | y.grad = y_grad
32 | y.backward()
33 |
34 | def test_backward_cpu(self):
35 | self.check_backward(self.x, self.gy)
36 |
37 | @attr.gpu
38 | def test_backward_gpu(self):
39 | self.l.to_gpu()
40 | self.check_backward(cuda.to_gpu(self.x), cuda.to_gpu(self.gy))
41 |
42 |
43 | testing.run_module(__name__, __file__)
44 |
--------------------------------------------------------------------------------
/tests/chainer_tests/functions_tests/noise_tests/test_gumbel_softmax.py:
--------------------------------------------------------------------------------
1 | import unittest
2 |
3 | import numpy
4 |
5 | import chainer
6 | from chainer.backends import cuda
7 | from chainer import functions
8 | from chainer import testing
9 | from chainer.testing import attr
10 |
11 |
12 | @testing.parameterize(*testing.product({
13 | 'shape': [(3, 2), ()],
14 | 'dtype': [numpy.float16, numpy.float32, numpy.float64],
15 | }))
16 | class TestGumbelSoftmax(unittest.TestCase):
17 |
18 | def setUp(self):
19 | self.log_pi = numpy.random.uniform(
20 | -1, 1, self.shape).astype(numpy.float32)
21 | self.tau = numpy.float32(numpy.random.uniform(0.1, 10.0))
22 |
23 | def check_forward(self, log_pi_data, tau):
24 | log_pi = chainer.Variable(log_pi_data)
25 | y = functions.gumbel_softmax(log_pi, tau=tau)
26 |
27 | # Only checks dtype and shape because its result contains noise
28 | self.assertEqual(y.dtype, numpy.float32)
29 | self.assertEqual(y.shape, log_pi.shape)
30 | self.assertEqual(
31 | cuda.get_array_module(y),
32 | cuda.get_array_module(log_pi))
33 |
34 | def test_forward_cpu(self):
35 | self.check_forward(self.log_pi, self.tau)
36 |
37 | @attr.gpu
38 | def test_forward_gpu(self):
39 | self.check_forward(cuda.to_gpu(self.log_pi), self.tau)
40 |
41 |
42 | testing.run_module(__name__, __file__)
43 |
--------------------------------------------------------------------------------
/chainer/links/connection/parameter.py:
--------------------------------------------------------------------------------
1 | from chainer.backends import cuda
2 | from chainer.functions.math import identity
3 | from chainer import link
4 |
5 |
6 | class Parameter(link.Link):
7 |
8 | """Link that just holds a parameter and returns it.
9 |
10 | .. deprecated:: v1.5
11 | The parameters are stored as variables as of v1.5. Use them directly
12 | instead.
13 |
14 | Args:
15 | array: Initial parameter array.
16 |
17 | Attributes:
18 | W (~chainer.Variable): Parameter variable.
19 |
20 | """
21 |
22 | def __init__(self, array):
23 | super(Parameter, self).__init__()
24 | self.add_param('W', array.shape, dtype=array.dtype)
25 | self.W.data = array
26 | if isinstance(array, cuda.ndarray):
27 | self.to_gpu(cuda.get_device_from_array(array))
28 |
29 | def __call__(self, volatile='off'):
30 | """Returns the parameter variable.
31 |
32 | Args:
33 | volatile (~chainer.Flag): The volatility of the returned variable.
34 |
35 | Returns:
36 | ~chainer.Variable: A copy of the parameter variable with given
37 | volatility.
38 |
39 | """
40 | # The first identity creates a copy of W, and the second identity cuts
41 | # the edge if volatility is ON
42 | W = identity.identity(self.W)
43 | W.volatile = volatile
44 | return identity.identity(W)
45 |
--------------------------------------------------------------------------------
/examples/imagenet/nin.py:
--------------------------------------------------------------------------------
1 | import chainer
2 | import chainer.functions as F
3 | import chainer.initializers as I
4 | import chainer.links as L
5 |
6 |
7 | class NIN(chainer.Chain):
8 |
9 | """Network-in-Network example model."""
10 |
11 | insize = 227
12 |
13 | def __init__(self):
14 | super(NIN, self).__init__()
15 | conv_init = I.HeNormal() # MSRA scaling
16 |
17 | with self.init_scope():
18 | self.mlpconv1 = L.MLPConvolution2D(
19 | None, (96, 96, 96), 11, stride=4, conv_init=conv_init)
20 | self.mlpconv2 = L.MLPConvolution2D(
21 | None, (256, 256, 256), 5, pad=2, conv_init=conv_init)
22 | self.mlpconv3 = L.MLPConvolution2D(
23 | None, (384, 384, 384), 3, pad=1, conv_init=conv_init)
24 | self.mlpconv4 = L.MLPConvolution2D(
25 | None, (1024, 1024, 1000), 3, pad=1, conv_init=conv_init)
26 |
27 | def __call__(self, x, t):
28 | h = F.max_pooling_2d(F.relu(self.mlpconv1(x)), 3, stride=2)
29 | h = F.max_pooling_2d(F.relu(self.mlpconv2(h)), 3, stride=2)
30 | h = F.max_pooling_2d(F.relu(self.mlpconv3(h)), 3, stride=2)
31 | h = self.mlpconv4(F.dropout(h))
32 | h = F.reshape(F.average_pooling_2d(h, 6), (len(x), 1000))
33 |
34 | loss = F.softmax_cross_entropy(h, t)
35 | chainer.report({'loss': loss, 'accuracy': F.accuracy(h, t)}, self)
36 | return loss
37 |
--------------------------------------------------------------------------------
/chainer/links/loss/crf1d.py:
--------------------------------------------------------------------------------
1 | from chainer.functions.loss import crf1d
2 | from chainer import link
3 | from chainer import variable
4 |
5 |
6 | class CRF1d(link.Link):
7 |
8 | """Linear-chain conditional random field loss layer.
9 |
10 | This link wraps the :func:`~chainer.functions.crf1d` function.
11 | It holds a transition cost matrix as a parameter.
12 |
13 | Args:
14 | n_label (int): Number of labels.
15 |
16 | .. seealso:: :func:`~chainer.functions.crf1d` for more detail.
17 |
18 | Attributes:
19 | cost (~chainer.Variable): Transition cost parameter.
20 | """
21 |
22 | def __init__(self, n_label):
23 | super(CRF1d, self).__init__()
24 | with self.init_scope():
25 | self.cost = variable.Parameter(0, (n_label, n_label))
26 |
27 | def __call__(self, xs, ys, reduce='mean'):
28 | return crf1d.crf1d(self.cost, xs, ys, reduce)
29 |
30 | def argmax(self, xs):
31 | """Computes a state that maximizes a joint probability.
32 |
33 | Args:
34 | xs (list of Variable): Input vector for each label.
35 |
36 | Returns:
37 | tuple: A tuple of :class:`~chainer.Variable` representing each
38 | log-likelihood and a list representing the argmax path.
39 |
40 | .. seealso:: See :func:`~chainer.frunctions.crf1d_argmax` for more
41 | detail.
42 |
43 | """
44 | return crf1d.argmax_crf1d(self.cost, xs)
45 |
--------------------------------------------------------------------------------
/examples/word2vec/search.py:
--------------------------------------------------------------------------------
1 | #!/usr/bin/env python
2 | import numpy
3 | import six
4 |
5 | n_result = 5 # number of search result to show
6 |
7 |
8 | with open('word2vec.model', 'r') as f:
9 | ss = f.readline().split()
10 | n_vocab, n_units = int(ss[0]), int(ss[1])
11 | word2index = {}
12 | index2word = {}
13 | w = numpy.empty((n_vocab, n_units), dtype=numpy.float32)
14 | for i, line in enumerate(f):
15 | ss = line.split()
16 | assert len(ss) == n_units + 1
17 | word = ss[0]
18 | word2index[word] = i
19 | index2word[i] = word
20 | w[i] = numpy.array([float(s) for s in ss[1:]], dtype=numpy.float32)
21 |
22 |
23 | s = numpy.sqrt((w * w).sum(1))
24 | w /= s.reshape((s.shape[0], 1)) # normalize
25 |
26 | try:
27 | while True:
28 | q = six.moves.input('>> ')
29 | if q not in word2index:
30 | print('"{0}" is not found'.format(q))
31 | continue
32 | v = w[word2index[q]]
33 | similarity = w.dot(v)
34 | print('query: {}'.format(q))
35 | count = 0
36 | for i in (-similarity).argsort():
37 | if numpy.isnan(similarity[i]):
38 | continue
39 | if index2word[i] == q:
40 | continue
41 | print('{0}: {1}'.format(index2word[i], similarity[i]))
42 | count += 1
43 | if count == n_result:
44 | break
45 |
46 | except EOFError:
47 | pass
48 |
--------------------------------------------------------------------------------
/tests/chainer_tests/initializer_tests/test_init.py:
--------------------------------------------------------------------------------
1 | import unittest
2 |
3 | from chainer import initializers
4 | from chainer import testing
5 |
6 | import numpy
7 |
8 |
9 | class TestGetInitializer(unittest.TestCase):
10 |
11 | def test_scalar(self):
12 | init = initializers._get_initializer(10)
13 | self.assertIsInstance(init, initializers.Constant)
14 |
15 | x = numpy.empty((2, 3), dtype=numpy.int32)
16 | init(x)
17 |
18 | expected = numpy.full((2, 3), 10, dtype=numpy.int32)
19 | numpy.testing.assert_array_equal(x, expected)
20 |
21 | def test_numpy_array(self):
22 | c = numpy.array([1, 2, 3])
23 | init = initializers._get_initializer(c)
24 |
25 | self.assertIsInstance(init, initializers.Constant)
26 |
27 | x = numpy.empty((3,), dtype=numpy.int32)
28 | init(x)
29 |
30 | expected = numpy.array([1, 2, 3], dtype=numpy.int32)
31 | numpy.testing.assert_array_equal(x, expected)
32 |
33 | def test_callable(self):
34 |
35 | def initializer(arr):
36 | arr[...] = 100
37 |
38 | init = initializers._get_initializer(initializer)
39 | self.assertTrue(callable(init))
40 |
41 | x = numpy.empty((2, 3), dtype=numpy.int32)
42 | init(x)
43 |
44 | expected = numpy.full((2, 3), 100, dtype=numpy.int32)
45 | numpy.testing.assert_array_equal(x, expected)
46 |
47 |
48 | testing.run_module(__name__, __file__)
49 |
--------------------------------------------------------------------------------
/docs/source/reference/debug.rst:
--------------------------------------------------------------------------------
1 | .. _debug:
2 |
3 | Debug mode
4 | ==========
5 |
6 | In debug mode, Chainer checks values of variables on runtime and shows more
7 | detailed error messages.
8 | It helps you to debug your programs.
9 | However, it requires some additional overhead time.
10 |
11 | You can enable debug mode with :func:`chainer.using_config`:
12 |
13 | .. testcode::
14 |
15 | with chainer.using_config('debug', True):
16 | ...
17 |
18 | See :ref:`configuration` for Chainer's configuration mechanism.
19 |
20 | You can also set ``CHAINER_DEBUG`` environment variable to ``1`` to enable this mode.
21 |
22 | In debug mode, Chainer checks all results of forward and backward computation, and if it finds a NaN value, it raises :class:`RuntimeError`.
23 | Some functions and links also check validity of input values.
24 |
25 | You can check if debug mode is enabled with :func:`chainer.is_debug` function.
26 |
27 | .. autosummary::
28 | :toctree: generated/
29 | :nosignatures:
30 |
31 | chainer.is_debug
32 | chainer.set_debug
33 |
34 |
35 | Deprecated interface
36 | --------------------
37 |
38 | As of v2.0.0, it is recommended to turn on the debug mode using ``chainer.config.debug``.
39 | See :ref:`configuration` for the way to use the config object.
40 | We leave the reference of the conventional way (which has been available since Chainer v1) as follows.
41 |
42 |
43 | .. autosummary::
44 | :toctree: generated/
45 | :nosignatures:
46 |
47 | chainer.DebugMode
48 |
--------------------------------------------------------------------------------
/chainer/initializer.py:
--------------------------------------------------------------------------------
1 | import numpy
2 |
3 |
4 | class Initializer(object):
5 | """Initializes array.
6 |
7 | It initializes the given array.
8 |
9 | Attributes:
10 | ~Initializer.dtype: Data type specifier. It is for type check in
11 | ``__call__`` function.
12 |
13 | """
14 |
15 | def __init__(self, dtype=None):
16 | self.dtype = dtype
17 |
18 | def __call__(self, array):
19 | """Initializes given array.
20 |
21 | This method destructively changes the value of array.
22 | The derived class is required to implement this method.
23 | The algorithms used to make the new values depend on the
24 | concrete derived classes.
25 |
26 | Args:
27 | array (numpy.ndarray or cupy.ndarray):
28 | An array to be initialized by this initializer.
29 |
30 | """
31 | raise NotImplementedError()
32 |
33 |
34 | # Original code forked from MIT licensed keras project
35 | # https://github.com/fchollet/keras/blob/master/keras/initializations.py
36 |
37 | def get_fans(shape):
38 | if not isinstance(shape, tuple):
39 | raise ValueError('shape must be tuple')
40 |
41 | if len(shape) < 2:
42 | raise ValueError('shape must be of length >= 2: shape={}', shape)
43 |
44 | receptive_field_size = numpy.prod(shape[2:], dtype=numpy.int32)
45 | fan_in = shape[1] * receptive_field_size
46 | fan_out = shape[0] * receptive_field_size
47 | return fan_in, fan_out
48 |
--------------------------------------------------------------------------------
/chainer/functions/math/fmod.py:
--------------------------------------------------------------------------------
1 | from chainer.backends import cuda
2 | from chainer import function_node
3 | import chainer.functions
4 | from chainer import utils
5 | from chainer.utils import type_check
6 |
7 |
8 | class Fmod(function_node.FunctionNode):
9 |
10 | @property
11 | def label(self):
12 | return 'fmod'
13 |
14 | def check_type_forward(self, in_types):
15 | type_check.expect(in_types.size() == 2)
16 | type_check.expect(
17 | in_types.size() == 2,
18 | in_types[0].dtype == in_types[1].dtype,
19 | in_types[0].dtype.kind == 'f',
20 | in_types[1].dtype.kind == 'f',
21 | )
22 |
23 | def forward(self, inputs):
24 | self.retain_inputs((0, 1))
25 | xp = cuda.get_array_module(*inputs)
26 | x, divisor = inputs
27 | m = xp.fmod(x, divisor)
28 | return utils.force_array(m, x.dtype),
29 |
30 | def backward(self, indexes, grad_outputs):
31 | x, divisor = self.get_retained_inputs()
32 | gw, = grad_outputs
33 | return gw, - chainer.functions.fix(x / divisor) * gw
34 |
35 |
36 | def fmod(x, divisor):
37 | """Elementwise mod function.
38 |
39 | .. math::
40 | y_i = x_i \\bmod \\mathrm{divisor}.
41 |
42 | Args:
43 | x (~chainer.Variable): Input variable.
44 | divisor (~chainer.Variable): Input divisor.
45 | Returns:
46 | ~chainer.Variable: Output variable.
47 | """
48 | return Fmod().apply((x, divisor))[0]
49 |
--------------------------------------------------------------------------------
/chainer/functions/noise/gumbel_softmax.py:
--------------------------------------------------------------------------------
1 | from chainer.backends import cuda
2 | import chainer.functions
3 | from chainer import variable
4 |
5 |
6 | def gumbel_softmax(log_pi, tau=0.1, axis=1):
7 | """Gumbel-Softmax sampling function.
8 |
9 | This function draws samples :math:`y_i` from Gumbel-Softmax distribution,
10 |
11 | .. math::
12 | y_i = {\\exp((g_i + \\log\\pi_i)/\\tau)
13 | \\over \\sum_{j}\\exp((g_j + \\log\\pi_j)/\\tau)},
14 |
15 | where :math:`\\tau` is a temperature parameter and
16 | :math:`g_i` s are samples drawn from
17 | Gumbel distribution :math:`Gumbel(0, 1)`
18 |
19 | See `Categorical Reparameterization with Gumbel-Softmax \
20 | `_.
21 |
22 | Args:
23 | log_pi (:class:`~chainer.Variable` or :class:`numpy.ndarray` or \
24 | :class:`cupy.ndarray`): Input variable representing pre-normalized
25 | log-probability :math:`\\log\\pi`.
26 | tau (:class:`~float` or :class:`~chainer.Variable`): \
27 | Input variable representing temperature :math:`\\tau`.
28 |
29 | Returns:
30 | ~chainer.Variable: Output variable.
31 |
32 | """
33 | xp = cuda.get_array_module(log_pi)
34 | if log_pi.ndim < 1:
35 | return variable.Variable(xp.ones((), log_pi.dtype))
36 | dtype = log_pi.dtype
37 | g = xp.random.gumbel(size=log_pi.shape).astype(dtype)
38 | y = chainer.functions.softmax((log_pi + g) / tau, axis=axis)
39 |
40 | return y
41 |
--------------------------------------------------------------------------------
/chainer/functions/math/squared_difference.py:
--------------------------------------------------------------------------------
1 | from chainer.backends import cuda
2 | from chainer import function_node
3 | from chainer import utils
4 | from chainer.utils import type_check
5 |
6 |
7 | class SquaredDifference(function_node.FunctionNode):
8 | """Squared difference of input variables."""
9 |
10 | def check_type_forward(self, in_types):
11 | type_check.expect(in_types.size() == 2)
12 | type_check.expect(
13 | in_types[0].dtype.kind == 'f',
14 | in_types[0].dtype == in_types[1].dtype,
15 | in_types[0].shape == in_types[1].shape
16 | )
17 |
18 | def forward(self, inputs):
19 | self.retain_inputs((0, 1))
20 | xp = cuda.get_array_module(*inputs)
21 | x1, x2 = inputs
22 | difference = x1 - x2
23 | y = xp.square(difference)
24 | return utils.force_array(y, dtype=x1.dtype),
25 |
26 | def backward(self, indexes, grads):
27 | gy, = grads
28 | x1, x2 = self.get_retained_inputs()
29 | difference = x1 - x2
30 | gx = gy * 2 * difference
31 | return gx, -gx
32 |
33 |
34 | def squared_difference(x1, x2):
35 | """Squared difference of input variables.
36 |
37 | Args:
38 | x1 (~chainer.Variable): Input variables to be compared.
39 | x2 (~chainer.Variable): Input variables to be compared.
40 |
41 | Returns:
42 | ~chainer.Variable: ``(x1 - x2) ** 2`` element-wise.
43 | """
44 | return SquaredDifference().apply((x1, x2))[0]
45 |
--------------------------------------------------------------------------------
/chainer/datasets/dict_dataset.py:
--------------------------------------------------------------------------------
1 | import six
2 |
3 |
4 | class DictDataset(object):
5 |
6 | """Dataset of a dictionary of datasets.
7 |
8 | It combines multiple datasets into one dataset. Each example is represented
9 | by a dictionary mapping a key to an example of the corresponding dataset.
10 |
11 | Args:
12 | datasets: Underlying datasets. The keys are used as the keys of each
13 | example. All datasets must have the same length.
14 |
15 | """
16 |
17 | def __init__(self, **datasets):
18 | if not datasets:
19 | raise ValueError('no datasets are given')
20 | length = None
21 | for key, dataset in six.iteritems(datasets):
22 | if length is None:
23 | length = len(dataset)
24 | elif length != len(dataset):
25 | raise ValueError(
26 | 'dataset length conflicts at "{}"'.format(key))
27 | self._datasets = datasets
28 | self._length = length
29 |
30 | def __getitem__(self, index):
31 | batches = {key: dataset[index]
32 | for key, dataset in six.iteritems(self._datasets)}
33 | if isinstance(index, slice):
34 | length = len(six.next(six.itervalues(batches)))
35 | return [{key: batch[i] for key, batch in six.iteritems(batches)}
36 | for i in six.moves.range(length)]
37 | else:
38 | return batches
39 |
40 | def __len__(self):
41 | return self._length
42 |
--------------------------------------------------------------------------------
/docs/source/guides/trainer.rst:
--------------------------------------------------------------------------------
1 | Trainer
2 | ~~~~~~~
3 |
4 | When we want to train neural networks, we have to run *training loops* that update the parameters many times.
5 | A typical training loop consists of the following procedures:
6 |
7 | 1. Iterations over training datasets
8 | 2. Preprocessing of extracted mini-batches
9 | 3. Forward/backward computations of the neural networks
10 | 4. Parameter updates
11 | 5. Evaluations of the current parameters on validation datasets
12 | 6. Logging and printing of the intermediate results
13 |
14 | Chainer provides a simple yet powerful way to make it easy to write such training processes.
15 | The training loop abstraction mainly consists of two components:
16 |
17 | - **Dataset abstraction**.
18 | It implements 1 and 2 in the above list.
19 | The core components are defined in the :mod:`~chainer.dataset` module.
20 | There are also many implementations of datasets and iterators in :mod:`~chainer.datasets` and :mod:`~chainer.iterators` modules, respectively.
21 | - **Trainer**.
22 | It implements 3, 4, 5, and 6 in the above list.
23 | The whole procedure is implemented by :class:`~training.Trainer`.
24 | The way to update parameters (3 and 4) is defined by :class:`~training.Updater`, which can be freely customized.
25 | 5 and 6 are implemented by instances of :class:`~training.Extension`, which appends an extra procedure to the training loop.
26 | Users can freely customize the training procedure by adding extensions. Users can also implement their own extensions.
27 |
28 |
--------------------------------------------------------------------------------
/chainer/testing/__init__.py:
--------------------------------------------------------------------------------
1 | from chainer.testing import array # NOQA
2 | from chainer.testing import helper # NOQA
3 | from chainer.testing import parameterized # NOQA
4 | from chainer.testing import serializer # NOQA
5 | from chainer.testing import training # NOQA
6 | from chainer.testing import unary_math_function_test # NOQA
7 |
8 | from chainer.testing.array import assert_allclose # NOQA
9 | from chainer.testing.helper import assert_warns # NOQA
10 | from chainer.testing.helper import with_requires # NOQA
11 | from chainer.testing.parameterized import parameterize # NOQA
12 | from chainer.testing.parameterized import product # NOQA
13 | from chainer.testing.parameterized import product_dict # NOQA
14 | from chainer.testing.random import fix_random # NOQA
15 | from chainer.testing.random import generate_seed # NOQA
16 | from chainer.testing.serializer import save_and_load # NOQA
17 | from chainer.testing.serializer import save_and_load_hdf5 # NOQA
18 | from chainer.testing.serializer import save_and_load_npz # NOQA
19 | from chainer.testing.training import get_trainer_with_mock_updater # NOQA
20 | from chainer.testing.unary_math_function_test import unary_math_function_unittest # NOQA
21 |
22 |
23 | def run_module(name, file):
24 | """Run current test cases of the file.
25 |
26 | Args:
27 | name: __name__ attribute of the file.
28 | file: __file__ attribute of the file.
29 | """
30 |
31 | if name == '__main__':
32 | import pytest
33 | pytest.main([file, '-vvs', '-x', '--pdb'])
34 |
--------------------------------------------------------------------------------
/examples/image_captioning/download.py:
--------------------------------------------------------------------------------
1 | #!/usr/bin/env python
2 | import argparse
3 | import os
4 | from six.moves.urllib import request
5 | import zipfile
6 |
7 |
8 | """Download the MSCOCO dataset (images and captions)."""
9 |
10 |
11 | urls = [
12 | 'http://images.cocodataset.org/zips/train2014.zip',
13 | 'http://images.cocodataset.org/zips/val2014.zip',
14 | 'http://images.cocodataset.org/annotations/annotations_trainval2014.zip'
15 | ]
16 |
17 |
18 | if __name__ == '__main__':
19 | parser = argparse.ArgumentParser()
20 | parser.add_argument('--out', type=str, default='data',
21 | help='Target MSOCO dataset root directory')
22 | args = parser.parse_args()
23 |
24 | try:
25 | os.makedirs(args.out)
26 | except OSError:
27 | raise OSError(
28 | "'{}' already exists, delete it and try again".format(args.out))
29 |
30 | for url in urls:
31 | print('Downloading {}...'.format(url))
32 |
33 | # Download the zip file
34 | file_name = os.path.basename(url)
35 | dst_file_path = os.path.join(args.out, file_name)
36 | request.urlretrieve(url, dst_file_path)
37 |
38 | # Unzip the file
39 | zf = zipfile.ZipFile(dst_file_path)
40 | for name in zf.namelist():
41 | dirname, filename = os.path.split(name)
42 | if not filename == '':
43 | zf.extract(name, args.out)
44 |
45 | # Remove the zip file since it has been extracted
46 | os.remove(dst_file_path)
47 |
--------------------------------------------------------------------------------
/chainer/_environment_check.py:
--------------------------------------------------------------------------------
1 | import os
2 | import sys
3 | import warnings
4 |
5 | import numpy.distutils.system_info
6 |
7 |
8 | def _check_python_350():
9 | if sys.version_info[:3] == (3, 5, 0):
10 | if not int(os.getenv('CHAINER_PYTHON_350_FORCE', '0')):
11 | msg = """
12 | Chainer does not work with Python 3.5.0.
13 |
14 | We strongly recommend to use another version of Python.
15 | If you want to use Chainer with Python 3.5.0 at your own risk,
16 | set 1 to CHAINER_PYTHON_350_FORCE environment variable."""
17 |
18 | raise Exception(msg)
19 |
20 |
21 | def _check_osx_numpy_backend():
22 | if sys.platform != 'darwin':
23 | return
24 |
25 | blas_opt_info = numpy.distutils.system_info.get_info('blas_opt')
26 | if blas_opt_info:
27 | extra_link_args = blas_opt_info.get('extra_link_args')
28 | if extra_link_args and '-Wl,Accelerate' in extra_link_args:
29 | warnings.warn('''\
30 | Accelerate has been detected as a NumPy backend library.
31 | vecLib, which is a part of Accelerate, is known not to work correctly with Chainer.
32 | We recommend using other BLAS libraries such as OpenBLAS.
33 | For details of the issue, please see
34 | https://docs.chainer.org/en/stable/tips.html#mnist-example-does-not-converge-in-cpu-mode-on-mac-os-x.
35 |
36 | Also note that Chainer does not officially support Mac OS X.
37 | Please use it at your own risk.
38 | ''') # NOQA
39 |
40 |
41 | def check():
42 | _check_python_350()
43 | _check_osx_numpy_backend()
44 |
--------------------------------------------------------------------------------
/chainer/functions/pooling/average_pooling_nd_kernel.py:
--------------------------------------------------------------------------------
1 | from chainer.functions.pooling import pooling_nd_kernel
2 |
3 |
4 | class AveragePoolingNDKernelForward(pooling_nd_kernel.PoolingNDKernelForward):
5 |
6 | def name(self):
7 | # avg_pool_{N}d_fwd
8 | return 'avg'
9 |
10 | def in_params(self):
11 | # 2D: raw T in, int32 d_0, int32 d_1, int32 out_0, int32 out_1,
12 | # int32 k_0, int32 k_1, int32 s_0, int32 s_1, int32 p_0,
13 | # int32 p_1, T coeff
14 | return ['T coeff']
15 |
16 | def before(self):
17 | return 'T val = 0;'
18 |
19 | def main(self, offset, xs):
20 | # 2D: val = val + in[offset_1];
21 | return 'val = val + in[{}];'.format(offset)
22 |
23 | def after(self, out_xs):
24 | return 'out = val * coeff;'
25 |
26 |
27 | class AveragePoolingNDKernelBackward(
28 | pooling_nd_kernel.PoolingNDKernelBackward):
29 |
30 | def name(self):
31 | # avg_pool_{N}d_bwd
32 | return 'avg'
33 |
34 | def in_params(self):
35 | # 2D: raw T gy, int32 d_0, int32 d_1, int32 out_0, int32 out_1,
36 | # int32 k_0, int32 k_1, int32 s_0, int32 s_1, int32 p_0,
37 | # int32 p_1, T coeff
38 | return ['T coeff']
39 |
40 | def before(self):
41 | return 'T val = 0;'
42 |
43 | def main(self, offset, xs, out_xs):
44 | # 2D: val = val + gy[offset_1];
45 | return 'val = val + gy[{}];'.format(offset)
46 |
47 | def after(self, xs):
48 | return 'gx = val * coeff;'
49 |
--------------------------------------------------------------------------------
/docs/source/reference/graph.rst:
--------------------------------------------------------------------------------
1 | Visualization of Computational Graph
2 | ====================================
3 |
4 | .. module:: chainer.computational_graph
5 |
6 | As neural networks get larger and complicated, it gets much harder to confirm if their architectures are constructed properly.
7 | Chainer supports visualization of computational graphs.
8 | Users can generate computational graphs by invoking :meth:`build_computational_graph`. Generated computational graphs are dumped to specified format (Currently `Dot Language `_ is supported).
9 |
10 | Basic usage is as follows::
11 |
12 | import chainer.computational_graph as c
13 | ...
14 | g = c.build_computational_graph(vs)
15 | with open('path/to/output/file', 'w') as o:
16 | o.write(g.dump())
17 |
18 | where ``vs`` is list of :class:`Variable` instances and ``g`` is an instance of :class:`ComputationalGraph`.
19 | This code generates the computational graph that are backward-reachable (i.e. reachable by repetition of steps backward) from at least one of ``vs``.
20 |
21 | Here is an example of (a part of) the generated graph (inception(3a) in `GoogLeNet `_). This example is from ``example/imagenet``.
22 |
23 | .. figure:: ../../image/googlenet.png
24 | :scale: 60%
25 | :align: center
26 |
27 | .. autosummary::
28 | :toctree: generated/
29 | :nosignatures:
30 |
31 | chainer.computational_graph.build_computational_graph
32 | chainer.computational_graph.ComputationalGraph
33 |
--------------------------------------------------------------------------------
/tests/chainer_tests/utils_tests/test_conv_nd_kernel.py:
--------------------------------------------------------------------------------
1 | import unittest
2 |
3 | import mock
4 |
5 | import chainer
6 | from chainer import testing
7 | from chainer.testing import attr
8 | from chainer.utils import conv_nd_kernel
9 |
10 |
11 | @testing.parameterize(*testing.product({
12 | 'ndim': [2, 3, 4],
13 | }))
14 | @attr.gpu
15 | class TestIm2colNDKernelMemo(unittest.TestCase):
16 |
17 | def setUp(self):
18 | chainer.backends.cuda.clear_memo()
19 |
20 | def test_im2col_nd_kernel_memo(self):
21 | ndim = self.ndim
22 | with mock.patch(
23 | 'chainer.utils.conv_nd_kernel.Im2colNDKernel._generate') as m:
24 | conv_nd_kernel.Im2colNDKernel.generate(ndim)
25 | m.assert_called_once_with(ndim)
26 | conv_nd_kernel.Im2colNDKernel.generate(ndim)
27 | m.assert_called_once_with(ndim)
28 |
29 |
30 | @testing.parameterize(*testing.product({
31 | 'ndim': [2, 3, 4],
32 | }))
33 | @attr.gpu
34 | class TestCol2imNDKernelMemo(unittest.TestCase):
35 |
36 | def setUp(self):
37 | chainer.backends.cuda.clear_memo()
38 |
39 | def test_col2im_nd_kernel_memo(self):
40 | ndim = self.ndim
41 | with mock.patch(
42 | 'chainer.utils.conv_nd_kernel.Col2imNDKernel._generate') as m:
43 | conv_nd_kernel.Col2imNDKernel.generate(ndim)
44 | m.assert_called_once_with(ndim)
45 | conv_nd_kernel.Col2imNDKernel.generate(ndim)
46 | m.assert_called_once_with(ndim)
47 |
48 |
49 | testing.run_module(__name__, __file__)
50 |
--------------------------------------------------------------------------------
/tests/chainer_tests/training_tests/extensions_tests/test_plot_report.py:
--------------------------------------------------------------------------------
1 | import unittest
2 | import warnings
3 |
4 | from chainer import testing
5 | from chainer.training import extensions
6 |
7 |
8 | class TestPlotReport(unittest.TestCase):
9 |
10 | def test_available(self):
11 | try:
12 | import matplotlib # NOQA
13 | available = True
14 | except ImportError:
15 | available = False
16 |
17 | with warnings.catch_warnings(record=True) as w:
18 | self.assertEqual(extensions.PlotReport.available(), available)
19 |
20 | # It shows warning only when matplotlib is not available
21 | if available:
22 | self.assertEqual(len(w), 0)
23 | else:
24 | self.assertEqual(len(w), 1)
25 |
26 | # In the following we explicitly use plot_report._available instead of
27 | # PlotReport.available() because in some cases `test_available()` fails
28 | # because it sometimes does not raise UserWarning despite
29 | # matplotlib is not installed (this is due to the difference between
30 | # the behavior of unittest in python2 and that in python3).
31 | @unittest.skipUnless(
32 | extensions.plot_report._available, 'matplotlib is not installed')
33 | def test_lazy_import(self):
34 | # To support python2, we do not use self.assertWarns()
35 | with warnings.catch_warnings(record=True) as w:
36 | import matplotlib
37 | matplotlib.use('Agg')
38 |
39 | self.assertEqual(len(w), 0)
40 |
41 |
42 | testing.run_module(__name__, __file__)
43 |
--------------------------------------------------------------------------------
/tests/chainer_tests/functions_tests/array_tests/test_transpose.py:
--------------------------------------------------------------------------------
1 | import unittest
2 |
3 | import numpy
4 |
5 | import chainer
6 | from chainer.backends import cuda
7 | from chainer import functions
8 | from chainer import testing
9 | from chainer.testing import attr
10 |
11 |
12 | @testing.parameterize(*testing.product({
13 | 'in_shape': [(4, 3, 2)],
14 | 'axes': [(-1, 0, 1), None],
15 | 'dtype': [numpy.float16, numpy.float32, numpy.float32],
16 | }))
17 | class TestTranspose(unittest.TestCase):
18 |
19 | def setUp(self):
20 | self.x = numpy.random.uniform(-1, 1, self.in_shape).astype(self.dtype)
21 |
22 | def check_forward(self, x_data):
23 | axes = self.axes
24 | x = chainer.Variable(x_data)
25 | y = functions.transpose(x, axes)
26 | self.assertEqual(y.data.dtype, self.dtype)
27 | self.assertTrue((self.x.transpose(axes) == cuda.to_cpu(y.data)).all())
28 |
29 | def test_forward_cpu(self):
30 | self.check_forward(self.x)
31 |
32 | @attr.gpu
33 | def test_forward_gpu(self):
34 | self.check_forward(cuda.to_gpu(self.x))
35 |
36 | def check_backward(self, x_data):
37 | x = chainer.Variable(x_data)
38 | y = functions.transpose(x, self.axes)
39 | y.grad = y.data
40 | y.backward()
41 | testing.assert_allclose(x.data, x.grad, atol=0, rtol=0)
42 |
43 | def test_backward_cpu(self):
44 | self.check_backward(self.x)
45 |
46 | @attr.gpu
47 | def test_backward_gpu(self):
48 | self.check_backward(cuda.to_gpu(self.x))
49 |
50 |
51 | testing.run_module(__name__, __file__)
52 |
--------------------------------------------------------------------------------
/chainer/functions/math/bias.py:
--------------------------------------------------------------------------------
1 | import chainer
2 | from chainer.functions.array import broadcast
3 | from chainer.functions.array import reshape
4 |
5 |
6 | def bias(x, y, axis=1):
7 | """Elementwise summation with broadcasting.
8 |
9 | Computes a elementwise summation of two input variables, with the shape of
10 | the latter variable broadcasted to match the shape of the former. ``axis``
11 | is the first axis of the first variable along which the second variable is
12 | applied.
13 |
14 | The term "broadcasting" here comes from Caffe's bias layer so the
15 | "broadcasting" with the following arguments::
16 |
17 | x : 100 x 3 x 40 x 60
18 | y : 3 x 40
19 | axis : 1
20 |
21 | is equivalent to the following numpy broadcasting::
22 |
23 | x : 100 x 3 x 40 x 60
24 | y : 1 x 3 x 40 x 1
25 |
26 | Note that how the ``axis`` indicates to which axis of ``x`` we apply ``y``.
27 |
28 | Args:
29 | x (~chainer.Variable): Input variable to be summed.
30 | y (~chainer.Variable): Input variable to sum, broadcasted.
31 | axis (int): The first axis of ``x`` along which ``y`` is applied.
32 |
33 | Returns:
34 | ~chainer.Variable: Output variable.
35 |
36 | """
37 | x_shape = x.shape
38 | y_shape = y.shape
39 | if chainer.is_debug():
40 | assert x_shape[axis:axis + len(y_shape)] == y_shape
41 | y1_shape = tuple([1] * axis + list(y_shape) +
42 | [1] * (len(x_shape) - axis - len(y_shape)))
43 | y1 = reshape.reshape(y, y1_shape)
44 | y2 = broadcast.broadcast_to(y1, x_shape)
45 | return x + y2
46 |
--------------------------------------------------------------------------------
/chainer/functions/math/scale.py:
--------------------------------------------------------------------------------
1 | import chainer
2 | from chainer.functions.array import broadcast
3 | from chainer.functions.array import reshape
4 |
5 |
6 | def scale(x, y, axis=1):
7 | """Elementwise product with broadcasting.
8 |
9 | Computes a elementwise product of two input variables, with the shape of
10 | the latter variable broadcasted to match the shape of the former. ``axis``
11 | is the first axis of the first variable along which the second variable is
12 | applied.
13 |
14 | The term "broadcasting" here comes from Caffe's scale layer so the
15 | "broadcasting" with the following arguments::
16 |
17 | x : 100 x 3 x 40 x 60
18 | y : 3 x 40
19 | axis : 1
20 |
21 | is equivalent to the following numpy broadcasting::
22 |
23 | x : 100 x 3 x 40 x 60
24 | y : 1 x 3 x 40 x 1
25 |
26 | Note that how the ``axis`` indicates to which axis of ``x`` we apply ``y``.
27 |
28 | Args:
29 | x (~chainer.Variable): Input variable to be scaled.
30 | y (~chainer.Variable): Input variable to scale, broadcasted.
31 | axis (int): The first axis of ``x`` along which ``y`` is applied.
32 |
33 | Returns:
34 | ~chainer.Variable: Output variable.
35 |
36 | """
37 | x_shape = x.shape
38 | y_shape = y.shape
39 | if chainer.is_debug():
40 | assert x_shape[axis:axis + len(y_shape)] == y_shape
41 | y1_shape = tuple([1] * axis + list(y_shape) +
42 | [1] * (len(x_shape) - axis - len(y_shape)))
43 | y1 = reshape.reshape(y, y1_shape)
44 | y2 = broadcast.broadcast_to(y1, x_shape)
45 | return x * y2
46 |
--------------------------------------------------------------------------------
/chainer/functions/loss/mean_absolute_error.py:
--------------------------------------------------------------------------------
1 | import numpy
2 |
3 | import chainer
4 | from chainer.backends import cuda
5 | from chainer import function_node
6 | from chainer.utils import type_check
7 |
8 |
9 | class MeanAbsoluteError(function_node.FunctionNode):
10 |
11 | """Mean absolute error function."""
12 |
13 | def check_type_forward(self, in_types):
14 | type_check.expect(in_types.size() == 2)
15 | type_check.expect(
16 | in_types[0].dtype == numpy.float32,
17 | in_types[1].dtype == numpy.float32,
18 | in_types[0].shape == in_types[1].shape
19 | )
20 |
21 | def forward_cpu(self, inputs):
22 | x0, x1 = inputs
23 | self.diff = x0 - x1
24 | diff = self.diff.ravel()
25 | return numpy.array(abs(diff).sum() / diff.size, dtype=diff.dtype),
26 |
27 | def forward_gpu(self, inputs):
28 | x0, x1 = inputs
29 | self.diff = x0 - x1
30 | diff = self.diff.ravel()
31 | return abs(diff).sum() / diff.dtype.type(diff.size),
32 |
33 | def backward(self, indexes, grad_outputs):
34 | gy, = grad_outputs
35 | coeff = gy * gy.data.dtype.type(1. / self.diff.size)
36 | coeff = chainer.functions.broadcast_to(coeff, self.diff.shape)
37 | gx0 = coeff * cuda.get_array_module(gy.data).sign(self.diff)
38 | return gx0, -gx0
39 |
40 |
41 | def mean_absolute_error(x0, x1):
42 | """Mean absolute error function.
43 |
44 | This function computes mean absolute error between two variables. The mean
45 | is taken over the minibatch.
46 |
47 | """
48 | return MeanAbsoluteError().apply((x0, x1))[0]
49 |
--------------------------------------------------------------------------------
/chainer/testing/helper.py:
--------------------------------------------------------------------------------
1 | import contextlib
2 | import pkg_resources
3 | import sys
4 | import unittest
5 | import warnings
6 |
7 |
8 | def with_requires(*requirements):
9 | """Run a test case only when given requirements are satisfied.
10 |
11 | .. admonition:: Example
12 |
13 | This test case runs only when `numpy>=1.10` is installed.
14 |
15 | >>> from chainer import testing
16 | ... class Test(unittest.TestCase):
17 | ... @testing.with_requires('numpy>=1.10')
18 | ... def test_for_numpy_1_10(self):
19 | ... pass
20 |
21 | Args:
22 | requirements: A list of string representing requirement condition to
23 | run a given test case.
24 |
25 | """
26 | ws = pkg_resources.WorkingSet()
27 | try:
28 | ws.require(*requirements)
29 | skip = False
30 | except pkg_resources.ResolutionError:
31 | skip = True
32 |
33 | msg = 'requires: {}'.format(','.join(requirements))
34 | return unittest.skipIf(skip, msg)
35 |
36 |
37 | @contextlib.contextmanager
38 | def assert_warns(expected):
39 | with warnings.catch_warnings(record=True) as w:
40 | warnings.simplefilter('always')
41 | yield
42 |
43 | # Python 2 does not raise warnings multiple times from the same stack
44 | # frame.
45 | if sys.version_info >= (3, 0):
46 | if not any(isinstance(m.message, expected) for m in w):
47 | try:
48 | exc_name = expected.__name__
49 | except AttributeError:
50 | exc_name = str(expected)
51 |
52 | raise AssertionError('%s not triggerred' % exc_name)
53 |
--------------------------------------------------------------------------------
/examples/modelzoo/download_model.py:
--------------------------------------------------------------------------------
1 | #!/usr/bin/env python
2 | from __future__ import print_function
3 | import argparse
4 | import zipfile
5 |
6 | import six
7 |
8 |
9 | parser = argparse.ArgumentParser(
10 | description='Download a Caffe reference model')
11 | parser.add_argument('model_type',
12 | choices=('alexnet', 'caffenet', 'googlenet', 'resnet'),
13 | help='Model type (alexnet, caffenet, googlenet)')
14 | args = parser.parse_args()
15 |
16 | if args.model_type == 'alexnet':
17 | url = 'http://dl.caffe.berkeleyvision.org/bvlc_alexnet.caffemodel'
18 | name = 'bvlc_alexnet.caffemodel'
19 | elif args.model_type == 'caffenet':
20 | url = 'http://dl.caffe.berkeleyvision.org/' \
21 | 'bvlc_reference_caffenet.caffemodel'
22 | name = 'bvlc_reference_caffenet.caffemodel'
23 | elif args.model_type == 'googlenet':
24 | url = 'http://dl.caffe.berkeleyvision.org/bvlc_googlenet.caffemodel'
25 | name = 'bvlc_googlenet.caffemodel'
26 | elif args.model_type == 'resnet':
27 | raise RuntimeError('The resnet model file cannot be downloaded '
28 | 'automatically. Please download manually: '
29 | 'https://github.com/KaimingHe/deep-residual-networks'
30 | '#models')
31 | else:
32 | raise RuntimeError('Invalid model type. Choose from '
33 | 'alexnet, caffenet and googlenet.')
34 |
35 | print('Downloading model file...')
36 | six.moves.urllib.request.urlretrieve(url, name)
37 |
38 | if args.model_type == 'resnet':
39 | with zipfile.ZipFile(name, 'r') as zf:
40 | zf.extractall('.')
41 |
42 | print('Done')
43 |
--------------------------------------------------------------------------------
/examples/dcgan/updater.py:
--------------------------------------------------------------------------------
1 | #!/usr/bin/env python
2 |
3 | from __future__ import print_function
4 |
5 | import chainer
6 | import chainer.functions as F
7 | from chainer import Variable
8 |
9 |
10 | class DCGANUpdater(chainer.training.updaters.StandardUpdater):
11 |
12 | def __init__(self, *args, **kwargs):
13 | self.gen, self.dis = kwargs.pop('models')
14 | super(DCGANUpdater, self).__init__(*args, **kwargs)
15 |
16 | def loss_dis(self, dis, y_fake, y_real):
17 | batchsize = len(y_fake)
18 | L1 = F.sum(F.softplus(-y_real)) / batchsize
19 | L2 = F.sum(F.softplus(y_fake)) / batchsize
20 | loss = L1 + L2
21 | chainer.report({'loss': loss}, dis)
22 | return loss
23 |
24 | def loss_gen(self, gen, y_fake):
25 | batchsize = len(y_fake)
26 | loss = F.sum(F.softplus(-y_fake)) / batchsize
27 | chainer.report({'loss': loss}, gen)
28 | return loss
29 |
30 | def update_core(self):
31 | gen_optimizer = self.get_optimizer('gen')
32 | dis_optimizer = self.get_optimizer('dis')
33 |
34 | batch = self.get_iterator('main').next()
35 | x_real = Variable(self.converter(batch, self.device)) / 255.
36 | xp = chainer.backends.cuda.get_array_module(x_real.data)
37 |
38 | gen, dis = self.gen, self.dis
39 | batchsize = len(batch)
40 |
41 | y_real = dis(x_real)
42 |
43 | z = Variable(xp.asarray(gen.make_hidden(batchsize)))
44 | x_fake = gen(z)
45 | y_fake = dis(x_fake)
46 |
47 | dis_optimizer.update(self.loss_dis, dis, y_fake, y_real)
48 | gen_optimizer.update(self.loss_gen, gen, y_fake)
49 |
--------------------------------------------------------------------------------
/docs/source/guides/define_by_run.rst:
--------------------------------------------------------------------------------
1 | Define-by-Run
2 | -------------
3 |
4 | .. currentmodule:: chainer
5 |
6 | As mentioned on the top page, Chainer is a flexible framework for neural networks.
7 | One major goal is flexibility, so it must enable us to write complex architectures simply and intuitively.
8 |
9 | Most existing deep learning frameworks are based on the **"Define-and-Run"** scheme.
10 | That is, first a network is defined and fixed, and then the user periodically feeds it with mini-batches of training data.
11 | Since the network is statically defined before any forward/backward computation, all the logic must be embedded into the network architecture as *data*.
12 | Consequently, defining a network architecture in such systems (e.g. Caffe) follows a declarative approach.
13 | Note that one can still produce such a static network definition using imperative languages (e.g. torch.nn, Theano-based frameworks, and TensorFlow).
14 |
15 | In contrast, Chainer adopts a **"Define-by-Run"** scheme, i.e., the network is defined dynamically via the actual forward computation.
16 | More precisely, Chainer stores the history of computation instead of programming logic.
17 | This strategy enables us to fully leverage the power of programming logic in Python.
18 | For example, Chainer does not need any magic to introduce conditionals and loops into the network definitions.
19 | The Define-by-Run scheme is the core concept of Chainer.
20 | We will show in this tutorial how to define networks dynamically.
21 |
22 | This strategy also makes it easy to write multi-GPU parallelization, since logic comes closer to network manipulation.
23 | We will review such amenities in later sections of this tutorial.
24 |
--------------------------------------------------------------------------------
/chainer/functions/array/cast.py:
--------------------------------------------------------------------------------
1 | import chainer
2 | from chainer import function_node
3 | from chainer.utils import type_check
4 |
5 |
6 | class Cast(function_node.FunctionNode):
7 |
8 | """Cast function."""
9 |
10 | def __init__(self, typ):
11 | self.type = typ
12 |
13 | def check_type_forward(self, in_types):
14 | type_check.expect(in_types.size() == 1)
15 | x_type = in_types[0]
16 |
17 | type_check.expect(x_type.dtype.kind == 'f')
18 |
19 | def forward(self, x):
20 | self._in_type = x[0].dtype.type
21 | return x[0].astype(self.type, copy=False),
22 |
23 | def backward(self, indexes, g):
24 | return cast(g[0], self._in_type),
25 |
26 |
27 | def cast(x, typ):
28 | """Cast an input variable to a given type.
29 |
30 | Args:
31 | x (:class:`~chainer.Variable` or :class:`numpy.ndarray` or \
32 | :class:`cupy.ndarray`):
33 | Input variable to be casted. A \
34 | :math:`(s_1, s_2, ..., s_N)`-shaped float array.
35 | typ (:class:`str` of dtype or :class:`numpy.dtype`):
36 | Typecode or data type to cast.
37 |
38 | Returns:
39 | ~chainer.Variable: Variable holding a casted array.
40 |
41 | .. admonition:: Example
42 |
43 | >>> x = np.arange(0, 3, dtype=np.float64)
44 | >>> x.dtype
45 | dtype('float64')
46 | >>> y = F.cast(x, np.float32)
47 | >>> y.dtype
48 | dtype('float32')
49 | >>> y = F.cast(x, 'float16')
50 | >>> y.dtype
51 | dtype('float16')
52 |
53 | """
54 | if x.dtype == typ:
55 | return chainer.as_variable(x)
56 | return Cast(typ).apply((x,))[0]
57 |
--------------------------------------------------------------------------------
/chainer/functions/math/sqrt.py:
--------------------------------------------------------------------------------
1 | from chainer.backends import cuda
2 | from chainer import function_node
3 | from chainer import utils
4 | from chainer.utils import type_check
5 |
6 |
7 | class Sqrt(function_node.FunctionNode):
8 |
9 | @property
10 | def label(self):
11 | return 'sqrt'
12 |
13 | def check_type_forward(self, in_types):
14 | type_check.expect(
15 | in_types.size() == 1,
16 | in_types[0].dtype.kind == 'f',
17 | )
18 |
19 | def forward(self, x):
20 | self.retain_outputs((0,))
21 | xp = cuda.get_array_module(*x)
22 | return utils.force_array(xp.sqrt(x[0], dtype=x[0].dtype)),
23 |
24 | def backward(self, indexes, grad_outputs):
25 | gx = self.get_retained_outputs()[0]
26 | gy = grad_outputs[0]
27 | return gy / (gx * 2.0),
28 |
29 |
30 | def sqrt(x):
31 | """Elementwise square root function.
32 |
33 | .. math::
34 | y_i = \\sqrt x_i.
35 |
36 | If the value of :math:`x_i` is negative, it returns ``Nan`` for :math:`y_i`
37 | respect to underlying numpy and cupy specification.
38 |
39 | Args:
40 | x (~chainer.Variable): Input variable.
41 |
42 | Returns:
43 | ~chainer.Variable: Output variable.
44 | """
45 | return Sqrt().apply((x,))[0]
46 |
47 |
48 | def rsqrt(x):
49 | """Computes elementwise reciprocal of square root of input :math:`x_i`.
50 |
51 | .. math::
52 | y_i = {1 \\over \\sqrt x_i}.
53 |
54 | Args:
55 | x (~chainer.Variable): Input variable.
56 |
57 | Returns:
58 | ~chainer.Variable: Output variable.
59 |
60 | .. seealso:: :func:`~chainer.functions.sqrt`
61 | """
62 | return 1.0 / sqrt(x)
63 |
--------------------------------------------------------------------------------
/tests/chainer_tests/functions_tests/array_tests/test_flatten.py:
--------------------------------------------------------------------------------
1 | import unittest
2 |
3 | import numpy
4 |
5 | import chainer
6 | from chainer.backends import cuda
7 | from chainer import functions
8 | from chainer import gradient_check
9 | from chainer import testing
10 | from chainer.testing import attr
11 |
12 |
13 | @testing.parameterize(*testing.product({
14 | 'shape': [(3, 4), ()],
15 | 'dtype': [numpy.float16, numpy.float32, numpy.float64],
16 | }))
17 | class TestFlatten(unittest.TestCase):
18 |
19 | dtype = numpy.float32
20 |
21 | def setUp(self):
22 | self.x = numpy.random.uniform(-1, 1, self.shape).astype(self.dtype)
23 | self.g_shape = (numpy.prod((1,) + self.shape),)
24 | self.g = numpy.random.uniform(-1, 1, self.g_shape).astype(self.dtype)
25 |
26 | def check_forward(self, x_data):
27 | x = chainer.Variable(x_data)
28 | y = functions.flatten(x)
29 |
30 | self.assertEqual(y.shape, self.g_shape)
31 | self.assertEqual(y.dtype, self.dtype)
32 | testing.assert_allclose(self.x.flatten(), y.data)
33 |
34 | def test_forward_cpu(self):
35 | self.check_forward(self.x)
36 |
37 | @attr.gpu
38 | def test_forward_gpu(self):
39 | self.check_forward(cuda.to_gpu(self.x))
40 |
41 | def check_backward(self, x_data, g_data):
42 | gradient_check.check_backward(
43 | functions.flatten, x_data, g_data, dtype=numpy.float64)
44 |
45 | def test_backward_cpu(self):
46 | self.check_backward(self.x, self.g)
47 |
48 | @attr.gpu
49 | def test_backward_gpu(self):
50 | self.check_backward(cuda.to_gpu(self.x), cuda.to_gpu(self.g))
51 |
52 |
53 | testing.run_module(__name__, __file__)
54 |
--------------------------------------------------------------------------------
/chainer/functions/array/swapaxes.py:
--------------------------------------------------------------------------------
1 | from chainer import function_node
2 | from chainer.utils import type_check
3 |
4 |
5 | class Swapaxes(function_node.FunctionNode):
6 | """Swap two axes of an array."""
7 |
8 | def __init__(self, axis1, axis2):
9 | self.axis1 = axis1
10 | self.axis2 = axis2
11 |
12 | def check_type_forward(self, in_types):
13 | type_check.expect(in_types.size() == 1,)
14 |
15 | @property
16 | def label(self):
17 | return 'Swapaxes'
18 |
19 | def forward(self, inputs):
20 | x, = inputs
21 | return x.swapaxes(self.axis1, self.axis2),
22 |
23 | def backward(self, indexes, grad_outputs):
24 | gy, = grad_outputs
25 | return Swapaxes(self.axis1, self.axis2).apply((gy,))
26 |
27 |
28 | def swapaxes(x, axis1, axis2):
29 | """Swap two axes of a variable.
30 |
31 | Args:
32 | x (:class:`~chainer.Variable` or :class:`numpy.ndarray` or \
33 | :class:`cupy.ndarray`): Input variable.
34 | A :math:`(s_1, s_2, ..., s_N)` -shaped float array.
35 | axis1 (int): The first axis to swap.
36 | axis2 (int): The second axis to swap.
37 |
38 | Returns:
39 | ~chainer.Variable: Variable whose axes are swapped.
40 |
41 | .. admonition:: Example
42 |
43 | >>> x = np.array([[[0, 1, 2], [3, 4, 5]]], np.float32)
44 | >>> x.shape
45 | (1, 2, 3)
46 | >>> y = F.swapaxes(x, axis1=0, axis2=1)
47 | >>> y.shape
48 | (2, 1, 3)
49 | >>> y.data
50 | array([[[0., 1., 2.]],
51 |
52 | [[3., 4., 5.]]], dtype=float32)
53 |
54 | """
55 | y, = Swapaxes(axis1, axis2).apply((x,))
56 | return y
57 |
--------------------------------------------------------------------------------
/tests/chainer_tests/functions_tests/math_tests/test_fix.py:
--------------------------------------------------------------------------------
1 | import unittest
2 |
3 | import numpy
4 |
5 | import chainer
6 | from chainer.backends import cuda
7 | import chainer.functions as F
8 | from chainer import testing
9 | from chainer.testing import attr
10 |
11 |
12 | class UnaryFunctionsTestBase(unittest.TestCase):
13 |
14 | def make_data(self):
15 | raise NotImplementedError
16 |
17 | def setUp(self):
18 | self.eps = 1e-3
19 | while True:
20 | self.x = self.make_data()
21 | if (numpy.abs(self.x - numpy.round(self.x)) > self.eps * 10).all():
22 | break
23 |
24 | def check_forward(self, op, op_xp, x_data):
25 | x = chainer.Variable(x_data)
26 | y = op(x)
27 | self.assertEqual(x.data.dtype, y.data.dtype)
28 | v = op_xp(x_data)
29 | testing.assert_allclose(v, y.data, atol=1e-7, rtol=1e-7)
30 |
31 | def check_forward_cpu(self, op, op_xp):
32 | self.check_forward(op, op_xp, self.x)
33 |
34 | def check_forward_gpu(self, op, op_xp):
35 | self.check_forward(op, op_xp, cuda.to_gpu(self.x))
36 |
37 |
38 | @testing.parameterize(*testing.product({
39 | 'shape': [(3, 2), ()],
40 | 'dtype': [numpy.float16, numpy.float32, numpy.float64],
41 | }))
42 | class TestFix(UnaryFunctionsTestBase):
43 |
44 | def make_data(self):
45 | x = numpy.random.uniform(-10.0, 10.0, self.shape).astype(self.dtype)
46 | return x
47 |
48 | def test_forward_cpu(self):
49 | self.check_forward_cpu(F.fix, numpy.fix)
50 |
51 | @attr.gpu
52 | def test_forward_gpu(self):
53 | self.check_forward_gpu(F.fix, cuda.cupy.fix)
54 |
55 |
56 | testing.run_module(__name__, __file__)
57 |
--------------------------------------------------------------------------------
/tests/chainer_tests/datasets_tests/test_transform_dataset.py:
--------------------------------------------------------------------------------
1 | import unittest
2 |
3 | import numpy
4 |
5 | from chainer import datasets
6 | from chainer import testing
7 |
8 |
9 | def _create_list_tuples(shape1, shape2, length):
10 | return [(numpy.random.uniform(shape1), numpy.random.uniform(shape2)) for
11 | _ in range(length)]
12 |
13 |
14 | @testing.parameterize(
15 | {'dataset': numpy.random.uniform(size=(2, 3, 32, 32))},
16 | {'dataset': _create_list_tuples((3, 32, 32), (32, 32), 5)}
17 | )
18 | class TestTransformDataset(unittest.TestCase):
19 |
20 | def setUp(self):
21 | def transform(in_data):
22 | if isinstance(in_data, tuple):
23 | return tuple([example * 3 for example in in_data])
24 | else:
25 | return in_data * 3
26 | self.transform = transform
27 |
28 | def test_transform_dataset(self):
29 | td = datasets.TransformDataset(self.dataset, self.transform)
30 | self.assertEqual(len(td), len(self.dataset))
31 |
32 | for i in range(len(td)):
33 | example = td[i]
34 | if isinstance(example, tuple):
35 | for j, arr in enumerate(example):
36 | numpy.testing.assert_array_equal(
37 | arr, self.transform(self.dataset[i][j]))
38 | else:
39 | numpy.testing.assert_array_equal(
40 | example, self.transform(self.dataset[i]))
41 |
42 | def test_transform_dataset_overrun(self):
43 | td = datasets.TransformDataset(self.dataset, self.transform)
44 | with self.assertRaises(IndexError):
45 | td[len(td) + 1]
46 |
47 |
48 | testing.run_module(__name__, __file__)
49 |
--------------------------------------------------------------------------------
/chainer/functions/loss/squared_error.py:
--------------------------------------------------------------------------------
1 | import numpy
2 |
3 | from chainer import function_node
4 | from chainer import utils
5 | from chainer.utils import type_check
6 |
7 |
8 | class SquaredError(function_node.FunctionNode):
9 |
10 | """Squared error function."""
11 |
12 | def check_type_forward(self, in_types):
13 | type_check.expect(in_types.size() == 2)
14 | type_check.expect(
15 | in_types[0].dtype == numpy.float32,
16 | in_types[1].dtype == numpy.float32,
17 | in_types[0].shape == in_types[1].shape
18 | )
19 |
20 | def forward(self, inputs):
21 | x0, x1 = inputs
22 | diff = x0 - x1
23 | self.retain_inputs((0, 1))
24 | return utils.force_array(diff * diff, dtype=x0.dtype),
25 |
26 | def backward(self, indexes, grad_outputs):
27 | x0, x1 = self.get_retained_inputs()
28 | gy, = grad_outputs
29 | gx = gy * 2 * (x0 - x1)
30 | return gx, -gx
31 |
32 |
33 | def squared_error(x0, x1):
34 | """Squared error function.
35 |
36 | This function computes the squared error between two variables:
37 |
38 | .. math::
39 |
40 | (x_0 - x_1)^2
41 |
42 | where operation is done in elementwise manner.
43 | Note that the error is not scaled by 1/2:
44 |
45 | Args:
46 | x0 (:class:`~chainer.Variable` or :class:`numpy.ndarray` or \
47 | :class:`cupy.ndarray`): Input variable.
48 | x1 (:class:`~chainer.Variable` or :class:`numpy.ndarray` or \
49 | :class:`cupy.ndarray`): Input variable.
50 |
51 | Returns:
52 | ~chainer.Variable:
53 | A variable holding an array representing the squared error of
54 | two inputs.
55 |
56 | """
57 | return SquaredError().apply((x0, x1))[0]
58 |
--------------------------------------------------------------------------------
/tests/chainer_tests/functions_tests/pooling_tests/test_pooling_nd_kernel.py:
--------------------------------------------------------------------------------
1 | import unittest
2 |
3 | import mock
4 |
5 | import chainer
6 | from chainer.functions.pooling import pooling_nd_kernel
7 | from chainer import testing
8 | from chainer.testing import attr
9 |
10 |
11 | @testing.parameterize(*testing.product({
12 | 'ndim': [2, 3, 4],
13 | }))
14 | @attr.gpu
15 | class TestPoolingNDKernelMemo(unittest.TestCase):
16 |
17 | def setUp(self):
18 | chainer.backends.cuda.clear_memo()
19 |
20 | def test_pooling_nd_kernel_forward_memo(self):
21 | ndim = self.ndim
22 | with mock.patch('chainer.functions.pooling.pooling_nd_kernel.'
23 | 'PoolingNDKernelForward._generate') as m:
24 | pooling_nd_kernel.PoolingNDKernelForward.generate(ndim)
25 | m.assert_called_once_with(ndim)
26 | pooling_nd_kernel.PoolingNDKernelForward.generate(ndim)
27 | # Check that the mocked _generate() function is called just once
28 | # because the result of generate() function is cached.
29 | m.assert_called_once_with(ndim)
30 |
31 | def test_pooling_nd_kernel_backward_memo(self):
32 | ndim = self.ndim
33 | with mock.patch('chainer.functions.pooling.pooling_nd_kernel.'
34 | 'PoolingNDKernelBackward._generate') as m:
35 | pooling_nd_kernel.PoolingNDKernelBackward.generate(ndim)
36 | m.assert_called_once_with(ndim)
37 | pooling_nd_kernel.PoolingNDKernelBackward.generate(ndim)
38 | # Check that the mocked _generate() function is called just once
39 | # because the result of generate() function is cached.
40 | m.assert_called_once_with(ndim)
41 |
42 |
43 | testing.run_module(__name__, __file__)
44 |
--------------------------------------------------------------------------------
/tests/chainer_tests/initializer_tests/test_uniform.py:
--------------------------------------------------------------------------------
1 | import unittest
2 |
3 | from chainer.backends import cuda
4 | from chainer import initializers
5 | from chainer import testing
6 | from chainer.testing import attr
7 | import numpy
8 |
9 |
10 | @testing.parameterize(*testing.product({
11 | 'target': [
12 | initializers.Uniform,
13 | initializers.LeCunUniform,
14 | initializers.HeUniform,
15 | initializers.GlorotUniform,
16 | ],
17 | 'shape': [(2, 3), (2, 3, 4)],
18 | 'dtype': [numpy.float16, numpy.float32, numpy.float64],
19 | }))
20 | class TestUniform(unittest.TestCase):
21 |
22 | scale = 0.1
23 |
24 | def check_initializer(self, w):
25 | initializer = self.target(scale=self.scale)
26 | initializer(w)
27 | self.assertTupleEqual(w.shape, self.shape)
28 | self.assertEqual(w.dtype, self.dtype)
29 |
30 | def test_initializer_cpu(self):
31 | w = numpy.empty(self.shape, dtype=self.dtype)
32 | self.check_initializer(w)
33 |
34 | @attr.gpu
35 | def test_initializer_gpu(self):
36 | w = cuda.cupy.empty(self.shape, dtype=self.dtype)
37 | self.check_initializer(w)
38 |
39 | def check_shaped_initializer(self, xp):
40 | initializer = self.target(scale=self.scale, dtype=self.dtype)
41 | w = initializers.generate_array(initializer, self.shape, xp)
42 | self.assertIs(cuda.get_array_module(w), xp)
43 | self.assertTupleEqual(w.shape, self.shape)
44 | self.assertEqual(w.dtype, self.dtype)
45 |
46 | def test_shaped_initializer_cpu(self):
47 | self.check_shaped_initializer(numpy)
48 |
49 | @attr.gpu
50 | def test_shaped_initializer_gpu(self):
51 | self.check_shaped_initializer(cuda.cupy)
52 |
53 |
54 | testing.run_module(__name__, __file__)
55 |
--------------------------------------------------------------------------------
/chainer/functions/loss/absolute_error.py:
--------------------------------------------------------------------------------
1 | import numpy
2 |
3 | from chainer.backends import cuda
4 | from chainer import function_node
5 | from chainer import utils
6 | from chainer.utils import type_check
7 |
8 |
9 | class AbsoluteError(function_node.FunctionNode):
10 |
11 | """Element-wise absolute error function."""
12 |
13 | def check_type_forward(self, in_types):
14 | type_check.expect(in_types.size() == 2)
15 | type_check.expect(
16 | in_types[0].dtype == numpy.float32,
17 | in_types[1].dtype == numpy.float32,
18 | in_types[0].shape == in_types[1].shape
19 | )
20 |
21 | def forward(self, inputs):
22 | x0, x1 = inputs
23 | self.diff = x0 - x1
24 | return utils.force_array(abs(self.diff), dtype=x0.dtype),
25 |
26 | def backward(self, indexes, grad_outputs):
27 | gy, = grad_outputs
28 | gx = gy * cuda.get_array_module(gy).sign(self.diff)
29 | return gx, -gx
30 |
31 |
32 | def absolute_error(x0, x1):
33 | """Element-wise absolute error function.
34 |
35 | Computes the element-wise absolute error :math:`L` between two inputs
36 | :math:`x_0` and :math:`x_1` defined as follows.
37 |
38 | .. math::
39 |
40 | L = |x_0 - x_1|
41 |
42 | Args:
43 | x0 (:class:`~chainer.Variable` or :class:`numpy.ndarray` or \
44 | :class:`cupy.ndarray`):
45 | First input variable.
46 | x1 (:class:`~chainer.Variable` or :class:`numpy.ndarray` or \
47 | :class:`cupy.ndarray`):
48 | Second input variable.
49 |
50 | Returns:
51 | ~chainer.Variable:
52 | An array representing the element-wise absolute error between the
53 | two inputs.
54 |
55 | """
56 | return AbsoluteError().apply((x0, x1))[0]
57 |
--------------------------------------------------------------------------------
/tests/chainer_tests/functions_tests/math_tests/test_ceil.py:
--------------------------------------------------------------------------------
1 | import unittest
2 |
3 | import numpy
4 |
5 | import chainer
6 | from chainer.backends import cuda
7 | import chainer.functions as F
8 | from chainer import testing
9 | from chainer.testing import attr
10 |
11 |
12 | class UnaryFunctionsTestBase(unittest.TestCase):
13 |
14 | def make_data(self):
15 | raise NotImplementedError
16 |
17 | def setUp(self):
18 | self.eps = 1e-3
19 | while True:
20 | self.x, self.gy = self.make_data()
21 | if (numpy.abs(self.x - numpy.round(self.x)) > self.eps * 10).all():
22 | break
23 |
24 | def check_forward(self, op, op_xp, x_data):
25 | x = chainer.Variable(x_data)
26 | y = op(x)
27 | self.assertEqual(x.data.dtype, y.data.dtype)
28 | v = op_xp(x_data)
29 | testing.assert_allclose(
30 | v, y.data, atol=1e-7, rtol=1e-7)
31 |
32 | def check_forward_cpu(self, op, op_xp):
33 | self.check_forward(op, op_xp, self.x)
34 |
35 | def check_forward_gpu(self, op, op_xp):
36 | self.check_forward(op, op_xp, cuda.to_gpu(self.x))
37 |
38 |
39 | @testing.parameterize(*testing.product({
40 | 'shape': [(3, 2), ()],
41 | 'dtype': [numpy.float16, numpy.float32, numpy.float64],
42 | }))
43 | class TestCeil(UnaryFunctionsTestBase):
44 |
45 | def make_data(self):
46 | x = numpy.random.uniform(-10.0, 10.0, self.shape).astype(self.dtype)
47 | gy = numpy.random.uniform(-1, 1, self.shape).astype(self.dtype)
48 | return x, gy
49 |
50 | def test_forward_cpu(self):
51 | self.check_forward_cpu(F.ceil, numpy.ceil)
52 |
53 | @attr.gpu
54 | def test_forward_gpu(self):
55 | self.check_forward_gpu(F.ceil, cuda.cupy.ceil)
56 |
57 |
58 | testing.run_module(__name__, __file__)
59 |
--------------------------------------------------------------------------------
/chainer/functions/array/flip.py:
--------------------------------------------------------------------------------
1 | from chainer.backends import cuda
2 | from chainer import function_node
3 | from chainer.utils import type_check
4 | import six
5 |
6 |
7 | def _flip(array, axis):
8 | indices = [slice(None)] * array.ndim
9 | indices[axis] = slice(None, None, -1)
10 | return array[indices]
11 |
12 |
13 | class Flip(function_node.FunctionNode):
14 | """Flips an input variable in reverse order along the given axis."""
15 |
16 | def __init__(self, axis):
17 | if not isinstance(axis, six.integer_types):
18 | raise TypeError('axis must be int')
19 | self.axis = axis
20 |
21 | def check_type_forward(self, in_types):
22 | type_check.expect(in_types.size() == 1)
23 | x_type = in_types[0]
24 |
25 | type_check.expect(x_type.ndim > 0)
26 | if self.axis >= 0:
27 | type_check.expect(x_type.ndim > self.axis)
28 | else:
29 | type_check.expect(x_type.ndim >= -self.axis)
30 |
31 | def forward(self, inputs):
32 | xp = cuda.get_array_module(*inputs)
33 | if hasattr(xp, 'flip'): # numpy.flip is supported from version 1.12.0
34 | return xp.flip(inputs[0], self.axis),
35 | else:
36 | return _flip(inputs[0], self.axis),
37 |
38 | def backward(self, indexes, grad_outputs):
39 | return flip(grad_outputs[0], self.axis),
40 |
41 |
42 | def flip(x, axis):
43 | """Flips an input variable in reverse order along the given axis.
44 |
45 | Args:
46 | x (:class:`~chainer.Variable` or :class:`numpy.ndarray` or \
47 | :class:`cupy.ndarray`):
48 | Input variable.
49 | axis (int): Axis along which the input variable is reversed.
50 |
51 | Returns:
52 | ~chainer.Variable: Output variable.
53 |
54 | """
55 | return Flip(axis).apply((x,))[0]
56 |
--------------------------------------------------------------------------------
/chainer/datasets/_mnist_helper.py:
--------------------------------------------------------------------------------
1 | import gzip
2 | import struct
3 |
4 | import numpy
5 | import six
6 |
7 | from chainer.dataset import download
8 | from chainer.datasets import tuple_dataset
9 |
10 |
11 | def make_npz(path, urls):
12 | x_url, y_url = urls
13 | x_path = download.cached_download(x_url)
14 | y_path = download.cached_download(y_url)
15 |
16 | with gzip.open(x_path, 'rb') as fx, gzip.open(y_path, 'rb') as fy:
17 | fx.read(4)
18 | fy.read(4)
19 | N, = struct.unpack('>i', fx.read(4))
20 | if N != struct.unpack('>i', fy.read(4))[0]:
21 | raise RuntimeError('wrong pair of MNIST images and labels')
22 | fx.read(8)
23 |
24 | x = numpy.empty((N, 784), dtype=numpy.uint8)
25 | y = numpy.empty(N, dtype=numpy.uint8)
26 |
27 | for i in six.moves.range(N):
28 | y[i] = ord(fy.read(1))
29 | for j in six.moves.range(784):
30 | x[i, j] = ord(fx.read(1))
31 |
32 | numpy.savez_compressed(path, x=x, y=y)
33 | return {'x': x, 'y': y}
34 |
35 |
36 | def preprocess_mnist(raw, withlabel, ndim, scale, image_dtype, label_dtype,
37 | rgb_format):
38 | images = raw['x']
39 | if ndim == 2:
40 | images = images.reshape(-1, 28, 28)
41 | elif ndim == 3:
42 | images = images.reshape(-1, 1, 28, 28)
43 | if rgb_format:
44 | images = numpy.broadcast_to(images,
45 | (len(images), 3) + images.shape[2:])
46 | elif ndim != 1:
47 | raise ValueError('invalid ndim for MNIST dataset')
48 | images = images.astype(image_dtype)
49 | images *= scale / 255.
50 |
51 | if withlabel:
52 | labels = raw['y'].astype(label_dtype)
53 | return tuple_dataset.TupleDataset(images, labels)
54 | else:
55 | return images
56 |
--------------------------------------------------------------------------------
/examples/cifar/README.md:
--------------------------------------------------------------------------------
1 | # Convolutional neural networks for CIFAR-10 and CIFAR-100 Classification
2 |
3 | This is an example of a convolutional neural network (convnet) applied to an image classification task using the CIFAR-10 or CIFAR-100 dataset. The CIFAR datasets can be a good choice for initial experiments with convnets because the size and number of images is small enough to allow typical models to be trained in a reasonable amount of time. However, the classification task is still challenging because natural images are used.
4 |
5 | Specifically, there are 50000 color training images of size 32x32 pixels with either 10 class labels (for CIFAR-10) or 100 class labels (for CIFAR-100).
6 |
7 | For CIFAR-10, state of the art methods without data augmentation can achieve similar to human-level classification accuracy of around 94%.
8 | For CIFAR-100, state of the art without data augmentation is around 20% (DenseNet).
9 |
10 | The code consists of three parts: dataset preparation, network and optimizer definition and learning loop, similar to the MNIST example.
11 |
12 | More models may be added in the future, but currently only one model is available that uses the VGG-style network from [here](http://torch.ch/blog/2015/07/30/cifar.html) which is based on the network architecture from the paper from [here](https://arxiv.org/pdf/1409.1556v6.pdf).
13 |
14 | No data augmentation is used and the classification accuracy on the CIFAR-10 test set for the VGG-style model should reach approximately 89% after 200 iterations or so.
15 |
16 | If you want to run this example on the N-th GPU, pass `--gpu=N` to the script. To run on CPU, pass `--gpu=-1`.
17 |
18 | For example, to run the default model, which uses CIFAR-10 and GPU 0:
19 | ```
20 | train_cifar.py
21 | ```
22 |
23 | to run the CIFAR-100 dataset on GPU 1:
24 | ```
25 | train_cifar.py --gpu=1 --dataset='cifar100'
26 | ```
27 |
--------------------------------------------------------------------------------
/chainer/testing/attr.py:
--------------------------------------------------------------------------------
1 | import os
2 | import unittest
3 |
4 |
5 | try:
6 | import pytest
7 | _error = None
8 | except ImportError as e:
9 | _error = e
10 |
11 |
12 | def is_available():
13 | return _error is None
14 |
15 |
16 | def check_available():
17 | if _error is not None:
18 | raise RuntimeError('''\
19 | {} is not available.
20 |
21 | Reason: {}: {}'''.format(__name__, type(_error).__name__, _error))
22 |
23 |
24 | def get_error():
25 | return _error
26 |
27 |
28 | if _error is None:
29 | _gpu_limit = int(os.getenv('CHAINER_TEST_GPU_LIMIT', '-1'))
30 |
31 | cudnn = pytest.mark.cudnn
32 | ideep = pytest.mark.ideep
33 | slow = pytest.mark.slow
34 |
35 | else:
36 | def _dummy_callable(*args, **kwargs):
37 | check_available()
38 | assert False # Not reachable
39 |
40 | cudnn = _dummy_callable
41 | ideep = _dummy_callable
42 | slow = _dummy_callable
43 |
44 |
45 | def multi_gpu(gpu_num):
46 | """Decorator to indicate number of GPUs required to run the test.
47 |
48 | Tests can be annotated with this decorator (e.g., ``@multi_gpu(2)``) to
49 | declare number of GPUs required to run. When running tests, if
50 | ``CHAINER_TEST_GPU_LIMIT`` environment variable is set to value greater
51 | than or equals to 0, test cases that require GPUs more than the limit will
52 | be skipped.
53 | """
54 |
55 | check_available()
56 | return unittest.skipIf(
57 | 0 <= _gpu_limit < gpu_num,
58 | reason='{} GPUs required'.format(gpu_num))
59 |
60 |
61 | def gpu(f):
62 | """Decorator to indicate that GPU is required to run the test.
63 |
64 | Tests can be annotated with this decorator (e.g., ``@gpu``) to
65 | declare that one GPU is required to run.
66 | """
67 |
68 | check_available()
69 | return multi_gpu(1)(pytest.mark.gpu(f))
70 |
--------------------------------------------------------------------------------
/chainer/testing/serializer.py:
--------------------------------------------------------------------------------
1 | import os
2 | import shutil
3 | import tempfile
4 |
5 | from chainer import serializers
6 |
7 |
8 | def save_and_load(src, dst, filename, saver, loader):
9 | """Saves ``src`` and loads it to ``dst`` using a de/serializer.
10 |
11 | This function simply runs a serialization and deserialization to check if
12 | the serialization code is correctly implemented. The save and load are
13 | done within a temporary directory.
14 |
15 | Args:
16 | src: An object to save from.
17 | dst: An object to load into.
18 | filename (str): File name used during the save/load.
19 | saver (callable): Function that saves the source object.
20 | loader (callable): Function that loads the file into the destination
21 | object.
22 |
23 | """
24 | tempdir = tempfile.mkdtemp()
25 | try:
26 | path = os.path.join(tempdir, filename)
27 | saver(path, src)
28 | loader(path, dst)
29 | finally:
30 | shutil.rmtree(tempdir)
31 |
32 |
33 | def save_and_load_npz(src, dst):
34 | """Saves ``src`` to an NPZ file and loads it to ``dst``.
35 |
36 | This is a short cut of :func:`save_and_load` using NPZ de/serializers.
37 |
38 | Args:
39 | src: An object to save.
40 | dst: An object to load to.
41 |
42 | """
43 | save_and_load(src, dst, 'tmp.npz',
44 | serializers.save_npz, serializers.load_npz)
45 |
46 |
47 | def save_and_load_hdf5(src, dst):
48 | """Saves ``src`` to an HDF5 file and loads it to ``dst``.
49 |
50 | This is a short cut of :func:`save_and_load` using HDF5 de/serializers.
51 |
52 | Args:
53 | src: An object to save.
54 | dst: An object to load to.
55 |
56 | """
57 | save_and_load(src, dst, 'tmp.h5',
58 | serializers.save_hdf5, serializers.load_hdf5)
59 |
--------------------------------------------------------------------------------
/docs/source/reference/initializers.rst:
--------------------------------------------------------------------------------
1 | .. _initializer:
2 |
3 | Weight Initializers
4 | ===================
5 |
6 | Weight initializers are used to initialize arrays.
7 | They destructively modify the content of :class:`numpy.ndarray`
8 | or :class:`cupy.ndarray`.
9 | Typically, weight initializers are passed to :class:`~chainer.Link`\ s
10 | to initialize their weights and biases.
11 |
12 | A weight initializer can be any of the following objects.
13 |
14 | * :class:`chainer.Initializer` class instance.
15 | * Python or NumPy scalar or :class:`numpy.ndarray`.
16 | * A callable that takes an array (:class:`numpy.ndarray` or :class:`cupy.ndarray`)
17 | and feeds the initial data into it.
18 | * ``None``, in which case *the default initializer* is used.
19 | Unless explicitly specified, it is :class:`~chainer.initializers.LeCunNormal`
20 | with scale value 1.
21 |
22 | Base class
23 | ----------
24 |
25 | .. autosummary::
26 | :toctree: generated/
27 | :nosignatures:
28 |
29 | chainer.Initializer
30 |
31 | .. module:: chainer.initializers
32 |
33 | Concrete initializers
34 | ---------------------
35 |
36 | .. autosummary::
37 | :toctree: generated/
38 | :nosignatures:
39 |
40 | chainer.initializers.Identity
41 | chainer.initializers.Constant
42 | chainer.initializers.Zero
43 | chainer.initializers.One
44 | chainer.initializers.NaN
45 | chainer.initializers.Normal
46 | chainer.initializers.LeCunNormal
47 | chainer.initializers.GlorotNormal
48 | chainer.initializers.HeNormal
49 | chainer.initializers.Orthogonal
50 | chainer.initializers.Uniform
51 | chainer.initializers.LeCunUniform
52 | chainer.initializers.GlorotUniform
53 | chainer.initializers.HeUniform
54 |
55 | Helper function
56 | ---------------
57 |
58 | .. autosummary::
59 | :toctree: generated/
60 | :nosignatures:
61 |
62 | chainer.initializers.generate_array
63 |
--------------------------------------------------------------------------------
/chainer/training/util.py:
--------------------------------------------------------------------------------
1 | from chainer.training.triggers import interval_trigger
2 |
3 |
4 | def get_trigger(trigger):
5 | """Gets a trigger object.
6 |
7 | Trigger object is a callable that accepts a
8 | :class:`~chainer.training.Trainer` object as an argument and returns a
9 | boolean value. When it returns True, various kinds of events can occur
10 | depending on the context in which the trigger is used. For example, if the
11 | trigger is passed to the :class:`~chainer.training.Trainer` as the `stop
12 | trigger`, the training loop breaks when the trigger returns True. If the
13 | trigger is passed to the :meth:`~chainer.training.Trainer.extend` method of
14 | a trainer, then the registered extension is invoked only when the trigger
15 | returns True.
16 |
17 | This function returns a trigger object based on the argument.
18 | If ``trigger`` is already a callable, it just returns the trigger. If
19 | ``trigger`` is ``None``, it returns a trigger that never fires. Otherwise,
20 | it passes the value to :class:`~chainer.training.triggers.IntervalTrigger`.
21 |
22 | Args:
23 | trigger: Trigger object. It can be either an already built trigger
24 | object (i.e., a callable object that accepts a trainer object and
25 | returns a bool value), or a tuple. In latter case, the tuple is
26 | passed to :class:`~chainer.training.triggers.IntervalTrigger`.
27 |
28 | Returns:
29 | ``trigger`` if it is a callable, otherwise a
30 | :class:`~chainer.training.triggers.IntervalTrigger`
31 | object made from ``trigger``.
32 |
33 | """
34 | if callable(trigger):
35 | return trigger
36 | elif trigger is None:
37 | return _never_fire_trigger
38 | else:
39 | return interval_trigger.IntervalTrigger(*trigger)
40 |
41 |
42 | def _never_fire_trigger(trainer):
43 | return False
44 |
--------------------------------------------------------------------------------
/chainer/datasets/__init__.py:
--------------------------------------------------------------------------------
1 | from chainer.datasets import cifar # NOQA
2 | from chainer.datasets import dict_dataset # NOQA
3 | from chainer.datasets import fashion_mnist # NOQA
4 | from chainer.datasets import image_dataset # NOQA
5 | from chainer.datasets import mnist # NOQA
6 | from chainer.datasets import ptb # NOQA
7 | from chainer.datasets import sub_dataset # NOQA
8 | from chainer.datasets import svhn # NOQA
9 | from chainer.datasets import transform_dataset # NOQA
10 | from chainer.datasets import tuple_dataset # NOQA
11 |
12 |
13 | # import class and function
14 | from chainer.datasets.cifar import get_cifar10 # NOQA
15 | from chainer.datasets.cifar import get_cifar100 # NOQA
16 | from chainer.datasets.concatenated_dataset import ConcatenatedDataset # NOQA
17 | from chainer.datasets.dict_dataset import DictDataset # NOQA
18 | from chainer.datasets.fashion_mnist import get_fashion_mnist # NOQA
19 | from chainer.datasets.image_dataset import ImageDataset # NOQA
20 | from chainer.datasets.image_dataset import LabeledImageDataset # NOQA
21 | from chainer.datasets.mnist import get_mnist # NOQA
22 | from chainer.datasets.ptb import get_ptb_words # NOQA
23 | from chainer.datasets.ptb import get_ptb_words_vocabulary # NOQA
24 | from chainer.datasets.sub_dataset import get_cross_validation_datasets # NOQA
25 | from chainer.datasets.sub_dataset import get_cross_validation_datasets_random # NOQA
26 | from chainer.datasets.sub_dataset import split_dataset # NOQA
27 | from chainer.datasets.sub_dataset import split_dataset_n # NOQA
28 | from chainer.datasets.sub_dataset import split_dataset_n_random # NOQA
29 | from chainer.datasets.sub_dataset import split_dataset_random # NOQA
30 | from chainer.datasets.sub_dataset import SubDataset # NOQA
31 | from chainer.datasets.svhn import get_svhn # NOQA
32 | from chainer.datasets.transform_dataset import TransformDataset # NOQA
33 | from chainer.datasets.tuple_dataset import TupleDataset # NOQA
34 |
--------------------------------------------------------------------------------
/chainer/training/extensions/__init__.py:
--------------------------------------------------------------------------------
1 | from chainer.training.extensions import _snapshot # NOQA
2 | from chainer.training.extensions import computational_graph # NOQA
3 | from chainer.training.extensions import evaluator # NOQA
4 | from chainer.training.extensions import exponential_shift # NOQA
5 | from chainer.training.extensions import linear_shift # NOQA
6 | from chainer.training.extensions import log_report # NOQA
7 | from chainer.training.extensions import micro_average # NOQA
8 | from chainer.training.extensions import plot_report # NOQA
9 | from chainer.training.extensions import print_report # NOQA
10 | from chainer.training.extensions import progress_bar # NOQA
11 | from chainer.training.extensions import value_observation # NOQA
12 |
13 |
14 | # import class and function
15 | from chainer.training.extensions._snapshot import snapshot # NOQA
16 | from chainer.training.extensions._snapshot import snapshot_object # NOQA
17 | from chainer.training.extensions.computational_graph import dump_graph # NOQA
18 | from chainer.training.extensions.evaluator import Evaluator # NOQA
19 | from chainer.training.extensions.exponential_shift import ExponentialShift # NOQA
20 | from chainer.training.extensions.linear_shift import LinearShift # NOQA
21 | from chainer.training.extensions.log_report import LogReport # NOQA
22 | from chainer.training.extensions.micro_average import MicroAverage # NOQA
23 | from chainer.training.extensions.parameter_statistics import ParameterStatistics # NOQA
24 | from chainer.training.extensions.plot_report import PlotReport # NOQA
25 | from chainer.training.extensions.print_report import PrintReport # NOQA
26 | from chainer.training.extensions.progress_bar import ProgressBar # NOQA
27 | from chainer.training.extensions.value_observation import observe_lr # NOQA
28 | from chainer.training.extensions.value_observation import observe_value # NOQA
29 | from chainer.training.extensions.variable_statistics_plot import VariableStatisticsPlot # NOQA
30 |
--------------------------------------------------------------------------------
/chainer/datasets/transform_dataset.py:
--------------------------------------------------------------------------------
1 | from chainer.dataset import dataset_mixin
2 |
3 |
4 | class TransformDataset(dataset_mixin.DatasetMixin):
5 |
6 | """Dataset that indexes the base dataset and transforms the data.
7 |
8 | This dataset wraps the base dataset by modifying the behavior of the base
9 | dataset's :meth:`__getitem__`. Arrays returned by :meth:`__getitem__` of
10 | the base dataset with integer as an argument are transformed by the given
11 | function :obj:`transform`.
12 | Also, :meth:`__len__` returns the integer returned by the base dataset's
13 | :meth:`__len__`.
14 |
15 | The function :obj:`transform` takes, as an argument, :obj:`in_data`, which
16 | is the output of the base dataset's :meth:`__getitem__`, and returns
17 | the transformed arrays as output. Please see the following example.
18 |
19 | >>> from chainer.datasets import get_mnist
20 | >>> from chainer.datasets import TransformDataset
21 | >>> dataset, _ = get_mnist()
22 | >>> def transform(in_data):
23 | ... img, label = in_data
24 | ... img -= 0.5 # scale to [-0.5, -0.5]
25 | ... return img, label
26 | >>> dataset = TransformDataset(dataset, transform)
27 |
28 | Args:
29 | dataset: The underlying dataset. The index of this dataset corresponds
30 | to the index of the base dataset. This object needs to support
31 | functions :meth:`__getitem__` and :meth:`__len__` as described
32 | above.
33 | transform (callable): A function that is called to transform values
34 | returned by the underlying dataset's :meth:`__getitem__`.
35 |
36 | """
37 |
38 | def __init__(self, dataset, transform):
39 | self._dataset = dataset
40 | self._transform = transform
41 |
42 | def __len__(self):
43 | return len(self._dataset)
44 |
45 | def get_example(self, i):
46 | in_data = self._dataset[i]
47 | return self._transform(in_data)
48 |
--------------------------------------------------------------------------------
/chainer/optimizers/sgd.py:
--------------------------------------------------------------------------------
1 | from chainer.backends import cuda
2 | from chainer.backends import intel64
3 | from chainer import optimizer
4 |
5 |
6 | _default_hyperparam = optimizer.Hyperparameter()
7 | _default_hyperparam.lr = 0.01
8 |
9 |
10 | class SGDRule(optimizer.UpdateRule):
11 |
12 | """Update rule of vanilla stochastic gradient descent.
13 |
14 | See :class:`~chainer.optimizers.SGD` for the default values of the
15 | hyperparameters.
16 |
17 | Args:
18 | parent_hyperparam (~chainer.optimizer.Hyperparameter): Hyperparameter
19 | that provides the default values.
20 | lr (float): Learning rate.
21 |
22 | """
23 |
24 | def __init__(self, parent_hyperparam=None, lr=None):
25 | super(SGDRule, self).__init__(
26 | parent_hyperparam or _default_hyperparam)
27 | if lr is not None:
28 | self.hyperparam.lr = lr
29 |
30 | def update_core_cpu(self, param):
31 | grad = param.grad
32 | if grad is None:
33 | return
34 | if isinstance(param.data, intel64.mdarray):
35 | param.data.inplace_axpby(1.0, -self.hyperparam.lr, grad)
36 | else:
37 | param.data -= self.hyperparam.lr * grad
38 |
39 | def update_core_gpu(self, param):
40 | grad = param.grad
41 | if grad is None:
42 | return
43 | cuda.elementwise('T grad, T lr', 'T param',
44 | 'param -= lr * grad',
45 | 'sgd')(grad, self.hyperparam.lr, param.data)
46 |
47 |
48 | class SGD(optimizer.GradientMethod):
49 |
50 | """Vanilla Stochastic Gradient Descent.
51 |
52 | Args:
53 | lr (float): Learning rate.
54 |
55 | """
56 |
57 | def __init__(self, lr=_default_hyperparam.lr):
58 | super(SGD, self).__init__()
59 | self.hyperparam.lr = lr
60 |
61 | lr = optimizer.HyperparameterProxy('lr')
62 |
63 | def create_update_rule(self):
64 | return SGDRule(self.hyperparam)
65 |
--------------------------------------------------------------------------------
/docs/source/reference/check.rst:
--------------------------------------------------------------------------------
1 | Assertion and Testing
2 | =====================
3 |
4 | Chainer provides some facilities to make debugging easy.
5 |
6 | .. _type-check-utils:
7 |
8 | Type checking utilities
9 | -----------------------
10 | :class:`~chainer.FunctionNode` uses a systematic type checking of the :mod:`chainer.utils.type_check` module.
11 | It enables users to easily find bugs of forward and backward implementations.
12 | You can find examples of type checking in some function implementations.
13 |
14 | .. autosummary::
15 | :toctree: generated/
16 | :nosignatures:
17 |
18 | chainer.utils.type_check.Expr
19 | chainer.utils.type_check.expect
20 | chainer.utils.type_check.TypeInfo
21 | chainer.utils.type_check.TypeInfoTuple
22 |
23 | Gradient checking utilities
24 | ---------------------------
25 | Most function implementations are numerically tested by *gradient checking*.
26 | This method computes numerical gradients of forward routines and compares their results with the corresponding backward routines.
27 | It enables us to make the source of issues clear when we hit an error of gradient computations.
28 | The :mod:`chainer.gradient_check` module makes it easy to implement the gradient checking.
29 |
30 | .. autosummary::
31 | :toctree: generated/
32 | :nosignatures:
33 |
34 | chainer.gradient_check.check_backward
35 | chainer.gradient_check.numerical_grad
36 |
37 | Standard Assertions
38 | -------------------
39 | The assertions have same names as NumPy's ones.
40 | The difference from NumPy is that they can accept both :class:`numpy.ndarray`
41 | and :class:`cupy.ndarray`.
42 |
43 | .. autosummary::
44 | :toctree: generated/
45 | :nosignatures:
46 |
47 | chainer.testing.assert_allclose
48 |
49 | Function testing utilities
50 | --------------------------
51 | Chainer provides some utilities for testing its functions.
52 |
53 | .. autosummary::
54 | :toctree: generated/
55 | :nosignatures:
56 |
57 | chainer.testing.unary_math_function_unittest
58 |
--------------------------------------------------------------------------------
/chainer/testing/array.py:
--------------------------------------------------------------------------------
1 | import numpy
2 | import six
3 |
4 | from chainer.backends import cuda
5 | from chainer import utils
6 |
7 |
8 | def assert_allclose(x, y, atol=1e-5, rtol=1e-4, verbose=True):
9 | """Asserts if some corresponding element of x and y differs too much.
10 |
11 | This function can handle both CPU and GPU arrays simultaneously.
12 |
13 | Args:
14 | x: Left-hand-side array.
15 | y: Right-hand-side array.
16 | atol (float): Absolute tolerance.
17 | rtol (float): Relative tolerance.
18 | verbose (bool): If ``True``, it outputs verbose messages on error.
19 |
20 | """
21 | x = cuda.to_cpu(utils.force_array(x))
22 | y = cuda.to_cpu(utils.force_array(y))
23 | try:
24 | numpy.testing.assert_allclose(
25 | x, y, atol=atol, rtol=rtol, verbose=verbose)
26 | except AssertionError as e:
27 | f = six.StringIO()
28 | f.write(str(e) + '\n\n')
29 | f.write(
30 | 'assert_allclose failed: \n' +
31 | ' shape: {} {}\n'.format(x.shape, y.shape) +
32 | ' dtype: {} {}\n'.format(x.dtype, y.dtype))
33 | if x.shape == y.shape:
34 | xx = x if x.ndim != 0 else x.reshape((1,))
35 | yy = y if y.ndim != 0 else y.reshape((1,))
36 | err = numpy.abs(xx - yy)
37 | i = numpy.unravel_index(numpy.argmax(err), err.shape)
38 | f.write(
39 | ' i: {}\n'.format(i) +
40 | ' x[i]: {}\n'.format(xx[i]) +
41 | ' y[i]: {}\n'.format(yy[i]) +
42 | ' err[i]: {}\n'.format(err[i]))
43 | opts = numpy.get_printoptions()
44 | try:
45 | numpy.set_printoptions(threshold=10000)
46 | f.write('x: ' + numpy.array2string(x, prefix='x: ') + '\n')
47 | f.write('y: ' + numpy.array2string(y, prefix='y: ') + '\n')
48 | finally:
49 | numpy.set_printoptions(**opts)
50 | raise AssertionError(f.getvalue())
51 |
--------------------------------------------------------------------------------
/tests/chainer_tests/datasets_tests/test_tuple_dataset.py:
--------------------------------------------------------------------------------
1 | import unittest
2 |
3 | import numpy
4 |
5 | from chainer.backends import cuda
6 | from chainer import datasets
7 | from chainer import testing
8 | from chainer.testing import attr
9 |
10 |
11 | class TestTupleDataset(unittest.TestCase):
12 |
13 | def setUp(self):
14 | self.x0 = numpy.random.rand(3, 4)
15 | self.x1 = numpy.random.rand(3, 5)
16 | self.z0 = numpy.random.rand(4, 4)
17 |
18 | def check_tuple_dataset(self, x0, x1):
19 | td = datasets.TupleDataset(x0, x1)
20 | self.assertEqual(len(td), len(x0))
21 |
22 | for i in range(len(x0)):
23 | example = td[i]
24 | self.assertEqual(len(example), 2)
25 |
26 | numpy.testing.assert_array_equal(
27 | cuda.to_cpu(example[0]), cuda.to_cpu(x0[i]))
28 | numpy.testing.assert_array_equal(
29 | cuda.to_cpu(example[1]), cuda.to_cpu(x1[i]))
30 |
31 | example_range = td[0: len(x0)]
32 | for i in range(len(x0)):
33 | example = example_range[i]
34 | self.assertEqual(len(example), 2)
35 |
36 | numpy.testing.assert_array_equal(
37 | cuda.to_cpu(example[0]), cuda.to_cpu(x0[i]))
38 | numpy.testing.assert_array_equal(
39 | cuda.to_cpu(example[1]), cuda.to_cpu(x1[i]))
40 |
41 | def test_tuple_dataset_cpu(self):
42 | self.check_tuple_dataset(self.x0, self.x1)
43 |
44 | @attr.gpu
45 | def test_tuple_dataset_gpu(self):
46 | self.check_tuple_dataset(cuda.to_gpu(self.x0), cuda.to_gpu(self.x1))
47 |
48 | def test_tuple_dataset_len_mismatch(self):
49 | with self.assertRaises(ValueError):
50 | datasets.TupleDataset(self.x0, self.z0)
51 |
52 | def test_tuple_dataset_overrun(self):
53 | td = datasets.TupleDataset(self.x0, self.x1)
54 | with self.assertRaises(IndexError):
55 | td[3]
56 |
57 |
58 | testing.run_module(__name__, __file__)
59 |
--------------------------------------------------------------------------------
/chainer/functions/math/erf.py:
--------------------------------------------------------------------------------
1 | import math
2 | import warnings
3 |
4 | import numpy
5 |
6 | import chainer
7 | from chainer import cuda
8 | from chainer import function_node
9 | from chainer import utils
10 | from chainer.utils import type_check
11 |
12 |
13 | _erf_cpu = None
14 |
15 |
16 | class Erf(function_node.FunctionNode):
17 |
18 | @property
19 | def label(self):
20 | return 'erf'
21 |
22 | def check_type_forward(self, in_types):
23 | type_check.expect(in_types.size() == 1)
24 | type_check.expect(in_types[0].dtype.kind == 'f')
25 |
26 | def forward_cpu(self, x):
27 | global _erf_cpu
28 | if _erf_cpu is None:
29 | try:
30 | from scipy import special
31 | _erf_cpu = special.erf
32 | except ImportError:
33 | warnings.warn(
34 | "SciPy is not available. Forward computation of erf in CPU"
35 | " can be slow without SciPy.")
36 | _erf_cpu = numpy.vectorize(math.erf)
37 | self.retain_inputs((0,))
38 | return utils.force_array(_erf_cpu(x[0]), dtype=x[0].dtype),
39 |
40 | def forward_gpu(self, x):
41 | self.retain_inputs((0,))
42 | return cuda.elementwise(
43 | 'T x', 'T y',
44 | 'y = erf(x)',
45 | 'elementwise_erf',
46 | )(x[0]),
47 |
48 | def backward(self, indexes, gy):
49 | x = self.get_retained_inputs()[0]
50 | return 2 / numpy.pi ** 0.5 * chainer.functions.exp(-x ** 2) * gy[0],
51 |
52 |
53 | def erf(x):
54 | """Elementwise error function.
55 |
56 | .. note::
57 | Forward computation in CPU can be slow if
58 | `SciPy `_ is not available.
59 |
60 | Args:
61 | x (:class:`~chainer.Variable` or :class:`numpy.ndarray` or \
62 | :class:`cupy.ndarray`): Input variable.
63 |
64 | Returns:
65 | ~chainer.Variable: Output variable.
66 | """
67 | return Erf().apply((x,))[0]
68 |
--------------------------------------------------------------------------------
/tests/chainer_tests/testing_tests/test_training.py:
--------------------------------------------------------------------------------
1 | from __future__ import division
2 |
3 | import math
4 | import unittest
5 |
6 | from chainer import testing
7 |
8 |
9 | def _dummy_extension(trainer):
10 | pass
11 |
12 |
13 | @testing.parameterize(*testing.product({
14 | 'stop_trigger': [(5, 'iteration'), (5, 'epoch')],
15 | 'iter_per_epoch': [0.5, 1, 1.5, 5],
16 | 'extensions': [[], [_dummy_extension]]
17 | }))
18 | class TestGetTrainerWithMockUpdater(unittest.TestCase):
19 |
20 | def setUp(self):
21 | self.trainer = testing.get_trainer_with_mock_updater(
22 | self.stop_trigger, self.iter_per_epoch,
23 | extensions=self.extensions)
24 |
25 | def test_run(self):
26 | iteration = [0]
27 |
28 | def check(trainer):
29 | iteration[0] += 1
30 |
31 | self.assertEqual(trainer.updater.iteration, iteration[0])
32 | self.assertEqual(
33 | trainer.updater.epoch, iteration[0] // self.iter_per_epoch)
34 | self.assertEqual(
35 | trainer.updater.epoch_detail,
36 | iteration[0] / self.iter_per_epoch)
37 | self.assertEqual(
38 | trainer.updater.is_new_epoch,
39 | (iteration[0] - 1) // self.iter_per_epoch !=
40 | iteration[0] // self.iter_per_epoch)
41 | self.assertEqual(
42 | trainer.updater.previous_epoch_detail,
43 | (iteration[0] - 1) / self.iter_per_epoch)
44 |
45 | self.assertEqual(len(self.extensions), len(self.trainer._extensions))
46 |
47 | self.trainer.extend(check)
48 | self.trainer.run()
49 |
50 | if self.stop_trigger[1] == 'iteration':
51 | self.assertEqual(iteration[0], self.stop_trigger[0])
52 | elif self.stop_trigger[1] == 'epoch':
53 | self.assertEqual(
54 | iteration[0],
55 | math.ceil(self.stop_trigger[0] * self.iter_per_epoch))
56 |
57 |
58 | testing.run_module(__name__, __file__)
59 |
--------------------------------------------------------------------------------
/chainer/functions/math/erfc.py:
--------------------------------------------------------------------------------
1 | import math
2 | import warnings
3 |
4 | import numpy
5 |
6 | import chainer
7 | from chainer import cuda
8 | from chainer import function_node
9 | from chainer import utils
10 | from chainer.utils import type_check
11 |
12 |
13 | _erfc_cpu = None
14 |
15 |
16 | class Erfc(function_node.FunctionNode):
17 |
18 | @property
19 | def label(self):
20 | return 'erfc'
21 |
22 | def check_type_forward(self, in_types):
23 | type_check.expect(in_types.size() == 1)
24 | type_check.expect(in_types[0].dtype.kind == 'f')
25 |
26 | def forward_cpu(self, x):
27 | global _erfc_cpu
28 | if _erfc_cpu is None:
29 | try:
30 | from scipy import special
31 | _erfc_cpu = special.erfc
32 | except ImportError:
33 | warnings.warn(
34 | "SciPy is not available. Forward computation of erfc in"
35 | " CPU can be slow without SciPy.")
36 | _erfc_cpu = numpy.vectorize(math.erfc)
37 | self.retain_inputs((0,))
38 | return utils.force_array(_erfc_cpu(x[0]), dtype=x[0].dtype),
39 |
40 | def forward_gpu(self, x):
41 | self.retain_inputs((0,))
42 | return cuda.elementwise(
43 | 'T x', 'T y',
44 | 'y = erfc(x)',
45 | 'elementwise_erfc',
46 | )(x[0]),
47 |
48 | def backward(self, indexes, gy):
49 | x = self.get_retained_inputs()[0]
50 | return -2 / numpy.pi ** 0.5 * chainer.functions.exp(-x ** 2) * gy[0],
51 |
52 |
53 | def erfc(x):
54 | """Elementwise complementary error function.
55 |
56 | .. note::
57 | Forward computation in CPU can be slow if
58 | `SciPy `_ is not available.
59 |
60 | Args:
61 | x (:class:`~chainer.Variable` or :class:`numpy.ndarray` or \
62 | :class:`cupy.ndarray`): Input variable.
63 |
64 | Returns:
65 | ~chainer.Variable: Output variable.
66 | """
67 | return Erfc().apply((x,))[0]
68 |
--------------------------------------------------------------------------------
/tests/chainer_tests/datasets_tests/test_dict_dataset.py:
--------------------------------------------------------------------------------
1 | import unittest
2 |
3 | import numpy
4 |
5 | from chainer.backends import cuda
6 | from chainer import datasets
7 | from chainer import testing
8 | from chainer.testing import attr
9 |
10 |
11 | class TestDictDataset(unittest.TestCase):
12 |
13 | def setUp(self):
14 | self.x = numpy.random.rand(3, 4)
15 | self.y = numpy.random.rand(3, 5)
16 | self.z = numpy.random.rand(4, 4)
17 |
18 | def check_dict_dataset(self, x, y):
19 | dd = datasets.DictDataset(x=x, y=y)
20 | self.assertEqual(len(dd), len(x))
21 |
22 | for i in range(len(x)):
23 | example = dd[i]
24 | self.assertIn('x', example)
25 | self.assertIn('y', example)
26 |
27 | numpy.testing.assert_array_equal(
28 | cuda.to_cpu(example['x']), cuda.to_cpu(x[i]))
29 | numpy.testing.assert_array_equal(
30 | cuda.to_cpu(example['y']), cuda.to_cpu(y[i]))
31 |
32 | example_range = dd[0: len(x)]
33 | for i in range(len(x)):
34 | example = example_range[i]
35 | self.assertIn('x', example)
36 | self.assertIn('y', example)
37 |
38 | numpy.testing.assert_array_equal(
39 | cuda.to_cpu(example['x']), cuda.to_cpu(x[i]))
40 | numpy.testing.assert_array_equal(
41 | cuda.to_cpu(example['y']), cuda.to_cpu(y[i]))
42 |
43 | def test_dict_dataset_cpu(self):
44 | self.check_dict_dataset(self.x, self.y)
45 |
46 | @attr.gpu
47 | def test_dict_dataset_gpu(self):
48 | self.check_dict_dataset(cuda.to_gpu(self.x), cuda.to_gpu(self.y))
49 |
50 | def test_dict_dataset_len_mismatch(self):
51 | with self.assertRaises(ValueError):
52 | datasets.DictDataset(x=self.x, z=self.z)
53 |
54 | def test_dict_dtaset_overrun(self):
55 | dd = datasets.DictDataset(x=self.x, y=self.y)
56 | with self.assertRaises(IndexError):
57 | dd[3]
58 |
59 |
60 | testing.run_module(__name__, __file__)
61 |
--------------------------------------------------------------------------------
/tests/chainer_tests/training_tests/triggers_tests/test_minmax_trigger.py:
--------------------------------------------------------------------------------
1 | import unittest
2 |
3 | import numpy
4 |
5 | from chainer import testing
6 | from chainer import training
7 | from chainer.training import triggers
8 |
9 |
10 | class DummyUpdater(training.Updater):
11 |
12 | def __init__(self):
13 | self.iteration = 0
14 |
15 | def finalize(self):
16 | pass
17 |
18 | def get_all_optimizers(self):
19 | return {}
20 |
21 | def update(self):
22 | self.iteration += 1
23 |
24 | @property
25 | def epoch(self):
26 | return 1
27 |
28 | @property
29 | def is_new_epoch(self):
30 | return False
31 |
32 |
33 | def _test_trigger(self, trigger, key, accuracies, expected):
34 | updater = DummyUpdater()
35 | trainer = training.Trainer(updater)
36 | for accuracy, expected in zip(accuracies, expected):
37 | updater.update()
38 | trainer.observation = {key: accuracy}
39 | self.assertEqual(trigger(trainer), expected)
40 |
41 |
42 | class TestMaxValueTrigger(unittest.TestCase):
43 |
44 | def test_max_value_trigger(self):
45 | key = 'main/accuracy'
46 | trigger = triggers.MaxValueTrigger(key, trigger=(2, 'iteration'))
47 | accuracies = numpy.asarray([0.5, 0.5, 0.5, 0.5, 0.4, 0.4, 0.6, 0.6],
48 | dtype=numpy.float32)
49 | expected = [False, True, False, False, False, False, False, True]
50 | _test_trigger(self, trigger, key, accuracies, expected)
51 |
52 |
53 | class TestMinValueTrigger(unittest.TestCase):
54 |
55 | def test_min_value_trigger(self):
56 | key = 'main/accuracy'
57 | trigger = triggers.MinValueTrigger(key, trigger=(2, 'iteration'))
58 | accuracies = numpy.asarray([0.5, 0.5, 0.5, 0.5, 0.4, 0.4, 0.6, 0.6],
59 | dtype=numpy.float32)
60 | expected = [False, True, False, False, False, True, False, False]
61 | _test_trigger(self, trigger, key, accuracies, expected)
62 |
63 |
64 | testing.run_module(__name__, __file__)
65 |
--------------------------------------------------------------------------------
/tests/chainer_tests/utils_tests/test_utils.py:
--------------------------------------------------------------------------------
1 | import unittest
2 |
3 | import numpy
4 |
5 | from chainer import testing
6 | from chainer import utils
7 |
8 |
9 | @testing.parameterize(*testing.product({
10 | 'dtype': [None, numpy.float16, numpy.float32, numpy.float64],
11 | }))
12 | class TestForceArray(unittest.TestCase):
13 |
14 | def test_scalar(self):
15 | x = utils.force_array(numpy.float32(1), dtype=self.dtype)
16 | self.assertIsInstance(x, numpy.ndarray)
17 | if self.dtype is None:
18 | self.assertEqual(x.dtype, numpy.float32)
19 | else:
20 | self.assertEqual(x.dtype, self.dtype)
21 |
22 | def test_0dim_array(self):
23 | x = utils.force_array(numpy.array(1, numpy.float32), dtype=self.dtype)
24 | self.assertIsInstance(x, numpy.ndarray)
25 | if self.dtype is None:
26 | self.assertEqual(x.dtype, numpy.float32)
27 | else:
28 | self.assertEqual(x.dtype, self.dtype)
29 |
30 | def test_array(self):
31 | x = utils.force_array(numpy.array([1], numpy.float32),
32 | dtype=self.dtype)
33 | self.assertIsInstance(x, numpy.ndarray)
34 | if self.dtype is None:
35 | self.assertEqual(x.dtype, numpy.float32)
36 | else:
37 | self.assertEqual(x.dtype, self.dtype)
38 |
39 |
40 | class TestForceType(unittest.TestCase):
41 |
42 | def test_force_type_scalar(self):
43 | x = numpy.int32(1)
44 | y = utils.force_type(numpy.dtype(numpy.float32), x)
45 | self.assertEqual(y.dtype, numpy.float32)
46 |
47 | def test_force_type_array(self):
48 | x = numpy.array([1], dtype=numpy.int32)
49 | y = utils.force_type(numpy.dtype(numpy.float32), x)
50 | self.assertEqual(y.dtype, numpy.float32)
51 |
52 | def test_force_type_array_no_change(self):
53 | x = numpy.array([1], dtype=numpy.float32)
54 | y = utils.force_type(numpy.dtype(numpy.float32), x)
55 | self.assertEqual(y.dtype, numpy.float32)
56 |
57 |
58 | testing.run_module(__name__, __file__)
59 |
--------------------------------------------------------------------------------
/chainer/functions/math/cumsum.py:
--------------------------------------------------------------------------------
1 | from chainer.backends import cuda
2 | from chainer import function_node
3 | from chainer.functions.array import flip
4 | from chainer.utils import type_check
5 |
6 |
7 | class Cumsum(function_node.FunctionNode):
8 | """Cumulative sum of array elements over a given axis."""
9 |
10 | def __init__(self, axis=None):
11 | if isinstance(axis, int) or axis is None:
12 | self.axis = axis
13 | else:
14 | raise TypeError('axis must be int or None')
15 |
16 | def check_type_forward(self, in_types):
17 | type_check.expect(
18 | in_types.size() == 1,
19 | in_types[0].dtype.kind == 'f',
20 | )
21 |
22 | if self.axis is not None:
23 | if self.axis >= 0:
24 | type_check.expect(self.axis < in_types[0].ndim)
25 | else:
26 | type_check.expect(-self.axis - 1 < in_types[0].ndim)
27 |
28 | def forward(self, inputs):
29 | x, = inputs
30 | self._in_shape = x.shape
31 | xp = cuda.get_array_module(x)
32 | return xp.cumsum(x, axis=self.axis),
33 |
34 | def backward(self, indexes, grad_outputs):
35 | gy = grad_outputs[0]
36 | axis = self.axis
37 |
38 | if axis is not None:
39 | gx = flip.flip(cumsum(flip.flip(gy, axis), axis), axis)
40 | else:
41 | gx = flip.flip(cumsum(flip.flip(gy, 0), 0), 0)
42 | gx = gx.reshape(self._in_shape)
43 |
44 | return gx,
45 |
46 |
47 | def cumsum(x, axis=None):
48 | """Cumulative sum of array elements over a given axis.
49 |
50 | Args:
51 | x (:class:`~chainer.Variable` or :class:`numpy.ndarray` or \
52 | :class:`cupy.ndarray`):
53 | Elements to calculate the cumulative sum.
54 | axis (int or None):
55 | Axis along which the cumulative sum is taken.
56 | If it is not specified, the input is flattened.
57 |
58 | Returns:
59 | ~chainer.Variable: Output variable.
60 |
61 | """
62 | return Cumsum(axis).apply((x,))[0]
63 |
--------------------------------------------------------------------------------
/chainer/functions/array/copy.py:
--------------------------------------------------------------------------------
1 | from chainer.backends import cuda
2 | from chainer import function_node
3 | from chainer.utils import type_check
4 |
5 |
6 | class Copy(function_node.FunctionNode):
7 |
8 | """Copies the input variable onto the specified device."""
9 |
10 | def __init__(self, out_device):
11 | self.out_device = out_device
12 |
13 | def check_type_forward(self, in_types):
14 | type_check.expect(
15 | in_types.size() == 1
16 | )
17 |
18 | def forward(self, inputs):
19 | x, = inputs
20 | self._in_device = cuda.get_device_from_array(x).id
21 | if int(self.out_device) == -1:
22 | return cuda.to_cpu(x),
23 | else:
24 | return cuda.to_gpu(x, self.out_device),
25 |
26 | def backward(self, indexes, grad_outputs):
27 | return Copy(self._in_device).apply(grad_outputs)
28 |
29 |
30 | def copy(x, dst):
31 | """Copies the input variable onto the specified device.
32 |
33 | This function copies the array of input variable onto the device specified
34 | by ``dst``. When ``dst == -1``, it copies the array onto the host memory.
35 | This function supports copies from host to host, from host to device,
36 | from device to device and from device to host.
37 |
38 | Args:
39 | x (:class:`~chainer.Variable` or :class:`numpy.ndarray` or \
40 | :class:`cupy.ndarray`):
41 | Variable to be copied.
42 | dst (int): Target device specifier.
43 |
44 | Returns:
45 | ~chainer.Variable: Output variable.
46 |
47 | .. admonition:: Example
48 |
49 | >>> import chainer.backends.cuda as cuda
50 | >>> x = np.random.uniform(-1, 1, (5, 10))
51 | >>> cuda.get_device_from_array(x).id
52 | -1
53 | >>> y = F.copy(x, 0) # from host to device0
54 | >>> cuda.get_device_from_array(y.data).id
55 | 0
56 | >>> z = F.copy(y, -1) # from device0 to host
57 | >>> cuda.get_device_from_array(z.data).id
58 | -1
59 |
60 | """
61 | y, = Copy(dst).apply((x,))
62 | return y
63 |
--------------------------------------------------------------------------------
/chainer/functions/math/hyperbolic.py:
--------------------------------------------------------------------------------
1 | from chainer.backends import cuda
2 | from chainer import function_node
3 | from chainer import utils
4 | from chainer.utils import type_check
5 |
6 |
7 | class Cosh(function_node.FunctionNode):
8 |
9 | @property
10 | def label(self):
11 | return 'cosh'
12 |
13 | def check_type_forward(self, in_types):
14 | type_check.expect(
15 | in_types.size() == 1,
16 | in_types[0].dtype.kind == 'f',
17 | )
18 |
19 | def forward(self, x):
20 | self.retain_inputs((0,))
21 | xp = cuda.get_array_module(*x)
22 | return utils.force_array(xp.cosh(x[0])),
23 |
24 | def backward(self, indexes, gy):
25 | x = self.get_retained_inputs()
26 | gx = sinh(x[0])
27 | gx *= gy[0]
28 | return gx,
29 |
30 |
31 | def cosh(x):
32 | """Elementwise hyperbolic cosine function.
33 |
34 | .. math::
35 | y_i = \\cosh x_i.
36 |
37 | Args:
38 | x (~chainer.Variable): Input variable.
39 |
40 | Returns:
41 | ~chainer.Variable: Output variable.
42 | """
43 | return Cosh().apply((x,))[0]
44 |
45 |
46 | class Sinh(function_node.FunctionNode):
47 |
48 | @property
49 | def label(self):
50 | return 'sinh'
51 |
52 | def check_type_forward(self, in_types):
53 | type_check.expect(
54 | in_types.size() == 1,
55 | in_types[0].dtype.kind == 'f',
56 | )
57 |
58 | def forward(self, x):
59 | self.retain_inputs((0,))
60 | xp = cuda.get_array_module(*x)
61 | return utils.force_array(xp.sinh(x[0])),
62 |
63 | def backward(self, x, gy):
64 | x = self.get_retained_inputs()
65 | gx = cosh(x[0])
66 | gx *= gy[0]
67 | return gx,
68 |
69 |
70 | def sinh(x):
71 | """Elementwise hyperbolic sine function.
72 |
73 | .. math::
74 | y_i = \\sinh x_i.
75 |
76 | Args:
77 | x (~chainer.Variable): Input variable.
78 |
79 | Returns:
80 | ~chainer.Variable: Output variable.
81 | """
82 | return Sinh().apply((x,))[0]
83 |
--------------------------------------------------------------------------------
/tests/chainer_tests/functions_tests/array_tests/test_reshape.py:
--------------------------------------------------------------------------------
1 | import unittest
2 |
3 | import numpy
4 |
5 | import chainer
6 | from chainer.backends import cuda
7 | from chainer import functions
8 | from chainer import testing
9 | from chainer.testing import attr
10 |
11 |
12 | @testing.parameterize(*testing.product({
13 | 'in_shape': [(4, 3, 2)],
14 | 'out_shape': [(2, 2, 6), (2, -1, 6)],
15 | 'dtype': [numpy.float16, numpy.float32, numpy.float64],
16 | }))
17 | class TestReshape(unittest.TestCase):
18 |
19 | def setUp(self):
20 | self.x = numpy.random.uniform(-1, 1, self.in_shape).astype(self.dtype)
21 |
22 | def check_forward(self, x_data):
23 | shape = self.out_shape
24 | x = chainer.Variable(x_data)
25 | y = functions.reshape(x, shape)
26 | self.assertEqual(y.data.dtype, self.dtype)
27 | self.assertTrue((self.x.reshape(shape) == cuda.to_cpu(y.data)).all())
28 |
29 | def test_forward_cpu(self):
30 | self.check_forward(self.x)
31 |
32 | @attr.gpu
33 | def test_forward_gpu(self):
34 | self.check_forward(cuda.to_gpu(self.x))
35 |
36 | def check_backward(self, x_data):
37 | x = chainer.Variable(x_data)
38 | y = functions.reshape(x, self.out_shape)
39 | y.grad = y.data
40 | y.backward()
41 | testing.assert_allclose(x.data, x.grad, atol=0, rtol=0)
42 |
43 | def test_backward_cpu(self):
44 | self.check_backward(self.x)
45 |
46 | @attr.gpu
47 | def test_backward_gpu(self):
48 | self.check_backward(cuda.to_gpu(self.x))
49 |
50 |
51 | class TestReshapeSkip(unittest.TestCase):
52 |
53 | shape = (2, 3)
54 |
55 | def setUp(self):
56 | self.data = numpy.random.uniform(0, 1, self.shape)
57 |
58 | def test_ndarray(self):
59 | ret = functions.reshape(self.data, self.shape)
60 | self.assertIs(self.data, ret.data)
61 |
62 | def test_variable(self):
63 | x = chainer.Variable(self.data)
64 | ret = functions.reshape(x, self.shape)
65 | self.assertIs(x, ret)
66 |
67 |
68 | testing.run_module(__name__, __file__)
69 |
--------------------------------------------------------------------------------
/tests/chainer_tests/testing_tests/test_parameterized.py:
--------------------------------------------------------------------------------
1 | import unittest
2 |
3 | from chainer import testing
4 |
5 |
6 | @testing.parameterize(
7 | {'actual': {'a': [1, 2], 'b': [3, 4, 5]},
8 | 'expect': [{'a': 1, 'b': 3}, {'a': 1, 'b': 4}, {'a': 1, 'b': 5},
9 | {'a': 2, 'b': 3}, {'a': 2, 'b': 4}, {'a': 2, 'b': 5}]},
10 | {'actual': {'a': [1, 2]}, 'expect': [{'a': 1}, {'a': 2}]},
11 | {'actual': {'a': [1, 2], 'b': []}, 'expect': []},
12 | {'actual': {'a': []}, 'expect': []},
13 | {'actual': {}, 'expect': [{}]})
14 | class ProductTest(unittest.TestCase):
15 |
16 | def test_product(self):
17 | self.assertListEqual(testing.product(self.actual), self.expect)
18 |
19 |
20 | @testing.parameterize(
21 | {'actual': [[{'a': 1, 'b': 3}, {'a': 2, 'b': 4}], [{'c': 5}, {'c': 6}]],
22 | 'expect': [{'a': 1, 'b': 3, 'c': 5}, {'a': 1, 'b': 3, 'c': 6},
23 | {'a': 2, 'b': 4, 'c': 5}, {'a': 2, 'b': 4, 'c': 6}]},
24 | {'actual': [[{'a': 1}, {'a': 2}], [{'b': 3}, {'b': 4}, {'b': 5}]],
25 | 'expect': [{'a': 1, 'b': 3}, {'a': 1, 'b': 4}, {'a': 1, 'b': 5},
26 | {'a': 2, 'b': 3}, {'a': 2, 'b': 4}, {'a': 2, 'b': 5}]},
27 | {'actual': [[{'a': 1}, {'a': 2}]], 'expect': [{'a': 1}, {'a': 2}]},
28 | {'actual': [[{'a': 1}, {'a': 2}], []], 'expect': []},
29 | {'actual': [[]], 'expect': []},
30 | {'actual': [], 'expect': [{}]})
31 | class ProductDictTest(unittest.TestCase):
32 |
33 | def test_product_dict(self):
34 | self.assertListEqual(testing.product_dict(*self.actual), self.expect)
35 |
36 |
37 | def f(x):
38 | return x
39 |
40 |
41 | class C(object):
42 |
43 | def __call__(self, x):
44 | return x
45 |
46 | def method(self, x):
47 | return x
48 |
49 |
50 | @testing.parameterize(
51 | {'callable': f},
52 | {'callable': lambda x: x},
53 | {'callable': C()},
54 | {'callable': C().method}
55 | )
56 | class TestParameterize(unittest.TestCase):
57 |
58 | def test_callable(self):
59 | y = self.callable(1)
60 | self.assertEqual(y, 1)
61 |
62 |
63 | testing.run_module(__name__, __file__)
64 |
--------------------------------------------------------------------------------
/tests/chainer_tests/test_init.py:
--------------------------------------------------------------------------------
1 | import unittest
2 |
3 | import chainer
4 | from chainer.backends import cuda
5 | from chainer import testing
6 | from chainer.testing import attr
7 |
8 |
9 | class TestUseCuDNN(unittest.TestCase):
10 |
11 | @attr.cudnn
12 | def test_valid_case_combination(self):
13 | with chainer.using_config('use_cudnn', 'always'):
14 | self.assertTrue(chainer.should_use_cudnn('==always'))
15 | self.assertTrue(chainer.should_use_cudnn('>=auto'))
16 |
17 | with chainer.using_config('use_cudnn', 'auto'):
18 | self.assertFalse(chainer.should_use_cudnn('==always'))
19 | self.assertTrue(chainer.should_use_cudnn('>=auto'))
20 |
21 | with chainer.using_config('use_cudnn', 'never'):
22 | self.assertFalse(chainer.should_use_cudnn('==always'))
23 | self.assertFalse(chainer.should_use_cudnn('>=auto'))
24 |
25 | @unittest.skip(not cuda.cudnn_enabled)
26 | def test_no_cudnn_available(self):
27 | with chainer.using_config('use_cudnn', 'always'):
28 | self.assertFalse(chainer.should_use_cudnn('==always'))
29 | self.assertFalse(chainer.should_use_cudnn('>=auto'))
30 |
31 | @attr.cudnn
32 | def test_invalid_level(self):
33 | self.assertRaises(ValueError, chainer.should_use_cudnn, '==auto')
34 |
35 | @attr.cudnn
36 | def test_invalid_config(self):
37 | with chainer.using_config('use_cudnn', True):
38 | self.assertRaises(ValueError, chainer.should_use_cudnn, '>=auto')
39 |
40 | with chainer.using_config('use_cudnn', False):
41 | self.assertRaises(ValueError, chainer.should_use_cudnn, '>=auto')
42 |
43 | with chainer.using_config('use_cudnn', 'on'):
44 | self.assertRaises(ValueError, chainer.should_use_cudnn, '>=auto')
45 |
46 | @attr.cudnn
47 | def test_higher_version_required(self):
48 | with chainer.using_config('use_cudnn', 'always'):
49 | self.assertFalse(chainer.should_use_cudnn(
50 | '>=auto', cuda.cuda.cudnn.getVersion() + 1))
51 |
52 |
53 | testing.run_module(__name__, __file__)
54 |
--------------------------------------------------------------------------------
/chainer/functions/array/rollaxis.py:
--------------------------------------------------------------------------------
1 | import six
2 |
3 | from chainer.backends import cuda
4 | from chainer import function_node
5 | from chainer.utils import type_check
6 |
7 |
8 | class Rollaxis(function_node.FunctionNode):
9 |
10 | """Roll axis of an array."""
11 |
12 | def __init__(self, axis, start):
13 | if not isinstance(axis, six.integer_types):
14 | raise TypeError('axis must be int')
15 | if not isinstance(start, six.integer_types):
16 | raise TypeError('start must be int')
17 |
18 | self.axis = axis
19 | self.start = start
20 |
21 | def check_type_forward(self, in_types):
22 | type_check.expect(in_types.size() == 1)
23 | x_type = in_types[0]
24 |
25 | if self.axis >= 0:
26 | type_check.expect(x_type.ndim > self.axis)
27 | else:
28 | type_check.expect(x_type.ndim > -self.axis - 1)
29 |
30 | if self.start >= 0:
31 | type_check.expect(x_type.ndim >= self.start)
32 | else:
33 | type_check.expect(x_type.ndim > -self.start - 1)
34 |
35 | def forward(self, inputs):
36 | self.retain_inputs(())
37 | self._in_ndim = inputs[0].ndim
38 | xp = cuda.get_array_module(*inputs)
39 | return xp.rollaxis(inputs[0], self.axis, self.start),
40 |
41 | def backward(self, indexes, gy):
42 | axis = self.axis
43 | if axis < 0:
44 | axis += self._in_ndim
45 | start = self.start
46 | if start < 0:
47 | start += self._in_ndim
48 |
49 | if axis > start:
50 | axis += 1
51 | else:
52 | start -= 1
53 |
54 | return Rollaxis(start, axis).apply(gy)
55 |
56 |
57 | def rollaxis(x, axis, start=0):
58 | """Roll the axis backwards to the given position.
59 |
60 | Args:
61 | x (~chainer.Variable): Input variable.
62 | axis (int): The axis to roll backwards.
63 | start (int): The place to which the axis is moved.
64 |
65 | Returns:
66 | ~chainer.Variable: Variable whose axis is rolled.
67 | """
68 | return Rollaxis(axis, start).apply((x,))[0]
69 |
--------------------------------------------------------------------------------
/tests/chainer_tests/datasets_tests/test_concatenated_dataset.py:
--------------------------------------------------------------------------------
1 | import numpy as np
2 | import six
3 | import unittest
4 |
5 |
6 | from chainer.datasets import ConcatenatedDataset
7 | from chainer import testing
8 |
9 |
10 | @testing.parameterize(
11 | # basic usage
12 | {'datasets': (
13 | np.random.uniform(size=(5, 3, 48, 32)),
14 | np.random.uniform(size=(15, 3, 64, 48)),
15 | )},
16 | # more than two datasets
17 | {'datasets': (
18 | np.random.uniform(size=(5, 3, 48, 32)),
19 | np.random.uniform(size=(15, 3, 16, 48)),
20 | np.random.uniform(size=(20, 3, 5, 5)),
21 | )},
22 | # single dataset
23 | {'datasets': (
24 | np.random.uniform(size=(5, 3, 48, 32)),
25 | )},
26 | # no dataset
27 | {'datasets': ()},
28 | # some datasets are empty
29 | {'datasets': (
30 | np.random.uniform(size=(5, 3, 48, 32)),
31 | [],
32 | np.random.uniform(size=(20, 3, 5, 5)),
33 | [],
34 | )},
35 | # all datasets are empty
36 | {'datasets': ([], [], [])},
37 | )
38 | class TestConcatenatedDataset(unittest.TestCase):
39 |
40 | def setUp(self):
41 | self.concatenated_dataset = ConcatenatedDataset(*self.datasets)
42 | self.expected_dataset = [
43 | sample for dataset in self.datasets for sample in dataset]
44 |
45 | def test_concatenated_dataset(self):
46 | self.assertEqual(
47 | len(self.concatenated_dataset), len(self.expected_dataset))
48 |
49 | for i, expected in enumerate(self.expected_dataset):
50 | np.testing.assert_equal(self.concatenated_dataset[i], expected)
51 |
52 | def test_concatenated_dataset_slice(self):
53 | concatenated_slice = self.concatenated_dataset[1:8:2]
54 | expected_slice = self.concatenated_dataset[1:8:2]
55 |
56 | self.assertEqual(
57 | len(concatenated_slice), len(expected_slice))
58 |
59 | for concatenated, expected in six.moves.zip(
60 | concatenated_slice, expected_slice):
61 | np.testing.assert_equal(concatenated, expected)
62 |
63 |
64 | testing.run_module(__name__, __file__)
65 |
--------------------------------------------------------------------------------
/tests/chainer_tests/test_init_docstring.py:
--------------------------------------------------------------------------------
1 | import importlib
2 | import inspect
3 | import pkgutil
4 | import unittest
5 |
6 | import chainer
7 | from chainer import testing
8 |
9 |
10 | def get_init_doc(klass):
11 | for attr in inspect.classify_class_attrs(klass):
12 | if attr.name == '__init__':
13 | if attr.defining_class is klass:
14 | return attr.object.__doc__
15 | else:
16 | # Ignore __init__ method inherited from a super class
17 | return None
18 | return None
19 |
20 |
21 | class TestInitDocstring(unittest.TestCase):
22 | """Make sure classes do not have a docstring in their __init__ method."""
23 |
24 | def check_init_docstring(self, mod, errors):
25 | for name, value in inspect.getmembers(mod):
26 | if not inspect.isclass(value):
27 | continue
28 | if 'chainer' not in value.__module__:
29 | continue
30 | init_doc = get_init_doc(value)
31 | if init_doc == object.__init__.__doc__:
32 | # Ignore doc string inherited from `object`
33 | continue
34 |
35 | if init_doc is not None:
36 | # Do not permit to write docstring in `__init__`
37 | errors.append((mod, value, init_doc))
38 |
39 | def test_init_docstring_empty(self):
40 | errors = []
41 | root = chainer.__path__
42 | for loader, modname, ispkg in pkgutil.walk_packages(root, 'chainer.'):
43 | # Skip modules generated by protobuf.
44 | if '_pb2' in modname:
45 | continue
46 |
47 | try:
48 | mod = importlib.import_module(modname)
49 | except ImportError:
50 | continue
51 |
52 | self.check_init_docstring(mod, errors)
53 |
54 | if errors:
55 | msg = ''
56 | for mod, value, init_doc in errors:
57 | msg += '{}.{} has __init__.__doc__:\n{}\n\n'.format(
58 | mod.__name__, value, init_doc)
59 | self.fail(msg)
60 |
61 |
62 | testing.run_module(__name__, __file__)
63 |
--------------------------------------------------------------------------------
/chainer/functions/loss/mean_squared_error.py:
--------------------------------------------------------------------------------
1 | import numpy
2 |
3 | from chainer import function_node
4 | import chainer.functions
5 | from chainer.utils import type_check
6 |
7 |
8 | class MeanSquaredError(function_node.FunctionNode):
9 |
10 | """Mean squared error (a.k.a. Euclidean loss) function."""
11 |
12 | def check_type_forward(self, in_types):
13 | type_check.expect(in_types.size() == 2)
14 | type_check.expect(
15 | in_types[0].dtype == numpy.float32,
16 | in_types[1].dtype == numpy.float32,
17 | in_types[0].shape == in_types[1].shape
18 | )
19 |
20 | def forward_cpu(self, inputs):
21 | self.retain_inputs((0, 1))
22 | diff = (inputs[0] - inputs[1]).ravel()
23 | return numpy.array(diff.dot(diff) / diff.size, dtype=diff.dtype),
24 |
25 | def forward_gpu(self, inputs):
26 | self.retain_inputs((0, 1))
27 | diff = (inputs[0] - inputs[1]).ravel()
28 | return diff.dot(diff) / diff.dtype.type(diff.size),
29 |
30 | def backward(self, indexes, gy):
31 | x0, x1 = self.get_retained_inputs()
32 | ret = []
33 | diff = x0 - x1
34 | gy0 = chainer.functions.broadcast_to(gy[0], diff.shape)
35 | gx0 = gy0 * diff * (2. / diff.size)
36 | if 0 in indexes:
37 | ret.append(gx0)
38 | if 1 in indexes:
39 | ret.append(-gx0)
40 | return ret
41 |
42 |
43 | def mean_squared_error(x0, x1):
44 | """Mean squared error function.
45 |
46 | This function computes mean squared error between two variables. The mean
47 | is taken over the minibatch. Note that the error is not scaled by 1/2.
48 |
49 | Args:
50 | x0 (:class:`~chainer.Variable` or :class:`numpy.ndarray` or \
51 | :class:`cupy.ndarray`): Input variable.
52 | x1 (:class:`~chainer.Variable` or :class:`numpy.ndarray` or \
53 | :class:`cupy.ndarray`): Input variable.
54 |
55 | Returns:
56 | ~chainer.Variable:
57 | A variable holding an array representing the mean squared
58 | error of two inputs.
59 |
60 | """
61 | return MeanSquaredError().apply((x0, x1))[0]
62 |
--------------------------------------------------------------------------------
/.travis.yml:
--------------------------------------------------------------------------------
1 | language: python
2 |
3 | cache:
4 | - pip
5 | - ccache
6 |
7 | matrix:
8 | include:
9 | - os: linux
10 | python: "2.7"
11 | - os: linux
12 | python: "3.4"
13 | - os: linux
14 | python: "3.5"
15 |
16 | - os: osx
17 | language: generic
18 | env:
19 | - PYTHON_VERSION=2.7.10
20 | - PYENV_ROOT=~/.pyenv
21 | - PATH=$PYENV_ROOT/shims:$PATH:$PYENV_ROOT/bin
22 | if: (branch = master OR branch = v3) AND (NOT type in (pull_request))
23 | - os: osx
24 | language: generic
25 | env:
26 | - PYTHON_VERSION=3.4.4
27 | - PYENV_ROOT=~/.pyenv
28 | - PATH=$PYENV_ROOT/shims:$PATH:$PYENV_ROOT/bin
29 | if: (branch = master OR branch = v3) AND (NOT type in (pull_request))
30 | - os: osx
31 | language: generic
32 | env:
33 | - PYTHON_VERSION=3.5.1
34 | - PYENV_ROOT=~/.pyenv
35 | - PATH=$PYENV_ROOT/shims:$PATH:$PYENV_ROOT/bin
36 | if: (branch = master OR branch = v3) AND (NOT type in (pull_request))
37 |
38 | before_install:
39 | # Remove oclint as it conflicts with GCC (indirect dependency of hdf5)
40 | - if [[ $TRAVIS_OS_NAME = "osx" ]]; then
41 | brew update >/dev/null;
42 | brew outdated pyenv || brew upgrade --quiet pyenv;
43 |
44 | PYTHON_CONFIGURE_OPTS="--enable-unicode=ucs2" pyenv install -ks $PYTHON_VERSION;
45 | pyenv global $PYTHON_VERSION;
46 | python --version;
47 |
48 | brew cask uninstall oclint;
49 | brew install hdf5;
50 | fi
51 |
52 | install:
53 | - pip install -U pip wheel
54 | - python setup.py sdist
55 | - pip install dist/*.tar.gz
56 | - travis_wait pip install -U -e .[travis]
57 |
58 | script:
59 | - flake8
60 | - autopep8 -r . --diff | tee check_autopep8
61 | - test ! -s check_autopep8
62 | - cd tests
63 | - CHAINER_TEST_GPU_LIMIT=0 pytest -m "not slow and not cudnn and not ideep" chainer_tests
64 | - if [[ $TRAVIS_OS_NAME == "linux" ]]; then
65 | cd ..;
66 | READTHEDOCS=True python setup.py develop;
67 | fi
68 |
69 | sudo: false
70 |
71 | addons:
72 | apt:
73 | packages:
74 | - gfortran
75 | - libhdf5-serial-dev
76 | - liblapack-dev
77 |
--------------------------------------------------------------------------------