当前位置: 首页>>代码示例>>Python>>正文


Python v2.Tensor方法代码示例

本文整理汇总了Python中tensorflow.compat.v2.Tensor方法的典型用法代码示例。如果您正苦于以下问题:Python v2.Tensor方法的具体用法?Python v2.Tensor怎么用?Python v2.Tensor使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在tensorflow.compat.v2的用法示例。


在下文中一共展示了v2.Tensor方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: compute_logits

# 需要导入模块: from tensorflow.compat import v2 [as 别名]
# 或者: from tensorflow.compat.v2 import Tensor [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 
开发者ID:microsoft,项目名称:machine-learning-for-programming-samples,代码行数:19,代码来源:model_tf2.py

示例2: ones_like

# 需要导入模块: from tensorflow.compat import v2 [as 别名]
# 或者: from tensorflow.compat.v2 import Tensor [as 别名]
def ones_like(a, dtype=None):
  """Returns an array of ones with the shape and type of the input array.

  Args:
    a: array_like. Could be an ndarray, a Tensor or any object that can be
      converted to a Tensor using `tf.convert_to_tensor`.
    dtype: Optional, defaults to dtype of the input array. The type of the
      resulting ndarray. Could be a python type, a NumPy type or a TensorFlow
      `DType`.

  Returns:
    An ndarray.
  """
  if isinstance(a, arrays_lib.ndarray):
    a = a.data
  if dtype is None:
    dtype = utils.result_type(a)
  else:
    dtype = utils.result_type(dtype)
  return arrays_lib.tensor_to_ndarray(tf.ones_like(a, dtype)) 
开发者ID:google,项目名称:trax,代码行数:22,代码来源:array_ops.py

示例3: diagflat

# 需要导入模块: from tensorflow.compat import v2 [as 别名]
# 或者: from tensorflow.compat.v2 import Tensor [as 别名]
def diagflat(v, k=0):
  """Returns a 2-d array with flattened `v` as diagonal.

  Args:
    v: array_like of any rank. Gets flattened when setting as diagonal. Could be
      an ndarray, a Tensor or any object that can be converted to a Tensor using
      `tf.convert_to_tensor`.
    k: Position of the diagonal. Defaults to 0, the main diagonal. Positive
      values refer to diagonals shifted right, negative values refer to
      diagonals shifted left.

  Returns:
    2-d ndarray.
  """
  v = asarray(v)
  return diag(tf.reshape(v.data, [-1]), k) 
开发者ID:google,项目名称:trax,代码行数:18,代码来源:array_ops.py

示例4: all

# 需要导入模块: from tensorflow.compat import v2 [as 别名]
# 或者: from tensorflow.compat.v2 import Tensor [as 别名]
def all(a, axis=None, keepdims=None):  # pylint: disable=redefined-builtin
  """Whether all array elements or those along an axis evaluate to true.

  Casts the array to bool type if it is not already and uses `tf.reduce_all` to
  compute the result.

  Args:
    a: array_like. Could be an ndarray, a Tensor or any object that can
      be converted to a Tensor using `tf.convert_to_tensor`.
    axis: Optional. Could be an int or a tuple of integers. If not specified,
      the reduction is performed over all array indices.
    keepdims: If true, retains reduced dimensions with length 1.

  Returns:
    An ndarray. Note that unlike NumPy this does not return a scalar bool if
    `axis` is None.
  """
  a = asarray(a, dtype=bool)
  return utils.tensor_to_ndarray(
      tf.reduce_all(input_tensor=a.data, axis=axis, keepdims=keepdims)) 
开发者ID:google,项目名称:trax,代码行数:22,代码来源:array_ops.py

示例5: imag

# 需要导入模块: from tensorflow.compat import v2 [as 别名]
# 或者: from tensorflow.compat.v2 import Tensor [as 别名]
def imag(a):
  """Returns imaginary parts of all elements in `a`.

  Uses `tf.imag`.

  Args:
    a: array_like. Could be an ndarray, a Tensor or any object that can
      be converted to a Tensor using `tf.convert_to_tensor`.

  Returns:
    An ndarray with the same shape as `a`.
  """
  a = asarray(a)
  # TODO(srbs): np.imag returns a scalar if a is a scalar, whereas we always
  # return an ndarray.
  return utils.tensor_to_ndarray(tf.math.imag(a.data)) 
开发者ID:google,项目名称:trax,代码行数:18,代码来源:array_ops.py

示例6: real

# 需要导入模块: from tensorflow.compat import v2 [as 别名]
# 或者: from tensorflow.compat.v2 import Tensor [as 别名]
def real(val):
  """Returns real parts of all elements in `a`.

  Uses `tf.real`.

  Args:
    val: array_like. Could be an ndarray, a Tensor or any object that can
      be converted to a Tensor using `tf.convert_to_tensor`.

  Returns:
    An ndarray with the same shape as `a`.
  """
  val = asarray(val)
  # TODO(srbs): np.real returns a scalar if val is a scalar, whereas we always
  # return an ndarray.
  return utils.tensor_to_ndarray(tf.math.real(val.data)) 
开发者ID:google,项目名称:trax,代码行数:18,代码来源:array_ops.py

示例7: squeeze

# 需要导入模块: from tensorflow.compat import v2 [as 别名]
# 或者: from tensorflow.compat.v2 import Tensor [as 别名]
def squeeze(a, axis=None):
  """Removes single-element axes from the array.

  Args:
    a: array_like. Could be an ndarray, a Tensor or any object that can
      be converted to a Tensor using `tf.convert_to_tensor`.
    axis: scalar or list/tuple of ints.

  TODO(srbs): tf.squeeze throws error when axis is a Tensor eager execution
  is enabled. So we cannot allow axis to be array_like here. Fix.

  Returns:
    An ndarray.
  """
  a = asarray(a)
  return utils.tensor_to_ndarray(tf.squeeze(a, axis)) 
开发者ID:google,项目名称:trax,代码行数:18,代码来源:array_ops.py

示例8: result_type

# 需要导入模块: from tensorflow.compat import v2 [as 别名]
# 或者: from tensorflow.compat.v2 import Tensor [as 别名]
def result_type(*arrays_and_dtypes):
  """Returns the type resulting from applying NumPy type promotion to arguments.

  Args:
    *arrays_and_dtypes: A list of array_like objects or dtypes.

  Returns:
    A numpy dtype.
  """
  def maybe_get_dtype(x):
    # Don't put np.ndarray in this list, because np.result_type looks at the
    # value (not just dtype) of np.ndarray to decide the result type.
    if isinstance(x, (arrays.ndarray, arrays.ShardedNdArray,
                      tf.Tensor, tf.IndexedSlices)):
      return _to_numpy_type(x.dtype)
    elif isinstance(x, tf.DType):
      return _to_numpy_type(x)
    return x
  arrays_and_dtypes = [maybe_get_dtype(x) for x in
                       tf.nest.flatten(arrays_and_dtypes)]
  if not arrays_and_dtypes:
    # If arrays_and_dtypes is an empty list, let numpy decide what the dtype is.
    arrays_and_dtypes = [np.asarray([])]
  return dtypes._result_type(*arrays_and_dtypes) 
开发者ID:google,项目名称:trax,代码行数:26,代码来源:utils.py

示例9: hessian

# 需要导入模块: from tensorflow.compat import v2 [as 别名]
# 或者: from tensorflow.compat.v2 import Tensor [as 别名]
def hessian(function: Callable[[Parameters], tf.Tensor],
            parameters: Parameters) -> Parameters:
  """Computes the Hessian of a given function.

  Useful for testing, although scales very poorly.

  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 or list of tensors of same nested structure as `Parameters`,
      representing the Hessian.
  """
  with tf.GradientTape() as outer_tape:
    with tf.GradientTape() as inner_tape:
      value = function(parameters)
    grads = inner_tape.gradient(value, parameters)
    grads = tensor_list_util.tensor_list_to_vector(grads)
  return outer_tape.jacobian(grads, parameters) 
开发者ID:google,项目名称:spectral-density,代码行数:22,代码来源:test_util.py

示例10: hessian_as_matrix

# 需要导入模块: from tensorflow.compat import v2 [as 别名]
# 或者: from tensorflow.compat.v2 import Tensor [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) 
开发者ID:google,项目名称:spectral-density,代码行数:20,代码来源:test_util.py

示例11: __init__

# 需要导入模块: from tensorflow.compat import v2 [as 别名]
# 或者: from tensorflow.compat.v2 import Tensor [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 
开发者ID:tensorflow,项目名称:ranking,代码行数:21,代码来源:feature.py

示例12: call

# 需要导入模块: from tensorflow.compat import v2 [as 别名]
# 或者: from tensorflow.compat.v2 import Tensor [as 别名]
def call(self, inputs):
    """Generates mask (whether example is valid) from features.

    Args:
      inputs: (dict) Features with a mix of context (2D) and example features
        (3D).

    Returns:
      mask: (tf.Tensor) Mask is a tensor of shape [batch_size, list_size], which
        is True for a valid example and False for invalid one.
    """
    example_feature = inputs[next(six.iterkeys(self._example_feature_columns))]
    list_size = tf.shape(example_feature)[1]
    sizes = inputs[self._size_feature_name]
    mask = tf.sequence_mask(sizes, maxlen=list_size)
    return mask 
开发者ID:tensorflow,项目名称:ranking,代码行数:18,代码来源:feature.py

示例13: transform

# 需要导入模块: from tensorflow.compat import v2 [as 别名]
# 或者: from tensorflow.compat.v2 import Tensor [as 别名]
def transform(self, features=None, training=None, mask=None):
    """Transforms the features into dense context features and example features.

    The user can overwrite this function for custom transformations.
    Mask is provided as an argument so that inherited models can have access
    to it for custom feature transformations, without modifying
    `call` explicitly.

    Args:
      features: (dict) with a mix of context (2D) and example features (3D).
      training: (bool) whether in train or inference mode.
      mask: (tf.Tensor) Mask is a tensor of shape [batch_size, list_size], which
        is True for a valid example and False for invalid one.

    Returns:
      context_features: (dict) context feature names to dense 2D tensors of
        shape [batch_size, feature_dims].
      example_features: (dict) example feature names to dense 3D tensors of
        shape [batch_size, list_size, feature_dims].
    """
    del mask
    context_features, example_features = self._listwise_dense_layer(
        inputs=features, training=training)
    return context_features, example_features 
开发者ID:tensorflow,项目名称:ranking,代码行数:26,代码来源:network.py

示例14: call

# 需要导入模块: from tensorflow.compat import v2 [as 别名]
# 或者: from tensorflow.compat.v2 import Tensor [as 别名]
def call(self, inputs=None, training=None, mask=None):
    """Defines the forward pass for ranking model.

    Args:
      inputs: (dict) with a mix of context (2D) and example features (3D).
      training: (bool) whether in train or inference mode.
      mask: (tf.Tensor) Mask is a tensor of shape [batch_size, list_size], which
        is True for a valid example and False for invalid one.

    Returns:
      (tf.Tensor) A score tensor of shape [batch_size, list_size].
    """
    context_features, example_features = self.transform(
        features=inputs, training=training, mask=mask)
    logits = self.compute_logits(
        context_features=context_features,
        example_features=example_features,
        training=training,
        mask=mask)
    return logits 
开发者ID:tensorflow,项目名称:ranking,代码行数:22,代码来源:network.py

示例15: _block_matmul

# 需要导入模块: from tensorflow.compat import v2 [as 别名]
# 或者: from tensorflow.compat.v2 import Tensor [as 别名]
def _block_matmul(m1, m2):
  """Multiplies block matrices represented as nested lists."""
  # Calls itself recursively to multiply blocks, until reaches the level of
  # tf.Tensors.
  if isinstance(m1, tf.Tensor):
    assert isinstance(m2, tf.Tensor)
    return tf.matmul(m1, m2)
  assert _is_nested_list(m1) and _is_nested_list(m2)

  i_max = len(m1)
  k_max = len(m2)
  j_max = 0 if k_max == 0 else len(m2[0])
  if i_max > 0:
    assert len(m1[0]) == k_max

  def row_by_column(i, j):
    return _block_add(*[_block_matmul(m1[i][k], m2[k][j])
                        for k in range(k_max)])
  return [[row_by_column(i, j) for j in range(j_max)] for i in range(i_max)] 
开发者ID:google,项目名称:tf-quant-finance,代码行数:21,代码来源:custom_loops.py


注:本文中的tensorflow.compat.v2.Tensor方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。