Пример #1
0
        /// <summary>
        /// Adds a dream layer to a neural network.
        /// </summary>
        /// <param name="input">The neural network to extend.</param>
        /// <param name="image">The content image.</param>
        /// <param name="width">The width of the content image.</param>
        /// <param name="height">The height of the content image.</param>
        /// <returns>The neural network extended with a dream layer.</returns>
        public static CNTK.Function DreamLayer(
            this CNTK.Function input,
            float[] image,
            int width,
            int height)
        {
            // set up the dream layer
            var dream_weights_init = new CNTK.NDArrayView(new int[] { width, height, 3 }, image, NetUtil.CurrentDevice);
            var dream_weights      = new CNTK.Parameter(dream_weights_init, "the_dream");
            var dummy_features     = CNTK.Variable.InputVariable(new int[] { 1 }, CNTK.DataType.Float, "dummy_features");
            var dream_layer        = CNTK.CNTKLib.ElementTimes(dream_weights, dummy_features, "the_dream_layer");

            // combine the dream layer with the content and style layers
            var replacements = new Dictionary <CNTK.Variable, CNTK.Variable>()
            {
                { input.Arguments[0], dream_layer.Output }
            };
            var model = input.Clone(CNTK.ParameterCloningMethod.Freeze, replacements);

            // return the finished model
            var all_outputs = new List <CNTK.Variable>()
            {
                dream_layer
            };

            all_outputs.AddRange(model.Outputs);
            return(CNTK.Function.Combine(all_outputs, name: "overall_model"));
        }
Пример #2
0
 /// <summary>
 /// Create a Gan by combining a generator and a discriminator.
 /// </summary>
 /// <param name="generator">The generator to use.</param>
 /// <param name="discriminator">The discriminator to use.</param>
 /// <returns>A new Gan network constructed out of the generator and discriminator.</returns>
 public static CNTK.Function CreateGan(
     CNTK.Function generator,
     CNTK.Function discriminator)
 {
     return(discriminator.Clone(
                CNTK.ParameterCloningMethod.Share,
                replacements: new Dictionary <CNTK.Variable, CNTK.Variable>()
     {
         { discriminator.Arguments[0], generator }
     }));
 }