chainer.functions.reshape(x, shape)[source]

入力値をコピーせずに形を変える。

 

Parameters:
  • x (Variable or numpy.ndarray or cupy.ndarray) –入力値。
  • shape (tuple of int s) – 出力配列のshapeとしたいもの。shape の配列が含む要素数は入力配列のshapeの配列が含む要素数と同等でなければならない。1 shape dimensionは -1をとり得る。この場合、その値は. 配列と残った次元の長さと予想される。
Returns:

入力値を変形したバージョンのVariable。

Return type:

Variable

See also

numpy.reshape(), cupy.reshape()

 

Example

 



>>> x = np.array([[1, 2, 3, 4], [5, 6, 7, 8]])
>>> y = F.reshape(x, (8,))
>>> y.shape (8,)
>>> y.data
array([1, 2, 3, 4, 5, 6, 7, 8])
>>> y = F.reshape(x, (4, -1)) # the shape of output is inferred
>>> y.shape (4, 2)
>>> y.data
array([[1, 2], [3, 4], [5, 6], [7, 8]])
>>> y = F.reshape(x, (4, 3)) # the shape of input and output are not consistent
Traceback (most recent call last):
...
Invalid operation is performed in: Reshape (Forward)
Expect: prod(in_types[0].shape) == prod((4, 3))
Actual: 8 != 12