CNTK Sequence Model Error: Different Route Layouts Detected

I am trying to train a model using CNTK which takes two input sequences and outputs a 2-dimensional scalar label. I have defined the model as follows:

def create_seq_model(num_tokens):
    with C.default_options(init=C.glorot_uniform()):
        i1 = sequence.input(shape=num_tokens, is_sparse=True, name='i1')
        i2 = sequence.input(shape=num_tokens, is_sparse=True, name='i2')
        s1 = Sequential([Embedding(300), Fold(GRU(64))])(i1)
        s2 = Sequential([Embedding(300), Fold(GRU(64))])(i2)
        combined = splice(s1, s2)
        model = Sequential([Dense(64, activation=sigmoid),
                        Dropout(0.1, seed=42),
                        Dense(2, activation=softmax)])
        return model(combined)

      

I converted my data to CTF format. When I try to train it using the following snippet (very slightly modified from the example here ), I get an error:

def train(reader, model, max_epochs=16):
    criterion = create_criterion_function(model)

    criterion.replace_placeholders({criterion.placeholders[0]: C.input(2, name='labels')})

    epoch_size = 500000
    minibatch_size=128

    lr_per_sample = [0.003]*4+[0.0015]*24+[0.0003]
    lr_per_minibatch= [x*minibatch_size for x in lr_per_sample]
    lr_schedule = learning_rate_schedule(lr_per_minibatch, UnitType.minibatch, epoch_size)

    momentum_as_time_constant = momentum_as_time_constant_schedule(700)

    learner = fsadagrad(criterion.parameters,
                   lr=lr_schedule, momentum=momentum_as_time_constant,
                   gradient_clipping_threshold_per_sample=15,
                   gradient_clipping_with_truncation=True)

    progress_printer = ProgressPrinter(freq=1000, first=10, tag='Training', num_epochs=max_epochs)

    trainer = Trainer(model, criterion, learner, progress_printer)

    log_number_of_parameters(model)

    t = 0
    for epoch in range(max_epochs):
        epoch_end = (epoch+1) * epoch_size
        while(t < epoch_end):
            data = reader.next_minibatch(minibatch_size, input_map={
                criterion.arguments[0]: reader.streams.i1,
                criterion.arguments[1]: reader.streams.i2,
                criterion.arguments[2]: reader.streams.labels
            })
            trainer.train_minibatch(data)
            t += data[criterion.arguments[1]].num_samples 
        trainer.summarize_training_progress()

      

Mistake:

Different minibatch layouts detected (difference in sequence lengths or count or start flags) in data specified for the Function arguments 'Input('i2', [#, *], [132033])' vs. 'Input('i1', [#, *], [132033])', though these arguments have the same dynamic axes '[*, #]'

      

I notice that if I select examples where both input sequences are of the same length, then the learning function works. Unfortunately, this represents a very small amount of data. What is the proper mechanism for working with sequences that have different data lengths? Do I need to fill in the input (like Keras pad_sequence ())?

+3


source to share


1 answer


The two sequences, i1

and i2

, are accidentally treated as having the same length. This is because the argument sequence_axis

sequence.input(...)

has a default value default_dynamic_axis()

. One way to fix this problem is to tell CNTK that the two sequences are not the same length by giving each a unique sequence axis as follows:



i1_axis = C.Axis.new_unique_dynamic_axis('1')
i2_axis = C.Axis.new_unique_dynamic_axis('2')
i1 = sequence.input(shape=num_tokens, is_sparse=True, sequence_axis=i1_axis, name='i1')
i2 = sequence.input(shape=num_tokens, is_sparse=True, sequence_axis=i2_axis, name='i2')

      

+6


source







All Articles