<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
 def einsum(xs, output_shape=None, reduced_dims=None, name=None):
 """Einstein summation. einsum(xs, output_shape) is equivalent to broadcasting all inputs to the union of all of their shapes, multiplying them componentwise, and finally reduce_summing down to output_shape. One common case of this is matrix multiplication: x has shape [a, b] y has shape [b, c] matmul(x, y) == einsum([x, y], output_shape=[a, c]) We provide a few options for specifying the output shape: If neither output_shape nor reduced_dims is specified, then the output shape is set to the contain all dimensions that appear exactly once in the inputs, in order of appearance. If output_shape is not specified, then the output shape is set to the contain all dimensions that appear in xs but not in reduced_dims, in the order that they appear in xs. If reduced_dims is also not specified, then reduced_dims is set to the set of all dimensions that appear at least twice in xs. If both output_shape and reduced_dims are specified, then we check that reduced_dims matches the set of dimensions present in xs but not in output_shape, and throw an exception if it does not. This helps to reduce bugs. Args: xs: a list of Tensors output_shape: an optional Shape. reduced_dims: an optional list of Dimensions. name: an optional string Returns: a Tensor Raises: ValueError: if reduced_dims contradicts output_shape """

  output_shape = convert_to_shape(output_shape)
  input_dim_count = collections.defaultdict(int)
  input_dims = []
  for x in xs:
    for d in x.shape.dims:
      if d not in input_dim_count:
        input_dims.append(d)
      input_dim_count[d] += 1
  if output_shape is None:
    if reduced_dims is None:
      reduced_dims = [d for d, c in six.iteritems(input_dim_count) if c > 1]
    output_shape = Shape([d for d in input_dims if d not in reduced_dims])
  elif reduced_dims is not None:
    for d in reduced_dims:
      if not isinstance(d, Dimension):
        raise ValueError("reduced_dims must be a list of Dimensions.  Got %s."
                         % (reduced_dims,))
    computed_reduced_dims = [
        d for d in input_dims if d not in output_shape.dims]
    if set(computed_reduced_dims) != set(reduced_dims):
      raise ValueError(
          "Specified reduced_dims and output_shape do not match."
          " xs=%s output_shape=%s reduced_dims=%s " % (
              xs, output_shape, reduced_dims))
  return EinsumOperation(xs, output_shape, name=name).outputs[0]