本文整理汇总了Python中tensorflow.compat.v2.int32方法的典型用法代码示例。如果您正苦于以下问题:Python v2.int32方法的具体用法?Python v2.int32怎么用?Python v2.int32使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类tensorflow.compat.v2
的用法示例。
在下文中一共展示了v2.int32方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: compute_logits
# 需要导入模块: from tensorflow.compat import v2 [as 别名]
# 或者: from tensorflow.compat.v2 import int32 [as 别名]
def compute_logits(self, token_ids: tf.Tensor, training: bool) -> tf.Tensor:
"""
Implements a language model, where each output is conditional on the current
input and inputs processed so far.
Args:
token_ids: int32 tensor of shape [B, T], storing integer IDs of tokens.
training: Flag indicating if we are currently training (used to toggle dropout)
Returns:
tf.float32 tensor of shape [B, T, V], storing the distribution over output symbols
for each timestep for each batch element.
"""
# TODO 5# 1) Embed tokens
# TODO 5# 2) Run RNN on embedded tokens
# TODO 5# 3) Project RNN outputs onto the vocabulary to obtain logits.
return rnn_output_logits
示例2: _key2seed
# 需要导入模块: from tensorflow.compat import v2 [as 别名]
# 或者: from tensorflow.compat.v2 import int32 [as 别名]
def _key2seed(a):
"""Converts an RNG key to an RNG seed.
Args:
a: an RNG key, an ndarray of shape [] and dtype `np.int64`.
Returns:
an RNG seed, a tensor of shape [2] and dtype `tf.int32`.
"""
def int64_to_int32s(a):
"""Converts an int64 tensor of shape [] to an int32 tensor of shape [2]."""
a = tf.cast(a, tf.uint64)
fst = tf.cast(a, tf.uint32)
snd = tf.cast(
tf.bitwise.right_shift(a, tf.constant(32, tf.uint64)), tf.uint32)
a = [fst, snd]
a = tf.nest.map_structure(lambda x: tf.cast(x, tf.int32), a)
a = tf.stack(a)
return a
return int64_to_int32s(a.data)
示例3: _seed2key
# 需要导入模块: from tensorflow.compat import v2 [as 别名]
# 或者: from tensorflow.compat.v2 import int32 [as 别名]
def _seed2key(a):
"""Converts an RNG seed to an RNG key.
Args:
a: an RNG seed, a tensor of shape [2] and dtype `tf.int32`.
Returns:
an RNG key, an ndarray of shape [] and dtype `np.int64`.
"""
def int32s_to_int64(a):
"""Converts an int32 tensor of shape [2] to an int64 tensor of shape []."""
a = tf.bitwise.bitwise_or(
tf.cast(a[0], tf.uint64),
tf.bitwise.left_shift(
tf.cast(a[1], tf.uint64), tf.constant(32, tf.uint64)))
a = tf.cast(a, tf.int64)
return a
return tf_np.asarray(int32s_to_int64(a))
示例4: true_divide
# 需要导入模块: from tensorflow.compat import v2 [as 别名]
# 或者: from tensorflow.compat.v2 import int32 [as 别名]
def true_divide(x1, x2):
def _avoid_float64(x1, x2):
if x1.dtype == x2.dtype and x1.dtype in (tf.int32, tf.int64):
x1 = tf.cast(x1, dtype=tf.float32)
x2 = tf.cast(x2, dtype=tf.float32)
return x1, x2
def f(x1, x2):
if x1.dtype == tf.bool:
assert x2.dtype == tf.bool
float_ = dtypes.default_float_type()
x1 = tf.cast(x1, float_)
x2 = tf.cast(x2, float_)
if not dtypes.is_allow_float64():
# tf.math.truediv in Python3 produces float64 when both inputs are int32
# or int64. We want to avoid that when is_allow_float64() is False.
x1, x2 = _avoid_float64(x1, x2)
return tf.math.truediv(x1, x2)
return _bin_op(f, x1, x2)
示例5: setUp
# 需要导入模块: from tensorflow.compat import v2 [as 别名]
# 或者: from tensorflow.compat.v2 import int32 [as 别名]
def setUp(self):
super(LogicTest, self).setUp()
self.array_transforms = [
lambda x: x, # Identity,
tf.convert_to_tensor,
np.array,
lambda x: np.array(x, dtype=np.int32),
lambda x: np.array(x, dtype=np.int64),
lambda x: np.array(x, dtype=np.float32),
lambda x: np.array(x, dtype=np.float64),
array_ops.array,
lambda x: array_ops.array(x, dtype=tf.int32),
lambda x: array_ops.array(x, dtype=tf.int64),
lambda x: array_ops.array(x, dtype=tf.float32),
lambda x: array_ops.array(x, dtype=tf.float64),
]
示例6: __init__
# 需要导入模块: from tensorflow.compat import v2 [as 别名]
# 或者: from tensorflow.compat.v2 import int32 [as 别名]
def __init__(self,
example_feature_columns,
size_feature_name,
name='generate_mask_layer',
**kwargs):
"""Constructs a mask generator layer.
Args:
example_feature_columns: (dict) example feature names to columns.
size_feature_name: (str) Name of feature for example list sizes. If not
None, this feature name corresponds to a `tf.int32` Tensor of size
[batch_size] corresponding to sizes of example lists. If `None`, all
examples are treated as valid.
name: (str) name of the layer.
**kwargs: keyword arguments.
"""
super(GenerateMask, self).__init__(name=name, **kwargs)
self._example_feature_columns = example_feature_columns
self._size_feature_name = size_feature_name
示例7: testOutputIsPermutation
# 需要导入模块: from tensorflow.compat import v2 [as 别名]
# 或者: from tensorflow.compat.v2 import int32 [as 别名]
def testOutputIsPermutation(self):
"""Checks that stateless_random_shuffle outputs a permutation."""
for dtype in (tf.int32, tf.int64, tf.float32, tf.float64):
identity_permutation = tf.range(10, dtype=dtype)
random_shuffle_seed_1 = tff_rnd.stateless_random_shuffle(
identity_permutation, seed=tf.constant((1, 42), tf.int64))
random_shuffle_seed_2 = tff_rnd.stateless_random_shuffle(
identity_permutation, seed=tf.constant((2, 42), tf.int64))
# Check that the shuffles are of the correct dtype
for shuffle in (random_shuffle_seed_1, random_shuffle_seed_2):
np.testing.assert_equal(shuffle.dtype, dtype.as_numpy_dtype)
random_shuffle_seed_1 = self.evaluate(random_shuffle_seed_1)
random_shuffle_seed_2 = self.evaluate(random_shuffle_seed_2)
identity_permutation = self.evaluate(identity_permutation)
# Check that the shuffles are different
self.assertTrue(
np.abs(random_shuffle_seed_1 - random_shuffle_seed_2).max())
# Check that the shuffles are indeed permutations
for shuffle in (random_shuffle_seed_1, random_shuffle_seed_2):
self.assertAllEqual(set(shuffle), set(identity_permutation))
示例8: testOutputIsIndependentOfInputValues
# 需要导入模块: from tensorflow.compat import v2 [as 别名]
# 或者: from tensorflow.compat.v2 import int32 [as 别名]
def testOutputIsIndependentOfInputValues(self):
"""stateless_random_shuffle output is independent of input_tensor values."""
# Generate sorted array of random numbers to control that the result
# is independent of `input_tesnor` values
np.random.seed(25)
random_input = np.random.normal(size=[10])
random_input.sort()
for dtype in (tf.int32, tf.int64, tf.float32, tf.float64):
# Permutation of a sequence [0, 1, .., 9]
random_permutation = tff_rnd.stateless_random_shuffle(
tf.range(10, dtype=dtype), seed=(100, 42))
random_permutation = self.evaluate(random_permutation)
# Shuffle `random_input` with the same seed
random_shuffle_control = tff_rnd.stateless_random_shuffle(
random_input, seed=(100, 42))
random_shuffle_control = self.evaluate(random_shuffle_control)
# Checks that the generated permutation does not depend on the underlying
# values
np.testing.assert_array_equal(
np.argsort(random_permutation), np.argsort(random_shuffle_control))
示例9: testOutputIsStatelessSession
# 需要导入模块: from tensorflow.compat import v2 [as 别名]
# 或者: from tensorflow.compat.v2 import int32 [as 别名]
def testOutputIsStatelessSession(self):
"""Checks that stateless_random_shuffle is stateless across Sessions."""
random_permutation_next_call = None
for dtype in (tf.int32, tf.int64, tf.float32, tf.float64):
random_permutation = tff_rnd.stateless_random_shuffle(
tf.range(10, dtype=dtype), seed=tf.constant((100, 42), tf.int64))
with tf.compat.v1.Session() as sess:
random_permutation_first_call = sess.run(random_permutation)
if random_permutation_next_call is not None:
# Checks that the values are the same across different dtypes
np.testing.assert_array_equal(random_permutation_first_call,
random_permutation_next_call)
with tf.compat.v1.Session() as sess:
random_permutation_next_call = sess.run(random_permutation)
np.testing.assert_array_equal(random_permutation_first_call,
random_permutation_next_call)
示例10: testMultiDimensionalShape
# 需要导入模块: from tensorflow.compat import v2 [as 别名]
# 或者: from tensorflow.compat.v2 import int32 [as 别名]
def testMultiDimensionalShape(self):
"""Check that stateless_random_shuffle works with multi-dim shapes."""
for dtype in (tf.int32, tf.int64, tf.float32, tf.float64):
input_permutation = tf.constant([[[1], [2], [3]], [[4], [5], [6]]],
dtype=dtype)
random_shuffle = tff_rnd.stateless_random_shuffle(
input_permutation, seed=(1, 42))
random_permutation_first_call = self.evaluate(random_shuffle)
random_permutation_next_call = self.evaluate(random_shuffle)
input_permutation = self.evaluate(input_permutation)
# Check that the dtype is correct
np.testing.assert_equal(random_permutation_first_call.dtype,
dtype.as_numpy_dtype)
# Check that the shuffles are the same
np.testing.assert_array_equal(random_permutation_first_call,
random_permutation_next_call)
# Check that the output shape is correct
np.testing.assert_equal(random_permutation_first_call.shape,
input_permutation.shape)
示例11: testFindsAllRootsUsingFloat16
# 需要导入模块: from tensorflow.compat import v2 [as 别名]
# 或者: from tensorflow.compat.v2 import int32 [as 别名]
def testFindsAllRootsUsingFloat16(self):
left_bracket = [-2, 1]
right_bracket = [2, -1]
expected_num_iterations = [9, 4]
expected_num_iterations, result = self.evaluate([
tf.constant(expected_num_iterations, dtype=tf.int32),
root_search.brentq(polynomial5,
tf.constant(left_bracket, dtype=tf.float16),
tf.constant(right_bracket, dtype=tf.float16))
])
_, value_at_roots, num_iterations, _ = result
# Simply check that the objective function is close to the root for the
# returned estimates. Do not check the estimates themselves.
# Using float16 may yield root estimates which differ from those returned
# by the SciPy implementation.
self.assertAllClose(value_at_roots, [0., 0.], atol=1e-3)
self.assertAllEqual(num_iterations, expected_num_iterations)
示例12: testFindsRootForFlatFunction
# 需要导入模块: from tensorflow.compat import v2 [as 别名]
# 或者: from tensorflow.compat.v2 import int32 [as 别名]
def testFindsRootForFlatFunction(self):
# Flat in the [-0.5, 0.5] range.
objective_fn = lambda x: 0 if x == 0 else x * exp(-1 / x**2)
left_bracket = [-10]
right_bracket = [1]
expected_num_iterations = [13]
expected_num_iterations, result = self.evaluate([
tf.constant(expected_num_iterations, dtype=tf.int32),
root_search.brentq(objective_fn,
tf.constant(left_bracket, dtype=tf.float64),
tf.constant(right_bracket, dtype=tf.float64))
])
_, value_at_roots, num_iterations, _ = result
# Simply check that the objective function is close to the root for the
# returned estimate. Do not check the estimate itself.
# Unlike Brent's original algorithm (and the SciPy implementation), this
# implementation stops the search as soon as a good enough root estimate is
# found. As a result, the estimate may significantly differ from the one
# returned by SciPy for functions which are extremely flat around the root.
self.assertAllClose(value_at_roots, [0.])
self.assertAllEqual(num_iterations, expected_num_iterations)
示例13: test_sobol_numbers_generation
# 需要导入模块: from tensorflow.compat import v2 [as 别名]
# 或者: from tensorflow.compat.v2 import int32 [as 别名]
def test_sobol_numbers_generation(self, dtype):
"""Sobol random dtype results in the correct draws."""
num_draws = tf.constant(2, dtype=tf.int32)
steps_num = tf.constant(3, dtype=tf.int32)
num_samples = tf.constant(4, dtype=tf.int32)
random_type = tff.math.random.RandomType.SOBOL
skip = 10
samples = utils.generate_mc_normal_draws(
num_normal_draws=num_draws, num_time_steps=steps_num,
num_sample_paths=num_samples, random_type=random_type,
dtype=dtype, skip=skip)
expected_samples = [[[0.8871465, 0.48877636],
[-0.8871465, -0.48877636],
[0.48877636, 0.8871465],
[-0.15731068, 0.15731068]],
[[0.8871465, -1.5341204],
[1.5341204, -0.15731068],
[-0.15731068, 1.5341204],
[-0.8871465, 0.48877636]],
[[-0.15731068, 1.5341204],
[0.15731068, -0.48877636],
[-1.5341204, 0.8871465],
[0.8871465, -1.5341204]]]
self.assertAllClose(samples, expected_samples, rtol=1e-5, atol=1e-5)
示例14: _euler_step
# 需要导入模块: from tensorflow.compat import v2 [as 别名]
# 或者: from tensorflow.compat.v2 import int32 [as 别名]
def _euler_step(*, i, written_count, current_state, result,
drift_fn, volatility_fn, wiener_mean,
num_samples, times, dt, sqrt_dt, keep_mask,
random_type, seed, normal_draws):
"""Performs one step of Euler scheme."""
current_time = times[i + 1]
written_count = tf.cast(written_count, tf.int32)
if normal_draws is not None:
dw = normal_draws[i]
else:
dw = random.mv_normal_sample(
(num_samples,), mean=wiener_mean, random_type=random_type,
seed=seed)
dw = dw * sqrt_dt[i]
dt_inc = dt[i] * drift_fn(current_time, current_state) # pylint: disable=not-callable
dw_inc = tf.linalg.matvec(volatility_fn(current_time, current_state), dw) # pylint: disable=not-callable
next_state = current_state + dt_inc + dw_inc
result = utils.maybe_update_along_axis(
tensor=result,
do_update=keep_mask[i + 1],
ind=written_count,
axis=1,
new_tensor=tf.expand_dims(next_state, axis=1))
written_count += tf.cast(keep_mask[i + 1], dtype=tf.int32)
return i + 1, written_count, next_state, result
示例15: business_days_in_period
# 需要导入模块: from tensorflow.compat import v2 [as 别名]
# 或者: from tensorflow.compat.v2 import int32 [as 别名]
def business_days_in_period(self, date_tensor, period_tensor):
"""Calculates number of business days in a period.
Includes the dates in `date_tensor`, but excludes final dates resulting from
addition of `period_tensor`.
Args:
date_tensor: DateTensor of starting dates.
period_tensor: PeriodTensor, should be broadcastable to `date_tensor`.
Returns:
An int32 Tensor with the number of business days in given periods that
start at given dates.
"""
return self.business_days_between(date_tensor, date_tensor + period_tensor)