2

I have a list of sentences as of word embeddings. So every sentence is a matrix in 16*300, so it is a 2d tensor. I want to connect them to a 3d tensor and use this 3d tensor as input for a CNN model. Unfortunately, I cannot get it into this 3d tensor.

In my opinion, at least connecting two of these 2d tensors to a smaller 3d tensor via tf.concat should work. Unfortunately, I get the following error message

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

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

If it works with two 2d tensors I would probably work with one loop

One of these 2d tensors in the list looks like this one:

<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)>
padul
  • 134
  • 11
  • 1
    Thank you, I edited the question and I hope that the quality is now higher. – padul Aug 14 '20 at 15:10
  • Does this answer your question? [Convert a list of tensors to tensors of tensors pytorch](https://stackoverflow.com/questions/61359162/convert-a-list-of-tensors-to-tensors-of-tensors-pytorch) – iacob Apr 30 '21 at 14:05

1 Answers1

6

You can found the solution in the documentation: https://www.tensorflow.org/api_docs/python/tf/stack

tf.stack: Stacks a list of rank-R tensors into one rank-(R+1) tensor.

>>> 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)>
khanh
  • 600
  • 5
  • 20