Exemplo n.º 1
0
        static void Main(string[] args)
        {
            var model = new Sequential();
            var dense = new Dense(units: 5, input_dim: 5,
                                  kernel_initializer: new Constant(Matrix.Identity(5)),
                                  bias_initializer: new Constant(0));

            model.Add(dense);

            float[,] input  = Vector.Range(25).Reshape(5, 5).ToSingle();
            float[,] output = MatrixEx.To <float[, ]>(model.predict(input)[0]);
        }
Exemplo n.º 2
0
        public void conv_bias(string backend)
        {
            KerasSharp.Backends.Current.Switch(backend);

            var model = new Sequential();
            var dense = new Dense(units: 5, input_dim: 5,
                                  kernel_initializer: new Constant(Matrix.Identity(5)),
                                  bias_initializer: new Constant(42));

            model.Add(dense);

            float[,] input  = Vector.Range(25).Reshape(5, 5).ToSingle();
            float[,] output = MatrixEx.To <float[, ]>(model.predict(input)[0]);

            Assert.IsTrue(input.Add(42).IsEqual(output, 1e-8f));
        }
Exemplo n.º 3
0
        private Constant _constant <T>(T value, int?[] shape, DataType?dtype, string name)
        {
            if (value is Array)
            {
                NDArrayView x = In((value as Array).Convert(dtype.ToType()));
                if (name != null)
                {
                    return(new Constant(x, name));
                }
                else
                {
                    return(new Constant(x));
                }
            }

            if (value is double)
            {
                return(Constant.Scalar <double>(MatrixEx.To <double>(value), device: DeviceDescriptor.CPUDevice));
            }
            if (value is float)
            {
                return(Constant.Scalar <double>(MatrixEx.To <float>(value), device: DeviceDescriptor.CPUDevice));
            }

            if (name == null)
            {
                return(new Constant(shape: InShape(shape),
                                    dataType: In(dtype.Value),
                                    initValue: (dynamic)value,
                                    device: DeviceDescriptor.CPUDevice));
            }

            return(new Constant(shape: InShape(shape),
                                dataType: In(dtype.Value),
                                initValue: (dynamic)value,
                                device: DeviceDescriptor.CPUDevice,
                                name: name));
        }