def apply_penalty(tensor_or_tensors, penalty, **kwargs):
 try:
  return sum((penalty(x, **kwargs) for x in tensor_or_tensors))
 except (TypeError, ValueError):
  return penalty(tensor_or_tensors, **kwargs)

def apply_penalty ( tensor_or_tensors , penalty , ** kwargs ) : try : return sum ( ( penalty ( x , ** kwargs ) for x in tensor_or_tensors ) ) except ( TypeError , ValueError ) : return penalty ( tensor_or_tensors , ** kwargs )
computes the total cost for applying a specified penalty to a tensor or group of tensors .