예제 #1
0
        C.Function create_transforming_autoencoder(
            int num_capsules,
            int[] input_shape,
            int[] extra_input_shape,
            int recognizer_dim,
            int generator_dim)
        {
            var capsules_outputs = new List <C.Function>();

            for (int i = 0; i < num_capsules; i++)
            {
                var capsule        = create_capsule($"capsule_{i}", input_shape, extra_input_shape, recognizer_dim, generator_dim);
                var capsule_output = Model.invoke_model(capsule, new C.Variable[] { imageVariable, transformationVariable });
                capsules_outputs.Add(capsule_output);
            }

            var inference = capsules_outputs[0];

            for (int i = 1; i < capsules_outputs.Count; i++)
            {
                inference = CC.Plus(inference, capsules_outputs[i]);
            }
            inference = CC.Sigmoid(inference, "autoencoder_inference");
            return(inference);
        }
예제 #2
0
        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));
        }
예제 #3
0
 public static Func <Variable, Function> Sigmoid(string name = null)
 {
     return(x => C.Sigmoid(x, name));
 }
예제 #4
0
 public Tensor sigmoid(Tensor x)
 {
     return(Out(C.Sigmoid(In(x))));
 }