0

I'm using keras with a theano backend. Now I have a variable x, which is an theano tensor with a type field "TensorType(float32, 3D)".
I added

    from keras import backend as K

at the beginning of my file. Then I wrote:

    x = K.expand_dims(x, dim = 1)

I expected that the type field of x should be "TensorType(float32, 4D)". However, it is "TensorType(float32, (False, True, False, False))", and I cannot figure out the reason. In addition, the document of keras doesn't provide further information of this function, it just says that "Adds a 1-sized dimension at index "dim" ".
Besides, if I perform

    x = K.squeeze(x, 1)

, the type field of x will be "TensorType(float32, matrix)", which is expected.

ptr
  • 1
  • 1
  • 4

1 Answers1

1

The command expand_dims has dimshuffle operation of Theano under the hood. The tuple (False, True, False, False) is telling you the dimensions that are broadcastable. You may be aware of the broadcasting abilities of Numpy. It is similar with some key differences.

From Theano developer: Theano needs all broadcastable dimensions to be declared in the graph before compilation. NumPy uses the run time shape information.

See this and this for more details.

In the case of your 4-D array, the second dimension i.e. channels is broadcastable. So let us assume that your 4-D array size is (10,N,20,30). Now you can do element-wise multiplication of your 4-D array and another array of size (10,1,20,30) without repeating second dimension N times. This is called broadcasting. Now you try to multiply your 4-D array and another array of size (1,N,20,30). This will fail since the first dimension is not broadcastable. I hope this is clear.

Community
  • 1
  • 1
Autonomous
  • 8,935
  • 1
  • 38
  • 77
  • I understand the broadcasting mechanism. In fact, I am trying to squeeze a tensor of `TensorType(float32, 4D)` to a tensor of `TensorType(float32, 3D)`, do something on it, and restore it to a tensor of `TensorType(float32, 4D)`. However, I can just restore it to a tensor of `TensorType(float32, (False, True, False, False))`, and I wonder how to restore it to a tensor of `TensorType(float32, 4D)`. – ptr Mar 23 '17 at 20:25