human
Complete this python program:
def ensemble(problems, weights=None):
assistant
#Here is the Python code you requested.

    """Ensemble of problems.
    Args:
    problems: List of problems. Each problem is specified by a dict containing
    the keys \name\ and \options\.
    weights: Optional list of weights for each problem.
    Returns:
    Sum of (weighted) losses.
    Raises:
    ValueError: If weights has an incorrect length.
    """
    if (weights and (len(weights) != len(problems))):
        raise ValueError('len(weights)    !=    len(problems)')
    build_fns = [getattr(sys.modules[__name__], p['name'])(**p['options']) for p in problems]
    def build():
        loss = 0
        for (i, build_fn) in enumerate(build_fns):
            with tf.variable_scope('problem_{}'.format(i)):
                loss_p = build_fn()
                if weights:
                    loss_p *= weights[i]
                loss += loss_p
        return loss
    return build