列出均匀形状的张量

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

示例:

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

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

value = tf.constant([1,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,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]

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]" is a sequence,while substructure "type=Tensor str=Tensor("map_6/while/Pad:0",dtype=int32)" is not
yqfjh 回答:列出均匀形状的张量

您只能在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],2,3]])

然后,您可以像平常一样使用tf.data.Dataset.from_tensor_slices。由于现在它们的长度都相同,所以很好。

,

我只是遍历列表并相应地填充零。

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

for i in range(len(out)):
    paddings = [[ 0,MAX_D - tf.shape(out[i])[0] ]]
    out[i] = tf.pad(out[i],paddings,"CONSTANT")

out = tf.convert_to_tensor(out)

with tf.compat.v1.Session() as sess:
    print( sess.run(out) )
本文链接:https://www.f2er.com/3161396.html

大家都在问