C.Function get_mask_and_infer_from_last_dimension(C.Function inputs, C.Function mask) { if (mask == null) { var inputs_shape = inputs.Output.Shape.Dimensions.ToArray(); var ndims = inputs_shape.Length - 1; var x = CC.Sqrt(CC.ReduceSum(CC.Square(inputs), new C.Axis(ndims - 1))); x = CC.Squeeze(x); System.Diagnostics.Debug.Assert(x.Output.Shape.Dimensions.Count == 1); x = CC.Argmax(x, new C.Axis(0)); mask = CC.OneHotOp(x, numClass: (uint)inputs_shape[0], outputSparse: false, axis: new C.Axis(0)); } mask = CC.Reshape(mask, mask.Output.Shape.AppendShape(new int[] { 1 })); var masked = CC.ElementTimes(inputs, mask); masked = CC.Flatten(masked); masked = CC.Squeeze(masked); return(masked); }
C.Function create_capsule(string name, int[] input_shape, int[] extra_input_shape, int recognizer_dim, int generator_dim) { var input_dim = Util.np_prod(input_shape); var x = Util.placeholderVariable(input_shape, "x"); var extra_input = Util.placeholderVariable(extra_input_shape, "extra_input"); var x_flat = CC.Flatten(x); var recognition = Layers.Dense(x_flat, recognizer_dim, computeDevice, "recognition_layer", CC.Sigmoid); var probability = Layers.Dense(recognition, 1, computeDevice, "probability", CC.Sigmoid); var learnt_transformation = Layers.Dense(recognition, 2, computeDevice, "xy_prediction"); var learnt_transformation_extended = CC.Plus(learnt_transformation, extra_input, "learnt_transformation_extended"); var generation = Layers.Dense(learnt_transformation_extended, generator_dim, computeDevice, "generator_layer", CC.Sigmoid); var out_flat = Layers.Dense(generation, input_dim, computeDevice, "output"); out_flat = CC.ElementTimes(out_flat, probability); var output = CC.Reshape(out_flat, input_shape); return(output); }