def set_dimension(tensor, axis, value):
    """Set the length of a tensor along the specified dimension.

  Args:
    tensor: Tensor to define shape of.
    axis: Dimension to set the static shape for.
    value: Integer holding the length.

  Raises:
    ValueError: When the tensor already has a different length specified.
  """
    shape = tensor.shape.as_list()
    if shape[axis] not in (value, None):
        message = 'Cannot set dimension {} of tensor {} to {}; is already {}.'
        raise ValueError(message.format(axis, tensor.name, value, shape[axis]))
    shape[axis] = value
    tensor.set_shape(shape)
