"torch_modules/git@gitlab.lis-lab.fr:franck.dary/macaon.git" did not exist on "d145be52a13ddda6a522338a2edc08ab777ea579"
Newer
Older
ContextModuleImpl::ContextModuleImpl(std::string name, const std::string & definition)
Franck Dary
committed
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)*)");
if (!util::doIfNameMatch(regex, definition, [this,&definition](auto sm)
{
try
{
Franck Dary
committed
for (auto & index : util::split(sm.str(1), ' '))
bufferContext.emplace_back(std::stoi(index));
Franck Dary
committed
for (auto & index : util::split(sm.str(2), ' '))
stackContext.emplace_back(std::stoi(index));
auto funcColumns = util::split(sm.str(3), ' ');
columns.clear();
for (auto & funcCol : funcColumns)
{
Franck Dary
committed
functions.emplace_back() = getFunction(funcCol);
columns.emplace_back(util::split(funcCol, ':').back());
Franck Dary
committed
auto subModuleType = sm.str(4);
auto subModuleArguments = util::split(sm.str(5), ' ');
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]));
Franck Dary
committed
inSize = std::stoi(sm.str(6));
int outSize = std::stoi(sm.str(7));
if (subModuleType == "LSTM")
myModule = register_module("myModule", LSTM(columns.size()*inSize, outSize, options));
else if (subModuleType == "GRU")
myModule = register_module("myModule", GRU(columns.size()*inSize, outSize, options));
else if (subModuleType == "Concat")
myModule = register_module("myModule", Concat(inSize));
else
util::myThrow(fmt::format("unknown sumodule type '{}'", subModuleType));
Franck Dary
committed
w2vFile = sm.str(8);
if (!w2vFile.empty())
{
getDict().loadWord2Vec(w2vFile);
getDict().setState(Dict::State::Closed);
dictSetPretrained(true);
}
} catch (std::exception & e) {util::myThrow(fmt::format("{} in '{}'",e.what(),definition));}
}))
util::myThrow(fmt::format("invalid definition '{}'", definition));
}
std::size_t ContextModuleImpl::getOutputSize()
{
return myModule->getOutputSize(bufferContext.size()+stackContext.size());
}
std::size_t ContextModuleImpl::getInputSize()
{
return columns.size()*(bufferContext.size()+stackContext.size());
}
void ContextModuleImpl::addToContext(std::vector<std::vector<long>> & context, const Config & config)
std::vector<long> contextIndexes;
for (int index : bufferContext)
contextIndexes.emplace_back(config.getRelativeWordIndex(index));
for (int index : stackContext)
if (config.hasStack(index))
contextIndexes.emplace_back(config.getStack(index));
else
contextIndexes.emplace_back(-1);
for (auto index : contextIndexes)
for (unsigned int colIndex = 0; colIndex < columns.size(); colIndex++)
{
auto & col = columns[colIndex];
if (index == -1)
{
for (auto & contextElement : context)
contextElement.push_back(dict.getIndexOrInsert(Dict::nullValueStr));
}
else
{
Franck Dary
committed
int dictIndex = dict.getIndexOrInsert(functions[colIndex](config.getAsFeature(col, index)));
for (auto & contextElement : context)
contextElement.push_back(dictIndex);
}
}
torch::Tensor ContextModuleImpl::forward(torch::Tensor input)
{
auto context = wordEmbeddings(input.narrow(1, firstInputIndex, getInputSize()));
context = context.view({context.size(0), context.size(1)/(int)columns.size(), (int)columns.size()*context.size(2)});
return myModule->forward(context);
}
Franck Dary
committed
void ContextModuleImpl::registerEmbeddings()
Franck Dary
committed
{
wordEmbeddings = register_module("embeddings", torch::nn::Embedding(torch::nn::EmbeddingOptions(getDict().size(), inSize)));
Franck Dary
committed
loadPretrainedW2vEmbeddings(wordEmbeddings, w2vFile);