微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

什么是 numpy 元组/数组索引的 tensorflow 等价物?

如何解决什么是 numpy 元组/数组索引的 tensorflow 等价物?

问题

选择非连续索引的 Numpy 元组/数组索引的 Tensorflow 等价物是什么?使用 numpy,可以使用元组或数组选择多行/多列。

a = np.arange(12).reshape(3,4)
print(a)
print(a[
    (0,2),# select row 0 and 2
    1        # select col 0 
])
---
[[ 0  1  2  3]    # a[0][1] -> 1
 [ 4  5  6  7]
 [ 8  9 10 11]]   # a[2][1] -> 9

[1 9]

看着 Multi-axis indexing 但似乎没有等效的方法

通过传递多个索引来索引更高等级的张量。

使用元组或数组会导致 ypeError: Only integers,slices (`:`),ellipsis (`...`),tf.newaxis (`None`) and scalar tf.int32/tf.int64 tensors are valid indices,got (0,2,5)

from tensorflow.keras.layers.experimental.preprocessing import TextVectorization

training_data = np.array([["This is the 1st sample."],["And here's the 2nd sample."]])
vectorizer = TextVectorization(output_mode="int")
vectorizer.adapt(training_data)

word_indices = vectorizer(training_data)
word_indices = tf.cast(word_indices,dtype=tf.int8)

print(f"word vocabulary:{vectorizer.get_vocabulary()}\n")
print(f"word indices:\n{word_indices}\n")
index_to_word = tf.reshape(tf.constant(vectorizer.get_vocabulary()),(-1,1))
print(f"index_to_word:\n{index_to_word}\n")

# Numpy tuple indexing
print(f"indices to words:{words.numpy()[(0,5),::]}")

# What is TF equivalent indexing?
print(f"indices to words:{words[(0,::]}")   # <--- cannot use tuple/array indexing

结果:

word vocabulary:['','[UNK]','the','sample','this','is','heres','and','2nd','1st']

word indices:
[[4 5 2 9 3]
 [7 6 2 8 3]]

index_to_word:
[[b'']
 [b'[UNK]']
 [b'the']
 [b'sample']
 [b'this']
 [b'is']
 [b'heres']
 [b'and']
 [b'2nd']
 [b'1st']]

indices to words:[[b'']
 [b'the']
 [b'is']]

TypeError: Only integers,5)

Tensorflow 中有哪些索引可以选择不连续的多个索引?

解决方法

您可以使用 tf.gather

>>> tf.gather(words,[0,2,5])
<tf.Tensor: shape=(3,1),dtype=string,numpy=
array([[b''],[b'the'],[b'is']],dtype=object)>

在指南中阅读更多内容:Introduction to tensor slicing

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。