Python ``tensorflow.math.unsorted_segment_prod()`` 无序分段乘积函数与实例


发布日期 : 2021-05-11 20:03:34 UTC

访问量: 10 次浏览

Python – tensorflow.math.unsorted\_segment\_prod()

TensorFlow是谷歌设计的开源Python库,用于开发机器学习模型和深度学习神经网络。

unsorted\_segment\_prod()是用来寻找段的乘积。

语法:
tensorflow.math.unsorted_segment_prod( data, segment_ids, num_segments, name )

参数 :

  • data: 它是一个张量。允许的 dtypes 是float32, float64, int32, uint8, int16, int8, int64, bfloat16, uint16, half, uint32, uint64。
  • segment_ids: 它是带有排序值的一维张量。它的大小应该等于数据的第一维大小。它代表不同的段ID的数量。允许的数据类型是int32和int64。
  • num_segments: 它是一个张量。允许的 dtypes 是int32和int64。
  • name(可选): 它定义了该操作的名称。

返回:它返回一个 dtype 的张量为x。

示例 1:

# importing the library
import tensorflow as tf

# Initializing the input tensor
data = tf.constant([1, 2, 3])
segment_ids = tf.constant([2, 2, 2])

# Printing the input tensor
print('data: ', data)
print('segment_ids: ', segment_ids)

# Calculating result
res = tf.math.unsorted_segment_prod(data, segment_ids, tf.constant(3))

# Printing the result
print('Result: ', res)

输出:

data: tf.Tensor([1 2 3], shape=(3, ), dtype=int32)
segment_ids: tf.Tensor([2 2 2], shape=(3, ), dtype=int32)
Result: tf.Tensor([1 1 6], shape=(3, ), dtype=int32)

示例 2:

# importing the library
import tensorflow as tf

# Initializing the input tensor
data = tf.constant([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
segment_ids = tf.constant([0, 0, 2])

# Printing the input tensor
print('data: ', data)
print('segment_ids: ', segment_ids)

# Calculating result
res = tf.math.unsorted_segment_prod(data, segment_ids, tf.constant(3))

# Printing the result
print('Result: ', res)

输出:

data: tf.Tensor(
[[1 2 3]
[4 5 6]
[7 8 9]], shape=(3, 3), dtype=int32)
segment_ids: tf.Tensor([0 0 2], shape=(3, ), dtype=int32)
Result: tf.Tensor(
[[ 4 10 18]
[ 1 1 1]
[ 7 8 9]], shape=(3, 3), dtype=int32)