FocusedColumnModule.cpp 6.04 KB
Newer Older
1
#include "FocusedColumnModule.hpp"
Franck Dary's avatar
Franck Dary committed
2

3
FocusedColumnModuleImpl::FocusedColumnModuleImpl(std::string name, const std::string & definition, std::filesystem::path path) : path(path)
Franck Dary's avatar
Franck Dary committed
4
{
Franck Dary's avatar
Franck Dary committed
5
  setName(name);
6
  std::regex regex("(?:(?:\\s|\\t)*)Column\\{(.*)\\}(?:(?:\\s|\\t)*)NbElem\\{(.*)\\}(?:(?:\\s|\\t)*)Buffer\\{(.*)\\}(?:(?:\\s|\\t)*)Stack\\{(.*)\\}(?:(?:\\s|\\t)*)(\\S+)\\{(.*)\\}(?:(?:\\s|\\t)*)In\\{(.*)\\}(?:(?:\\s|\\t)*)Out\\{(.*)\\}(?:(?:\\s|\\t)*)w2v\\{(.*)\\}(?:(?:\\s|\\t)*)");
Franck Dary's avatar
Franck Dary committed
7
8
9
10
  if (!util::doIfNameMatch(regex, definition, [this,&definition](auto sm)
        {
          try
          {
11
12
            func = getFunction(sm.str(1));
            column = util::split(sm.str(1), ':').back();
Franck Dary's avatar
Franck Dary committed
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
            maxNbElements = std::stoi(sm.str(2));

            for (auto & index : util::split(sm.str(3), ' '))
              focusedBuffer.emplace_back(std::stoi(index));

            for (auto & index : util::split(sm.str(4), ' '))
              focusedStack.emplace_back(std::stoi(index));

            auto subModuleType = sm.str(5);
            auto subModuleArguments = util::split(sm.str(6), ' ');

            auto options = MyModule::ModuleOptions(true)
              .bidirectional(std::stoi(subModuleArguments[0]))
              .num_layers(std::stoi(subModuleArguments[1]))
              .dropout(std::stof(subModuleArguments[2]))
              .complete(std::stoi(subModuleArguments[3]));

30
            inSize = std::stoi(sm.str(7));
Franck Dary's avatar
Franck Dary committed
31
32
33
34
35
36
            int outSize = std::stoi(sm.str(8));

            if (subModuleType == "LSTM")
              myModule = register_module("myModule", LSTM(inSize, outSize, options));
            else if (subModuleType == "GRU")
              myModule = register_module("myModule", GRU(inSize, outSize, options));
Franck Dary's avatar
Franck Dary committed
37
38
            else if (subModuleType == "Concat")
              myModule = register_module("myModule", Concat(inSize));
Franck Dary's avatar
Franck Dary committed
39
40
41
            else
              util::myThrow(fmt::format("unknown sumodule type '{}'", subModuleType));

42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
            w2vFiles = sm.str(9);

            if (!w2vFiles.empty())
            {
              auto pathes = util::split(w2vFiles.string(), ' ');
              for (auto & p : pathes)
              {
                auto splited = util::split(p, ',');
                if (splited.size() != 2)
                  util::myThrow("expected 'prefix,pretrained.w2v'");
                getDict().loadWord2Vec(this->path / splited[1], splited[0]);
                getDict().setState(Dict::State::Closed);
                dictSetPretrained(true);
              }
            }

Franck Dary's avatar
Franck Dary committed
58
59
60
          } catch (std::exception & e) {util::myThrow(fmt::format("{} in '{}'",e.what(),definition));}
        }))
    util::myThrow(fmt::format("invalid definition '{}'", definition));
Franck Dary's avatar
Franck Dary committed
61
62
}

Franck Dary's avatar
Franck Dary committed
63
torch::Tensor FocusedColumnModuleImpl::forward(torch::Tensor input)
Franck Dary's avatar
Franck Dary committed
64
65
66
{
  std::vector<torch::Tensor> outputs;
  for (unsigned int i = 0; i < focusedBuffer.size()+focusedStack.size(); i++)
Franck Dary's avatar
Franck Dary committed
67
    outputs.emplace_back(myModule->forward(wordEmbeddings(input.narrow(1, firstInputIndex+i*maxNbElements, maxNbElements))).reshape({input.size(0), -1}));
Franck Dary's avatar
Franck Dary committed
68
69
70
71

  return torch::cat(outputs, 1);
}

Franck Dary's avatar
Franck Dary committed
72
std::size_t FocusedColumnModuleImpl::getOutputSize()
Franck Dary's avatar
Franck Dary committed
73
{
74
  return (focusedBuffer.size()+focusedStack.size())*myModule->getOutputSize(maxNbElements);
Franck Dary's avatar
Franck Dary committed
75
76
}

Franck Dary's avatar
Franck Dary committed
77
std::size_t FocusedColumnModuleImpl::getInputSize()
Franck Dary's avatar
Franck Dary committed
78
79
80
81
{
  return (focusedBuffer.size()+focusedStack.size()) * maxNbElements;
}

Franck Dary's avatar
Franck Dary committed
82
void FocusedColumnModuleImpl::addToContext(std::vector<std::vector<long>> & context, const Config & config)
Franck Dary's avatar
Franck Dary committed
83
{
Franck Dary's avatar
Franck Dary committed
84
  auto & dict = getDict();
Franck Dary's avatar
Franck Dary committed
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
  std::vector<long> focusedIndexes;

  for (int index : focusedBuffer)
    focusedIndexes.emplace_back(config.getRelativeWordIndex(index));

  for (int index : focusedStack)
    if (config.hasStack(index))
      focusedIndexes.emplace_back(config.getStack(index));
    else
      focusedIndexes.emplace_back(-1);

  for (auto & contextElement : context)
  {
    for (auto index : focusedIndexes)
    {
      if (index == -1)
      {
        for (int i = 0; i < maxNbElements; i++)
103
          contextElement.emplace_back(dict.getIndexOrInsert(Dict::nullValueStr, column));
Franck Dary's avatar
Franck Dary committed
104
105
106
107
108
109
        continue;
      }

      std::vector<std::string> elements;
      if (column == "FORM")
      {
110
        auto asUtf8 = util::splitAsUtf8(func(config.getAsFeature(column, index).get()));
Franck Dary's avatar
Franck Dary committed
111

112
        //TODO don't use nullValueStr here
Franck Dary's avatar
Franck Dary committed
113
114
115
116
117
118
119
120
        for (int i = 0; i < maxNbElements; i++)
          if (i < (int)asUtf8.size())
            elements.emplace_back(fmt::format("{}", asUtf8[i]));
          else
            elements.emplace_back(Dict::nullValueStr);
      }
      else if (column == "FEATS")
      {
121
        auto splited = util::split(func(config.getAsFeature(column, index).get()), '|');
Franck Dary's avatar
Franck Dary committed
122
123
124

        for (int i = 0; i < maxNbElements; i++)
          if (i < (int)splited.size())
125
            elements.emplace_back(splited[i]);
Franck Dary's avatar
Franck Dary committed
126
127
128
129
130
131
          else
            elements.emplace_back(Dict::nullValueStr);
      }
      else if (column == "ID")
      {
        if (config.isTokenPredicted(index))
132
          elements.emplace_back("TOKEN");
Franck Dary's avatar
Franck Dary committed
133
        else if (config.isMultiwordPredicted(index))
134
          elements.emplace_back("MULTIWORD");
Franck Dary's avatar
Franck Dary committed
135
        else if (config.isEmptyNodePredicted(index))
136
          elements.emplace_back("EMPTYNODE");
Franck Dary's avatar
Franck Dary committed
137
      }
138
139
      else if (column == "EOS")
      {
140
        bool isEOS = func(config.getAsFeature(Config::EOSColName, index)) == Config::EOSSymbol1;
141
        elements.emplace_back(fmt::format("{}", isEOS));
142
      }
Franck Dary's avatar
Franck Dary committed
143
144
      else
      {
145
        elements.emplace_back(func(config.getAsFeature(column, index)));
Franck Dary's avatar
Franck Dary committed
146
147
148
149
150
151
      }

      if ((int)elements.size() != maxNbElements)
        util::myThrow(fmt::format("elements.size ({}) != maxNbElements ({})", elements.size(), maxNbElements));

      for (auto & element : elements)
152
        contextElement.emplace_back(dict.getIndexOrInsert(element, column));
Franck Dary's avatar
Franck Dary committed
153
154
155
156
    }
  }
}

157
void FocusedColumnModuleImpl::registerEmbeddings()
158
{
159
  wordEmbeddings = register_module("embeddings", WordEmbeddings(getDict().size(), inSize));
160
161
162
163
  auto pathes = util::split(w2vFiles.string(), ' ');
  for (auto & p : pathes)
  {
    auto splited = util::split(p, ',');
164
    loadPretrainedW2vEmbeddings(wordEmbeddings->get(), path / splited[1], splited[0]);
165
  }
166
167
}