【问题标题】:List of 2d Tensors to one 3d Tensor2d 张量到一个 3d 张量的列表
【发布时间】:2020-12-04 11:40:09
【问题描述】:

我有一个词嵌入的句子列表。所以每个句子都是16*300的矩阵,所以是2d张量。我想将它们连接到一个 3d 张量并将这个 3d 张量用作 CNN 模型的输入。不幸的是,我无法将它放入这个 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)>

【问题讨论】:

标签: python tensorflow tensor conv-neural-network word-embedding


【解决方案1】:

您可以在文档中找到解决方案: 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)>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-07-02
    • 1970-01-01
    • 2019-06-24
    • 1970-01-01
    • 2020-10-14
    • 2017-08-10
    相关资源
    最近更新 更多