什么是 numpy 元组/数组索引的 tensorflow 等价物? [英] What is the tensorflow equivalent of numpy tuple/array indexing?
问题描述
问题
选择非连续索引的 Numpy 元组/数组索引的 Tensorflow 等价物是什么?使用 numpy,可以使用元组或数组选择多行/多列.
a = np.arange(12).reshape(3,4)打印(一)打印(一个[(0,2), # 选择第 0 行和第 2 行1 # 选择列 0])---[[ 0 1 2 3] # a[0][1] ->1[ 4 5 6 7][ 8 9 10 11]] # a[2][1] ->9[1 9]
查看 多轴索引,但似乎没有等效的方法.
<块引用>通过传递多个索引来索引更高等级的张量.
使用元组或数组会导致 ypeError: Only integers, slices (`:`), ellipsis (`...`), tf.newaxis (`None`) 和 scalar tf.int32/tf.int64张量是有效的索引,得到 (0, 2, 5)
.
from tensorflow.keras.layers.experimental.preprocessing import TextVectorizationtraining_data = np.array([[这是第一个样本."], [这是第二个样本."]])vectorizer = TextVectorization(output_mode=int")vectorizer.adapt(training_data)word_indices = vectorizer(training_data)word_indices = tf.cast(word_indices, dtype=tf.int8)打印(f单词词汇:{vectorizer.get_vocabulary()}\n")打印(f词索引:\n{word_indices}\n")index_to_word = tf.reshape(tf.constant(vectorizer.get_vocabulary()), (-1, 1))打印(findex_to_word:\n{index_to_word}\n")# Numpy 元组索引打印(f词的索引:{words.numpy()[(0,2,5),::]}")# 什么是 TF 等效索引?print(f"indices to words:{words[(0,2,5),::]}") # <--- 不能使用元组/数组索引
结果:
词表:['', '[UNK]', 'the', 'sample', 'this', 'is', 'heres', 'and', '2nd', '1st']词索引:[[4 5 2 9 3][7 6 2 8 3]]index_to_word:[[b''][假寐]'][b'the'][b'样品'][b'这个'][b'是'][b'这里'][乐队'][b'2nd'][b'1st']]词索引:[[b''][b'the'][b'是']]类型错误:只有整数、切片 (`:`)、省略号 (`...`)、tf.newaxis (`None`) 和标量 tf.int32/tf.int64 张量是有效索引,得到 (0, 2, 5)
Tensorflow 中有哪些索引可以选择不连续的多个索引?
您可以使用 tf.gather
.
在指南中阅读更多内容:张量切片简介
Question
What is the Tensorflow equivalent of Numpy tuple/array indexing to select non-continuous indices? With numpy, multiple rows / columns can be selected with tuple or array.
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]
Looking at Multi-axis indexing but there seems no equivalent way.
Higher rank tensors are indexed by passing multiple indices.
Using the tuple or array causes 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,2,5),::]}")
# What is TF equivalent indexing?
print(f"indices to words:{words[(0,2,5),::]}") # <--- cannot use tuple/array indexing
Result:
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, slices (`:`), ellipsis (`...`), tf.newaxis (`None`) and scalar tf.int32/tf.int64 tensors are valid indices, got (0, 2, 5)
What indexing are available in Tensorflow to select non-consecutive multiple indices?
You can use tf.gather
.
>>> tf.gather(words,[0,2,5])
<tf.Tensor: shape=(3, 1), dtype=string, numpy=
array([[b''],
[b'the'],
[b'is']], dtype=object)>
Read more in the guide: Introduction to tensor slicing
这篇关于什么是 numpy 元组/数组索引的 tensorflow 等价物?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!