<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
 def _convert_and_assert_per_example_weights_compatible( input_, per_example_weights, dtype):
 """Converts per_example_weights to a tensor and validates the shape."""

  per_example_weights = tf.convert_to_tensor(
      per_example_weights, name='per_example_weights', dtype=dtype)
  if input_.get_shape().ndims:
    expected_length = input_.get_shape().dims[0]
    message = ('per_example_weights must have rank 1 and length %s, but was: %s'
               % (expected_length, per_example_weights.get_shape()))
  else:
    expected_length = None
    message = ('per_example_weights must have rank 1 and length equal to the '
               'first dimension of inputs (unknown), but was: %s'
               % per_example_weights.get_shape())

  if per_example_weights.get_shape().ndims not in (1, None):
    raise ValueError(message)

  if not per_example_weights.get_shape().is_compatible_with((expected_length,)):
    raise ValueError(message)

  return per_example_weights