public Tensor categorical_crossentropy(Tensor target, Tensor output, bool from_logits = false) { // https://github.com/fchollet/keras/blob/f65a56fb65062c8d14d215c9f4b1015b97cc5bf3/keras/backend/cntk_backend.py#L1480 var _output = In(output); var _target = In(target); if (from_logits) { var result = C.CrossEntropyWithSoftmax(_output, _target); // cntk's result shape is (batch, 1), while keras expect (batch, ) CNTK.Function r = C.Reshape(result, NDShape.CreateNDShape(new int[] { })); return(Out(r)); } else { // scale preds so that the class probas of each sample sum to 1 var o = C.ElementDivide(_output.function, C.ReduceSum(_output, Axis.EndStaticAxis())); var eps = Constant.Scalar(epsilon(), DeviceDescriptor.CPUDevice); var omeps = Constant.Scalar(1.0 - epsilon(), DeviceDescriptor.CPUDevice); // avoid numerical instability with _EPSILON clipping o = C.Clip(o, eps, omeps); CNTK.Function r = C.Negate(C.ReduceSum(C.ElementTimes(_target, C.Log(_output)), Axis.EndStaticAxis())); return(Out(r)); } }
public Tensor binary_crossentropy(Tensor output, Tensor target, bool from_logits = false) { log(new { output, target, from_logits }); var _output = new Variable(In(output).function); var _target = new Variable(In(target).function); if (from_logits) { _output = C.Sigmoid(_output); } // scale preds so that the class probas of each sample sum to 1 var eps = InConstant(epsilon()); var omeps = InConstant(1.0); // avoid numerical instability with _EPSILON clipping _output = C.Clip(_output, eps, omeps); var a = new Variable(C.Negate(C.ElementTimes(_target, C.Log(_output)))); var b = new Variable(C.Negate( C.Minus(C.ElementTimes(InConstant(1.0), _target), C.Minus(C.Log(InConstant(1.0)), _output)))); _output = a + b; return(Out(_output)); }
public Tensor subtract <T>(T a, Tensor b) { return(Out(InGeneric(a) + C.Negate(In(b)))); }
public Tensor subtract <T>(Tensor a, T b) { return(Out(new Variable(In(a)) + C.Negate(InGeneric(b)))); }
public Tensor subtract(Tensor a, Tensor b) { return(Out(In(a) + new Variable(C.Negate(In(b))))); }
public Tensor Neg(Tensor x) { return(Out(C.Negate(In(x), ""))); }