嘗試使用tf.ragged.stack. 我正在嘗試堆疊兩個張量 t1、t2,但是它們具有不同的等級。t2 比 t1 少 1 個等級。因此,我曾經tf.expand_dims增加 t2 的排名,以便排名匹配。但是,當我將它們堆疊起來時,會出現錯誤:
import tensorflow as tf
t1 = tf.ones([2,10,10], tf.int32)
t2 = tf.ragged.constant([
[0,1,2,3,4,5],
[0,1,2,3,4]
])
t2 = tf.expand_dims(t2, -1)
tf.ragged.stack([t1, t2])
我得到的錯誤是
InvalidArgumentError:ConcatOp:輸入的尺寸應匹配:shape[0] = [20,10] 與 shape[1] = [11,1] [Op:ConcatV2] 名稱:concat
但是,當我從頭開始創建等效張量時,我沒有收到錯誤。
t2_new = tf.ragged.constant([
[[0],[1],[2],[3],[4],[5]],
[[0],[1],[2],[3],[4]]
tf.ragged.stack([t1, t2_new]) # no error
t2 和 t2 new 的區別在于 tensorflow 認為它們的形狀是不同的,即使它們實際上代表的是同一個張量
print(t2.shape) # == (2,None,1)
print(t2_new.shape) # == (2, None, None)
print(tf.math.reduce_all(t2 == t2_new)) # == True i.e actually the same tensor
uj5u.com熱心網友回復:
它實際上并不是那么微不足道,因為它tf.expand_dims添加了一個新維度,但不是一個參差不齊的維度,這是事后堆疊兩個張量所必需的。一般來說,這是可能的,但比僅僅添加一個新維度要復雜一些。我建議將tf.RaggedTensor.from_value_rowids與tf.RaggedTensor.from_row_splits一起使用:
import tensorflow as tf
t1 = tf.ones([2,10,10], tf.int32)
t2 = tf.ragged.constant([[0,1,2,3,4,5],
[0,1,2,3,4]])
t2_new = tf.ragged.constant([
[[0],[1],[2],[3],[4],[5]],
[[0],[1],[2],[3],[4]]])
flattened_ragged_tensor = t2.flat_values
rows = tf.cast(t2.bounding_shape()[0], dtype=tf.int32)
t2 = tf.RaggedTensor.from_value_rowids(
values=tf.RaggedTensor.from_row_splits(
values=flattened_ragged_tensor,
row_splits=tf.range(tf.shape(flattened_ragged_tensor)[0] 1)),
value_rowids=tf.concat([tf.tile([i], [t2[i].shape[0]]) for i in tf.range(rows)], axis=0),
nrows=rows)
print(t2.shape)
print(t2_new.shape)
print(tf.ragged.stack([t1, t2]))
print(tf.ragged.stack([t1, t2_new]))
(2, None, None)
(2, None, None)
<tf.RaggedTensor [[[[1, 1, 1, 1, 1, 1, 1, 1, 1, 1], [1, 1, 1, 1, 1, 1, 1, 1, 1, 1], [1, 1, 1, 1, 1, 1, 1, 1, 1, 1], [1, 1, 1, 1, 1, 1, 1, 1, 1, 1], [1, 1, 1, 1, 1, 1, 1, 1, 1, 1], [1, 1, 1, 1, 1, 1, 1, 1, 1, 1], [1, 1, 1, 1, 1, 1, 1, 1, 1, 1], [1, 1, 1, 1, 1, 1, 1, 1, 1, 1], [1, 1, 1, 1, 1, 1, 1, 1, 1, 1], [1, 1, 1, 1, 1, 1, 1, 1, 1, 1]], [[1, 1, 1, 1, 1, 1, 1, 1, 1, 1], [1, 1, 1, 1, 1, 1, 1, 1, 1, 1], [1, 1, 1, 1, 1, 1, 1, 1, 1, 1], [1, 1, 1, 1, 1, 1, 1, 1, 1, 1], [1, 1, 1, 1, 1, 1, 1, 1, 1, 1], [1, 1, 1, 1, 1, 1, 1, 1, 1, 1], [1, 1, 1, 1, 1, 1, 1, 1, 1, 1], [1, 1, 1, 1, 1, 1, 1, 1, 1, 1], [1, 1, 1, 1, 1, 1, 1, 1, 1, 1], [1, 1, 1, 1, 1, 1, 1, 1, 1, 1]]], [[[0], [1], [2], [3], [4], [5]], [[0], [1], [2], [3], [4]]]]>
<tf.RaggedTensor [[[[1, 1, 1, 1, 1, 1, 1, 1, 1, 1], [1, 1, 1, 1, 1, 1, 1, 1, 1, 1], [1, 1, 1, 1, 1, 1, 1, 1, 1, 1], [1, 1, 1, 1, 1, 1, 1, 1, 1, 1], [1, 1, 1, 1, 1, 1, 1, 1, 1, 1], [1, 1, 1, 1, 1, 1, 1, 1, 1, 1], [1, 1, 1, 1, 1, 1, 1, 1, 1, 1], [1, 1, 1, 1, 1, 1, 1, 1, 1, 1], [1, 1, 1, 1, 1, 1, 1, 1, 1, 1], [1, 1, 1, 1, 1, 1, 1, 1, 1, 1]], [[1, 1, 1, 1, 1, 1, 1, 1, 1, 1], [1, 1, 1, 1, 1, 1, 1, 1, 1, 1], [1, 1, 1, 1, 1, 1, 1, 1, 1, 1], [1, 1, 1, 1, 1, 1, 1, 1, 1, 1], [1, 1, 1, 1, 1, 1, 1, 1, 1, 1], [1, 1, 1, 1, 1, 1, 1, 1, 1, 1], [1, 1, 1, 1, 1, 1, 1, 1, 1, 1], [1, 1, 1, 1, 1, 1, 1, 1, 1, 1], [1, 1, 1, 1, 1, 1, 1, 1, 1, 1], [1, 1, 1, 1, 1, 1, 1, 1, 1, 1]]], [[[0], [1], [2], [3], [4], [5]], [[0], [1], [2], [3], [4]]]]>
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/408863.html
標籤:
