提交 d9a66a7d authored 作者: Gijs van Tulder's avatar Gijs van Tulder

pad_dims examples, and keep as many dimensions as possible.

上级 d39be759
...@@ -341,6 +341,24 @@ def pad_dims(input, leftdims, rightdims): ...@@ -341,6 +341,24 @@ def pad_dims(input, leftdims, rightdims):
This reduces or expands the number of dimensions of the input to This reduces or expands the number of dimensions of the input to
exactly `leftdims`, by adding extra dimensions on the left or by exactly `leftdims`, by adding extra dimensions on the left or by
combining some existing dimensions on the left of the input. combining some existing dimensions on the left of the input.
Use `unpad_dims` to reshape back to the original dimensions.
Examples
--------
Given input of shape (3, 5, 7), ``pad_dims(input, 2, 2)``
adds a singleton dimension and reshapes to (3, 1, 5, 7).
Given that output from pad_dims, ``unpad_dims(output, input, 2, 2)``
reshapes back to (3, 5, 7).
Given input of shape (3, 5, 7, 9), ``pad_dims(input, 2, 2)``
does not reshape and returns output with shape (3, 5, 7, 9).
Given input of shape (3, 5, 7, 9, 11), ``pad_dims(input, 2, 2)``
combines the first two dimensions and reshapes to (8, 7, 9, 11).
Given input of shape (3, 5, 7, 9), ``pad_dims(input, 2, 3)``
adds a singleton dimension and reshapes to (3, 1, 5, 7, 9).
""" """
assert input.ndim >= rightdims assert input.ndim >= rightdims
...@@ -350,15 +368,25 @@ def pad_dims(input, leftdims, rightdims): ...@@ -350,15 +368,25 @@ def pad_dims(input, leftdims, rightdims):
# extract image dimensions # extract image dimensions
img_shape = input.shape[-rightdims:] img_shape = input.shape[-rightdims:]
# count the number of "leading" dimensions, store as dmatrix non_pool_ndim = input.ndim - rightdims
batch_size = tensor.prod(input.shape[:-rightdims]) if non_pool_ndim < leftdims:
batch_size = tensor.shape_padright(batch_size, 1) # too few dimensions, pad on the left
dummy_dims = tensor.as_tensor([1] * (leftdims - non_pool_ndim))
# store in the required shape, for example as a 4D tensor new_shape = tensor.join(0, dummy_dims,
# with shape: (batch_size,1,height,width) input.shape[:non_pool_ndim],
new_shape = tensor.cast(tensor.join(0, batch_size, img_shape)
tensor.as_tensor([1] * (leftdims - 1)), else:
img_shape), 'int64') # too many dimensions, combine the leading dimensions
batched_ndim = non_pool_ndim - leftdims + 1
batch_size = tensor.prod(input.shape[:batched_ndim])
# convert to a vector for tensor.join
batch_size = tensor.shape_padright(batch_size, 1)
new_shape = tensor.join(0, batch_size,
input.shape[batched_ndim:non_pool_ndim],
img_shape)
# store in the required shape
new_shape = tensor.cast(new_shape, 'int64')
input_ND = GpuReshape(leftdims + rightdims)(input, new_shape) input_ND = GpuReshape(leftdims + rightdims)(input, new_shape)
return input_ND return input_ND
......
...@@ -138,6 +138,24 @@ def pad_dims(input, leftdims, rightdims): ...@@ -138,6 +138,24 @@ def pad_dims(input, leftdims, rightdims):
This reduces or expands the number of dimensions of the input to This reduces or expands the number of dimensions of the input to
exactly `leftdims`, by adding extra dimensions on the left or by exactly `leftdims`, by adding extra dimensions on the left or by
combining some existing dimensions on the left of the input. combining some existing dimensions on the left of the input.
Use `unpad_dims` to reshape back to the original dimensions.
Examples
--------
Given input of shape (3, 5, 7), ``pad_dims(input, 2, 2)``
adds a singleton dimension and reshapes to (3, 1, 5, 7).
Given that output from pad_dims, ``unpad_dims(output, input, 2, 2)``
reshapes back to (3, 5, 7).
Given input of shape (3, 5, 7, 9), ``pad_dims(input, 2, 2)``
does not reshape and returns output with shape (3, 5, 7, 9).
Given input of shape (3, 5, 7, 9, 11), ``pad_dims(input, 2, 2)``
combines the first two dimensions and reshapes to (8, 7, 9, 11).
Given input of shape (3, 5, 7, 9), ``pad_dims(input, 2, 3)``
adds a singleton dimension and reshapes to (3, 1, 5, 7, 9).
""" """
assert input.ndim >= rightdims assert input.ndim >= rightdims
...@@ -147,15 +165,25 @@ def pad_dims(input, leftdims, rightdims): ...@@ -147,15 +165,25 @@ def pad_dims(input, leftdims, rightdims):
# extract image dimensions # extract image dimensions
img_shape = input.shape[-rightdims:] img_shape = input.shape[-rightdims:]
# count the number of "leading" dimensions, store as dmatrix non_pool_ndim = input.ndim - rightdims
batch_size = tensor.prod(input.shape[:-rightdims]) if non_pool_ndim < leftdims:
batch_size = tensor.shape_padright(batch_size, 1) # too few dimensions, pad on the left
dummy_dims = tensor.as_tensor([1] * (leftdims - non_pool_ndim))
# store in the required shape, for example as a 4D tensor new_shape = tensor.join(0, dummy_dims,
# with shape: (batch_size,1,height,width) input.shape[:non_pool_ndim],
new_shape = tensor.cast(tensor.join(0, batch_size, img_shape)
tensor.as_tensor([1] * (leftdims - 1)), else:
img_shape), 'int64') # too many dimensions, combine the leading dimensions
batched_ndim = non_pool_ndim - leftdims + 1
batch_size = tensor.prod(input.shape[:batched_ndim])
# convert to a vector for tensor.join
batch_size = tensor.shape_padright(batch_size, 1)
new_shape = tensor.join(0, batch_size,
input.shape[batched_ndim:non_pool_ndim],
img_shape)
# store in the required shape
new_shape = tensor.cast(new_shape, 'int64')
input_ND = GpuReshape(leftdims + rightdims)(input, new_shape) input_ND = GpuReshape(leftdims + rightdims)(input, new_shape)
return input_ND return input_ND
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论