Home > Software design >  Question about tensorflow.tile with a tensor of 5 dimensions
Question about tensorflow.tile with a tensor of 5 dimensions

Time:12-17

I'm trying to understand the following thing from an implementation of a paper I'm currently reading:

In tensorflow, if I have a tensor x of shape (4,64,5,5)

  • Then I create a new dimension by doing

    x = x[:,:,tf.newaxis]
    

    ending with a new tensor of shape (4,64,1,5,5)

  • Then I do

    x = tf.tile(x, (1, 1, 5, 1, 1))
    

ending up with something of shape (4,64,5,5,5)

Reading the documentation for tf.tile, I still don't understand what is it exactly doing in this case. Am I replicating the new dimension for 5 times? And if yes, what is exactly placed in the new dimension by tensorflow? What am I exactly replicating?

CodePudding user response:

It is literally creating a new tensor by repeating the input across the third dimension 5 times resulting in 5 x 5 rows x 5 columns:

x = tf.random.uniform(((1,1,5,5)))
x = x[:,:,tf.newaxis]
print('Before tile -->', x)
x = tf.tile(x, (1, 1, 5, 1, 1))
print('After tile -->', x)
Before tile --> tf.Tensor(
[[[[[0.86033905 0.91900826 0.39433706 0.9772172  0.32149637]
    [0.01335323 0.03711665 0.664286   0.11703181 0.7997707 ]
    [0.7063314  0.01817334 0.685941   0.6407242  0.59115565]
    [0.819417   0.46511436 0.00940382 0.12464321 0.9256897 ]
    [0.45731974 0.8999344  0.3199395  0.41329288 0.05623758]]]]], shape=(1, 1, 1, 5, 5), dtype=float32)
After tile --> tf.Tensor(
[[[[[0.86033905 0.91900826 0.39433706 0.9772172  0.32149637]
    [0.01335323 0.03711665 0.664286   0.11703181 0.7997707 ]
    [0.7063314  0.01817334 0.685941   0.6407242  0.59115565]
    [0.819417   0.46511436 0.00940382 0.12464321 0.9256897 ]
    [0.45731974 0.8999344  0.3199395  0.41329288 0.05623758]]

   [[0.86033905 0.91900826 0.39433706 0.9772172  0.32149637]
    [0.01335323 0.03711665 0.664286   0.11703181 0.7997707 ]
    [0.7063314  0.01817334 0.685941   0.6407242  0.59115565]
    [0.819417   0.46511436 0.00940382 0.12464321 0.9256897 ]
    [0.45731974 0.8999344  0.3199395  0.41329288 0.05623758]]

   [[0.86033905 0.91900826 0.39433706 0.9772172  0.32149637]
    [0.01335323 0.03711665 0.664286   0.11703181 0.7997707 ]
    [0.7063314  0.01817334 0.685941   0.6407242  0.59115565]
    [0.819417   0.46511436 0.00940382 0.12464321 0.9256897 ]
    [0.45731974 0.8999344  0.3199395  0.41329288 0.05623758]]

   [[0.86033905 0.91900826 0.39433706 0.9772172  0.32149637]
    [0.01335323 0.03711665 0.664286   0.11703181 0.7997707 ]
    [0.7063314  0.01817334 0.685941   0.6407242  0.59115565]
    [0.819417   0.46511436 0.00940382 0.12464321 0.9256897 ]
    [0.45731974 0.8999344  0.3199395  0.41329288 0.05623758]]

   [[0.86033905 0.91900826 0.39433706 0.9772172  0.32149637]
    [0.01335323 0.03711665 0.664286   0.11703181 0.7997707 ]
    [0.7063314  0.01817334 0.685941   0.6407242  0.59115565]
    [0.819417   0.46511436 0.00940382 0.12464321 0.9256897 ]
    [0.45731974 0.8999344  0.3199395  0.41329288 0.05623758]]]]], shape=(1, 1, 5, 5, 5), dtype=float32)
  • Related