#Add Time Distributed Dense layer #Time distributed outputs a prediction for each time step out = TimeDistributed(Dense(num_classes, activation=args.dense_act), name="dense_layer")(model) #Finalise model model = Model(input, out) #Used for loading pre-saved model weights if load_weights or trans_learn: print("Loading Weights") model.load_weights(args.weights_filepath, by_name=True) #Compile model model.compile(loss='categorical_crossentropy', optimizer=opt, metrics=["accuracy"]) #Prints a summary of model layers and parameters #print(model.summary()) #Custom metric calculations metrics = Metrics_Approx(tag_index=all_labels, k=k) #Train model if not trans_learn: history = model.fit(X_train, np.array(y_train), batch_size=batch_size, epochs=int(args.num_epochs), verbose=1,
embedding_weight_matrix = getEmbeddingWeightMatrix(embedding_vectors, tokenizer_words.word_index) print(embedding_weight_matrix.shape) #build model input = Input(shape=(X_train.shape[1],)) model = Embedding(input_dim=vocab_size, output_dim=word_embed_size, input_length=seq_maxlen, weights=[embedding_weight_matrix], trainable = False) (input) model = LSTM(100, return_sequences=True)(model) output = TimeDistributed(Dense(num_tags, activation="softmax"))(model) model = Model(inputs=input, outputs=output) print(model.summary()) model.compile(optimizer="rmsprop", loss="categorical_crossentropy", metrics=["accuracy"]) save_weights = ModelCheckpoint('model.h5', monitor='val_loss', save_best_only=True) model.fit(x=X_train, y=y_train, batch_size=batch_size, epochs=epochs, validation_data=(X_validation, y_validation), callbacks=[save_weights]) sentences_test, tags_test = load_data(os.path.join(data_dir, 'test.txt')) X_test = getSequences(sentences_test, tokenizer_words, seq_maxlen, 0) y_test = getSequences(tags_test, tokenizer_tags, seq_maxlen, tokenizer_tags.word_index['o']) y_pred = model.predict(X_test) y_pred_tags, y_test_tags = decode_output(X_test, y_pred, y_test, tokenizer_words, tokenizer_tags, seq_maxlen) report = classification_report(y_pred=y_pred_tags, y_true=y_test_tags)