Skip to content
Snippets Groups Projects
Commit 8deeef3e authored by Franck Dary's avatar Franck Dary
Browse files

ContextModule : now possible to refer to childrens

parent e53b090a
Branches
No related tags found
Loading
...@@ -2,6 +2,7 @@ ...@@ -2,6 +2,7 @@
#define CONTEXTMODULE__H #define CONTEXTMODULE__H
#include <torch/torch.h> #include <torch/torch.h>
#include <optional>
#include "Submodule.hpp" #include "Submodule.hpp"
#include "MyModule.hpp" #include "MyModule.hpp"
#include "GRU.hpp" #include "GRU.hpp"
...@@ -16,8 +17,7 @@ class ContextModuleImpl : public Submodule ...@@ -16,8 +17,7 @@ class ContextModuleImpl : public Submodule
std::shared_ptr<MyModule> myModule{nullptr}; std::shared_ptr<MyModule> myModule{nullptr};
std::vector<std::string> columns; std::vector<std::string> columns;
std::vector<std::function<std::string(const std::string &)>> functions; std::vector<std::function<std::string(const std::string &)>> functions;
std::vector<int> bufferContext; std::vector<std::tuple<Config::Object, int, std::optional<int>>> targets;
std::vector<int> stackContext;
int inSize; int inSize;
std::filesystem::path w2vFile; std::filesystem::path w2vFile;
......
...@@ -4,18 +4,20 @@ ContextModuleImpl::ContextModuleImpl(std::string name, const std::string & defin ...@@ -4,18 +4,20 @@ ContextModuleImpl::ContextModuleImpl(std::string name, const std::string & defin
{ {
setName(name); setName(name);
std::regex regex("(?:(?:\\s|\\t)*)Buffer\\{(.*)\\}(?:(?:\\s|\\t)*)Stack\\{(.*)\\}(?:(?:\\s|\\t)*)Columns\\{(.*)\\}(?:(?:\\s|\\t)*)(\\S+)\\{(.*)\\}(?:(?:\\s|\\t)*)In\\{(.*)\\}(?:(?:\\s|\\t)*)Out\\{(.*)\\}(?:(?:\\s|\\t)*)w2v\\{(.*)\\}(?:(?:\\s|\\t)*)"); std::regex regex("(?:(?:\\s|\\t)*)Targets\\{(.*)\\}(?:(?:\\s|\\t)*)Columns\\{(.*)\\}(?:(?:\\s|\\t)*)(\\S+)\\{(.*)\\}(?:(?:\\s|\\t)*)In\\{(.*)\\}(?:(?:\\s|\\t)*)Out\\{(.*)\\}(?:(?:\\s|\\t)*)w2v\\{(.*)\\}(?:(?:\\s|\\t)*)");
if (!util::doIfNameMatch(regex, definition, [this,&definition](auto sm) if (!util::doIfNameMatch(regex, definition, [this,&definition](auto sm)
{ {
try try
{ {
for (auto & index : util::split(sm.str(1), ' ')) for (auto & target : util::split(sm.str(1), ' '))
bufferContext.emplace_back(std::stoi(index)); {
auto splited = util::split(target, '.');
for (auto & index : util::split(sm.str(2), ' ')) if (splited.size() != 2 and splited.size() != 3)
stackContext.emplace_back(std::stoi(index)); util::myThrow(fmt::format("invalid target '{}' expected 'object.index(.childIndex)'", target));
targets.emplace_back(std::make_tuple(Config::str2object(splited[0]), std::stoi(splited[1]), splited.size() == 3 ? std::optional<int>(std::stoi(splited[2])) : std::optional<int>()));
}
auto funcColumns = util::split(sm.str(3), ' '); auto funcColumns = util::split(sm.str(2), ' ');
columns.clear(); columns.clear();
for (auto & funcCol : funcColumns) for (auto & funcCol : funcColumns)
{ {
...@@ -23,8 +25,8 @@ ContextModuleImpl::ContextModuleImpl(std::string name, const std::string & defin ...@@ -23,8 +25,8 @@ ContextModuleImpl::ContextModuleImpl(std::string name, const std::string & defin
columns.emplace_back(util::split(funcCol, ':').back()); columns.emplace_back(util::split(funcCol, ':').back());
} }
auto subModuleType = sm.str(4); auto subModuleType = sm.str(3);
auto subModuleArguments = util::split(sm.str(5), ' '); auto subModuleArguments = util::split(sm.str(4), ' ');
auto options = MyModule::ModuleOptions(true) auto options = MyModule::ModuleOptions(true)
.bidirectional(std::stoi(subModuleArguments[0])) .bidirectional(std::stoi(subModuleArguments[0]))
...@@ -32,8 +34,8 @@ ContextModuleImpl::ContextModuleImpl(std::string name, const std::string & defin ...@@ -32,8 +34,8 @@ ContextModuleImpl::ContextModuleImpl(std::string name, const std::string & defin
.dropout(std::stof(subModuleArguments[2])) .dropout(std::stof(subModuleArguments[2]))
.complete(std::stoi(subModuleArguments[3])); .complete(std::stoi(subModuleArguments[3]));
inSize = std::stoi(sm.str(6)); inSize = std::stoi(sm.str(5));
int outSize = std::stoi(sm.str(7)); int outSize = std::stoi(sm.str(6));
if (subModuleType == "LSTM") if (subModuleType == "LSTM")
myModule = register_module("myModule", LSTM(columns.size()*inSize, outSize, options)); myModule = register_module("myModule", LSTM(columns.size()*inSize, outSize, options));
...@@ -44,7 +46,7 @@ ContextModuleImpl::ContextModuleImpl(std::string name, const std::string & defin ...@@ -44,7 +46,7 @@ ContextModuleImpl::ContextModuleImpl(std::string name, const std::string & defin
else else
util::myThrow(fmt::format("unknown sumodule type '{}'", subModuleType)); util::myThrow(fmt::format("unknown sumodule type '{}'", subModuleType));
w2vFile = sm.str(8); w2vFile = sm.str(7);
if (!w2vFile.empty()) if (!w2vFile.empty())
{ {
...@@ -60,12 +62,12 @@ ContextModuleImpl::ContextModuleImpl(std::string name, const std::string & defin ...@@ -60,12 +62,12 @@ ContextModuleImpl::ContextModuleImpl(std::string name, const std::string & defin
std::size_t ContextModuleImpl::getOutputSize() std::size_t ContextModuleImpl::getOutputSize()
{ {
return myModule->getOutputSize(bufferContext.size()+stackContext.size()); return myModule->getOutputSize(targets.size());
} }
std::size_t ContextModuleImpl::getInputSize() std::size_t ContextModuleImpl::getInputSize()
{ {
return columns.size()*(bufferContext.size()+stackContext.size()); return columns.size()*(targets.size());
} }
void ContextModuleImpl::addToContext(std::vector<std::vector<long>> & context, const Config & config) void ContextModuleImpl::addToContext(std::vector<std::vector<long>> & context, const Config & config)
...@@ -73,12 +75,24 @@ void ContextModuleImpl::addToContext(std::vector<std::vector<long>> & context, c ...@@ -73,12 +75,24 @@ void ContextModuleImpl::addToContext(std::vector<std::vector<long>> & context, c
auto & dict = getDict(); auto & dict = getDict();
std::vector<long> contextIndexes; std::vector<long> contextIndexes;
for (int index : bufferContext) for (auto & target : targets)
contextIndexes.emplace_back(config.getRelativeWordIndex(index)); if (config.hasRelativeWordIndex(std::get<0>(target), std::get<1>(target)))
{
for (int index : stackContext) int baseIndex = config.getRelativeWordIndex(std::get<0>(target), std::get<1>(target));
if (config.hasStack(index)) if (!std::get<2>(target))
contextIndexes.emplace_back(config.getStack(index)); contextIndexes.emplace_back(baseIndex);
else
{
int childIndex = *std::get<2>(target);
auto childs = util::split(config.getAsFeature(Config::childsColName, baseIndex).get(), '|');
if (childIndex >= 0 and childIndex < (int)childs.size())
contextIndexes.emplace_back(std::stoi(childs[childIndex]));
else if (childIndex < 0 and ((int)childs.size())+childIndex >= 0)
contextIndexes.emplace_back(std::stoi(childs[childs.size()+childIndex]));
else
contextIndexes.emplace_back(-1);
}
}
else else
contextIndexes.emplace_back(-1); contextIndexes.emplace_back(-1);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment