Search code examples
tensorflowtflearn

TF Learn CNN example: How to add one more layer?


Considering Text Classification Using Convolutional Neural Networks on Characters example: I want to have three layers of convolution (instead of two in the original code). Here is the modified code with three layers. It seems that I have problems with shapes. Would you please consider the code and let me know what's wrong with it?


Solution

  • If you put print(pool1), print(pool2), and print(pool3) in lines 68, 83, and 90, respectively, you'll notice only the first two are printed:

    Tensor("CNN_Layer1/transpose:0", shape=(?, 41, 10, 1), dtype=float32)
    Tensor("CNN_Layer2/transpose:0", shape=(?, 11, 10, 1), dtype=float32)
    

    And that's because you weren't careful with data dimension while creating new operations. Take a look at the changes in the dimensionality of your network:

    byte_list output shape = (?, 100, 256, 1)
    # apply 10x conv1 filters of size (20,256)
    conv1 output shape = (?, 81, 1, 10)
    # apply max_pooling of size 4 and stride 2 + transpose filter dimension
    pool1 output shape = (?, 41, 10, 1)
    # apply 10x conv2 filters of size (20,10)
    conv2 output shape = (?, 22, 1, 10)
    # apply max_pooling of size 4 and stride 2 + transpose filter dimension
    pool2 output shape = (?, 11, 10, 1)
    

    Then you try to apply 10x conv2 filters of size (20,10), but your tensor has shape (?, 11, 10, 1). If this operation were succesfully applied to it, its shape would be (?, -8, 1, 10), but negative dimensions are impossible. Therefore, the problem comes from line 86.

    There are multiple ways to approach this, and all of them involve either changing the filter sizes and/or network architecture. Just try writing down these dimensions to keep track of what is going on, and you should be able to figure out how to do it. :-)