簡體   English   中英

一個 3d 張量的二維張量列表

[英]List of 2d Tensors to one 3d Tensor

我有一個詞嵌入的句子列表。 所以每個句子都是16*300的矩陣,所以是2d張量。 我想將它們連接到 3d 張量並使用此 3d 張量作為 CNN model 的輸入。 不幸的是,我無法將它放入這個 3d 張量。

在我看來,至少通過 tf.concat 將這些 2d 張量中的兩個連接到一個較小的 3d 張量應該可以工作。 不幸的是,我收到以下錯誤消息

tf.concat(0, [Tweets_final.Text_M[0], Tweets_final.Text_M[1]])

ValueError: Shape (3, 16, 300) must have rank 0

如果它適用於兩個二維張量,我可能會使用一個循環

列表中的這些 2d 張量之一如下所示:

<tf.Tensor: shape=(16, 300), dtype=float32, numpy= array([[-0.03571776,  0.07699937, -0.02208528, ...,  0.00873246,
    -0.05967658, -0.03735098],
   [-0.03044251,  0.050944  , -0.02236165, ..., -0.01745957,
     0.01311598,  0.01744673],
   [ 0.        ,  0.        ,  0.        , ...,  0.        ,
     0.        ,  0.        ],
   ...,
   [ 0.        ,  0.        ,  0.        , ...,  0.        ,
     0.        ,  0.        ],
   [ 0.        ,  0.        ,  0.        , ...,  0.        ,
     0.        ,  0.        ],
   [ 0.        ,  0.        ,  0.        , ...,  0.        ,
     0.        ,  0.        ]], dtype=float32)>

您可以在文檔中找到解決方案: https://www.tensorflow.org/api_docs/python/tf/stack

tf.stack :將 rank-R 張量列表堆疊成一個 rank-(R+1) 張量。

>>> x = tf.constant([1, 4])
>>> y = tf.constant([2, 5])
>>> z = tf.constant([3, 6])
>>> tf.stack([x, y, z])
<tf.Tensor: shape=(3, 2), dtype=int32, numpy=
array([[1, 4],
       [2, 5],
       [3, 6]], dtype=int32)>
>>> tf.stack([x, y, z], axis=1)
<tf.Tensor: shape=(2, 3), dtype=int32, numpy=
array([[1, 2, 3],
       [4, 5, 6]], dtype=int32)>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM