|
1 | | -import chainer |
2 | | - |
3 | | - |
4 | | -@chainer.dataset.converter() |
5 | | -def concat_mols(batch, device=None, padding=0): |
6 | | - """Concatenates a list of molecules into array(s). |
7 | | -
|
8 | | - This function converts an "array of tuples" into a "tuple of arrays". |
9 | | - Specifically, given a list of examples each of which consists of |
10 | | - a list of elements, this function first makes an array |
11 | | - by taking the element in the same position from each example |
12 | | - and concatenates them along the newly-inserted first axis |
13 | | - (called `batch dimension`) into one array. |
14 | | - It repeats this for all positions and returns the resulting arrays. |
15 | | -
|
16 | | - The output type depends on the type of examples in ``batch``. |
17 | | - For instance, consider each example consists of two arrays ``(x, y)``. |
18 | | - Then, this function concatenates ``x`` 's into one array, and ``y`` 's |
19 | | - into another array, and returns a tuple of these two arrays. Another |
20 | | - example: consider each example is a dictionary of two entries whose keys |
21 | | - are ``'x'`` and ``'y'``, respectively, and values are arrays. Then, this |
22 | | - function concatenates ``x`` 's into one array, and ``y`` 's into another |
23 | | - array, and returns a dictionary with two entries ``x`` and ``y`` whose |
24 | | - values are the concatenated arrays. |
25 | | -
|
26 | | - When the arrays to concatenate have different shapes, the behavior depends |
27 | | - on the ``padding`` value. If ``padding`` is ``None``, it raises an error. |
28 | | - Otherwise, it builds an array of the minimum shape that the |
29 | | - contents of all arrays can be substituted to. The padding value is then |
30 | | - used to the extra elements of the resulting arrays. |
31 | | -
|
32 | | - The current implementation is identical to |
33 | | - :func:`~chainer.dataset.concat_examples` of Chainer, except the default |
34 | | - value of the ``padding`` option is changed to ``0``. |
35 | | -
|
36 | | - .. admonition:: Example |
37 | | -
|
38 | | - >>> import numpy |
39 | | - >>> from chainer_chemistry.dataset.converters import concat_mols |
40 | | - >>> x0 = numpy.array([1, 2]) |
41 | | - >>> x1 = numpy.array([4, 5, 6]) |
42 | | - >>> dataset = [x0, x1] |
43 | | - >>> results = concat_mols(dataset) |
44 | | - >>> print(results) |
45 | | - [[1 2 0] |
46 | | - [4 5 6]] |
47 | | -
|
48 | | - .. seealso:: :func:`chainer.dataset.concat_examples` |
49 | | -
|
50 | | - Args: |
51 | | - batch (list): |
52 | | - A list of examples. This is typically given by a dataset |
53 | | - iterator. |
54 | | - device (int): |
55 | | - Device ID to which each array is sent. Negative value |
56 | | - indicates the host memory (CPU). If it is omitted, all arrays are |
57 | | - left in the original device. |
58 | | - padding: |
59 | | - Scalar value for extra elements. If this is None (default), |
60 | | - an error is raised on shape mismatch. Otherwise, an array of |
61 | | - minimum dimensionalities that can accommodate all arrays is |
62 | | - created, and elements outside of the examples are padded by this |
63 | | - value. |
64 | | -
|
65 | | - Returns: |
66 | | - Array, a tuple of arrays, or a dictionary of arrays: |
67 | | - The type depends on the type of each example in the batch. |
68 | | - """ |
69 | | - return chainer.dataset.concat_examples(batch, device, padding=padding) |
| 1 | +import chainer |
| 2 | + |
| 3 | + |
| 4 | +@chainer.dataset.converter() |
| 5 | +def concat_mols(batch, device=None, padding=0): |
| 6 | + """Concatenates a list of molecules into array(s). |
| 7 | +
|
| 8 | + This function converts an "array of tuples" into a "tuple of arrays". |
| 9 | + Specifically, given a list of examples each of which consists of |
| 10 | + a list of elements, this function first makes an array |
| 11 | + by taking the element in the same position from each example |
| 12 | + and concatenates them along the newly-inserted first axis |
| 13 | + (called `batch dimension`) into one array. |
| 14 | + It repeats this for all positions and returns the resulting arrays. |
| 15 | +
|
| 16 | + The output type depends on the type of examples in ``batch``. |
| 17 | + For instance, consider each example consists of two arrays ``(x, y)``. |
| 18 | + Then, this function concatenates ``x`` 's into one array, and ``y`` 's |
| 19 | + into another array, and returns a tuple of these two arrays. Another |
| 20 | + example: consider each example is a dictionary of two entries whose keys |
| 21 | + are ``'x'`` and ``'y'``, respectively, and values are arrays. Then, this |
| 22 | + function concatenates ``x`` 's into one array, and ``y`` 's into another |
| 23 | + array, and returns a dictionary with two entries ``x`` and ``y`` whose |
| 24 | + values are the concatenated arrays. |
| 25 | +
|
| 26 | + When the arrays to concatenate have different shapes, the behavior depends |
| 27 | + on the ``padding`` value. If ``padding`` is ``None``, it raises an error. |
| 28 | + Otherwise, it builds an array of the minimum shape that the |
| 29 | + contents of all arrays can be substituted to. The padding value is then |
| 30 | + used to the extra elements of the resulting arrays. |
| 31 | +
|
| 32 | + The current implementation is identical to |
| 33 | + :func:`~chainer.dataset.concat_examples` of Chainer, except the default |
| 34 | + value of the ``padding`` option is changed to ``0``. |
| 35 | +
|
| 36 | + .. admonition:: Example |
| 37 | +
|
| 38 | + >>> import numpy |
| 39 | + >>> from chainer_chemistry.dataset.converters import concat_mols |
| 40 | + >>> x0 = numpy.array([1, 2]) |
| 41 | + >>> x1 = numpy.array([4, 5, 6]) |
| 42 | + >>> dataset = [x0, x1] |
| 43 | + >>> results = concat_mols(dataset) |
| 44 | + >>> print(results) |
| 45 | + [[1 2 0] |
| 46 | + [4 5 6]] |
| 47 | +
|
| 48 | + .. seealso:: :func:`chainer.dataset.concat_examples` |
| 49 | +
|
| 50 | + Args: |
| 51 | + batch (list): |
| 52 | + A list of examples. This is typically given by a dataset |
| 53 | + iterator. |
| 54 | + device (int): |
| 55 | + Device ID to which each array is sent. Negative value |
| 56 | + indicates the host memory (CPU). If it is omitted, all arrays are |
| 57 | + left in the original device. |
| 58 | + padding: |
| 59 | + Scalar value for extra elements. If this is None (default), |
| 60 | + an error is raised on shape mismatch. Otherwise, an array of |
| 61 | + minimum dimensionalities that can accommodate all arrays is |
| 62 | + created, and elements outside of the examples are padded by this |
| 63 | + value. |
| 64 | +
|
| 65 | + Returns: |
| 66 | + Array, a tuple of arrays, or a dictionary of arrays: |
| 67 | + The type depends on the type of each example in the batch. |
| 68 | + """ |
| 69 | + return chainer.dataset.concat_examples(batch, device, padding=padding) |
0 commit comments