def categorical_crossentropy(target, output, from_logits=False): if from_logits: output = softmax(output) elif output.opname != 'softmax': output /= sum(output, axis=(-1,), keepdims=True) output = clip(output, epsilon(), 1.0 - epsilon()) T = target.tensor O = output.tensor ndims = O.shape.ndims fixed_dims = edsl.TensorDims(ndims - 1) fixed_idxs = edsl.TensorIndexes(ndims - 1) Y = edsl.TensorDim() y = edsl.TensorIndex() input_dims = fixed_dims + [Y] O.bind_dims(*input_dims) T.bind_dims(*input_dims) LO = edsl.log(O) TR = edsl.TensorOutput(*fixed_dims) TR[fixed_idxs] += T[fixed_idxs + [y]] * LO[fixed_idxs + [y]] R = -TR return _KerasNode('categorical_crossentropy', tensor=R)
def log(x): logger.debug('log(x: {})'.format(x)) return _KerasNode('log', tensor=edsl.log(x.tensor))
def log(x): return _KerasNode('log', tensor=edsl.log(x.tensor))