Exemplo n.º 1
0
        /// <summary>
        /// Instantiate a Keras tensor.
        /// </summary>
        /// <param name="shape"></param>
        /// <param name="batch_size"></param>
        /// <param name="dtype"></param>
        /// <param name="name"></param>
        /// <param name="sparse">
        /// A boolean specifying whether the placeholder to be created is sparse.
        /// </param>
        /// <param name="ragged">
        /// A boolean specifying whether the placeholder to be created is ragged.
        /// </param>
        /// <param name="tensor">
        /// Optional existing tensor to wrap into the `Input` layer.
        /// If set, the layer will not create a placeholder tensor.
        /// </param>
        /// <returns></returns>
        public Tensor Input(TensorShape shape             = null,
                            int batch_size                = -1,
                            TensorShape batch_input_shape = null,
                            TF_DataType dtype             = TF_DataType.DtInvalid,
                            string name   = null,
                            bool sparse   = false,
                            bool ragged   = false,
                            Tensor tensor = null)
        {
            if (batch_input_shape != null)
            {
                shape = batch_input_shape.dims.Skip(1).ToArray();
            }

            var args = new InputLayerArgs
            {
                Name            = name,
                InputShape      = shape,
                BatchInputShape = batch_input_shape,
                BatchSize       = batch_size,
                DType           = dtype,
                Sparse          = sparse,
                Ragged          = ragged,
                InputTensor     = tensor
            };

            var layer = new InputLayer(args);

            return(layer.InboundNodes[0].Outputs);
        }
Exemplo n.º 2
0
        public InputLayer(InputLayerArgs args) :
            base(args)
        {
            this.args            = args;
            built                = true;
            this.SupportsMasking = true;

            if (BatchInputShape != null)
            {
                args.BatchSize  = BatchInputShape.dims[0];
                args.InputShape = BatchInputShape.dims[1..];
Exemplo n.º 3
0
        public InputLayer(InputLayerArgs args) :
            base(args)
        {
            this.args       = args;
            built           = true;
            SupportsMasking = true;

            if (BatchInputShape != null)
            {
                args.BatchSize  = BatchInputShape.dims[0];
                args.InputShape = BatchInputShape.dims.Skip(1).ToArray();
            }

            // moved to base class
            if (string.IsNullOrEmpty(args.Name))
            {
                var prefix = "input";
                name      = prefix + '_' + keras.backend.get_uid(prefix);
                args.Name = name;
            }

            if (args.DType == TF_DataType.DtInvalid)
            {
                args.DType = args.InputTensor == null ? tf.float32 : args.InputTensor.dtype;
            }

            if (args.InputTensor == null)
            {
                if (args.InputShape != null)
                {
                    args.BatchInputShape = new int[] { args.BatchSize }
                    .Concat(args.InputShape.dims)
                    .ToArray();
                }
                else
                {
                    args.BatchInputShape = null;
                }

                var graph = keras.backend.get_graph();
                graph.as_default();

                args.InputTensor = keras.backend.placeholder(
                    shape: BatchInputShape,
                    dtype: DType,
                    name: Name,
                    sparse: args.Sparse,
                    ragged: args.Ragged);

                graph.Exit();

                isPlaceholder = true;
            }

            // Create an input node to add to self.outbound_node
            // and set output_tensors' _keras_history.
            // input_tensor._keras_history = base_layer.KerasHistory(self, 0, 0)
            // input_tensor._keras_mask = None
            var node = new Node(new NodeArgs
            {
                Outputs = args.InputTensor
            });

            node.Connect(this);

            typeSpec = new TensorSpec(args.InputTensor.TensorShape,
                                      dtype: args.InputTensor.dtype,
                                      name: Name);
        }