LIMA
Libre Multilingual Analyzer — C++ API
Loading...
Searching...
No Matches
conllu_file_iterator.h
Go to the documentation of this file.
1// Copyright 2022 CEA LIST
2// SPDX-FileCopyrightText: 2022 CEA LIST <gael.de-chalendar@cea.fr>
3//
4// SPDX-License-Identifier: MIT
5
6#ifndef DEEPLIMA_LIBS_TASKS_GRAPH_DP_CONLLU_FILE_ITERATOR_H
7#define DEEPLIMA_LIBS_TASKS_GRAPH_DP_CONLLU_FILE_ITERATOR_H
8
10#include "conllu/treebank.h"
15
16namespace deeplima
17{
18namespace graph_dp
19{
20namespace train
21{
22
24{
25public:
26
28 size_t batch_size,
30 const DictsHolder& morph_tag_dh,
31 std::shared_ptr<FeatureVectorizerBase<>> p_embd,
32 bool add_root = false,
33 std::shared_ptr<const std::map<std::string, int64_t>> deprel2id = nullptr,
34 size_t tag_embd_dim = 8,
35 size_t min_sentence_len = 4);
36
37 // Target value used in the gold to mark positions that must be ignored by the
38 // loss/accuracy (e.g. the synthetic <ROOT> token, which is not a dependent).
39 // Matches the ignore_index passed to nll_loss in the classifier.
40 static constexpr int64_t IGNORE_INDEX = -100;
41
42 // Seed of the training batches' sampling, shared by all data sets; unseeded,
43 // it starts from std::random_device
44 static void seed(uint64_t s);
45
46 void init();
47
48 std::vector<nets::embd_descr_t> get_embd_descr();
49
50 // Dicts of the embeddable (morph) features, in the SAME order as
51 // get_embd_descr() returns them. Needed to build the model's dict holder
52 // parallel to its embd_descr, so the per-embedding `dict=<i>` indices in the
53 // generated script reference the correct dict (the script numbers dicts by
54 // position in embd_descr, not by position in the full tag dict holder).
56
57 class Iterator : public BatchIterator
58 {
59 public:
60 Iterator(const CoNLLUDataSet& dataset)
61 : m_dataset(dataset),
62 m_batch_size(0),
63 m_current_bucket(0),
64 m_iter_counter(0)
65 {
66 }
67
68 virtual ~Iterator() = default;
69 virtual void set_batch_size(int64_t batch_size);
70 virtual void start_epoch();
71 virtual bool end();
72 virtual const Batch next_batch();
73
74 private:
75 const CoNLLUDataSet& m_dataset;
76 int64_t m_batch_size;
77
78 size_t m_current_bucket;
79 int64_t m_iter_counter;
80
81 friend class CoNLLUDataSet;
82 };
83
84 virtual std::shared_ptr<BatchIterator> get_iterator() const;
85
86private:
87 bool m_add_root;
88 size_t m_min_sentence_len; // shorter sentences are not trained on
89 const size_t m_batch_size;
90 const DictsHolder m_morph_tag_dh;
91 // deprel (syntactic relation label) string -> class id; shared between the
92 // train and dev datasets so ids are consistent. May be null (rel column then
93 // filled with IGNORE_INDEX).
94 std::shared_ptr<const std::map<std::string, int64_t>> m_deprel2id;
95 const CoNLLU::Annotation& m_annot;
96 std::vector<std::shared_ptr<FeatureVectorizerBase<>>> m_feat_vectorizers;
97
98 // Input features
103 TorchMatrix<float> > CoNLLUToTorchMatrix;
104
106
107 std::vector<CoNLLUToTorchMatrix::feature_descr_t> m_feat_descr;
108 std::vector<CoNLLUToTorchMatrix::embeddable_feature_descr_t> m_embd_feat_descr;
109
110 // Vectorized inputs
111 std::vector<size_t> m_bucket_keys;
112 std::map<size_t, CoNLLUToTorchMatrix::vectorization_t> m_input_buckets;
113
114 // Vectorized gold values
115 std::map<size_t, std::shared_ptr<TorchMatrix<int64_t>>> m_gold_buckets;
116
117 //void load_embeddings();
118 void vectorize();
119 size_t vectorize_bucket(size_t len, const std::vector<size_t>& sents, const CoNLLUToTorchMatrix& vectorizer);
120 void vectorize_bucket_gold(const CoNLLU::BoundedWordLevelAdapter& src,
122 uint64_t timepoint);
123};
124
125} // train
126} // graph_dp
127} // deeplima
128
129#endif // DEEPLIMA_LIBS_TASKS_GRAPH_DP_CONLLU_FILE_ITERATOR_H
CoNLLUDataSet(const CoNLLU::Annotation &annot, size_t batch_size, const ConlluFeatExtractor< CoNLLU::WordLevelAdapter::token_t > &feat_extractor, const DictsHolder &morph_tag_dh, std::shared_ptr< FeatureVectorizerBase<> > p_embd, bool add_root=false, std::shared_ptr< const std::map< std::string, int64_t > > deprel2id=nullptr, size_t tag_embd_dim=8, size_t min_sentence_len=4)
std::vector< nets::embd_descr_t > get_embd_descr()
virtual std::shared_ptr< BatchIterator > get_iterator() const
Edit rules for lemmatizing words absent from the lemma dictionary.
DictsHolderImpl DictsHolder
Definition dict_base.h:42