code: def padded_variance_explained(predictions,
                                labels,
                                weights_fn=common_layers.weights_all):
    
    predictions, labels = common_layers.pad_with_zeros(predictions, labels)
    targets = labels
    weights = weights_fn(targets)
  
    y_bar = tf.reduce_mean(weights * targets)
    tot_ss = tf.reduce_sum(weights * tf.pow(targets - y_bar, 2))
    res_ss = tf.reduce_sum(weights * tf.pow(targets - predictions, 2))
    r2 = 1. - res_ss / tot_ss
    return r2, tf.reduce_sum(weights)
docs: Explained variance, also known as R^2.