[英]How to pre-pad tensorflow ragged tensor with beginning values
這個問題類似於這個問題:
除了我想用起始值預先填充這個張量。 鑒於參差不齊的張量:
[[1],
[4, 2],
[1, 2, 3]]
我希望 output 是:
[[1 1 1],
[4 4 2],
[1 2 3]]
我希望能夠將解決方案應用於更大的參差不齊的張量。
只需使用參差不齊的張量的屬性:
import tensorflow as tf
x = tf.ragged.constant([[1],
[4, 2],
[1, 2, 3]])
rows_to_pad = tf.abs(x.row_lengths() - tf.reduce_max(x.row_lengths()))
padded_x = tf.concat([tf.RaggedTensor.from_row_lengths(
values=tf.repeat(tf.gather(x.merge_dims(0, -1), x.row_starts()), rows_to_pad, axis=0),
row_lengths=rows_to_pad), x], axis=-1).to_tensor()
[[1 1 1]
[4 4 2]
[1 2 3]]
一個不同的參差不齊的張量:
x = tf.ragged.constant([[1, 4, 5, 6, 7, 8, 3],
[4, 2],
[1, 2, 3],
[1, 2, 3, 4, 5],
[1]])
預填充:
[[1 4 5 6 7 8 3]
[4 4 4 4 4 4 2]
[1 1 1 1 1 2 3]
[1 1 1 2 3 4 5]
[1 1 1 1 1 1 1]]
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.