Пример #1
0
        /// <summary>
        /// <para>
        /// Builds the layer by converting the abstract definition of the layer into
        /// a concrete set of instructions for Tensorflow and a layer configuration
        /// for use when training the model.
        /// </para>
        /// <para>This method should register any parameters and initializers with the compilation context.
        /// So that they can be used during the training phase. </para>
        /// <para>Additionally you are required to store the layer configuration in the
        /// <see cref="context"/> property. This information is required as metadata
        /// when the model is used.</para>
        /// </summary>
        /// <param name="context">Use this context to register trainable parameters
        /// and build the computational graph for the layer</param>
        public override TFOutput Compile(ModelCompilationContext context)
        {
            if (Configuration != null)
            {
                return(Configuration.Output);
            }

            var input          = _input.Compile(context);
            var inputDimension = _input.OutputShape[_input.OutputShape.Length - 1];

            using (var scope = context.Graph.WithScope(Name))
            {
                TFShape weightsShape = new TFShape(inputDimension, _units);
                TFShape biasShape    = new TFShape(_units);

                var weights = context.Graph.VariableV2(
                    weightsShape,
                    TFDataType.Double, operName: "Weights");

                var initializers = new List <TFOperation>
                {
                    context.Graph.Assign(weights, _weightsInitializer.Compile(context.Graph, weightsShape)).Operation
                };

                var parameters = new List <TFOutput>
                {
                    weights
                };

                context.AddParameters(weights);

                var output = context.Graph.MatMul(input, weights);

                if (_useBias)
                {
                    var bias = context.Graph.VariableV2(
                        biasShape,
                        TFDataType.Double, operName: "Bias");

                    initializers.Add(context.Graph.Assign(bias,
                                                          _biasInitializer.Compile(context.Graph, biasShape)).Operation);

                    parameters.Add(bias);

                    output = context.Graph.Add(output, bias);
                }

                output = _activation.Compile(context, output);

                Configuration = new LayerConfiguration(parameters, initializers, output);

                context.AddInitializers(initializers);

                return(output);
            }
        }
Пример #2
0
        /// <summary>
        /// <para>
        /// Builds the layer by converting the abstract definition of the layer into
        /// a concrete set of instructions for Tensorflow and a layer configuration
        /// for use when training the model.
        /// </para>
        /// <para>This method should register any parameters and initializers with the compilation context.
        /// So that they can be used during the training phase. </para>
        /// <para>Additionally you are required to store the layer configuration in the
        /// <see cref="Layer.Configuration"/> property. This information is required as metadata
        /// when the model is used.</para>
        /// <param name="context">Use this context to register trainable parameters
        /// and build the computational graph for the layer</param>
        public override TFOutput Compile(ModelCompilationContext context)
        {
            var inputLayer = _input.Compile(context);
            var keepProb   = context.Graph.Const(_rate);

            var output = context.Graph.Dropout(inputLayer, keepProb,
                                               context.Graph.GetTensorShape(inputLayer), _seed);

            Configuration = new LayerConfiguration(new TFOutput[] { }, new TFOperation[] { }, output);

            return(output);
        }
Пример #3
0
        /// <summary>
        /// <para>
        /// Builds the layer by converting the abstract definition of the layer into
        /// a concrete set of instructions for Tensorflow and a layer configuration
        /// for use when training the model.
        /// </para>
        /// <para>This method should register any parameters and initializers with the compilation context.
        /// So that they can be used during the training phase. </para>
        /// <para>Additionally you are required to store the layer configuration in the
        /// <see cref="Configuration"/> property. This information is required as metadata
        /// when the model is used.</para>
        /// <param name="context">Use this context to register trainable parameters
        /// and build the computational graph for the layer</param>
        public override TFOutput Compile(ModelCompilationContext context)
        {
            if (Configuration != null)
            {
                return(Configuration.Output);
            }

            if (_shape.Length == 0)
            {
                throw new ModelCompilationException("Shape must have at least one dimension");
            }

            var placeholder = context.Graph.Placeholder(TFDataType.Double,
                                                        new TFShape(OutputShape), operName: Name);

            Configuration = new LayerConfiguration(new TFOutput[] { }, new TFOperation[] {  }, placeholder);

            return(placeholder);
        }