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

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

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

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

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

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

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

Franck Dary's avatar
Franck Dary committed
45
std::size_t RawInputModuleImpl::getOutputSize()
Franck Dary's avatar
Franck Dary committed
46
{
47
  return myModule->getOutputSize(leftWindow + rightWindow + 1);
Franck Dary's avatar
Franck Dary committed
48
49
}

Franck Dary's avatar
Franck Dary committed
50
std::size_t RawInputModuleImpl::getInputSize()
Franck Dary's avatar
Franck Dary committed
51
52
53
54
{
  return leftWindow + rightWindow + 1;
}

Franck Dary's avatar
Franck Dary committed
55
void RawInputModuleImpl::addToContext(std::vector<std::vector<long>> & context, const Config & config)
Franck Dary's avatar
Franck Dary committed
56
57
58
59
{
  if (leftWindow < 0 or rightWindow < 0)
    return;

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

Franck Dary's avatar
Franck Dary committed
62
  auto & dict = getDict();
63
64
65
66
  for (auto & contextElement : context)
  {
    for (int i = 0; i < leftWindow; i++)
      if (config.hasCharacter(config.getCharacterIndex()-leftWindow+i))
67
        contextElement.push_back(dict.getIndexOrInsert(fmt::format("{}({})", prefix, config.getLetter(config.getCharacterIndex()-leftWindow+i)), ""));
68
      else
69
        contextElement.push_back(dict.getIndexOrInsert(Dict::nullValueStr, prefix));
70
71
72

    for (int i = 0; i <= rightWindow; i++)
      if (config.hasCharacter(config.getCharacterIndex()+i))
73
        contextElement.push_back(dict.getIndexOrInsert(fmt::format("{}({})", prefix, config.getLetter(config.getCharacterIndex()+i)), ""));
74
      else
75
        contextElement.push_back(dict.getIndexOrInsert(Dict::nullValueStr, prefix));
76
  }
Franck Dary's avatar
Franck Dary committed
77
78
}

79
void RawInputModuleImpl::registerEmbeddings()
80
{
81
  wordEmbeddings = register_module("embeddings", WordEmbeddings(getDict().size(), inSize));
82
83
}