Skip to content
Snippets Groups Projects
Select Git revision
  • f39ab522dc01d031f3cf9a7eb597bfa5cd3494f2
  • main default protected
2 results

main.py

Blame
  • RawInputModule.cpp 3.17 KiB
    #include "RawInputModule.hpp"
    
    RawInputModuleImpl::RawInputModuleImpl(std::string name, const std::string & definition)
    {
      setName(name);
      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]));
    
                inSize = std::stoi(sm.str(5));
                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));
                else if (subModuleType == "Concat")
                  myModule = register_module("myModule", Concat(inSize));
                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 RawInputModuleImpl::forward(torch::Tensor input)
    {
      return myModule->forward(wordEmbeddings(input.narrow(1, firstInputIndex, getInputSize()))).reshape({input.size(0), -1});
    }
    
    std::size_t RawInputModuleImpl::getOutputSize()
    {
      return myModule->getOutputSize(leftWindow + rightWindow + 1);
    }
    
    std::size_t RawInputModuleImpl::getInputSize()
    {
      return leftWindow + rightWindow + 1;
    }
    
    void RawInputModuleImpl::addToContext(std::vector<std::vector<long>> & context, const Config & config)
    {
      if (leftWindow < 0 or rightWindow < 0)
        return;
    
      std::string prefix = "LETTER";
    
      auto & dict = getDict();
      for (auto & contextElement : context)
      {
        for (int i = 0; i < leftWindow; i++)
          if (config.hasCharacter(config.getCharacterIndex()-leftWindow+i))
            contextElement.push_back(dict.getIndexOrInsert(fmt::format("{}({})", prefix, config.getLetter(config.getCharacterIndex()-leftWindow+i)), ""));
          else
            contextElement.push_back(dict.getIndexOrInsert(Dict::nullValueStr, prefix));
    
        for (int i = 0; i <= rightWindow; i++)
          if (config.hasCharacter(config.getCharacterIndex()+i))
            contextElement.push_back(dict.getIndexOrInsert(fmt::format("{}({})", prefix, config.getLetter(config.getCharacterIndex()+i)), ""));
          else
            contextElement.push_back(dict.getIndexOrInsert(Dict::nullValueStr, prefix));
      }
    }
    
    void RawInputModuleImpl::registerEmbeddings()
    {
      if (!wordEmbeddings)
        wordEmbeddings = register_module("embeddings", WordEmbeddings(getDict().size(), inSize));
    }