Python – tensorflow.math.unsorted_segment_sqrt_n()
TensorFlow 是由 Google 设计的开源Python库,用于开发机器学习模型和深度学习神经网络。
unsorted_segment_sqrt_n()用于查找段的总和除以 sqrt(N)。
Syntax: tensorflow.math.unsorted_segment_sqrt_n( data, segment_ids, num_segments, name )
Parameter:
- data: It is a tensor. Allowed dtypes are floating point or complex.
- segment_ids: It’s 1-D tensor with sorted values. It’s size should be equal to size of first dimension of data. It represents number of distinct segment IDs. Allowed dtypes are int32 and int64.
- num_segments: It is a Tensor. Allowed dtypes are int32 and int64.
- name(optional): It defines the name for the operation.
Return: It returns a tensor of dtype as x.
示例 1:
Python3
# importing the library
import tensorflow as tf
# Initializing the input tensor
data = tf.constant([1, 2, 3], dtype = tf.float64)
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_sqrt_n(data, segment_ids, tf.constant(3))
# Printing the result
print('Result: ', res)
Python3
# importing the library
import tensorflow as tf
# Initializing the input tensor
data = tf.constant([[1, 2, 3], [4, 5, 6], [7, 8, 9]], dtype = tf.float64)
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_sqrt_n(data, segment_ids, tf.constant(3))
# Printing the result
print('Result: ', res)
输出:
data: tf.Tensor([1. 2. 3.], shape=(3, ), dtype=float64)
segment_ids: tf.Tensor([2 2 2], shape=(3, ), dtype=int32)
Result: tf.Tensor([0. 0. 3.46410162], shape=(3, ), dtype=float64)
示例 2:
Python3
# importing the library
import tensorflow as tf
# Initializing the input tensor
data = tf.constant([[1, 2, 3], [4, 5, 6], [7, 8, 9]], dtype = tf.float64)
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_sqrt_n(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=float64)
segment_ids: tf.Tensor([0 0 2], shape=(3, ), dtype=int32)
Result: tf.Tensor(
[[3.53553391 4.94974747 6.36396103]
[0. 0. 0. ]
[7. 8. 9. ]], shape=(3, 3), dtype=float64)