2.5 Flatten

Flattenレイヤは文字通り多次元配列を平坦化します。

from ivory.layers.core import Flatten

f = Flatten((2, 3))

for p in f.parameters:
    print(p)

[1] 2019-06-12 20:01:08 (185ms) python3 (185ms)

<Input('Flatten.1.x', (2, 3)) at 0x1e5316e57b8>
<Output('Flatten.1.y', (6,)) at 0x1e5316e5748>
import numpy as np

x = np.arange(2 * 2 * 3).reshape(2, 2, 3)
f.set_variables()
f.set_data(x)
print(f.x.d)

[2] 2019-06-12 20:01:08 (17.0ms) python3 (202ms)

[[[ 0  1  2]
  [ 3  4  5]]

 [[ 6  7  8]
  [ 9 10 11]]]
f.forward()
print(f.y.d)

[3] 2019-06-12 20:01:08 (7.00ms) python3 (209ms)

[[ 0  1  2  3  4  5]
 [ 6  7  8  9 10 11]]

実装コードを確認しておきます。

Code 2.8 Flattenクラス

class Flatten(Layer):
    def init(self):
        self.x.shape = self.shape
        self.shape = self.shape + (reduce(operator.mul, self.shape, 1),)
        self.y.shape = self.shape[-1:]

    def forward(self):
        self.y.d = self.x.d.reshape(self.x.d.shape[0], -1)

    def backward(self):
        self.x.g = self.y.g.reshape(*self.x.d.shape)