TensorFlow ``IndexedSlices()`` 使用方法


发布日期 : 2022-04-01 08:34:56 UTC

访问量: 10 次浏览

Python – tensorflow.IndexedSlices()

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

IndexedSlices()是用来寻找一组张量片在给定索引处的稀疏表示。

语法:
tensorflow.IndexedSlices(values, indices, dense_shape = None)

参数:

  • values: 它是一个任意 dtype 的张量。
  • indices: 它是一个一维张量。

返回:
它返回一个 IndexedSlices 对象。

示例 1:

# Importing the library
import tensorflow as tf

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

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

# Calculating result
res = tf.IndexedSlices(data, [0])

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

输出:

data: tf.Tensor([1 2 3], shape=(3, ), dtype=int32)
res: IndexedSlices(indices=[0], values=tf.Tensor([1 2 3], shape=(3, ), dtype=int32))

示例 2:

# Importing the library
import tensorflow as tf

# Initializing the input
data = tf.constant([[1, 2, 3], [4, 5, 6]])

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

# Calculating result
res = tf.IndexedSlices(data, [0])

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

输出:

data: tf.Tensor(
[[1 2 3]
[4 5 6]], shape=(2, 3), dtype=int32)
res: IndexedSlices(indices=[0], values=tf.Tensor(
[[1 2 3]
[4 5 6]], shape=(2, 3), dtype=int32))