pad*_*dul 2 python tensorflow word-embedding tensor cnn
我有一个单词嵌入的句子列表。所以每个句子都是一个16*300的矩阵,所以它是一个二维张量。我想将它们连接到 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
Run Code Online (Sandbox Code Playgroud)
如果它适用于两个二维张量,我可能会使用一个循环
列表中的这些 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)>
Run Code Online (Sandbox Code Playgroud)
您可以在文档中找到解决方案: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)>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
671 次 |
| 最近记录: |