def calculate_class_metrics(y_true: List[List[int]], y_pred: List[List[int]], labels: Dictionary) -> List[Metric]: """ Calculates the metrics for the individual classes for the given predictions. The labels should be converted into a one-hot-list. :param y_true: list of true labels :param y_pred: list of predicted labels :param labels: the label dictionary :return: the metrics for every class """ metrics = [] for label in labels.get_items(): metric = Metric(label) label_idx = labels.get_idx_for_item(label) for true, pred in zip(y_true, y_pred): if true[label_idx] == 1 and pred[label_idx] == 1: metric.tp() elif true[label_idx] == 1 and pred[label_idx] == 0: metric.fn() elif true[label_idx] == 0 and pred[label_idx] == 1: metric.fp() elif true[label_idx] == 0 and pred[label_idx] == 0: metric.tn() metrics.append(metric) return metrics
def test_training(): # get default dictionary dictionary: Dictionary = Dictionary.load('chars') # init forward LM with 128 hidden states and 1 layer language_model: LanguageModel = LanguageModel(dictionary, is_forward_lm=True, hidden_size=128, nlayers=1) # get the example corpus and process at character level in forward direction corpus: TextCorpus = TextCorpus('resources/corpora/lorem_ipsum', dictionary, language_model.is_forward_lm, character_level=True) # train the language model trainer: LanguageModelTrainer = LanguageModelTrainer( language_model, corpus) trainer.train('./results', sequence_length=10, mini_batch_size=10, max_epochs=5) # use the character LM as embeddings to embed the example sentence 'I love Berlin' char_lm_embeddings = CharLMEmbeddings('./results/best-lm.pt') sentence = Sentence('I love Berlin') char_lm_embeddings.embed(sentence) print(sentence[1].embedding.size()) # clean up results directory shutil.rmtree('./results')
def test_dictionary_get_item_for_index(): dictionary: Dictionary = Dictionary(add_unk=False) dictionary.add_item('class_1') dictionary.add_item('class_2') dictionary.add_item('class_3') item = dictionary.get_item_for_index(0) assert ('class_1' == item)
def test_dictionary_get_idx_for_item(): dictionary: Dictionary = Dictionary(add_unk=False) dictionary.add_item('class_1') dictionary.add_item('class_2') dictionary.add_item('class_3') idx = dictionary.get_idx_for_item('class_2') assert (1 == idx)
def test_dictionary_get_items_without_unk(): dictionary: Dictionary = Dictionary(add_unk=False) dictionary.add_item('class_1') dictionary.add_item('class_2') dictionary.add_item('class_3') items = dictionary.get_items() assert (3 == len(items)) assert ('class_1' == items[0]) assert ('class_2' == items[1]) assert ('class_3' == items[2])
def test_dictionary_get_items_with_unk(): dictionary: Dictionary = Dictionary() dictionary.add_item('class_1') dictionary.add_item('class_2') dictionary.add_item('class_3') items = dictionary.get_items() assert (4 == len(items)) assert ('<unk>' == items[0]) assert ('class_1' == items[1]) assert ('class_2' == items[2]) assert ('class_3' == items[3])
def init(): y_true = [[0, 1, 1], [0, 0, 1], [1, 1, 0]] y_pred = [[0, 1, 1], [0, 0, 0], [1, 0, 0]] labels = Dictionary(add_unk=False) labels.add_item('class-1') labels.add_item('class-2') labels.add_item('class-3') return y_true, y_pred, labels
def test_dictionary_save_and_load(): dictionary: Dictionary = Dictionary(add_unk=False) dictionary.add_item('class_1') dictionary.add_item('class_2') dictionary.add_item('class_3') file_path = 'dictionary.txt' dictionary.save(file_path) loaded_dictionary = dictionary.load_from_file(file_path) assert (len(dictionary) == len(loaded_dictionary)) assert (len(dictionary.get_items()) == len(loaded_dictionary.get_items())) # clean up file os.remove(file_path)
def convert_labels_to_one_hot(label_list: List[List[str]], label_dict: Dictionary) -> List[List[int]]: """ Convert list of labels (strings) to a one hot list. :param label_list: list of labels :param label_dict: label dictionary :return: converted label list """ converted_label_list = [] for labels in label_list: arr = np.empty(len(label_dict)) arr.fill(0) for label in labels: arr[label_dict.get_idx_for_item(label)] = 1 converted_label_list.append(arr.tolist()) return converted_label_list