HistoryModule.cpp 2.68 KB
Newer Older
Franck Dary's avatar
Franck Dary committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include "HistoryModule.hpp"

HistoryModuleImpl::HistoryModuleImpl(std::string name, const std::string & definition)
{
  setName(name);
  std::regex regex("(?:(?:\\s|\\t)*)NbElem\\{(.*)\\}(?:(?:\\s|\\t)*)(\\S+)\\{(.*)\\}(?:(?:\\s|\\t)*)In\\{(.*)\\}(?:(?:\\s|\\t)*)Out\\{(.*)\\}(?:(?:\\s|\\t)*)");
  if (!util::doIfNameMatch(regex, definition, [this,&definition](auto sm)
        {
          try
          {
            maxNbElements = std::stoi(sm.str(1));

            auto subModuleType = sm.str(2);
            auto subModuleArguments = util::split(sm.str(3), ' ');

            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]));

            inSize = std::stoi(sm.str(4));
            int outSize = std::stoi(sm.str(5));

            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
29
30
            else if (subModuleType == "CNN")
              myModule = register_module("myModule", CNN(inSize, outSize, options));
Franck Dary's avatar
Franck Dary committed
31
32
            else if (subModuleType == "Concat")
              myModule = register_module("myModule", Concat(inSize));
Franck Dary's avatar
Franck Dary committed
33
34
35
36
37
38
39
40
41
42
            else
              util::myThrow(fmt::format("unknown sumodule type '{}'", subModuleType));

          } catch (std::exception & e) {util::myThrow(fmt::format("{} in '{}'",e.what(),definition));}
        }))
    util::myThrow(fmt::format("invalid definition '{}'", definition));
}

torch::Tensor HistoryModuleImpl::forward(torch::Tensor input)
{
Franck Dary's avatar
Franck Dary committed
43
  return myModule->forward(wordEmbeddings(input.narrow(1, firstInputIndex, maxNbElements))).reshape({input.size(0), -1});
Franck Dary's avatar
Franck Dary committed
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
}

std::size_t HistoryModuleImpl::getOutputSize()
{
  return myModule->getOutputSize(maxNbElements);
}

std::size_t HistoryModuleImpl::getInputSize()
{
  return maxNbElements;
}

void HistoryModuleImpl::addToContext(std::vector<std::vector<long>> & context, const Config & config)
{
  auto & dict = getDict();

60
61
  std::string prefix = "HISTORY";

Franck Dary's avatar
Franck Dary committed
62
63
64
  for (auto & contextElement : context)
    for (int i = 0; i < maxNbElements; i++)
      if (config.hasHistory(i))
65
        contextElement.emplace_back(dict.getIndexOrInsert(config.getHistory(i), prefix));
Franck Dary's avatar
Franck Dary committed
66
      else
67
        contextElement.emplace_back(dict.getIndexOrInsert(Dict::nullValueStr, prefix));
Franck Dary's avatar
Franck Dary committed
68
69
}

70
void HistoryModuleImpl::registerEmbeddings()
Franck Dary's avatar
Franck Dary committed
71
{
72
  wordEmbeddings = register_module("embeddings", WordEmbeddings(getDict().size(), inSize));
Franck Dary's avatar
Franck Dary committed
73
74
}