예제 #1
0
        public void test_rand_UD_1()
        {
            var     random = new np.random(new UserDefinedRandomGenerator());
            ndarray arr    = random.rand(new shape(2, 3, 4));

            Assert.AreEqual(arr.TypeNum, NPY_TYPES.NPY_DOUBLE);
            AssertShape(arr, 2, 3, 4);

            random.seed(8765);
            double f = random.rand();

            print(f);
            Assert.AreEqual(0.78412213771795958, f);

            arr = random.rand(new shape(5000000));

            var amax = np.amax(arr);

            print(amax);
            Assert.AreEqual(0.99999997671693563, (double)amax);

            var amin = np.amin(arr);

            print(amin);
            Assert.AreEqual(5.1688402915228346E-08, (double)amin);

            var avg = np.average(arr);

            print(avg);
            Assert.AreEqual(0.50011801294498859, (double)avg);
        }
예제 #2
0
        public void test_randn_UC_1()
        {
            var random = new np.random(new UserDefinedRandomGenerator());

            double  fr  = random.randn();
            ndarray arr = random.randn(new shape(2, 3, 4));

            Assert.AreEqual(arr.TypeNum, NPY_TYPES.NPY_DOUBLE);
            AssertShape(arr, 2, 3, 4);

            random.seed(1234);

            fr = random.randn();
            print(fr);
            //Assert.AreEqual(-0.29042058667075177, fr);

            arr = random.randn(new shape(5000000));
            Assert.AreEqual(arr.TypeNum, NPY_TYPES.NPY_DOUBLE);

            var amax = np.amax(arr);

            print(amax);
            Assert.AreEqual(5.070295032710753, (double)amax);

            var amin = np.amin(arr);

            print(amin);
            Assert.AreEqual(-5.04633622266897, (double)amin);

            var avg = np.average(arr);

            print(avg);
            Assert.AreEqual(-0.00044428007674556522, (double)avg);
        }
예제 #3
0
        public void test_histogram2d_2()
        {
            var random = new np.random();

            random.seed(8765);

            var x = random.normal(2, 1, new shape(100));
            var y = random.normal(1, 1, new shape(100));

            var xedges = new int[] { 0, 1, 3, 5 };
            var yedges = new int[] { 0, 2, 3, 4, 6 };

            var weights = np.arange(300000 / 4, dtype: np.Float64);

            weights.fill(0.5);

            var bins = new List <Int32[]>()
            {
                xedges, yedges
            };

            var result = np.histogram2d(x, y, bins: 2);

            AssertArray(result.H, new double[, ] {
                { 29.0, 17.0 }, { 33.0, 21.0 }
            });
            print(result.H);
            AssertArray(result.xedges, new double[] { -0.267399252084981, 2.06371407063072, 4.39482739334641 });
            print(result.xedges);
            AssertArray(result.yedges, new double[] { -0.984644701475661, 1.45595900692665, 3.89656271532895 });
            print(result.yedges);
        }
예제 #4
0
        public void test_histogram2d_3()
        {
            var random = new np.random();

            random.seed(8765);

            var x = random.normal(2, 1, new shape(100));
            var y = random.normal(1, 1, new shape(100));

            var xedges = np.array(new int[] { 0, 1, 3, 5 });
            var yedges = np.array(new int[] { 0, 2, 3, 4, 6 });

            var weights = np.arange(300000 / 4, dtype: np.Float64);

            weights.fill(0.5);

            var bins = new List <ndarray>()
            {
                xedges, yedges
            };

            var result = np.histogram2d(x, y, bins: bins.ToArray());

            AssertArray(result.H, new double[, ] {
                { 11.0, 1.0, 2.0, 0.0 }, { 37.0, 15.0, 2.0, 0.0 }, { 13.0, 1.0, 0.0, 0.0 }
            });
            print(result.H);
            AssertArray(result.xedges, new double[] { 0, 1, 3, 5 });
            print(result.xedges);
            AssertArray(result.yedges, new double[] { 0, 2, 3, 4, 6 });
            print(result.yedges);
        }
예제 #5
0
        public void test_histogramdd_3()
        {
            var random = new np.random();

            random.seed(8765);

            var r = random.randint(10, 30, new shape(300000));

            var weights = np.arange(300000 / 4, dtype: np.Float64);

            weights.fill(0.5);

            System.Tuple <int, int>[] range1 = new Tuple <int, int>[]
            {
                Tuple.Create(15, 25),
                Tuple.Create(15, 25),
                Tuple.Create(15, 25),
                Tuple.Create(15, 25),
            };
            var x            = np.histogramdd(r.reshape(-1, 4), bins: new int[] { 2, 2, 2, 2 }, range: range1, weights: weights);
            var ExpectedHist = new double[, , , ]
            {
                { { { 149.5, 176.0 }, { 187.5, 223.5 } }, { { 171.0, 227.0 }, { 194.5, 259.5 } } },
                { { { 188.5, 186.0 }, { 217.5, 267.5 } }, { { 223.0, 248.5 }, { 258.0, 312.5 } } }
            };

            AssertArray(x.hist, ExpectedHist);
            AssertArray(x.bin_edges[0], new double[] { 15, 20, 25 });
            AssertArray(x.bin_edges[1], new double[] { 15, 20, 25 });
            AssertArray(x.bin_edges[2], new double[] { 15, 20, 25 });
            AssertArray(x.bin_edges[3], new double[] { 15, 20, 25 });
            print(x.hist);
            print(x.bin_edges);
        }
예제 #6
0
        public void test_randint8_UD_1()
        {
            var random = new np.random(new UserDefinedRandomGenerator());

            random.seed(9292);

            ndarray arr = random.randint(2, 3, new shape(4), dtype: np.Int8);

            Assert.AreEqual(arr.TypeNum, NPY_TYPES.NPY_BYTE);
            AssertShape(arr, 4);
            print(arr);
            AssertArray(arr, new SByte[] { 2, 2, 2, 2 });

            arr = random.randint(2, 8, new shape(5000000), dtype: np.Int8);
            Assert.AreEqual(arr.TypeNum, NPY_TYPES.NPY_BYTE);

            var amax = np.amax(arr);

            print(amax);
            Assert.AreEqual((sbyte)7, (sbyte)amax);

            var amin = np.amin(arr);

            print(amin);
            Assert.AreEqual((sbyte)2, (sbyte)amin);

            var avg = np.average(arr);

            print(avg);
            Assert.AreEqual(2.4605506, (double)avg);

            var first10 = arr["0:10:1"] as ndarray;

            print(first10);
            AssertArray(first10, new sbyte[] { 2, 2, 2, 2, 2, 2, 2, 2, 2, 2 });


            arr = random.randint(-2, 3, new shape(5000000), dtype: np.Int8);
            Assert.AreEqual(arr.TypeNum, NPY_TYPES.NPY_BYTE);

            amax = np.amax(arr);
            print(amax);
            Assert.AreEqual((sbyte)2, (sbyte)amax);

            amin = np.amin(arr);
            print(amin);
            Assert.AreEqual((sbyte)(-2), (sbyte)amin);

            avg = np.average(arr);
            print(avg);
            Assert.AreEqual(-1.6648296, (double)avg);

            first10 = arr["0:10:1"] as ndarray;
            print(first10);
            AssertArray(first10, new sbyte[] { -2, -2, 0, -1, -2, -2, -2, -2, -2, -2 });
        }
예제 #7
0
        public void test_histogramdd_4()
        {
            var random = new np.random();

            random.seed(8765);

            var r = random.randint(10, 30, new shape(300000));

            System.Tuple <int, int>[] range1 = new Tuple <int, int>[]
            {
                Tuple.Create(15, 25),
                Tuple.Create(15, 25),
                Tuple.Create(15, 25),
                Tuple.Create(15, 25),
            };
            var x            = np.histogramdd(r.reshape(-1, 4), bins: 3, range: range1);
            var ExpectedHist = new double[, , , ]
            {
                { { { 131.0, 84.0, 112.0 }, { 100.0, 71.0, 95.0 }, { 125.0, 90.0, 127.0 } }, { { 79.0, 70.0, 97.0 }, { 69.0, 59.0, 79.0 }, { 80.0, 84.0, 99.0 } },
                  { { 112.0, 105.0, 113.0 }, { 80.0, 67.0, 91.0 }, { 107.0, 92.0, 129.0 } } }, { { { 109.0, 64.0, 78.0 }, { 73.0, 41.0, 64.0 }, { 91.0, 77.0, 110.0 } },
                                                                                                 { { 65.0, 47.0, 59.0 }, { 59.0, 50.0, 56.0 }, { 71.0, 59.0, 71.0 } }, { { 96.0, 66.0, 103.0 }, { 89.0, 60.0, 65.0 }, { 95.0, 59.0, 85.0 } } },
                { { { 127.0, 81.0, 100.0 }, { 89.0, 66.0, 82.0 }, { 126.0, 78.0, 143.0 } }, { { 86.0, 68.0, 76.0 }, { 65.0, 52.0, 69.0 }, { 78.0, 63.0, 101.0 } },
                  { { 123.0, 95.0, 124.0 }, { 111.0, 68.0, 99.0 }, { 102.0, 77.0, 122.0 } } }
            };

            var ExpectedBinEdges = new double[] { 15.0, 18.3333333333333, 21.6666666666667, 25.0 };

            AssertArray(x.hist, ExpectedHist);
            AssertArray(x.bin_edges[0], ExpectedBinEdges);
            AssertArray(x.bin_edges[1], ExpectedBinEdges);
            AssertArray(x.bin_edges[2], ExpectedBinEdges);
            AssertArray(x.bin_edges[3], ExpectedBinEdges);
            print(x.hist);
            print(x.bin_edges);

            /////
            System.Tuple <int, int>[] range3 = new Tuple <int, int>[]
            {
                Tuple.Create(15, 25),
                Tuple.Create(15, 25),
            };
            x = np.histogramdd(r.reshape(-1, 2), bins: 2, density: true, range: range3);
            print(x.hist);
            print(x.bin_edges);
            /////
        }
예제 #8
0
        public double[] GenerateArt(int batch_size = 1, int net_size = 16, int h_size = 8, int width = 512, int height = 512, float scaling = 10.0f, bool RGB = true, int?seed = null)
        {
            int?KeyId = null;

            if (seed != null)
            {
                KeyId  = seed;
                random = new np.random();
                random.seed(KeyId);
            }
            else
            {
                KeyId  = Math.Abs(DateTime.Now.GetHashCode());
                random = new np.random();
                random.seed(KeyId);
            }

            var art = new ArtGenNumpyDotNet();

            art.InitialiseCPPN(batch_size, net_size, RGB: RGB);

            if (RGB)
            {
                C_Dim = 3;
            }
            else
            {
                C_Dim = 1;
            }

            var imageData = art.Generate(width, height, scaling);

            var imgData = np.array(np.array(1) - imageData);

            if (C_Dim > 1)
            {
                imgData = np.array(imgData.reshape((height, width, C_Dim)) * 255.0);
            }
            else
            {
                imgData = np.array(imgData.reshape((height, width)) * 255.0);
            }

            return(imgData.ToArray <double>());
        }
예제 #9
0
파일: ESN.cs 프로젝트: thild/csESN
        //  """
        // Args:
        //     n_inputs: nr of input dimensions
        //         n_outputs: nr of output dimensions
        //         n_reservoir: nr of reservoir neurons
        //         spectral_radius: spectral radius of the recurrent weight matrix
        //     sparsity: proportion of recurrent weights set to zero
        //     noise: noise added to each neuron(regularization)
        //         input_shift: scalar or vector of length n_inputs to add to each
        //                     input dimension before feeding it to the network.
        //     input_scaling: scalar or vector of length n_inputs to multiply
        //                 with each input dimension before feeding it to the netw.
        //     teacher_forcing: if True, feed the target back into output units
        //     teacher_scaling: factor applied to the target signal
        //     teacher_shift: additive term applied to the target signal
        //     out_activation: output activation function (applied to the readout)
        //         inverse_out_activation: inverse of the output activation function
        //         random_state: positive integer seed, np.rand.RandomState object,
        //                   or None to use numpy's builting RandomState.
        //     silent: supress messages
        // """
        public ESN(int n_inputs, int n_outputs, int n_reservoir = 200,
                   double spectral_radius   = 0.95, double sparsity = 0, double noise = 0.001,
                   ndarray input_shift      = null,
                   ndarray input_scaling    = null,
                   bool teacher_forcing     = true,
                   ndarray feedback_scaling = null,
                   double teacher_scaling   = 0, double teacher_shift = 0,
                   Func <ndarray, ndarray> out_activation         = null,
                   Func <ndarray, ndarray> inverse_out_activation = null,
                   object random_state = null,
                   bool silent         = true)
        {
            if (out_activation == null)
            {
                out_activation = IdentityFunction <ndarray>();
            }
            if (inverse_out_activation == null)
            {
                inverse_out_activation = IdentityFunction <ndarray>();
            }
            // check for proper dimensionality of all arguments and write them down.
            this.n_inputs        = n_inputs;
            this.n_reservoir     = n_reservoir;
            this.n_outputs       = n_outputs;
            this.spectral_radius = spectral_radius;
            this.sparsity        = sparsity;
            this.noise           = noise;
            this.input_shift     = correct_dimensions(input_shift, n_inputs);
            this.input_scaling   = correct_dimensions(input_scaling, n_inputs);

            this.teacher_scaling = teacher_scaling;
            this.teacher_shift   = teacher_shift;

            this.out_activation         = out_activation;
            this.inverse_out_activation = inverse_out_activation;

            // the given random_state might be either an actual RandomState object,
            // a seed or None (in which case we use numpy's builtin RandomState)
            if (random_state is RandomState)
            {
                this.random_state_ = random_state as np.random;
            }
            else if (random_state is int)
            {
                try
                {
                    var rnd = new np.random();
                    rnd.seed((int)random_state);
                    this.random_state_ = rnd;
                }
                catch (System.Exception ex)
                {
                    throw new Exception("Invalid seed: " + ex);
                }
            }
            else
            {
                this.random_state_ = new np.random();
            }

            this.teacher_forcing = teacher_forcing;
            this.silent          = silent;
            this.initweights();
        }
예제 #10
0
        public void test_histogramdd_5()
        {
            var random = new np.random();

            random.seed(8765);

            var r = random.randint(10, 30, new shape(300000));

            System.Tuple <int, int>[] range1 = new Tuple <int, int>[]
            {
                Tuple.Create(15, 25),
                Tuple.Create(15, 25),
                Tuple.Create(15, 25),
                Tuple.Create(15, 25),
            };
            var x            = np.histogramdd(r.reshape(-1, 4), bins: np.array(new int[] { 2, 2, 2, 2 }), range: range1);
            var ExpectedHist = new double[, , , ]
            {
                { { { 299, 352 }, { 375, 447 } }, { { 342, 454 }, { 389, 519 } } },
                { { { 377, 372 }, { 435, 535 } }, { { 446, 497 }, { 516, 625 } } }
            };

            AssertArray(x.hist, ExpectedHist);
            AssertArray(x.bin_edges[0], new double[] { 15, 20, 25 });
            AssertArray(x.bin_edges[1], new double[] { 15, 20, 25 });
            AssertArray(x.bin_edges[2], new double[] { 15, 20, 25 });
            AssertArray(x.bin_edges[3], new double[] { 15, 20, 25 });
            print(x.hist);
            print(x.bin_edges);

            /////
            System.Tuple <int, int>[] range2 = new Tuple <int, int>[]
            {
                Tuple.Create(20, 20),
                Tuple.Create(20, 20),
                Tuple.Create(20, 20),
                Tuple.Create(20, 20),
            };
            x            = np.histogramdd(r.reshape(-1, 4), bins:  np.array(new double[] { 2, 2, 2, 2 }), range: range2);
            ExpectedHist = new double[, , , ]
            {
                { { { 0, 0 }, { 0, 0 } }, { { 0, 0 }, { 0, 0 } } },
                { { { 0, 0 }, { 0, 0 } }, { { 0, 0 }, { 0, 0 } } }
            };
            AssertArray(x.hist, ExpectedHist);
            AssertArray(x.bin_edges[0], new double[] { 19.5, 20, 20.5 });
            AssertArray(x.bin_edges[1], new double[] { 19.5, 20, 20.5 });
            AssertArray(x.bin_edges[2], new double[] { 19.5, 20, 20.5 });
            AssertArray(x.bin_edges[3], new double[] { 19.5, 20, 20.5 });
            print(x.hist);
            print(x.bin_edges);

            /////
            System.Tuple <int, int>[] range3 = new Tuple <int, int>[]
            {
                Tuple.Create(15, 25),
                Tuple.Create(15, 25),
            };
            x = np.histogramdd(r.reshape(-1, 2), bins: np.array(new Int32[] { 3, 3 }), density: true, range: range3);
            var ExpectedHist2 = new double[, ]
            {
                { 0.0119444935087874, 0.00873134328358209, 0.011668285789985 },
                { 0.00910293208513644, 0.00670052106332243, 0.00903537048485383 },
                { 0.0117239247549236, 0.00896184756689923, 0.0121312814625099 }
            };

            AssertArray(x.hist, ExpectedHist2);
            AssertArray(x.bin_edges[0], new double[] { 15.0, 18.3333333333333, 21.6666666666667, 25.0 });
            AssertArray(x.bin_edges[1], new double[] { 15.0, 18.3333333333333, 21.6666666666667, 25.0 });
            print(x.hist);
            print(x.bin_edges);
            /////

            System.Tuple <int, int>[] range4 = new Tuple <int, int>[]
            {
                Tuple.Create(15, 25),
                Tuple.Create(15, 25),
                Tuple.Create(15, 25),
            };
            x = np.histogramdd(r.reshape(-1, 3), bins: np.array(new Int32[] { 4, 4, 4 }), density: false, range: range4);
            var ExpectedHist3 = new double[, , ]
            {
                { { 317.0, 222.0, 322.0, 333.0 }, { 198.0, 151.0, 230.0, 228.0 }, { 339.0, 196.0, 333.0, 360.0 }, { 340.0, 211.0, 341.0, 324.0 } },
                { { 221.0, 164.0, 231.0, 213.0 }, { 147.0, 101.0, 162.0, 162.0 }, { 226.0, 165.0, 228.0, 235.0 }, { 239.0, 161.0, 242.0, 220.0 } },
                { { 334.0, 213.0, 361.0, 364.0 }, { 224.0, 157.0, 232.0, 217.0 }, { 372.0, 226.0, 331.0, 351.0 }, { 350.0, 249.0, 344.0, 347.0 } },
                { { 348.0, 214.0, 337.0, 313.0 }, { 207.0, 169.0, 234.0, 206.0 }, { 347.0, 225.0, 305.0, 343.0 }, { 331.0, 225.0, 357.0, 374.0 } }
            };

            AssertArray(x.hist, ExpectedHist3);
            AssertArray(x.bin_edges[0], new double[] { 15.0, 17.5, 20.0, 22.5, 25.0 });
            AssertArray(x.bin_edges[1], new double[] { 15.0, 17.5, 20.0, 22.5, 25.0 });
            AssertArray(x.bin_edges[2], new double[] { 15.0, 17.5, 20.0, 22.5, 25.0 });
            print(x.hist);
            print(x.bin_edges);
        }
예제 #11
0
        public void test_histogramdd_1()
        {
            var random = new np.random();

            random.seed(8765);

            var r = random.randint(10, 30, new shape(3000));


            var x            = np.histogramdd(r.reshape(-1, 4), bins: new int[] { 2, 2, 2, 2 });
            var ExpectedHist = new double[, , , ]
            {
                { { { 48.0, 48.0 }, { 38.0, 58.0 } }, { { 50.0, 45.0 }, { 46.0, 49.0 } } },
                { { { 44.0, 40.0 }, { 44.0, 50.0 } }, { { 43.0, 36.0 }, { 60.0, 51.0 } } }
            };

            AssertArray(x.hist, ExpectedHist);
            AssertArray(x.bin_edges[0], new double[] { 10.0, 19.5, 29 });
            AssertArray(x.bin_edges[1], new double[] { 10.0, 19.5, 29 });
            AssertArray(x.bin_edges[2], new double[] { 10.0, 19.5, 29 });
            AssertArray(x.bin_edges[3], new double[] { 10.0, 19.5, 29 });
            print(x.hist);
            print(x.bin_edges);

            /////
            x            = np.histogramdd(r.reshape(-1, 4), bins: new double[] { 2, 2, 2, 2 });
            ExpectedHist = new double[, , , ]
            {
                { { { 48.0, 48.0 }, { 38.0, 58.0 } }, { { 50.0, 45.0 }, { 46.0, 49.0 } } },
                { { { 44.0, 40.0 }, { 44.0, 50.0 } }, { { 43.0, 36.0 }, { 60.0, 51.0 } } }
            };
            AssertArray(x.hist, ExpectedHist);
            AssertArray(x.bin_edges[0], new double[] { 10.0, 19.5, 29 });
            AssertArray(x.bin_edges[1], new double[] { 10.0, 19.5, 29 });
            AssertArray(x.bin_edges[2], new double[] { 10.0, 19.5, 29 });
            AssertArray(x.bin_edges[3], new double[] { 10.0, 19.5, 29 });
            print(x.hist);
            print(x.bin_edges);

            /////
            x = np.histogramdd(r.reshape(-1, 2), bins: new Int32[] { 3, 3 }, density: true);
            var ExpectedHist2 = new double[, ]
            {
                { 0.00307479224376731, 0.00259279778393352, 0.00282548476454294 },
                { 0.00245983379501385, 0.00237673130193906, 0.00280886426592798 },
                { 0.00292520775623269, 0.00250969529085873, 0.0033573407202216 }
            };

            AssertArray(x.hist, ExpectedHist2);
            AssertArray(x.bin_edges[0], new double[] { 10.0, 16.3333333333333, 22.6666666666667, 29.0 });
            AssertArray(x.bin_edges[1], new double[] { 10.0, 16.3333333333333, 22.6666666666667, 29.0 });
            print(x.hist);
            print(x.bin_edges);

            x = np.histogramdd(r.reshape(-1, 3), bins: new Int32[] { 4, 4, 4 }, density: false);
            var ExpectedHist3 = new double[, , ]
            {
                { { 19.0, 21.0, 19.0, 15.0 }, { 22.0, 11.0, 11.0, 10.0 }, { 17.0, 13.0, 7.0, 12.0 }, { 12.0, 11.0, 7.0, 23.0 } },
                { { 12.0, 13.0, 16.0, 14.0 }, { 12.0, 14.0, 15.0, 20.0 }, { 17.0, 16.0, 19.0, 12.0 }, { 13.0, 13.0, 17.0, 16.0 } },
                { { 23.0, 22.0, 9.0, 13.0 }, { 12.0, 23.0, 20.0, 21.0 }, { 23.0, 14.0, 14.0, 25.0 }, { 19.0, 19.0, 13.0, 21.0 } },
                { { 18.0, 18.0, 13.0, 11.0 }, { 11.0, 11.0, 15.0, 20.0 }, { 14.0, 15.0, 15.0, 24.0 }, { 13.0, 15.0, 11.0, 16.0 } }
            };

            AssertArray(x.hist, ExpectedHist3);
            AssertArray(x.bin_edges[0], new double[] { 10.0, 14.75, 19.5, 24.25, 29.0 });
            AssertArray(x.bin_edges[1], new double[] { 10.0, 14.75, 19.5, 24.25, 29.0 });
            AssertArray(x.bin_edges[2], new double[] { 10.0, 14.75, 19.5, 24.25, 29.0 });
            print(x.hist);
            print(x.bin_edges);
        }