Outputs random values from a uniform distribution. The generated values follow a uniform distribution in the range `[minval, maxval)`. The lower bound `minval` is included in the range, while the upper bound `maxval` is excluded. (For float numbers especially low-precision types lik
(self, shape, minval=0, maxval=None,
dtype=dtypes.float32, name=None)
| 458 | self.state.handle, self.algorithm, shape=shape, dtype=dtype) |
| 459 | |
| 460 | def uniform(self, shape, minval=0, maxval=None, |
| 461 | dtype=dtypes.float32, name=None): |
| 462 | """Outputs random values from a uniform distribution. |
| 463 | |
| 464 | The generated values follow a uniform distribution in the range |
| 465 | `[minval, maxval)`. The lower bound `minval` is included in the range, while |
| 466 | the upper bound `maxval` is excluded. (For float numbers especially |
| 467 | low-precision types like bfloat16, because of |
| 468 | rounding, the result may sometimes include `maxval`.) |
| 469 | |
| 470 | For floats, the default range is `[0, 1)`. For ints, at least `maxval` must |
| 471 | be specified explicitly. |
| 472 | |
| 473 | In the integer case, the random integers are slightly biased unless |
| 474 | `maxval - minval` is an exact power of two. The bias is small for values of |
| 475 | `maxval - minval` significantly smaller than the range of the output (either |
| 476 | `2**32` or `2**64`). |
| 477 | |
| 478 | Args: |
| 479 | shape: A 1-D integer Tensor or Python array. The shape of the output |
| 480 | tensor. |
| 481 | minval: A 0-D Tensor or Python value of type `dtype`. The lower bound on |
| 482 | the range of random values to generate. Defaults to 0. |
| 483 | maxval: A 0-D Tensor or Python value of type `dtype`. The upper bound on |
| 484 | the range of random values to generate. Defaults to 1 if `dtype` is |
| 485 | floating point. |
| 486 | dtype: The type of the output. |
| 487 | name: A name for the operation (optional). |
| 488 | |
| 489 | Returns: |
| 490 | A tensor of the specified shape filled with random uniform values. |
| 491 | |
| 492 | Raises: |
| 493 | ValueError: If `dtype` is integral and `maxval` is not specified. |
| 494 | """ |
| 495 | dtype = dtypes.as_dtype(dtype) |
| 496 | if maxval is None: |
| 497 | if dtype.is_integer: |
| 498 | raise ValueError("Must specify maxval for integer dtype %r" % dtype) |
| 499 | maxval = 1 |
| 500 | with ops.name_scope(name, "stateful_uniform", |
| 501 | [shape, minval, maxval]) as name: |
| 502 | shape = _shape_tensor(shape) |
| 503 | minval = ops.convert_to_tensor(minval, dtype=dtype, name="min") |
| 504 | maxval = ops.convert_to_tensor(maxval, dtype=dtype, name="max") |
| 505 | if dtype.is_integer: |
| 506 | return gen_stateful_random_ops.stateful_uniform_int( |
| 507 | self.state.handle, self.algorithm, shape=shape, |
| 508 | minval=minval, maxval=maxval, name=name) |
| 509 | else: |
| 510 | rnd = self._uniform(shape=shape, dtype=dtype) |
| 511 | return math_ops.add(rnd * (maxval - minval), minval, name=name) |
| 512 | |
| 513 | def uniform_full_int(self, shape, dtype=dtypes.uint64, name=None): |
| 514 | """Uniform distribution on an integer type's entire range. |