def fans(shape):
    """Returns fan_in and fan_out according to the shape.

    Assumes the len of shape is 2 or 4.
    """
    assert(len(shape) == 2 or len(shape) == 4)
    if len(shape) == 2:
        return shape[0], shape[1]
    else:
        S = shape[0] * shape[1]
        return S * shape[2], S * shape[3]