class_docstr: NeuroNER model.
  
  Args:
      param_filepath (type): description
      pretrained_model_folder (type): description
      dataset_text_folder (type): description
      character_embedding_dimension (type): description
      character_lstm_hidden_state_dimension (type): description
      check_for_digits_replaced_with_zeros (type): description
      check_for_lowercase (type): description
      debug (type): description
      dropout_rate (type): description
      experiment_name (type): description
      freeze_token_embeddings (type): description
      gradient_clipping_value (type): description
      learning_rate (type): description
      load_only_pretrained_token_embeddings (type): description
      load_all_pretrained_token_embeddings (type): description
      main_evaluation_mode (type): description
      maximum_number_of_epochs (type): description
      number_of_cpu_threads (type): description
      number_of_gpus (type): description
      optimizer (type): description
      output_folder (type): description
      output_scores (bool): description
      patience (type): description
      plot_format (type): description
      reload_character_embeddings (type): description
      reload_character_lstm (type): description
      reload_crf (type): description
      reload_feedforward (type): description
      reload_token_embeddings (type): description
      reload_token_lstm (type): description
      remap_unknown_tokens_to_unk (type): description
      spacylanguage (type): description
      tagging_format (type): description
      token_embedding_dimension (type): description
      token_lstm_hidden_state_dimension (type): description
      token_pretrained_embedding_filepath (type): description
      tokenizer (type): description
      train_model (type): description
      use_character_lstm (type): description
      use_crf (type): description
      use_pretrained_model (type): description
      verbose (type): description
class_docstr_tok: ["NeuroNER","model",".","Args",":","param_filepath","(","type",")",":","description","pretrained_model_folder","(","type",")",":","description","dataset_text_folder","(","type",")",":","description","character_embedding_dimension","(","type",")",":","description","character_lstm_hidden_state_dimension","(","type",")",":","description","check_for_digits_replaced_with_zeros","(","type",")",":","description","check_for_lowercase","(","type",")",":","description","debug","(","type",")",":","description","dropout_rate","(","type",")",":","description","experiment_name","(","type",")",":","description","freeze_token_embeddings","(","type",")",":","description","gradient_clipping_value","(","type",")",":","description","learning_rate","(","type",")",":","description","load_only_pretrained_token_embeddings","(","type",")",":","description","load_all_pretrained_token_embeddings","(","type",")",":","description","main_evaluation_mode","(","type",")",":","description","maximum_number_of_epochs","(","type",")",":","description","number_of_cpu_threads","(","type",")",":","description","number_of_gpus","(","type",")",":","description","optimizer","(","type",")",":","description","output_folder","(","type",")",":","description","output_scores","(","bool",")",":","description","patience","(","type",")",":","description","plot_format","(","type",")",":","description","reload_character_embeddings","(","type",")",":","description","reload_character_lstm","(","type",")",":","description","reload_crf","(","type",")",":","description","reload_feedforward","(","type",")",":","description","reload_token_embeddings","(","type",")",":","description","reload_token_lstm","(","type",")",":","description","remap_unknown_tokens_to_unk","(","type",")",":","description","spacylanguage","(","type",")",":","description","tagging_format","(","type",")",":","description","token_embedding_dimension","(","type",")",":","description","token_lstm_hidden_state_dimension","(","type",")",":","description","token_pretrained_embedding_filepath","(","type",")",":","description","tokenizer","(","type",")",":","description","train_model","(","type",")",":","description","use_character_lstm","(","type",")",":","description","use_crf","(","type",")",":","description","use_pretrained_model","(","type",")",":","description","verbose","(","type",")",":","description"]
docstr: Fit the model.
docstr_tok: ["Fit","the","model","."]
code: def fit(self):
      """
      Fit the model.
      """
      parameters = self.parameters
      conf_parameters = self.conf_parameters
      dataset_filepaths = self.dataset_filepaths
      modeldata = self.modeldata
      dataset_brat_folders = self.dataset_brat_folders
      sess = self.sess
      model = self.model
      transition_params_trained = self.transition_params_trained
      (
          stats_graph_folder,
          experiment_timestamp,
      ) = self._create_stats_graph_folder(parameters)
  
      # Initialize and save execution details
      start_time = time.time()
      results = {}
      results["epoch"] = {}
      results["execution_details"] = {}
      results["execution_details"]["train_start"] = start_time
      results["execution_details"]["time_stamp"] = experiment_timestamp
      results["execution_details"]["early_stop"] = False
      results["execution_details"]["keyboard_interrupt"] = False
      results["execution_details"]["num_epochs"] = 0
      results["model_options"] = copy.copy(parameters)
  
      model_folder = os.path.join(stats_graph_folder, "model")
      utils.create_folder_if_not_exists(model_folder)
      with open(
          os.path.join(model_folder, "parameters.ini"), "w"
      ) as parameters_file:
          conf_parameters.write(parameters_file)
      pickle.dump(
          modeldata,
          open(os.path.join(model_folder, "dataset.pickle"), "wb"),
      )
  
      tensorboard_log_folder = os.path.join(
          stats_graph_folder, "tensorboard_logs"
      )
      utils.create_folder_if_not_exists(tensorboard_log_folder)
      tensorboard_log_folders = {}
      for dataset_type in dataset_filepaths.keys():
          tensorboard_log_folders[dataset_type] = os.path.join(
              stats_graph_folder, "tensorboard_logs", dataset_type
          )
          utils.create_folder_if_not_exists(
              tensorboard_log_folders[dataset_type]
          )
  
      # Instantiate the writers for TensorBoard
      writers = {}
      for dataset_type in dataset_filepaths.keys():
          writers[dataset_type] = tf.summary.FileWriter(
              tensorboard_log_folders[dataset_type], graph=sess.graph
          )
  
      # embedding_writer has to write in model_folder, otherwise TensorBoard won't be able to view embeddings
      embedding_writer = tf.summary.FileWriter(model_folder)
  
      embeddings_projector_config = projector.ProjectorConfig()
      tensorboard_token_embeddings = (
          embeddings_projector_config.embeddings.add()
      )
      tensorboard_token_embeddings.tensor_name = (
          model.token_embedding_weights.name
      )
      token_list_file_path = os.path.join(
          model_folder, "tensorboard_metadata_tokens.tsv"
      )
      tensorboard_token_embeddings.metadata_path = os.path.relpath(
          token_list_file_path, "."
      )
  
      tensorboard_character_embeddings = (
          embeddings_projector_config.embeddings.add()
      )
      tensorboard_character_embeddings.tensor_name = (
          model.character_embedding_weights.name
      )
      character_list_file_path = os.path.join(
          model_folder, "tensorboard_metadata_characters.tsv"
      )
      tensorboard_character_embeddings.metadata_path = os.path.relpath(
          character_list_file_path, "."
      )
  
      projector.visualize_embeddings(
          embedding_writer, embeddings_projector_config
      )
  
      # Write metadata for TensorBoard embeddings
      token_list_file = codecs.open(token_list_file_path, "w", "UTF-8")
      for token_index in range(modeldata.vocabulary_size):
          token_list_file.write(
              "{0}\n".format(modeldata.index_to_token[token_index])
          )
      token_list_file.close()
  
      character_list_file = codecs.open(
          character_list_file_path, "w", "UTF-8"
      )
      for character_index in range(modeldata.alphabet_size):
          if character_index == modeldata.PADDING_CHARACTER_INDEX:
              character_list_file.write("PADDING\n")
          else:
              character_list_file.write(
                  "{0}\n".format(
                      modeldata.index_to_character[character_index]
                  )
              )
      character_list_file.close()
  
      # Start training + evaluation loop. Each iteration corresponds to 1 epoch.
      # number of epochs with no improvement on the validation test in terms of F1-score
      bad_counter = 0
      previous_best_valid_f1_score = 0
      epoch_number = -1
      try:
          while True:
              step = 0
              epoch_number += 1
              print("\nStarting epoch {0}".format(epoch_number))
  
              epoch_start_time = time.time()
  
              if epoch_number != 0:
                  # Train model: loop over all sequences of training set with shuffling
                  sequence_numbers = list(
                      range(len(modeldata.token_indices["train"]))
                  )
                  random.shuffle(sequence_numbers)
                  for sequence_number in sequence_numbers:
                      transition_params_trained = train.train_step(
                          sess,
                          modeldata,
                          sequence_number,
                          model,
                          parameters,
                      )
                      step += 1
                      if step % 10 == 0:
                          print(
                              "Training {0:.2f}% done".format(
                                  step / len(sequence_numbers) * 100
                              ),
                              end="\r",
                              flush=True,
                          )
  
              epoch_elapsed_training_time = (
                  time.time() - epoch_start_time
              )
              print(
                  "Training completed in {0:.2f} seconds".format(
                      epoch_elapsed_training_time
                  ),
                  flush=True,
              )
  
              y_pred, y_true, output_filepaths = train.predict_labels(
                  sess,
                  model,
                  transition_params_trained,
                  parameters,
                  modeldata,
                  epoch_number,
                  stats_graph_folder,
                  dataset_filepaths,
              )
  
              # Evaluate model: save and plot results
              evaluate.evaluate_model(
                  results,
                  modeldata,
                  y_pred,
                  y_true,
                  stats_graph_folder,
                  epoch_number,
                  epoch_start_time,
                  output_filepaths,
                  parameters,
              )
  
              if (
                  parameters["use_pretrained_model"]
                  and not parameters["train_model"]
              ):
                  conll_to_brat.output_brat(
                      output_filepaths,
                      dataset_brat_folders,
                      stats_graph_folder,
                  )
                  break
  
              # Save model
              model.saver.save(
                  sess,
                  os.path.join(
                      model_folder,
                      "model_{0:05d}.ckpt".format(epoch_number),
                  ),
              )
  
              # Save TensorBoard logs
              summary = sess.run(model.summary_op, feed_dict=None)
              writers["train"].add_summary(summary, epoch_number)
              writers["train"].flush()
              utils.copytree(
                  writers["train"].get_logdir(), model_folder
              )
  
              # Early stop
              valid_f1_score = results["epoch"][epoch_number][0][
                  "valid"
              ]["f1_score"]["micro"]
              if valid_f1_score > previous_best_valid_f1_score:
                  bad_counter = 0
                  previous_best_valid_f1_score = valid_f1_score
                  conll_to_brat.output_brat(
                      output_filepaths,
                      dataset_brat_folders,
                      stats_graph_folder,
                      overwrite=True,
                  )
                  self.transition_params_trained = (
                      transition_params_trained
                  )
              else:
                  bad_counter += 1
              print(
                  "The last {0} epochs have not shown improvements on the validation set.".format(
                      bad_counter
                  )
              )
  
              if bad_counter >= parameters["patience"]:
                  print("Early Stop!")
                  results["execution_details"]["early_stop"] = True
                  break
  
              if epoch_number >= parameters["maximum_number_of_epochs"]:
                  break
  
      except KeyboardInterrupt:
          results["execution_details"]["keyboard_interrupt"] = True
          print("Training interrupted")
  
      print("Finishing the experiment")
      end_time = time.time()
      results["execution_details"]["train_duration"] = (
          end_time - start_time
      )
      results["execution_details"]["train_end"] = end_time
      evaluate.save_results(results, stats_graph_folder)
      for dataset_type in dataset_filepaths.keys():
          writers[dataset_type].close()
  
imports: import codecs
  import configparser
  import copy
  import distutils.util
  import glob
  import os
  import pickle
  from pprint import pprint
  import random
  import shutil
  import sys
  import time
  import warnings
  import pkg_resources
  import numpy
  import matplotlib
  import tensorflow
  from tensorflow.contrib.tensorboard.plugins import projector
  from neuroner import train
  from neuroner import dataset
  from neuroner.entity_lstm import EntityLSTM
  from neuroner import utils
  from neuroner import conll_to_brat
  from neuroner import evaluate
  from neuroner import brat_to_conll
  from neuroner import utils_nlp
label_desc: Use image node_id 5 for calling the NeuroNER obj's underlying member method code with example usage: obj.fit() without return types