public void perceptron_should_learn_all_except_xor_nor(
            [ValueSource("Backends")] string backend,
            [ValueSource("Targets")] float[] y,
            [Values(false, true)] bool useBias)
        {
            KerasSharp.Backends.Current.Switch(backend);

            var model = new Sequential();

            model.Add(new Dense(1, input_dim: 2,
                                kernel_initializer: new GlorotUniform(),
                                bias_initializer: new GlorotUniform(),
                                use_bias: useBias,
                                activation: new Sigmoid()));

            model.Compile(loss: new MeanSquareError(), optimizer: new SGD(lr: 1), metrics: new[] { new Accuracy() });

            model.fit(x, y, epochs: 1000, batch_size: y.Length);

            Array yy = model.predict(x, batch_size: y.Length)[0];

            float[] pred = MatrixEx.Round(yy.To <float[, ]>()).GetColumn(0);

            if ((useBias && (y == xor)) ||
                (!useBias && (y == xor || y == nor || y == and)))
            {
                Assert.AreNotEqual(y, pred);
            }
            else
            {
                Assert.AreEqual(y, pred);
            }
        }
        public void mlp_should_learn_all(
            [ValueSource("Backends")] string backend,
            [ValueSource("Targets")] float[] y,
            [Values(false, true)] bool useBias)
        {
            KerasSharp.Backends.Current.Switch(backend);

            var model = new Sequential();

            model.Add(new Dense(5, input_dim: 2,
                                kernel_initializer: new GlorotUniform(),
                                bias_initializer: new GlorotUniform(),
                                use_bias: useBias,
                                activation: new Sigmoid()));
            model.Add(new Dense(1,
                                kernel_initializer: new GlorotUniform(),
                                bias_initializer: new GlorotUniform(),
                                use_bias: useBias,
                                activation: new Sigmoid()));

            model.Compile(loss: new MeanSquareError(), optimizer: new SGD(lr: 1), metrics: new[] { new Accuracy() });

            model.fit(x, y, epochs: 1000, batch_size: y.Length);

            double[] pred = Matrix.Round(model.predict(x, batch_size: y.Length)[0].To <double[, ]>()).GetColumn(0);

            Assert.AreEqual(y, pred);
        }
Exemple #3
0
        private List <MLFoundPoint> Test(List <Candle> candles)
        {
            Log.Info("Running test");
            try
            {
                var ret     = new List <MLFoundPoint>();
                var x       = new List <float>();
                var xpoints = 0;
                for (var i = 20; i < candles.Count; i++)
                {
                    var z = _dataGenerator.GetMLPointXData(candles, i);
                    if (xpoints == 0)
                    {
                        xpoints = z.Count;
                    }

                    x.AddRange(z);
                }

                var inX     = np.array(x.ToArray());
                var countIn = (int)(x.Count / (decimal)xpoints);
                inX = inX.reshape(countIn, xpoints);

                var outY = _model.predict(inX, countIn);
                for (var i = 0; i < countIn; i++)
                {
                    var values = outY[0][i].ToArray <float>();
                    if (values.Any(v => v > 1F || v < 0F))
                    {
                        throw new ApplicationException("Test values is <0 or >1");
                    }

                    var buy  = values[0];
                    var sell = values[1];

                    if (buy > 0.95F)
                    {
                        ret.Add(new MLFoundPoint(candles[i].CloseTimeTicks, TradeDirection.Long, (decimal)candles[i].CloseBid));
                    }
                    if (sell > 0.95F)
                    {
                        ret.Add(new MLFoundPoint(candles[i].CloseTimeTicks, TradeDirection.Short, (decimal)candles[i].CloseBid));
                    }
                }

                Log.Info($"Test complete - {ret.Count} points found");
                return(ret);
            }
            catch (Exception ex)
            {
                Log.Error("Failing to run test", ex);
                return(null);
            }
        }
Exemple #4
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]);
        }
        public void DoSomeWork(double[][] myoData)
        {
            using (var session = new TFSession())
            {
                float[,] x = myoData.ToMatrix().ToSingle();
                float[] y = myoData.GetColumn(0).ToSingle();

                var inputDim = x.GetLength(1);


                KerasSharp.Backends.Current.Switch("KerasSharp.Backends.TensorFlowBackend");


                // Create the model
                var model = new Sequential();
                model.Add(new Dense(512, input_dim: inputDim, activation: new ReLU()));
                model.Add(new Dense(8, activation: new Softmax()));



                // Compile the model (for the moment, only the mean square
                // error loss is supported, but this should be solved soon)
                model.Compile(loss: new CategoricalCrossEntropy(),
                              optimizer: new SGD(),
                              metrics: new[] { new Accuracy() });

                // Fit the model for 150 epochs
                model.fit(x, y, epochs: 150, batch_size: 32);

                // Use the model to make predictions
                float[] pred = model.predict(x)[0].To <float[]>();

                // Evaluate the model
                double[] scores = model.evaluate(x, y);
                Console.WriteLine($"{model.metrics_names[1]}: {scores[1] * 100}");
            }


            /*
             * using (var session = new TFSession())
             * {
             *  var graph = session.Graph;
             *
             *  var a = graph.Const(2);
             *  var b = graph.Const(3);
             *
             *  TFTensor addingTensor = session.GetRunner().Run(graph.Add(a, b));
             *  object TResVal = addingTensor.GetValue();
             *
             * }
             */
        }
Exemple #6
0
        public double act(Array state)
        {
            Random random = new Random();

            if (np.random.rand() < this.exploration_rate)
            {
                int actionindex = random.Next(0, this.action_space);
                return(actionindex);
            }
            //var model = new Sequential();
            Array[] pred = model.predict(state);
            return(np.argmax(pred[0]));
        }
Exemple #7
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));
        }
Exemple #8
0
        static void Main(string[] args)
        {
            GradientEngine.UseEnvironmentFromVariable();
            TensorFlowSetup.Instance.EnsureInitialized();

            // this allows SIREN to oversaturate channels without adding to the loss
            var clampToValidChannelRange = PythonFunctionContainer.Of <Tensor, Tensor>(ClampToValidChannelValueRange);
            var siren = new Sequential(new object[] {
                new GaussianNoise(stddev: 1f / (128 * 1024)),
                new Siren(2, Enumerable.Repeat(256, 5).ToArray()),
                new Dense(units: 4, activation: clampToValidChannelRange),
                new GaussianNoise(stddev: 1f / 128),
            });

            siren.compile(
                // too slow to converge
                //optimizer: new SGD(momentum: 0.5),
                // lowered learning rate to avoid destabilization
                optimizer: new Adam(learning_rate: 0.00032),
                loss: "mse");

            if (args.Length == 0)
            {
                siren.load_weights("sample.weights");
                Render(siren, 1034 * 3, 1536 * 3, "sample6X.png");
                return;
            }

            foreach (string imagePath in args)
            {
                using var original = new Bitmap(imagePath);
                byte[,,] image     = ToBytesHWC(original);
                int height   = image.GetLength(0);
                int width    = image.GetLength(1);
                int channels = image.GetLength(2);
                Debug.Assert(channels == 4);

                var imageSamples = PrepareImage(image);

                var coords = ImageTools.Coord(height, width).ToNumPyArray()
                             .reshape(new[] { width *height, 2 });

                var upscaleCoords = ImageTools.Coord(height * 2, width * 2).ToNumPyArray();

                var improved = ImprovedCallback.Create((sender, eventArgs) => {
                    if (eventArgs.Epoch < 10)
                    {
                        return;
                    }
                    ndarray <float> upscaled = siren.predict(
                        upscaleCoords.reshape(new[] { height *width * 4, 2 }),
                        batch_size: 1024);
                    upscaled         = (ndarray <float>)upscaled.reshape(new[] { height * 2, width * 2, channels });
                    using var bitmap = ToImage(RestoreImage(upscaled));
                    bitmap.Save("sample4X.png", ImageFormat.Png);

                    siren.save_weights("sample.weights");

                    Console.WriteLine();
                    Console.WriteLine("saved!");
                });

                siren.fit(coords, imageSamples, epochs: 100, batchSize: 16 * 1024,
                          shuffleMode: TrainingShuffleMode.Epoch,
                          callbacks: new ICallback[] { improved });
            }
        }
Exemple #9
0
        static void Main(string[] args)
        {
            /*
             # Example from https://machinelearningmastery.com/tutorial-first-neural-network-python-keras/
             #
             # from keras.models import Sequential
             # from keras.layers import Dense
             # import numpy
             #
             # fix random seed for reproducibility
             # numpy.random.seed(7)
             #
             # load pima indians dataset
             # dataset = numpy.loadtxt("pima-indians-diabetes.csv", delimiter=",")
             # split into input (X) and output (Y) variables
             # X = dataset[:,0:8]
             # Y = dataset[:,8]
             #
             # create model
             # model = Sequential()
             # model.add(Dense(12, input_dim=8, activation='relu'))
             # model.add(Dense(8, activation='relu'))
             # model.add(Dense(1, activation='sigmoid'))
             #
             # Compile model
             # model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])
             #
             # Fit the model
             # model.fit(X, Y, epochs=150, batch_size=10)
             #
             # evaluate the model
             # scores = model.evaluate(X, Y)
             # print("\n%s: %.2f%%" % (model.metrics_names[1], scores[1]*100))
             */

            //KerasSharp.Backends.Current.Switch("KerasSharp.Backends.TensorFlowBackend");
            KerasSharp.Backends.Current.Switch("KerasSharp.Backends.CNTKBackend");

            // Load the Pima Indians Data Set
            var pima = new Accord.DataSets.PimaIndiansDiabetes();

            float[,] x = pima.Instances.ToMatrix().ToSingle();
            float[] y = pima.ClassLabels.ToSingle();

            // Create the model
            var model = new Sequential();

            model.Add(new Dense(12, input_dim: 8, activation: new ReLU()));
            model.Add(new Dense(8, activation: new ReLU()));
            model.Add(new Dense(1, activation: new Sigmoid()));

            // Compile the model (for the moment, only the mean square
            // error loss is supported, but this should be solved soon)
            model.Compile(loss: new MeanSquareError(),
                          optimizer: new Adam(),
                          metrics: new[] { new Accuracy() });

            // Fit the model for 150 epochs
            model.fit(x, y, epochs: 150, batch_size: 10);

            // Use the model to make predictions
            float[] pred = model.predict(x)[0].To <float[]>();

            // Evaluate the model
            double[] scores = model.evaluate(x, y);
            Console.WriteLine($"{model.metrics_names[1]}: {scores[1] * 100}");

            Console.ReadLine();
        }