def compute_out_dims(in_dims, kernel_size, stride, padding, output_padding,
    dilation):
    """Computes the output dimensions of convolution.

  The formulas below are based on what Keras does.

  Args:
    in_dims: number of input dimensions.
    kernel_size: size of kernel.
    stride: size of stride.
    padding: amount of padding on both ends of input.
    output_padding: padding adjustment for disambiguating out_dims.
    dilation: amount of dilation for convolution.

  Returns:
    The computed value of output dimensions.
  """
    kernel_size = (kernel_size - 1) * dilation + 1
    if output_padding is None:
        if padding == 'same':
            out_dims = in_dims * stride
        elif padding == 'valid':
            out_dims = in_dims * stride + max(kernel_size - stride, 0)
    elif padding == 'same':
        out_dims = (in_dims - 1) * stride + output_padding
    elif padding == 'valid':
        out_dims = (in_dims - 1) * stride + kernel_size + output_padding
    return out_dims
