gpt4 book ai didi

python - 列出形状一致的张量

转载 作者:行者123 更新时间:2023-12-01 06:59:16 24 4
gpt4 key购买 nike

我有一个张量列表,其中每个张量都有不同的形状。我想将零填充到张量以使张量的整体列表统一。

示例:

input: [[1],[2,2],[3,3,3],[4,4,4,4],[5,5,5,5,5]]
output: [[1,0,0,0,0],[2,2,0,0,0],[3,3,3,0,0],[4,4,4,4,0],[5,5,5,5,5]]

我尝试使用 map 函数,但它给了我错误。这是我的代码

value = tf.constant([1,2,2,3,3,3,4,4,4,4,5,5,5,5,5])
y, idx, count = tf.unique_with_counts(value)
partitions = idx
out = tf.dynamic_partition(value, partitions, 10)

MAX_D = 10
def pad_zero(x):
paddings = [[ 0, MAX_D - tf.shape(x)[0] ]]
return tf.pad(x, paddings, "CONSTANT")

y = tf.map_fn(pad_zero, out)

with tf.compat.v1.Session() as sess:
print(sess.run(out))
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
/usr/local/lib/python3.7/site-packages/tensorflow/python/util/nest.py in assert_same_structure(nest1, nest2, check_types)
178 try:
--> 179 _pywrap_tensorflow.AssertSameStructure(nest1, nest2, check_types)
180 except (ValueError, TypeError) as e:

ValueError: The two structures don't have the same nested structure.

First structure: type=list str=[tf.int32, tf.int32, tf.int32, tf.int32, tf.int32, tf.int32, tf.int32, tf.int32, tf.int32, tf.int32]

Second structure: type=Tensor str=Tensor("map_6/while/Pad:0", shape=(10,), dtype=int32)

More specifically: Substructure "type=list str=[tf.int32, tf.int32, tf.int32, tf.int32, tf.int32, tf.int32, tf.int32, tf.int32, tf.int32, tf.int32]" is a sequence, while substructure "type=Tensor str=Tensor("map_6/while/Pad:0", shape=(10,), dtype=int32)" is not

最佳答案

您可以只使用 keras 包中的 pad_sequences 方法。例如,我会这样做:

input = [[1], [2, 2], [3, 3, 3]]
padded_input = tf.keras.preprocessing.sequence.pad_sequences(input, padding='post')

padded_input 将变成您想要的:

>>padded_input

array([[1, 0, 0],
[2, 2, 0],
[3, 3, 3]])

然后你就可以像平常一样使用tf.data.Dataset.from_tensor_slices。既然现在它们的长度都一样了,那就没问题了。

关于python - 列出形状一致的张量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58706844/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com