Beispiel #1
0
        public static Tensor _standard_gamma(Tensor input, torch.Generator generator = null)
        {
            var res = THSTensor_standard_gamma_(input.handle, (generator is null) ? IntPtr.Zero : generator.Handle);

            if (res == IntPtr.Zero)
            {
                torch.CheckForErrors();
            }
            return(new Tensor(res));
        }
Beispiel #2
0
        public static Tensor _sample_dirichlet(Tensor input, torch.Generator generator = null)
        {
            var res = THSTensor_sample_dirichlet_(input.Handle, (generator is null) ? IntPtr.Zero : generator.Handle);

            if (res == IntPtr.Zero)
            {
                torch.CheckForErrors();
            }
            return(new Tensor(res));
        }
Beispiel #3
0
        public void TestExplicitGenerators()
        {
            // This tests that the default generator can be disposed, but will keep on going.
            lock (_lock) {
                long a, b, c, d;
                var  gen = torch.random.manual_seed(4711);
                a = gen.initial_seed();

                torch.Generator genA = torch.Generator.Default;
                torch.Generator genB = new torch.Generator(4355);
                torch.Generator genC = new torch.Generator(4355);

                b = genA.initial_seed();
                c = genB.initial_seed();
                d = genC.initial_seed();

                Assert.Equal(a, b);
                Assert.Equal(c, d);
                Assert.NotEqual(a, c);
                Assert.Equal(4355, c);

                {
                    var x = torch.rand(100, generator: genB);
                    var y = torch.rand(100, generator: genC);
                    Assert.Equal(new long[] { 100 }, x.shape);
                    Assert.True(x.allclose(y));
                }

                {
                    var x = torch.randn(100, generator: genB);
                    var y = torch.randn(100, generator: genC);
                    Assert.Equal(new long[] { 100 }, x.shape);
                    Assert.True(x.allclose(y));
                }

                {
                    var x = torch.randint(1000, 100, generator: genB);
                    var y = torch.randint(1000, 100, generator: genC);
                    Assert.Equal(new long[] { 100 }, x.shape);
                    Assert.True(x.allclose(y));
                }

                {
                    var x = torch.randperm(1000, generator: genB);
                    var y = torch.randperm(1000, generator: genC);
                    Assert.Equal(new long[] { 1000 }, x.shape);
                    Assert.True(x.allclose(y));
                }
            }
        }
Beispiel #4
0
 /// <summary>
 /// Creates a Bernoulli distribution parameterized by `probs` or `logits` (but not both).
 /// </summary>
 /// <param name="probs">The probability of sampling '1'</param>
 /// <param name="logits">The log-odds of sampling '1'</param>
 /// <param name="generator">An optional random number generator object.</param>
 /// <returns></returns>
 public static Bernoulli Bernoulli(double?probs, double?logits, torch.Generator generator = null)
 {
     if (probs.HasValue && !logits.HasValue)
     {
         return(new Bernoulli(torch.tensor(probs.Value), null, generator));
     }
     else if (!probs.HasValue && logits.HasValue)
     {
         return(new Bernoulli(null, torch.tensor(logits.Value), generator));
     }
     else
     {
         throw new ArgumentException("One and only one of 'probs' and 'logits' should be non-null");
     }
 }
Beispiel #5
0
 /// <summary>
 /// Creates a Bernoulli distribution parameterized by `probs` or `logits` (but not both).
 /// </summary>
 /// <param name="probs">The probability of sampling '1'</param>
 /// <param name="logits">The log-odds of sampling '1'</param>
 /// <param name="generator">An optional random number generator object.</param>
 /// <returns></returns>
 public static Bernoulli Bernoulli(float?probs, float?logits, torch.Generator generator = null)
 {
     if (probs.HasValue && !logits.HasValue)
     {
         return(new Bernoulli(torch.tensor(probs.Value), null, generator));
     }
     else if (!probs.HasValue && logits.HasValue)
     {
         return(new Bernoulli(null, torch.tensor(logits.Value), generator));
     }
     else
     {
         throw new ArgumentException("One and only one of 'probs' and logits should be provided.");
     }
 }
Beispiel #6
0
 public void TestExplicitGenerators()
 {
     // This tests that the default generator can be disposed, but will keep on going.
     lock (_lock) {
         long a, b, c;
         using (var gen = torch.random.manual_seed(4711)) {
             a = gen.initial_seed();
         }
         using (torch.Generator gen = torch.Generator.Default, genA = new torch.Generator(4355)) {
             b = gen.initial_seed();
             c = genA.initial_seed();
         }
         Assert.Equal(a, b);
         Assert.NotEqual(a, c);
         Assert.Equal(4355, c);
     }
 }
Beispiel #7
0
 /// <summary>
 /// Creates a Bernoulli distribution parameterized by `probs` or `logits` (but not both).
 /// </summary>
 /// <param name="probs">The probability of sampling '1'</param>
 /// <param name="logits">The log-odds of sampling '1'</param>
 /// <param name="generator">An optional random number generator object.</param>
 /// <returns></returns>
 public static Bernoulli Bernoulli(Tensor probs = null, Tensor logits = null, torch.Generator generator = null)
 {
     return(new Bernoulli(probs, logits, generator));
 }
Beispiel #8
0
 public Distribution(torch.Generator generator, long[] batch_shape = null, long[] event_shape = null)
 {
     this.generator   = generator;
     this.batch_shape = batch_shape != null ? batch_shape : new long[0];
     this.event_shape = event_shape != null ? event_shape : new long[0];
 }
Beispiel #9
0
 /// <summary>
 /// Creates a Fisher-Snedecor distribution parameterized by `df1` and `df2`.
 /// </summary>
 /// <param name="df1">Degrees of freedom parameter 1</param>
 /// <param name="df2">Degrees of freedom parameter 2</param>
 /// <param name="generator">An optional random number generator object.</param>
 /// <returns></returns>
 public static FisherSnedecor FisherSnedecor(Tensor df1, Tensor df2, torch.Generator generator = null)
 {
     return(new FisherSnedecor(df1, df2, generator));
 }
Beispiel #10
0
 /// <summary>
 /// Creates a Dirichlet distribution parameterized by shape `concentration` and `rate`.
 /// </summary>
 /// <param name="concentration">Shape parameter of the distribution (often referred to as 'α')</param>
 /// <param name="generator">An optional random number generator object.</param>
 public static Dirichlet Dirichlet(Tensor concentration, torch.Generator generator = null)
 {
     return(new Dirichlet(concentration, generator));
 }
Beispiel #11
0
 /// <summary>
 /// Creates a Binomial distribution parameterized by `probs` or `logits` (but not both).
 /// `total_count` must be broadcastable with `probs`/`logits`.
 /// </summary>
 /// <param name="total_count">Number of Bernoulli trials</param>
 /// <param name="probs">The probability of sampling '1'</param>
 /// <param name="logits">The log-odds of sampling '1'</param>
 /// <param name="generator">An optional random number generator object.</param>
 /// <returns></returns>
 public static Binomial Binomial(Tensor total_count, Tensor probs = null, Tensor logits = null, torch.Generator generator = null)
 {
     return(new Binomial(total_count, probs, logits));
 }
Beispiel #12
0
 /// <summary>
 /// Creates a Binomial distribution parameterized by `probs` or `logits` (but not both).
 /// </summary>
 /// <param name="total_count">Number of Bernoulli trials</param>
 /// <param name="probs">The probability of sampling '1'</param>
 /// <param name="logits">The log-odds of sampling '1'</param>
 /// <param name="generator">An optional random number generator object.</param>
 /// <returns></returns>
 public static Binomial Binomial(int total_count, double?probs, double?logits, torch.Generator generator = null)
 {
     if (probs.HasValue && !logits.HasValue)
     {
         return(new Binomial(torch.tensor(total_count), torch.tensor(probs.Value), null, generator));
     }
     else if (!probs.HasValue && logits.HasValue)
     {
         return(new Binomial(torch.tensor(total_count), null, torch.tensor(logits.Value), generator));
     }
     else
     {
         throw new ArgumentException("One and only one of 'probs' and logits should be provided.");
     }
 }
Beispiel #13
0
            /// <summary>
            /// Creates an equal-probability categorical distribution parameterized by the number of categories.
            ///
            /// </summary>
            /// <param name="categories">The number of categories.</param>
            /// <param name="generator">An optional random number generator object.</param>
            /// <returns></returns>
            public static Categorical Categorical(int categories, torch.Generator generator = null)
            {
                var probs = torch.tensor(1.0 / categories).expand(categories);

                return(new Categorical(probs, null, generator));
            }
Beispiel #14
0
 /// <summary>
 /// Creates a Geometric distribution parameterized by probs,
 /// where probs is the probability of success of Bernoulli trials.
 ///
 /// It represents the probability that in k+1 Bernoulli trials, the
 /// first k trials failed, before seeing a success.
 /// </summary>
 /// <param name="probs">The probability of sampling '1'. Must be in range (0, 1]</param>
 /// <param name="logits">The log-odds of sampling '1'</param>
 /// <param name="generator">An optional random number generator object.</param>
 /// <returns></returns>
 public static Geometric Geometric(Tensor probs = null, Tensor logits = null, torch.Generator generator = null)
 {
     return(new Geometric(probs, logits, generator));
 }
Beispiel #15
0
 /// <summary>
 /// Creates a Multinomial distribution parameterized by `probs` or `logits` (but not both).
 /// `total_count` must be broadcastable with `probs`/`logits`.
 /// </summary>
 /// <param name="total_count">Number of Bernoulli trials</param>
 /// <param name="probs">The probability of sampling '1'</param>
 /// <param name="logits">The log-odds of sampling '1'</param>
 /// <param name="generator">An optional random number generator object.</param>
 /// <returns></returns>
 public static Multinomial Multinomial(int total_count, Tensor probs = null, Tensor logits = null, torch.Generator generator = null)
 {
     return(new Multinomial(total_count, probs, logits, generator));
 }
Beispiel #16
0
 /// <summary>
 /// Samples from a Normal (Gaussian) distribution. The distribution of the ratio of
 /// independent normally distributed random variables with means `0` follows a Normal distribution.
 /// </summary>
 /// <param name="loc">Mode or median of the distribution.</param>
 /// <param name="scale">Standard deviation.</param>
 /// <param name="generator">An optional random number generator object.</param>
 /// <returns></returns>
 public static Normal Normal(Tensor loc, Tensor scale, torch.Generator generator = null)
 {
     return(new Normal(loc, scale, generator));
 }
Beispiel #17
0
 /// <summary>
 /// Samples from a Normal (Gaussian) distribution. The distribution of the ratio of
 /// independent normally distributed random variables with means `0` follows a Normal distribution.
 /// </summary>
 /// <param name="loc">Mode or median of the distribution.</param>
 /// <param name="scale">Standard deviation.</param>
 /// <param name="generator">An optional random number generator object.</param>
 /// <returns></returns>
 public static Normal Normal(float loc, float scale = 1.0f, torch.Generator generator = null)
 {
     return(new Normal(torch.tensor(loc), torch.tensor(scale), generator));
 }
Beispiel #18
0
 /// <summary>
 /// Creates a Poisson distribution parameterized by `rate`.
 /// </summary>
 /// <param name="rate">rate = 1 / scale of the distribution (often referred to as 'β')</param>
 /// <param name="generator">An optional random number generator object.</param>
 /// <returns></returns>
 public static Poisson Poisson(double rate, torch.Generator generator = null)
 {
     return(new Poisson(torch.tensor(rate), generator));
 }
Beispiel #19
0
 /// <summary>
 /// Creates a Poisson distribution parameterized by `rate`.
 /// </summary>
 /// <param name="rate">rate = 1 / scale of the distribution (often referred to as 'β')</param>
 /// <param name="generator">An optional random number generator object.</param>
 /// <returns></returns>
 public static Poisson Poisson(Tensor rate, torch.Generator generator = null)
 {
     return(new Poisson(rate, generator));
 }
Beispiel #20
0
 /// <summary>
 /// Creates a Beta distribution parameterized by concentration1 and concentration0.
 /// </summary>
 /// <param name="concentration1">1st concentration parameter of the distribution (often referred to as 'α')</param>
 /// <param name="concentration0">2nd concentration parameter of the distribution (often referred to as 'β')</param>
 /// <param name="generator">An optional random number generator object.</param>
 /// <returns></returns>
 /// <remarks>The order of the arguments is not a mistake -- the original source has them ordered this way.
 /// </remarks>
 public static Beta Beta(Tensor concentration1, Tensor concentration0, torch.Generator generator = null)
 {
     return(new Beta(concentration1, concentration0, generator));
 }
Beispiel #21
0
 /// <summary>
 /// Samples from a Normal (Gaussian) distribution. The distribution of the ratio of
 /// independent normally distributed random variables with means `0` follows a Normal distribution.
 /// </summary>
 /// <param name="loc">Mode or median of the distribution.</param>
 /// <param name="scale">Standard deviation.</param>
 /// <param name="generator">An optional random number generator object.</param>
 /// <returns></returns>
 public static Normal Normal(double loc, double scale = 1.0, torch.Generator generator = null)
 {
     return(new Normal(torch.tensor(loc), torch.tensor(scale), generator));
 }
Beispiel #22
0
 /// <summary>
 /// Creates a Gamma distribution parameterized by shape `concentration` and `rate`.
 /// </summary>
 /// <param name="concentration">Shape parameter of the distribution (often referred to as 'α')</param>
 /// <param name="rate">rate = 1 / scale of the distribution (often referred to as 'β')</param>
 /// <param name="generator">An optional random number generator object.</param>
 /// <returns></returns>
 public static Gamma Gamma(Tensor concentration, Tensor rate, torch.Generator generator = null)
 {
     return(new Gamma(concentration, rate, generator));
 }
Beispiel #23
0
 /// <summary>
 /// Creates a Exponential distribution parameterized by `rate`.
 /// </summary>
 /// <param name="rate">rate = 1 / scale of the distribution (often referred to as 'β')</param>
 /// <param name="generator">An optional random number generator object.</param>
 /// <returns></returns>
 public static Exponential Exponential(Tensor rate, torch.Generator generator = null)
 {
     return(new Exponential(rate, generator));
 }
Beispiel #24
0
            /// <summary>
            /// Creates an equal-probability multinomial distribution parameterized by the number of categories.
            /// `total_count` must be broadcastable with `probs`/`logits`.
            /// </summary>
            /// <param name="total_count">Number of Bernoulli trials</param>
            /// <param name="categories">The number of categories.</param>
            /// <param name="generator">An optional random number generator object.</param>
            /// <returns></returns>
            public static Multinomial Multinomial(int total_count, int categories, torch.Generator generator = null)
            {
                var probs = torch.tensor(1.0 / categories).expand(categories);

                return(new Multinomial(total_count, probs, null, generator));
            }
Beispiel #25
0
 protected ExponentialFamily(torch.Generator generator) : base(generator)
 {
 }
Beispiel #26
0
 /// <summary>
 /// Creates a Categorical distribution parameterized by `probs` or `logits` (but not both).
 ///
 /// Samples are integers from [0, K-1]` where `K` is probs.size(-1).
 ///
 /// If `probs` is 1-dimensional with length- `K`, each element is the relative probability
 /// of sampling the class at that index.
 ///
 /// If `probs` is N-dimensional, the first N-1 dimensions are treated as a batch of
 /// relative probability vectors.
 /// </summary>
 /// <param name="probs">The probability of sampling '1'</param>
 /// <param name="logits">The log-odds of sampling '1'</param>
 /// <param name="generator">An optional random number generator object.</param>
 /// <returns></returns>
 public static Categorical Categorical(Tensor probs = null, Tensor logits = null, torch.Generator generator = null)
 {
     return(new Categorical(probs, logits));
 }
Beispiel #27
0
 /// <summary>
 /// Creates a Gamma distribution parameterized by a single shape parameter.
 /// </summary>
 /// <param name="df">Shape parameter of the distribution</param>
 /// <param name="generator">An optional random number generator object.</param>
 /// <returns></returns>
 public static Chi2 Chi2(Tensor df, torch.Generator generator = null)
 {
     return(new Chi2(df, generator));
 }
Beispiel #28
0
 /// <summary>
 /// Samples from a Cauchy (Lorentz) distribution. The distribution of the ratio of
 /// independent normally distributed random variables with means `0` follows a Cauchy distribution.
 /// </summary>
 /// <param name="loc">Mode or median of the distribution.</param>
 /// <param name="scale">Half width at half maximum.</param>
 /// <param name="generator">An optional random number generator object.</param>
 /// <returns></returns>
 public static Cauchy Cauchy(Tensor loc, Tensor scale, torch.Generator generator = null)
 {
     return(new Cauchy(loc, scale, generator));
 }
Beispiel #29
0
 /// <summary>
 /// Generates uniformly distributed random samples from the half-open interval [low, high[.
 /// </summary>
 /// <param name="low">Lower bound (inclusive)</param>
 /// <param name="high">Upper bound (exclusive)</param>
 /// <param name="generator">An optional random number generator object.</param>
 /// <returns></returns>
 public static Uniform Uniform(Tensor low, Tensor high, torch.Generator generator = null)
 {
     return(new Uniform(low, high, generator));
 }