本文整理汇总了Python中tensorflow.compat.v2.concat方法的典型用法代码示例。如果您正苦于以下问题:Python v2.concat方法的具体用法?Python v2.concat怎么用?Python v2.concat使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类tensorflow.compat.v2
的用法示例。
在下文中一共展示了v2.concat方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_conjugate_preset
# 需要导入模块: from tensorflow.compat import v2 [as 别名]
# 或者: from tensorflow.compat.v2 import concat [as 别名]
def test_conjugate_preset(self):
"""Tests if the conjugate function is providing correct results."""
x_init = test_helpers.generate_preset_test_dual_quaternions()
x = tf.convert_to_tensor(value=x_init)
y = tf.convert_to_tensor(value=x_init)
x = dual_quaternion.conjugate(x)
x_real, x_dual = tf.split(x, (4, 4), axis=-1)
y_real, y_dual = tf.split(y, (4, 4), axis=-1)
xyz_y_real, w_y_real = tf.split(y_real, (3, 1), axis=-1)
xyz_y_dual, w_y_dual = tf.split(y_dual, (3, 1), axis=-1)
y_real = tf.concat((-xyz_y_real, w_y_real), axis=-1)
y_dual = tf.concat((-xyz_y_dual, w_y_dual), axis=-1)
self.assertAllEqual(x_real, y_real)
self.assertAllEqual(x_dual, y_dual)
示例2: generate_preset_test_dual_quaternions
# 需要导入模块: from tensorflow.compat import v2 [as 别名]
# 或者: from tensorflow.compat.v2 import concat [as 别名]
def generate_preset_test_dual_quaternions():
"""Generates pre-set test quaternions."""
angles = generate_preset_test_euler_angles()
preset_quaternion_real = quaternion.from_euler(angles)
translations = generate_preset_test_translations()
translations = np.concatenate(
(translations / 2.0, np.zeros((np.ma.size(translations, 0), 1))), axis=1)
preset_quaternion_translation = tf.convert_to_tensor(value=translations)
preset_quaternion_dual = quaternion.multiply(preset_quaternion_translation,
preset_quaternion_real)
preset_dual_quaternion = tf.concat(
(preset_quaternion_real, preset_quaternion_dual), axis=-1)
return preset_dual_quaternion
示例3: generate_random_test_dual_quaternions
# 需要导入模块: from tensorflow.compat import v2 [as 别名]
# 或者: from tensorflow.compat.v2 import concat [as 别名]
def generate_random_test_dual_quaternions():
"""Generates random test dual quaternions."""
angles = generate_random_test_euler_angles()
random_quaternion_real = quaternion.from_euler(angles)
min_translation = -3.0
max_translation = 3.0
translations = np.random.uniform(min_translation, max_translation,
angles.shape)
translations_quaternion_shape = np.asarray(translations.shape)
translations_quaternion_shape[-1] = 1
translations = np.concatenate(
(translations / 2.0, np.zeros(translations_quaternion_shape)), axis=-1)
random_quaternion_translation = tf.convert_to_tensor(value=translations)
random_quaternion_dual = quaternion.multiply(random_quaternion_translation,
random_quaternion_real)
random_dual_quaternion = tf.concat(
(random_quaternion_real, random_quaternion_dual), axis=-1)
return random_dual_quaternion
示例4: hessian_as_matrix
# 需要导入模块: from tensorflow.compat import v2 [as 别名]
# 或者: from tensorflow.compat.v2 import concat [as 别名]
def hessian_as_matrix(function: Callable[[Parameters], tf.Tensor],
parameters: Parameters) -> tf.Tensor:
"""Computes the Hessian of a given function.
Same as `hessian`, although return a matrix of size [w_dim, w_dim], where
`w_dim` is the number of parameters, which makes it easier to work with.
Args:
function: A function for which we want to compute the Hessian.
parameters: Parameters with respect to the Hessian should be computed.
Returns:
A tensor of size [w_dim, w_dim] representing the Hessian.
"""
hessian_as_tensor_list = hessian(function, parameters)
hessian_as_tensor_list = [
tf.reshape(e, [e.shape[0], -1]) for e in hessian_as_tensor_list]
return tf.concat(hessian_as_tensor_list, axis=1)
示例5: test_expected_continuation
# 需要导入模块: from tensorflow.compat import v2 [as 别名]
# 或者: from tensorflow.compat.v2 import concat [as 别名]
def test_expected_continuation(self):
"""Tests that expected continuation works in V=1 case.
In particular this verifies that the regression done to get the expected
continuation value is performed on those elements which have a positive
exercise value.
"""
for dtype in (np.float32, np.float64):
a = tf.range(start=-2, limit=3, delta=1, dtype=dtype)
design = tf.concat([a, a], axis=0)
design = tf.concat([[tf.ones_like(design), design]], axis=1)
# These values ensure that the expected continuation value is `(1,...,1).`
exercise_now = tf.expand_dims(
tf.concat([tf.ones_like(a), tf.zeros_like(a)], axis=0), -1)
cashflow = tf.expand_dims(
tf.concat([tf.ones_like(a), -tf.ones_like(a)], axis=0), -1)
expected_exercise = lsm.expected_exercise_fn(
design, cashflow, exercise_now)
self.assertAllClose(expected_exercise, tf.ones_like(cashflow))
示例6: _exact_discretization_setup
# 需要导入模块: from tensorflow.compat import v2 [as 别名]
# 或者: from tensorflow.compat.v2 import concat [as 别名]
def _exact_discretization_setup(self, dim):
"""Initial setup for efficient computations."""
self._zero_padding = tf.zeros((dim, 1), dtype=self._dtype)
self._jump_locations = tf.concat(
[self._volatility.jump_locations(),
self._mean_reversion.jump_locations()], axis=-1)
self._jump_values_vol = self._volatility(self._jump_locations)
self._jump_values_mr = self._mean_reversion(self._jump_locations)
if dim == 1:
self._padded_knots = tf.concat([
self._zero_padding,
tf.expand_dims(self._jump_locations[:-1], axis=0)
], axis=1)
self._jump_values_vol = tf.expand_dims(self._jump_values_vol, axis=0)
self._jump_values_mr = tf.expand_dims(self._jump_values_mr, axis=0)
self._jump_locations = tf.expand_dims(self._jump_locations, axis=0)
else:
self._padded_knots = tf.concat(
[self._zero_padding, self._jump_locations[:, :-1]], axis=1)
示例7: _compute_yt
# 需要导入模块: from tensorflow.compat import v2 [as 别名]
# 或者: from tensorflow.compat.v2 import concat [as 别名]
def _compute_yt(self, t, mr_t, sigma_t):
"""Computes y(t) as described in [1], section 10.1.6.1."""
t = tf.repeat(tf.expand_dims(t, axis=0), self._dim, axis=0)
time_index = tf.searchsorted(self._jump_locations, t)
y_between_vol_knots = self._y_integral(
self._padded_knots, self._jump_locations, self._jump_values_vol,
self._jump_values_mr)
y_at_vol_knots = tf.concat(
[self._zero_padding,
_cumsum_using_matvec(y_between_vol_knots)], axis=1)
vn = tf.concat(
[self._zero_padding, self._jump_locations], axis=1)
y_t = self._y_integral(
tf.gather(vn, time_index, batch_dims=1), t, sigma_t, mr_t)
y_t = y_t + tf.gather(y_at_vol_knots, time_index, batch_dims=1)
return tf.math.exp(-2 * mr_t * t) * y_t
示例8: _conditional_variance_x
# 需要导入模块: from tensorflow.compat import v2 [as 别名]
# 或者: from tensorflow.compat.v2 import concat [as 别名]
def _conditional_variance_x(self, t, mr_t, sigma_t):
"""Computes the variance of x(t), see [1], Eq. 10.41."""
t = tf.repeat(tf.expand_dims(t, axis=0), self._dim, axis=0)
var_x_between_vol_knots = self._variance_int(self._padded_knots,
self._jump_locations,
self._jump_values_vol,
self._jump_values_mr)
varx_at_vol_knots = tf.concat(
[self._zero_padding,
_cumsum_using_matvec(var_x_between_vol_knots)],
axis=1)
time_index = tf.searchsorted(self._jump_locations, t)
vn = tf.concat(
[self._zero_padding,
self._jump_locations], axis=1)
var_x_t = self._variance_int(
tf.gather(vn, time_index, batch_dims=1), t, sigma_t, mr_t)
var_x_t = var_x_t + tf.gather(varx_at_vol_knots, time_index, batch_dims=1)
var_x_t = (var_x_t[:, 1:] - var_x_t[:, :-1]) * tf.math.exp(
-2 * tf.broadcast_to(mr_t, t.shape)[:, 1:] * t[:, 1:])
return var_x_t
示例9: test_interpolation_differentiable
# 需要导入模块: from tensorflow.compat import v2 [as 别名]
# 或者: from tensorflow.compat.v2 import concat [as 别名]
def test_interpolation_differentiable(self):
dtype = tf.float64
interval_times = tf.constant([0.25, 0.5, 1.0, 2.0, 3.0], dtype=dtype)
knot_1y = tf.constant([0.052], dtype=dtype)
interval_values = tf.concat([
tf.constant([0.05, 0.051], dtype=dtype), knot_1y,
tf.constant([0.053, 0.055], dtype=dtype)
],
axis=0)
test_time = tf.constant([1.1, 2.7], dtype=dtype)
interpolated, _ = monotone_convex.interpolate(test_time, interval_values,
interval_times)
gradient_1y = self.evaluate(tf.convert_to_tensor(
tf.gradients(interpolated[0], knot_1y)[0]))
gradient_zero = self.evaluate(tf.convert_to_tensor(
tf.gradients(interpolated[1], knot_1y)[0]))
self.assertAlmostEqual(gradient_1y[0], 0.42)
self.assertAlmostEqual(gradient_zero[0], 0.0)
示例10: test_interpolated_yields_consistency
# 需要导入模块: from tensorflow.compat import v2 [as 别名]
# 或者: from tensorflow.compat.v2 import concat [as 别名]
def test_interpolated_yields_consistency(self):
dtypes = [np.float32, np.float64]
for dtype in dtypes:
reference_times = np.array([1.0, 2.0, 3.0, 4.0], dtype=dtype)
yields = np.array([5.0, 4.75, 4.53333333, 4.775], dtype=dtype)
# Times for which the interpolated values are required.
interpolation_times_1 = tf.constant([0.25, 0.5, 1.0, 2.0, 3.0],
dtype=dtype)
interpolation_times_2 = tf.constant([1.1, 2.5, 2.9, 3.6, 4.0],
dtype=dtype)
expected = np.array([
5.1171875, 5.09375, 5.0, 4.75, 4.533333, 4.9746, 4.624082, 4.535422,
4.661777, 4.775
],
dtype=dtype)
actual_1 = monotone_convex.interpolate_yields(
interpolation_times_1, reference_times, yields=yields)
actual_2 = monotone_convex.interpolate_yields(
interpolation_times_2, reference_times, yields=yields)
actual = self.evaluate(tf.concat([actual_1, actual_2], axis=0))
np.testing.assert_allclose(actual, expected, rtol=1e-5)
示例11: _initial_discount_rates
# 需要导入模块: from tensorflow.compat import v2 [as 别名]
# 或者: from tensorflow.compat.v2 import concat [as 别名]
def _initial_discount_rates(bond_cashflows,
bond_cashflow_times,
present_values,
name='initial_discount_rates'):
"""Constructs a guess for the initial rates as the yields to maturity."""
n = len(bond_cashflows)
groups = []
for i in range(n):
groups.append(tf.fill(tf.shape(bond_cashflows[i]), i))
bond_cashflows = tf.concat(bond_cashflows, axis=0)
bond_cashflow_times = tf.concat(bond_cashflow_times, axis=0)
groups = tf.concat(groups, axis=0)
return cashflows.yields_from_pv(
bond_cashflows,
bond_cashflow_times,
present_values,
groups=groups,
name=name)
示例12: _apply_sequence_to_tensor_op
# 需要导入模块: from tensorflow.compat import v2 [as 别名]
# 或者: from tensorflow.compat.v2 import concat [as 别名]
def _apply_sequence_to_tensor_op(cls, op_fn, tensor_wrappers):
"""Applies given sequence-to-tensor op.
This method is used for implementing ops that take a sequence of tensors and
return a new tensor, such as tf.concat and tf.stack. Implementing wrappers
should apply `op_fn` to the backing tensor(s) and return an new wrapper
instance with the combined backing tensor.
Args:
op_fn: Callable that applies sequence-to-tensor op to the given sequence
of Tensors. E.g. applies tf.concat.
tensor_wrappers: a sequence of tensor wrappers to be transformed. All
elements have the type of the implementing TensorWrapper class.
Returns:
A TensorWrapper instance with combined backing tensor(s).
"""
raise NotImplementedError()
示例13: _reshape_inner_dims
# 需要导入模块: from tensorflow.compat import v2 [as 别名]
# 或者: from tensorflow.compat.v2 import concat [as 别名]
def _reshape_inner_dims(
tensor: tf.Tensor,
shape: tf.TensorShape,
new_shape: tf.TensorShape) -> tf.Tensor:
"""Reshapes tensor to: shape(tensor)[:-len(shape)] + new_shape."""
tensor_shape = tf.shape(tensor)
ndims = shape.rank
tensor.shape[-ndims:].assert_is_compatible_with(shape)
new_shape_inner_tensor = tf.cast(
[-1 if d is None else d for d in new_shape.as_list()], tf.int64)
new_shape_outer_tensor = tf.cast(
tensor_shape[:-ndims], tf.int64)
full_new_shape = tf.concat(
(new_shape_outer_tensor, new_shape_inner_tensor), axis=0)
new_tensor = tf.reshape(tensor, full_new_shape)
new_tensor.set_shape(tensor.shape[:-ndims] + new_shape)
return new_tensor
示例14: _get_bi_lstm_encoder
# 需要导入模块: from tensorflow.compat import v2 [as 别名]
# 或者: from tensorflow.compat.v2 import concat [as 别名]
def _get_bi_lstm_encoder(self, num_hidden_layers, hidden_dim):
"""Get Bi-LSTM encoder.
Args:
num_hidden_layers: Number of stacked layers.
hidden_dim: The hidden size of LSTM.
Returns:
A list of 2N+1 elements. The first element is output of all timesteps
and the others are LSTM state. The first N are forward states and the
last N are backward states.
"""
self._cells = []
for layer_id in range(num_hidden_layers):
self._cells.append(
tf.keras.layers.LSTMCell(
hidden_dim, name='lstm_layer_{}'.format(layer_id)))
self._cells_rnn = tf.keras.layers.RNN(
self._cells, return_sequences=True, return_state=True)
return tf.keras.layers.Bidirectional(self._cells_rnn, merge_mode='concat')
示例15: _get_initial_state
# 需要导入模块: from tensorflow.compat import v2 [as 别名]
# 或者: from tensorflow.compat.v2 import concat [as 别名]
def _get_initial_state(self, observation, batch_size):
text_token_ids = observation[constants.INS_TOKEN_IDS]
text_enc_outputs, final_state = self._instruction_encoder(text_token_ids)
if self._ndh_instruction_encoder is not None:
ndh_text_enc_outputs, ndh_final_state = self._ndh_instruction_encoder(
text_token_ids)
mask = tf.equal(observation[constants.PROBLEM_TYPE],
constants.PROBLEM_VLN)
text_enc_outputs = tf.nest.map_structure(
lambda x, y: tf.compat.v1.where(mask, x, y), text_enc_outputs,
ndh_text_enc_outputs)
final_state = tf.nest.map_structure(
lambda x, y: tf.compat.v1.where(mask, x, y), final_state,
ndh_final_state)
if self._ins_classifier is not None:
# Concatenate all hidden layers' state vectors. Use state.h
ins_classifier_logits = self._ins_classifier(
tf.concat([s[0] for s in final_state], axis=1))
else:
ins_classifier_logits = tf.zeros(shape=(batch_size, 2))
return (final_state, text_enc_outputs, ins_classifier_logits)