def z_score_normalize(tensor, scale_to_range=None):
    """Performs z-score normalization on a tensor and scales to a range if specified."""
    mean = tensor.mean()
    std = tensor.std()

    tensor = (tensor - mean) / std

    if scale_to_range:
        delta1 = tensor.max() - tensor.min()
        delta2 = scale_to_range[1] - scale_to_range[0]
        tensor = (delta2 * (tensor - tensor.min()) / delta1) + scale_to_range[0]

    return tensor