06
The implementation makes padding, sequence shaping, and evaluation explicit
CodeImplementationThe source separates sequence preparation, model construction, training, and padding-aware evaluation into reusable components.
The tokenizer converts sequences to indices and post-pads them to a fixed width.
data_prep.py + util.pyOpen full file
def seq2ngrams(seqs, n=3): return np.array([",".join( [seq[i:i+n] for i in range(len(seq))] ) for seq in seqs])def process_features(self, input_grams): input_data = self.tokenizer_encoder.texts_to_sequences(input_grams) return sequence.pad_sequences( input_data, maxlen=self.maxseq_len, padding='post' )def process_target(self, target_seqs): target_data = self.tokenizer_decoder.texts_to_sequences(target_seqs) target_data = sequence.pad_sequences( target_data, maxlen=self.maxseq_len, padding='post' ) return to_categorical(target_data)util.pyOpen full file
def custome_accuracy(y_true, y_pred): """Calculate accuracy over non-padding positions.""" y = tf.argmax(y_true, axis=-1) y_hat = tf.argmax(y_pred, axis=-1) mask = tf.greater(y, 0) return K.cast( K.equal( tf.boolean_mask(y, mask), tf.boolean_mask(y_hat, mask) ), K.floatx() )main.pyOpen full file
network = FinalCRRNNNetwork( train_df, n_grams=1, optimizer=Adam(lr=0.0004), maxseq_len=512)network.fit(X_train, y_train, X_val, y_val, epochs=20, batch_size=32)kfold = KFold(n_splits=10, shuffle=True, random_state=123456)for train, test in kfold.split(...): network.fit(train_input_seqs[train], train_target_seqs[train], train_input_seqs[test], train_target_seqs[test], epochs=1, batch_size=96) network.save_model( 'FinalCRRNN_model_v2{}.h5'.format(i) )main.pyTraining and prediction exportmodel.pyNetwork definitionsdata_prep.pyTokenization and paddingutil.pyMetric and decoding helpers
Download the reference implementation
Four Python source files plus a short README. The code uses the Keras API available when the model was developed.