def l1_loss(tensor, batch_norm=True, reduce_batches=True): if struct.isstruct(tensor): all_tensors = struct.flatten(tensor) return sum(l1_loss(tensor, batch_norm, reduce_batches) for tensor in all_tensors) if reduce_batches: total_loss = math.sum(math.abs(tensor)) else: total_loss = math.sum(math.abs(tensor), axis=list(range(1, len(tensor.shape)))) if batch_norm and reduce_batches: batch_size = math.shape(tensor)[0] return math.div(total_loss, math.to_float(batch_size)) else: return total_loss
def _max_residual_condition(residual_index, accuracy): """continue if the maximum deviation from zero is bigger than desired accuracy""" if accuracy is None: return lambda *args: True else: return lambda *args: math.max(math.abs(args[residual_index])) > accuracy
def loop_condition(_1, _2, _3, residual, _i): '''continue if the maximum deviation from zero is bigger than desired accuracy''' return math.max(math.abs(residual)) >= accuracy