Exemplo n.º 1
0
        public static KerasSymbol[] CreateInput(
            Shape shape        = null,
            Shape batch_shape  = null,
            string name        = null,
            DType dtype        = null,
            bool sparse        = false,
            KerasSymbol tensor = null)
        {
            if (batch_shape == null && tensor == null)
            {
                Debug.Assert(shape != null, "Please provide to Input either a `shape` or a `batch_shape` argument. Note that `shape` does not include the batch dimension.");
            }

            if (shape != null && batch_shape != null)
            {
                var list = shape.Data.ToList();
                list.Insert(0, 0);
                batch_shape = new Shape(list);
            }

            if (dtype == null)
            {
                dtype = K.FloatX();
            }

            var input_layer = new InputLayer(batch_input_shape: batch_shape, name: name, dtype: dtype, sparse: sparse, input_tensor: tensor);
            // Return tensor including _keras_shape and _keras_history.
            // Note that in this case train_output and test_output are the same pointer.
            var outputs = input_layer._inbound_nodes[0].output_tensors;

            return(outputs);
        }
Exemplo n.º 2
0
        public override KerasSymbol[] Invoke(KerasSymbol[] inputs, FuncArgs kwargs = null)
        {
            List <KerasSymbol> result = new List <KerasSymbol>();

            foreach (var input in inputs)
            {
                result.Add(input * K.Cast(K.Greater(inputs[0], this.theta), K.FloatX()));
            }

            return(result.ToArray());
        }
Exemplo n.º 3
0
        public override void Build(Shape input_shape = null)
        {
            if (input_shape != null && this.inputs == null && this.inputs.Count > 0)
            {
                var batch_shape = input_shape;
                var dtype       = K.FloatX();
                var x           = InputLayer.CreateInput(batch_shape: batch_shape, dtype: dtype, name: this.name + "_input");
                this.inputs = x.ToList();
                foreach (var layer in this._layers)
                {
                    x = layer.Invoke(x, null);
                }

                this.outputs            = x.ToList();
                this._build_input_shape = input_shape;
            }

            if (this.inputs != null && this.inputs.Count > 0)
            {
                this.InitGraphNetwork(this.inputs.ToArray(), this.outputs.ToArray(), name: this.name);
                this.built = true;
            }
        }
Exemplo n.º 4
0
        public InputLayer(
            Shape input_shape        = null,
            int?batch_size           = null,
            Shape batch_input_shape  = null,
            DType dtype              = null,
            KerasSymbol input_tensor = null,
            bool sparse              = false,
            string name              = null)
        {
            if (name != null)
            {
                var prefix = "input";
                name = prefix + "_" + K.GetUid(prefix).ToString();
            }

            this.trainable        = false;
            this.built            = true;
            this.sparse           = sparse;
            this.supports_masking = true;
            if (input_shape != null && batch_input_shape != null)
            {
                throw new Exception("Only provide the input_shape OR batch_input_shape argument to InputLayer, not both at the same time.");
            }

            if (input_tensor != null && batch_input_shape == null)
            {
                // If input_tensor is set, and batch_input_shape is not set:
                // Attempt automatic input shape inference.
                try
                {
                    batch_input_shape = input_tensor.Shape;
                }
                catch (Exception)
                {
                    if (input_shape == null && batch_input_shape == null)
                    {
                        throw new Exception("InputLayer was provided an input_tensor argument, but its input shape cannot be automatically inferred. You should pass an input_shape or batch_input_shape argument.");
                    }
                }
            }
            if (batch_input_shape == null)
            {
                if (input_shape == null)
                {
                    throw new Exception("An Input layer should be passed either a `batch_input_shape` or an `input_shape`.");
                }
                else
                {
                    var batchShapeData = input_shape.Data.ToList();
                    batchShapeData.Insert(0, batch_size.Value);
                    batch_input_shape = new Shape(batchShapeData);
                }
            }
            else
            {
                batch_input_shape = new Shape(batch_input_shape);
            }

            if (dtype == null)
            {
                if (input_tensor == null)
                {
                    dtype = K.FloatX();
                }
                else
                {
                    dtype = K.DataType(input_tensor);
                }
            }
            this.batch_input_shape = batch_input_shape;
            this.dtype             = dtype;
            if (input_tensor == null)
            {
                this.is_placeholder = true;
                input_tensor        = K.Placeholder(shape: batch_input_shape, dtype: dtype, sparse: this.sparse, name: this.name);
            }
            else
            {
                this.is_placeholder       = false;
                input_tensor._keras_shape = batch_input_shape;
            }
            // Create an input node to add to this.outbound_node
            // and set output_tensors' _keras_history.
            input_tensor._uses_learning_phase = false;
            input_tensor._keras_history       = (this, 0, 0);

            var node = new Node(this,
                                inbound_layers: new Layer[0],
                                node_indices: new int[0],
                                tensor_indices: new int[0],
                                input_tensors: new KerasSymbol[] { input_tensor },
                                output_tensors: new KerasSymbol[] { input_tensor },
                                input_masks: new KerasSymbol[] { null },
                                output_masks: new KerasSymbol[] { null },
                                input_shapes: new Shape[] { batch_input_shape },
                                output_shapes: new Shape[] { batch_input_shape }
                                );
        }
Exemplo n.º 5
0
 public override KerasSymbol Call(KerasSymbol w)
 {
     w *= K.Cast(K.GreaterEqual(w, 0), K.FloatX());
     return(w);
 }
Exemplo n.º 6
0
 public static KerasSymbol MultiHotSparseCategoricalAccuracy(KerasSymbol y_true, KerasSymbol y_pred)
 {
     return(K.Cast(K.Equal(K.Max(y_true, axis: -1), K.Cast(K.Argmax(y_pred, axis: -1), K.FloatX())), K.FloatX()));
 }