def permute_dimensions(x, pattern):
    """Transpose dimensions.
    pattern should be a tuple or list of
    dimension indices, e.g. [0, 2, 1].
    """
    pattern = tuple(pattern)
    return x.transpose(pattern)