Search code examples
pythonconvolutioncntk

CNTK Convolution1d


I am trying to create a simple convolution model in CNTK as shown below

def create_model(hidden_dim, output_dim):
    nn=C.layers.Sequential([ C.layers.Embedding(shape=50,name='embedding'),
        C.layers.Convolution1D((40,),num_filters=5, activation=C.ops.relu),
        C.layers.GlobalMaxPooling(),
        C.layers.Dense(shape=40, activation=C.ops.tanh, init_bias=0.1), 
        C.layers.Dense(shape=2, activation=None, init_bias=0.1)
        ])
    return nn

but I keep on getting the following error ValueError: Convolution map tensor must have rank 1 or the same as the input tensor.


Solution

  • I was able to fix this issue by adding the reduction_rank=0 as a parameter to the Convolution1d layer.

    def create_model(hidden_dim, output_dim):
    nn=C.layers.Sequential([ C.layers.Embedding(shape=50,name='embedding', **reduction_rank=0**),
        C.layers.Convolution1D((40,),num_filters=5, activation=C.ops.relu),
        C.layers.GlobalMaxPooling(),
        C.layers.Dense(shape=40, activation=C.ops.tanh, init_bias=0.1), 
        C.layers.Dense(shape=2, activation=None, init_bias=0.1)
        ])
    return nn
    

    Quoting from the CNTK Layers Documentation

    reduction_rank (int, defaults to 1) – set to 0 if input items are scalars (input has no depth axis), e.g. an audio signal or a black-and-white image that is stored with tensor shape (H,W) instead of (1,H,W)

    I was expecting that CNTK would be able to infer this thing automatically