Python tensorflow.Dtype() Examples

The following are 12 code examples of tensorflow.Dtype(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module tensorflow , or try the search function .
Example #1
Source File: nn.py    From THUMT with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def maxout(inputs, output_size, maxpart=2, use_bias=True, concat=True,
           dtype=None, scope=None):
    """
    Maxout layer
    :param inputs: see the corresponding description of ``linear''
    :param output_size: see the corresponding description of ``linear''
    :param maxpart: an integer, the default value is 2
    :param use_bias: a boolean value indicate whether to use bias term
    :param concat: concat all tensors if inputs is a list of tensors
    :param dtype: an optional instance of tf.Dtype
    :param scope: the scope of this layer, the default value is ``maxout''
    :returns: a Tensor with shape [batch, output_size]
    :raises RuntimeError: see the corresponding description of ``linear''
    """

    candidate = linear(inputs, output_size * maxpart, use_bias, concat,
                       dtype=dtype, scope=scope or "maxout")
    shape = tf.concat([tf.shape(candidate)[:-1], [output_size, maxpart]],
                      axis=0)
    value = tf.reshape(candidate, shape)
    output = tf.reduce_max(value, -1)

    return output 
Example #2
Source File: nn.py    From Document-Transformer with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def maxout(inputs, output_size, maxpart=2, use_bias=True, concat=True,
           dtype=None, scope=None):
    """
    Maxout layer
    :param inputs: see the corresponding description of ``linear''
    :param output_size: see the corresponding description of ``linear''
    :param maxpart: an integer, the default value is 2
    :param use_bias: a boolean value indicate whether to use bias term
    :param concat: concat all tensors if inputs is a list of tensors
    :param dtype: an optional instance of tf.Dtype
    :param scope: the scope of this layer, the default value is ``maxout''
    :returns: a Tensor with shape [batch, output_size]
    :raises RuntimeError: see the corresponding description of ``linear''
    """

    candidate = linear(inputs, output_size * maxpart, use_bias, concat,
                       dtype=dtype, scope=scope or "maxout")
    shape = tf.concat([tf.shape(candidate)[:-1], [output_size, maxpart]],
                      axis=0)
    value = tf.reshape(candidate, shape)
    output = tf.reduce_max(value, -1)

    return output 
Example #3
Source File: nn.py    From transformer-aan with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def maxout(inputs, output_size, maxpart=2, use_bias=True, concat=True,
           dtype=None, scope=None):
    """
    Maxout layer
    :param inputs: see the corresponding description of ``linear''
    :param output_size: see the corresponding description of ``linear''
    :param maxpart: an integer, the default value is 2
    :param use_bias: a boolean value indicate whether to use bias term
    :param concat: concat all tensors if inputs is a list of tensors
    :param dtype: an optional instance of tf.Dtype
    :param scope: the scope of this layer, the default value is ``maxout''
    :returns: a Tensor with shape [batch, output_size]
    :raises RuntimeError: see the corresponding description of ``linear''
    """

    candidate = linear(inputs, output_size * maxpart, use_bias, concat,
                       dtype=dtype, scope=scope or "maxout")
    shape = tf.concat([tf.shape(candidate)[:-1], [output_size, maxpart]],
                      axis=0)
    value = tf.reshape(candidate, shape)
    output = tf.reduce_max(value, -1)

    return output 
Example #4
Source File: lrp.py    From THUMT with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def maxout_v2n(inputs, output_size, maxpart, w, params, use_bias=True,
               concat=True, dtype=None, scope=None):
    """
    Maxout layer
    :param inputs: see the corresponding description of ``linear''
    :param output_size: see the corresponding description of ``linear''
    :param maxpart: an integer, the default value is 2
    :param use_bias: a boolean value indicate whether to use bias term
    :param concat: concat all tensors if inputs is a list of tensors
    :param dtype: an optional instance of tf.Dtype
    :param scope: the scope of this layer, the default value is ``maxout''
    :returns: a Tensor with shape [batch, output_size]
    :raises RuntimeError: see the corresponding description of ``linear''
    """

    w_x_dec, w_x_ctx = w
    w_x_dec = tf.transpose(w_x_dec, [1, 2, 0, 3])
    w_x_ctx = tf.transpose(w_x_ctx, [1, 2, 0, 3])
    w_x_y = tf.zeros(tf.shape(w_x_dec), dtype=tf.float32)
    candidate_linear = linear_v2n(inputs, output_size * maxpart, use_bias,
                                  [w_x_y, w_x_dec, w_x_ctx], params, concat,
                                  dtype=dtype, scope=scope or "maxout")
    candidate = candidate_linear["output"]
    _, w_x_dec_readout, w_x_ctx_readout = candidate_linear["weight_ratios"]
    w_x_readout = w_x_dec_readout + w_x_ctx_readout
    w_x_readout = tf.transpose(w_x_readout, [0, 2, 1, 3])

    output_maxout = maxpool(candidate, output_size, params)
    output = output_maxout["output"]

    # direct
    w_readout_maxout = output_maxout["weight_ratio"]

    #propagate
    propagater = tf.matmul

    w_x_maxout = propagater(w_x_readout, w_readout_maxout)

    weight_ratios = [w_x_maxout]

    return {"output": output, "weight_ratios": weight_ratios} 
Example #5
Source File: swaption.py    From tf-quant-finance with Apache License 2.0 5 votes vote down vote up
def __init__(self,
               swap,
               expiry_date=None,
               dtype=None,
               name=None):
    """Initialize a batch of European swaptions.

    Args:
      swap: An instance of `InterestRateSwap` specifying the interest rate
        swaps underlying the swaptions. The batch size of the swaptions being
        created would be the same as the batch size of the `swap`.
      expiry_date: An optional rank 1 `DateTensor` specifying the expiry dates
        for each swaption. The shape of the input should be the same as the
        batch size of the `swap` input.
        Default value: None in which case the option expity date is the same as
        the start date of each underlying swap.
      dtype: `tf.Dtype`. If supplied the dtype for the real variables or ops
        either supplied to the Swaption object or created by the Swaption
        object.
        Default value: None which maps to the default dtype inferred by
        TensorFlow.
      name: Python str. The name to give to the ops created by this class.
        Default value: `None` which maps to 'swaption'.
    """
    self._name = name or 'swaption'

    with tf.name_scope(self._name):
      self._dtype = dtype
      self._expiry_date = dates.convert_to_date_tensor(expiry_date)
      self._swap = swap 
Example #6
Source File: cms_swap.py    From tf-quant-finance with Apache License 2.0 5 votes vote down vote up
def __init__(self,
               start_date,
               end_date,
               coupon_spec,
               dtype=None,
               name=None):
    """Initialize a batch of CMS cashflow streams.

    Args:
      start_date: A rank 1 `DateTensor` specifying the starting dates of the
        accrual of the first coupon of the cashflow stream. The shape of the
        input correspond to the numbercof streams being created.
      end_date: A rank 1 `DateTensor` specifying the end dates for accrual of
        the last coupon in each cashflow stream. The shape of the input should
        be the same as that of `start_date`.
      coupon_spec: A list of `CMSCouponSpecs` specifying the details of the
        coupon payment for the cashflow stream. The length of the list should
        be the same as the number of streams being created. Each coupon within
        the list must have the same daycount_convention and businessday_rule.
      dtype: `tf.Dtype`. If supplied the dtype for the real variables or ops
        either supplied to the FloatingCashflowStream object or created by the
        object.
        Default value: None which maps to the default dtype inferred by
        TensorFlow.
      name: Python str. The name to give to the ops created by this class.
        Default value: `None` which maps to 'floating_cashflow_stream'.
    """

    super(CMSCashflowStream, self).__init__()
    self._name = name or 'cms_cashflow_stream'

    with tf.name_scope(self._name):
      self._start_date = dates.convert_to_date_tensor(start_date)
      self._end_date = dates.convert_to_date_tensor(end_date)
      self._batch_size = self._start_date.shape[0]
      self._first_coupon_date = None
      self._penultimate_coupon_date = None
      self._dtype = dtype

      self._setup(coupon_spec) 
Example #7
Source File: test_model.py    From TGAN with MIT License 5 votes vote down vote up
def check_operation_nodes(graph, name, node_type, dtype, shape, consumers):
        """Test a graph node parameters.

        Args:
            graph(tf): Graph object the node belongs to.
            name(str): Name of the node.
            node_type(str): Operation type of the node.
            dtype(tf.Dtype): Dtype of the output tensor.
            shape(tuple[int]): Shape of the output tensor.
            consumers(list[str]): List of names of nodes consuming the node's output.

        Returns:
            None.

        Raises:
            AssertionError: If any check fail.

        """
        operation = graph.get_operation_by_name(name)
        assert len(operation.outputs) == 1
        output = operation.outputs[0]

        assert operation.type == node_type
        assert output.dtype == dtype
        assert output.shape.as_list() == shape
        assert output.consumers() == [graph.get_operation_by_name(cons) for cons in consumers] 
Example #8
Source File: neighbor_features.py    From neural-structured-learning with Apache License 2.0 4 votes vote down vote up
def make_missing_neighbor_inputs(neighbor_config,
                                 inputs,
                                 weight_dtype=tf.float32):
  """Makes additional inputs for neighbor features if necessary.

  Args:
    neighbor_config: An instance of `configs.GraphNeighborConfig` specifying the
      number of neighbors and how neighbor features should be named.
    inputs: Dictionary of input tensors that may be missing neighbor features.
      The keys are the features names. See `utils.unpack_neighbor_features` for
      expected names of neighbor features and weights.
    weight_dtype: `tf.Dtype` for neighbors weights. Defaults to `tf.float32`.

  Returns:
    A dictionary of neighbor feature and weight tensors that do not already
    exist in `inputs`. The keys are specified according to `neighbor_config`.
  """
  existing_feature_names = set(inputs.keys())
  neighbor_inputs = {}
  for i in range(neighbor_config.max_neighbors):  # For each potential neighbor.
    # Weight of the neighbor.
    weight_name = '{}{}{}'.format(neighbor_config.prefix, i,
                                  neighbor_config.weight_suffix)
    if weight_name not in existing_feature_names:
      neighbor_inputs[weight_name] = tf.keras.Input((1,),
                                                    dtype=weight_dtype,
                                                    name=weight_name)
    # For inputs without existing neighbor features, replicate them.
    for feature_name, tensor in inputs.items():
      if feature_name.startswith(neighbor_config.prefix):
        continue
      neighbor_feature_name = '{}{}_{}'.format(neighbor_config.prefix, i,
                                               feature_name)
      if neighbor_feature_name not in existing_feature_names:
        neighbor_inputs[neighbor_feature_name] = tf.keras.Input(
            tensor.shape[1:],
            batch_size=tensor.shape[0],
            dtype=tensor.dtype,
            name=neighbor_feature_name,
            ragged=isinstance(tensor, tf.RaggedTensor),
            sparse=isinstance(tensor, tf.sparse.SparseTensor))
  return neighbor_inputs 
Example #9
Source File: interest_rate_swap.py    From tf-quant-finance with Apache License 2.0 4 votes vote down vote up
def __init__(self,
               start_date,
               maturity_date,
               pay_leg,
               receive_leg,
               holiday_calendar=None,
               dtype=None,
               name=None):
    """Initialize a batch of IRS contracts.

    Args:
      start_date: A rank 1 `DateTensor` specifying the dates for the inception
        (start of the accrual) of the swap contracts. The shape of the input
        correspond to the number of instruments being created.
      maturity_date: A rank 1 `DateTensor` specifying the maturity dates for
        each contract. The shape of the input should be the same as that of
        `start_date`.
      pay_leg: A scalar or a list of either `FixedCouponSpecs` or
        `FloatCouponSpecs` specifying the coupon payments for the payment leg
        of the swap. If specified as a list then the length of the list should
        be the same as the number of instruments being created. If specified as
        a scalar, then the elements of the namedtuple must be of the same shape
        as (or compatible to) the shape of `start_date`.
      receive_leg: A scalar or a list of either `FixedCouponSpecs` or
        `FloatCouponSpecs` specifying the coupon payments for the receiving leg
        of the swap. If specified as a list then the length of the list should
        be the same as the number of instruments being created. If specified as
        a scalar, then the elements of the namedtuple must be of the same shape
        as (or compatible with) the shape of `start_date`.
      holiday_calendar: An instance of `dates.HolidayCalendar` to specify
        weekends and holidays.
        Default value: None in which case a holiday calendar would be created
        with Saturday and Sunday being the holidays.
      dtype: `tf.Dtype`. If supplied the dtype for the real variables or ops
        either supplied to the IRS object or created by the IRS object.
        Default value: None which maps to the default dtype inferred by
        TensorFlow.
      name: Python str. The name to give to the ops created by this class.
        Default value: `None` which maps to 'interest_rate_swap'.
    """
    self._name = name or 'interest_rate_swap'

    if holiday_calendar is None:
      holiday_calendar = dates.create_holiday_calendar(
          weekend_mask=dates.WeekendMask.SATURDAY_SUNDAY)

    with tf.name_scope(self._name):
      self._dtype = dtype
      self._start_date = dates.convert_to_date_tensor(start_date)
      self._maturity_date = dates.convert_to_date_tensor(maturity_date)
      self._holiday_calendar = holiday_calendar
      self._floating_leg = None
      self._fixed_leg = None
      self._pay_leg = self._setup_leg(pay_leg)
      self._receive_leg = self._setup_leg(receive_leg)
      self._is_payer = isinstance(self._pay_leg, cs.FixedCashflowStream) 
Example #10
Source File: cms_swap.py    From tf-quant-finance with Apache License 2.0 4 votes vote down vote up
def __init__(self,
               start_date,
               maturity_date,
               pay_leg,
               receive_leg,
               holiday_calendar=None,
               dtype=None,
               name=None):
    """Initialize a batch of CMS swap contracts.

    Args:
      start_date: A rank 1 `DateTensor` specifying the dates for the inception
        (start of the accrual) of the swap cpntracts. The shape of the input
        correspond to the numbercof instruments being created.
      maturity_date: A rank 1 `DateTensor` specifying the maturity dates for
        each contract. The shape of the input should be the same as that of
        `start_date`.
      pay_leg: A list of either `FixedCouponSpecs`, `FloatCouponSpecs` or
        `CMSCouponSpecs` specifying the coupon payments for the payment leg of
        the swap. The length of the list should be the same as the number of
        instruments being created.
      receive_leg: A list of either `FixedCouponSpecs` or `FloatCouponSpecs` or
        `CMSCouponSpecs` specifying the coupon payments for the receiving leg
        of the swap. The length of the list should be the same as the number of
        instruments being created.
      holiday_calendar: An instance of `dates.HolidayCalendar` to specify
        weekends and holidays.
        Default value: None in which case a holiday calendar would be created
        with Saturday and Sunday being the holidays.
      dtype: `tf.Dtype`. If supplied the dtype for the real variables or ops
        either supplied to the IRS object or created by the IRS object.
        Default value: None which maps to the default dtype inferred by
        TensorFlow.
      name: Python str. The name to give to the ops created by this class.
        Default value: `None` which maps to 'cms_swap'.
    """
    self._name = name or 'cms_swap'

    if holiday_calendar is None:
      holiday_calendar = dates.create_holiday_calendar(
          weekend_mask=dates.WeekendMask.SATURDAY_SUNDAY)

    with tf.name_scope(self._name):
      self._dtype = dtype
      self._start_date = dates.convert_to_date_tensor(start_date)
      self._maturity_date = dates.convert_to_date_tensor(maturity_date)
      self._holiday_calendar = holiday_calendar
      self._floating_leg = None
      self._fixed_leg = None
      self._cms_leg = None
      self._pay_leg = self._setup_leg(pay_leg)
      self._receive_leg = self._setup_leg(receive_leg) 
Example #11
Source File: bond.py    From tf-quant-finance with Apache License 2.0 4 votes vote down vote up
def __init__(self,
               settlement_date,
               maturity_date,
               coupon_spec,
               start_date=None,
               first_coupon_date=None,
               penultimate_coupon_date=None,
               holiday_calendar=None,
               dtype=None,
               name=None):
    """Initialize a batch of fixed coupon bonds.

    Args:
      settlement_date: A rank 1 `DateTensor` specifying the settlement date of
        the bonds.
      maturity_date: A rank 1 `DateTensor` specifying the maturity dates of the
        bonds. The shape of the input should be the same as that of
        `settlement_date`.
      coupon_spec: A list of `FixedCouponSpecs` specifying the coupon payments.
        The length of the list should be the same as the number of bonds
        being created.
      start_date: An optional `DateTensor` specifying the dates when the
        interest starts to accrue for the coupons. The input can be used to
        specify a forward start date for the coupons. The shape of the input
        correspond to the numbercof instruments being created.
        Default value: None in which case the coupons start to accrue from the
        `settlement_date`.
      first_coupon_date: An optional rank 1 `DateTensor` specifying the dates
        when first coupon will be paid for bonds with irregular first coupon.
      penultimate_coupon_date: An optional rank 1 `DateTensor` specifying the
        dates when the penultimate coupon (or last regular coupon) will be paid
        for bonds with irregular last coupon.
      holiday_calendar: An instance of `dates.HolidayCalendar` to specify
        weekends and holidays.
        Default value: None in which case a holiday calendar would be created
        with Saturday and Sunday being the holidays.
      dtype: `tf.Dtype`. If supplied the dtype for the real variables or ops
        either supplied to the bond object or created by the bond object.
        Default value: None which maps to the default dtype inferred by
        TensorFlow.
      name: Python str. The name to give to the ops created by this class.
        Default value: `None` which maps to 'bond'.
    """
    self._name = name or 'bond'

    if holiday_calendar is None:
      holiday_calendar = dates.create_holiday_calendar(
          weekend_mask=dates.WeekendMask.SATURDAY_SUNDAY)

    with tf.name_scope(self._name):
      self._dtype = dtype
      self._settlement_date = dates.convert_to_date_tensor(settlement_date)
      self._maturity_date = dates.convert_to_date_tensor(maturity_date)
      self._holiday_calendar = holiday_calendar
      self._setup(coupon_spec, start_date, first_coupon_date,
                  penultimate_coupon_date) 
Example #12
Source File: result_types.py    From fold with Apache License 2.0 4 votes vote down vote up
def convert_to_type(type_like):
  """Converts `type_like` to a `Type`.

  If `type_like` is already a `Type`, it is returned. The following
  conversions are performed:

  * Python tuples become `Tuple`s; items are recursively converted.

  * A `tf.TensorShape` becomes a corresponding `TensorType` with
  `dtype=float32`. Must be fully defined.

  * Lists of `shape + [dtype]` (e.g. `[3, 4, 'int32']`) become
  `TensorType`s, with the default `dtype=float32` if omitted.

  * A `tf.Dtype` or stringified version thereof (e.g. `'int64'`)
  becomes a corresponding scalar `TensorType((), dtype)`.

  * An integer `vector_len` becomes a corresponding vector
  `TensorType((vector_len,), dtype=float32)`.

  Args:
    type_like: Described above.

  Returns:
    A `Type`.

  Raises:
    TypeError: If `type_like` cannot be converted to a `Type`.

  """
  if isinstance(type_like, ResultType):
    return type_like
  if isinstance(type_like, tf.TensorShape):
    # Check this *before* calling as_list() otherwise it throws.
    if not type_like.is_fully_defined():
      raise TypeError('shape %s is not fully defined' % type_like)
    return TensorType(type_like.as_list())
  if isinstance(type_like, tuple):
    return TupleType(convert_to_type(item) for item in type_like)
  if isinstance(type_like, list):
    if type_like and isinstance(type_like[-1], six.string_types):
      return TensorType(type_like[:-1], dtype=type_like[-1])
    else:
      return TensorType(type_like)
  if isinstance(type_like, tf.DType) or isinstance(type_like, six.string_types):
    return TensorType((), dtype=type_like)
  if isinstance(type_like, numbers.Integral):
    return TensorType((type_like,))
  raise TypeError('Cannot covert %s to a type.' % (type_like,))