本文整理汇总了Python中tensorflow.repeat方法的典型用法代码示例。如果您正苦于以下问题:Python tensorflow.repeat方法的具体用法?Python tensorflow.repeat怎么用?Python tensorflow.repeat使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类tensorflow
的用法示例。
在下文中一共展示了tensorflow.repeat方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: sparse_add_self_loops
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import repeat [as 别名]
def sparse_add_self_loops(indices, N=None):
"""
Given the indices of a square SparseTensor, adds the diagonal entries (i, i)
and returns the reordered indices.
:param indices: Tensor of rank 2, the indices to a SparseTensor.
:param N: the size of the N x N SparseTensor indexed by the indices. If `None`,
N is calculated as the maximum entry in the indices plus 1.
:return: Tensor of rank 2, the indices to a SparseTensor.
"""
N = tf.reduce_max(indices) + 1 if N is None else N
row, col = indices[..., 0], indices[..., 1]
mask = tf.ensure_shape(row != col, row.shape)
sl_indices = tf.range(N, dtype=row.dtype)[:, None]
sl_indices = tf.repeat(sl_indices, 2, -1)
indices = tf.concat((indices[mask], sl_indices), 0)
dummy_values = tf.ones_like(indices[:, 0])
indices, _ = gen_sparse_ops.sparse_reorder(indices, dummy_values, (N, N))
return indices
示例2: _get_forward_rate
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import repeat [as 别名]
def _get_forward_rate(self, valuation_date, market):
"""Returns the relevant forward rates from the market data."""
forward_rates = market.reference_curve.get_forward_rate(
self._accrual_start_dates,
self._accrual_end_dates,
self._daycount_fractions)
forward_rates = tf.where(self._daycount_fractions > 0.0, forward_rates,
tf.zeros_like(forward_rates))
libor_rate = rc.get_rate_index(
market, self._start_date, rc.RateIndexType.LIBOR, dtype=self._dtype)
libor_rate = tf.repeat(
tf.convert_to_tensor(libor_rate, dtype=self._dtype), self._num_caplets)
forward_rates = tf.where(
self._accrual_end_dates < valuation_date,
tf.constant(0., dtype=self._dtype),
tf.where(self._accrual_start_dates < valuation_date, libor_rate,
forward_rates))
return forward_rates
示例3: _setup_tensors
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import repeat [as 别名]
def _setup_tensors(self):
"""Sets up tensors for efficient computations."""
date_schedule = dates.PeriodicSchedule(
start_date=self._start_date,
end_date=self._maturity_date,
tenor=self._reset_frequency).dates()
# rates reset at the begining of coupon period
reset_dates = date_schedule[:, :-1]
# payments occur at the end of the coupon period
payment_dates = date_schedule[:, 1:]
daycount_fractions = rc.get_daycount_fraction(
date_schedule[:, :-1],
date_schedule[:, 1:],
self._daycount_convention,
dtype=self._dtype)
contract_index = tf.repeat(
tf.range(0, self._batch_size),
payment_dates.shape.as_list()[-1])
self._num_caplets = daycount_fractions.shape.as_list()[-1]
# TODO(b/152164086): Use the functionality from dates library
self._rate_term = tf.repeat(tf.cast(reset_dates[:, 0].days_until(
payment_dates[:, 0]), dtype=self._dtype) / 365.0, self._num_caplets)
self._reset_dates = dates.DateTensor.reshape(reset_dates, [-1])
self._payment_dates = dates.DateTensor.reshape(payment_dates, [-1])
self._accrual_start_dates = dates.DateTensor.reshape(reset_dates, [-1])
self._accrual_end_dates = dates.DateTensor.reshape(payment_dates, [-1])
self._daycount_fractions = tf.reshape(daycount_fractions, [-1])
self._contract_index = contract_index
self._strike = tf.repeat(self._strike, self._num_caplets)
self._is_cap = tf.repeat(self._is_cap, self._num_caplets)
示例4: price
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import repeat [as 别名]
def price(self, valuation_date, market, model=None, pricing_context=None,
name=None):
"""Returns the present value of the stream on the valuation date.
Args:
valuation_date: A scalar `DateTensor` specifying the date on which
valuation is being desired.
market: A namedtuple of type `InterestRateMarket` which contains the
necessary information for pricing the cashflow stream.
model: An optional input of type `InterestRateModelType` to specify which
model to use for pricing.
Default value: `None` in which case `NORMAL_RATE` model is used.
pricing_context: An optional input to provide additional parameters (such
as model parameters) relevant for pricing.
name: Python str. The name to give to the ops created by this function.
Default value: `None` which maps to 'price'.
Returns:
A Rank 1 `Tensor` of real type containing the modeled price of each stream
contract based on the input market data.
"""
name = name or (self._name + '_price')
with tf.name_scope(name):
valuation_date = dates.convert_to_date_tensor(valuation_date)
discount_curve = market.discount_curve
past_fixing = rc.get_rate_index(
market, self._start_date, rc.RateIndexType.SWAP, dtype=self._dtype)
past_fixing = tf.repeat(
tf.convert_to_tensor(past_fixing, dtype=self._dtype),
self._num_cashflows)
discount_factors = discount_curve.get_discount_factor(self._payment_dates)
cms_rates = self._swap.par_rate(valuation_date, market, model)
cms_rates = tf.where(self._daycount_fractions > 0., cms_rates,
tf.zeros_like(cms_rates))
# If coupon end date is before the valuation date, the payment is in the
# past. If valuation date is between coupon start date and coupon end
# date, then the rate has been fixed but not paid. Otherwise the rate is
# not fixed and should be read from the curve.
cms_rates = tf.where(
self._coupon_end_dates < valuation_date,
tf.constant(0., dtype=self._dtype),
tf.where(self._coupon_start_dates < valuation_date,
past_fixing, cms_rates))
cms_rates = self._adjust_convexity(
valuation_date, market, model, pricing_context, cms_rates,
discount_factors)
coupon_rate = self._coupon_multiplier * (
cms_rates + self._coupon_basis)
cashflow_pvs = self._notional * (
self._daycount_fractions * coupon_rate * discount_factors)
return tf.math.segment_sum(cashflow_pvs, self._contract_index)
示例5: _setup
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import repeat [as 别名]
def _setup(self, coupon_spec):
"""Setup tensors for efficient computations."""
if isinstance(coupon_spec, list):
cpn_frequency = dates.PeriodTensor.stack(
[x.coupon_frequency for x in coupon_spec], axis=0)
businessday_rule = coupon_spec[-1].businessday_rule
notional = tf.convert_to_tensor([x.notional for x in coupon_spec],
dtype=self._dtype)
fixed_rate = tf.convert_to_tensor([x.coupon_rate for x in coupon_spec],
dtype=self._dtype)
daycount_convention = coupon_spec[-1].daycount_convention
else:
cpn_frequency = coupon_spec.coupon_frequency
businessday_rule = coupon_spec.businessday_rule
notional = tf.broadcast_to(
tf.convert_to_tensor(coupon_spec.notional, dtype=self._dtype),
self._start_date.shape)
fixed_rate = tf.broadcast_to(
tf.convert_to_tensor(coupon_spec.coupon_rate, dtype=self._dtype),
self._start_date.shape)
daycount_convention = coupon_spec.daycount_convention
cpn_dates, _ = self._generate_schedule(cpn_frequency, businessday_rule)
payment_dates = cpn_dates[:, 1:]
notional = tf.repeat(notional, payment_dates.shape.as_list()[-1])
daycount_fractions = rc.get_daycount_fraction(
cpn_dates[:, :-1],
cpn_dates[:, 1:],
daycount_convention,
dtype=self._dtype)
coupon_rate = tf.expand_dims(fixed_rate, axis=-1)
coupon_rate = tf.repeat(coupon_rate, payment_dates.shape.as_list()[-1])
contract_index = tf.repeat(tf.range(0, self._batch_size),
payment_dates.shape.as_list()[-1])
self._num_cashflows = payment_dates.shape.as_list()[-1]
self._payment_dates = payment_dates.reshape([-1])
self._notional = notional
self._daycount_fractions = tf.reshape(daycount_fractions, [-1])
self._coupon_rate = coupon_rate
self._fixed_rate = tf.convert_to_tensor(fixed_rate, dtype=self._dtype)
self._contract_index = contract_index
示例6: price
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import repeat [as 别名]
def price(self, valuation_date, market, model=None, pricing_context=None,
name=None):
"""Returns the present value of the stream on the valuation date.
Args:
valuation_date: A scalar `DateTensor` specifying the date on which
valuation is being desired.
market: A namedtuple of type `InterestRateMarket` which contains the
necessary information for pricing the cashflow stream.
model: Reserved for future use.
pricing_context: Additional context relevant for pricing.
name: Python str. The name to give to the ops created by this function.
Default value: `None` which maps to 'price'.
Returns:
A Rank 1 `Tensor` of real type containing the modeled price of each stream
contract based on the input market data.
"""
del model, pricing_context
name = name or (self._name + '_price')
with tf.name_scope(name):
discount_curve = market.discount_curve
reference_curve = market.reference_curve
libor_rate = rc.get_rate_index(market, self._start_date,
rc.RateIndexType.LIBOR,
dtype=self._dtype)
libor_rate = tf.repeat(tf.convert_to_tensor(
libor_rate, dtype=self._dtype), self._num_cashflows)
discount_factors = discount_curve.get_discount_factor(self._payment_dates)
forward_rates = reference_curve.get_forward_rate(self._accrual_start_date,
self._accrual_end_date,
self._daycount_fractions)
forward_rates = tf.where(self._daycount_fractions > 0., forward_rates,
tf.zeros_like(forward_rates))
# If coupon end date is before the valuation date, the payment is in the
# past. If valuation date is between coupon start date and coupon end
# date, then the rate has been fixed but not paid. Otherwise the rate is
# not fixed and should be read from the curve.
forward_rates = tf.where(
self._coupon_end_dates < valuation_date,
tf.constant(0., dtype=self._dtype),
tf.where(self._coupon_start_dates < valuation_date,
libor_rate, forward_rates))
coupon_rate = self._coupon_multiplier * (
forward_rates + self._coupon_basis)
cashflow_pvs = self._notional * (
self._daycount_fractions * coupon_rate * discount_factors)
return tf.math.reduce_sum(
tf.reshape(cashflow_pvs, (self._batch_size, self._num_cashflows)),
axis=1)