如何将特定位置的衣衫的tessor插入tesnsorflow中另一个堆叠的衣衫agged的张量中

时间:2020-10-05 17:23:48

标签: tensorflow2.0 ragged

我有这个参差不齐的张量:[[s1,s2],[o1,o2]]

现在我有了这个新的参差不齐的张量= [p1,p2]

(s1,s2,o1,o2,p1,p2也是参差不齐的张量

最终预期输出为[[s1,s2,p1,p2],[o1,o2]] (请注意,这些是参差不齐的张量,而不是正常的张量)

如何实现?

我们可以使用以下代码进行操作:

c =tf.constant([1,2,3,4],dtype=tf.int32)
q =tf.constant([5,6],dtype=tf.int32)
s =tf.constant([7],dtype=tf.int32)

c1 =tf.constant([11,22,33,44],dtype=tf.int32)
q1 =tf.constant([55,66],dtype=tf.int32)
s1 =tf.constant([77],dtype=tf.int32)


c2 =tf.constant([111,222],dtype=tf.int32)
q2 =tf.constant([555],dtype=tf.int32)
s2 =tf.constant([777,888,999],dtype=tf.int32)

c3 =tf.constant([1111,2222],dtype=tf.int32)
q3 =tf.constant([5555],dtype=tf.int32)
s3 =tf.constant([7777,8888,9999],dtype=tf.int32)

def stack_ragged(tensors):
    values = tf.concat(tensors, axis=0)
    # print("values --",values)
    lens = tf.stack([tf.shape(t, out_type=tf.int64)[0] for t in tensors])
    # print("lens --",lens)
    return tf.RaggedTensor.from_row_lengths(values, lens)

r = stack_ragged([c,q,s])
r1 = stack_ragged([c1,q1,s1])
rr = tf.stack([r,r1])

r2 = stack_ragged([c2,q2,s2])



Expected output = [ [c,q,s,c2,q2,s2] ,   [c1,q1,s1]  ] 

0 个答案:

没有答案