Skip to content

Reshape

Description

Info

Parent class: Module

Derived classes: -

This module reduces the tensor to a new specified dimension.

Initializing

def __init__(self, shape, showWarnings=True, name=None):

Parameters

Parameter Allowed types Description Default
shape tuple Tuple of a new tensor shape -
showWarnings bool Whether to show warnings True
name str Layer name None

Explanations

shape - can take special indicators 0 and -1 (see examples).

Examples

Necessary imports.

import numpy as np
from PuzzleLib.Backend import gpuarray
from PuzzleLib.Modules import Reshape

Info

gpuarray is required to properly place the tensor in the GPU

inshape = (10, 10, 10, 10)
data = gpuarray.to_gpu(np.random.randn(*inshape).astype(np.float32))
reshape = Reshape((10, 10, 100))
reshape(data)
print(reshape.data.shape)
(10, 10, 100)

We can pass -1 in the shape tuple for the axis whose dimension will be calculated according to the residual principle:

reshape = Reshape((10, -1, 50))
reshape(data)
print(reshape.data.shape)
(10, 20, 50)

It is also possible to pass 0 to leave the dimension of the corresponding axis unchanged:

inshape = (1, 4, 7, 7)
data = gpuarray.to_gpu(np.random.randn(*inshape).astype(np.float32))\
reshape = Reshape((0, 2, -1, 0))
reshape(data)
print(reshape.data.shape)
(1, 2, 14, 7)