SplitTransModule.cpp 2.6 KB
Newer Older
1
2
3
#include "SplitTransModule.hpp"
#include "Transition.hpp"

Franck Dary's avatar
Franck Dary committed
4
SplitTransModuleImpl::SplitTransModuleImpl(std::string name, int maxNbTrans, const std::string & definition)
5
{
Franck Dary's avatar
Franck Dary committed
6
  setName(name);
7
  this->maxNbTrans = maxNbTrans;
Franck Dary's avatar
Franck Dary committed
8
9
10
11
12
13
14
15
16
17
18
19
20
21
  std::regex regex("(?:(?:\\s|\\t)*)(\\S+)\\{(.*)\\}(?:(?:\\s|\\t)*)In\\{(.*)\\}(?:(?:\\s|\\t)*)Out\\{(.*)\\}(?:(?:\\s|\\t)*)");
  if (!util::doIfNameMatch(regex, definition, [this,&definition](auto sm)
        {
          try
          {
            auto subModuleType = sm.str(1);
            auto subModuleArguments = util::split(sm.str(2), ' ');

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

22
            inSize = std::stoi(sm.str(3));
Franck Dary's avatar
Franck Dary committed
23
24
25
26
27
28
            int outSize = std::stoi(sm.str(4));

            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 == "Concat")
              myModule = register_module("myModule", Concat(inSize));
Franck Dary's avatar
Franck Dary committed
31
32
33
34
35
36
            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));
37
38
}

Franck Dary's avatar
Franck Dary committed
39
torch::Tensor SplitTransModuleImpl::forward(torch::Tensor input)
40
{
Franck Dary's avatar
Franck Dary committed
41
  return myModule->forward(wordEmbeddings(input.narrow(1, firstInputIndex, getInputSize()))).reshape({input.size(0), -1});
42
43
}

Franck Dary's avatar
Franck Dary committed
44
std::size_t SplitTransModuleImpl::getOutputSize()
45
46
47
48
{
  return myModule->getOutputSize(maxNbTrans);
}

Franck Dary's avatar
Franck Dary committed
49
std::size_t SplitTransModuleImpl::getInputSize()
50
51
52
53
{
  return maxNbTrans;
}

Franck Dary's avatar
Franck Dary committed
54
void SplitTransModuleImpl::addToContext(std::vector<std::vector<long>> & context, const Config & config)
55
{
Franck Dary's avatar
Franck Dary committed
56
  auto & dict = getDict();
57
58
59
60
  auto & splitTransitions = config.getAppliableSplitTransitions();
  for (auto & contextElement : context)
    for (int i = 0; i < maxNbTrans; i++)
      if (i < (int)splitTransitions.size())
61
        contextElement.emplace_back(dict.getIndexOrInsert(splitTransitions[i]->getName(), ""));
62
      else
63
        contextElement.emplace_back(dict.getIndexOrInsert(Dict::nullValueStr, ""));
64
65
}

66
void SplitTransModuleImpl::registerEmbeddings()
67
{
68
  wordEmbeddings = register_module("embeddings", WordEmbeddings(getDict().size(), inSize));
69
70
}