Newer
Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#include "UppercaseRateModule.hpp"
#include "NeuralNetwork.hpp"
UppercaseRateModuleImpl::UppercaseRateModuleImpl(std::string name, const std::string & definition)
{
setName(name);
std::regex regex("(?:(?:\\s|\\t)*)Buffer\\{(.*)\\}(?:(?:\\s|\\t)*)Stack\\{(.*)\\}(?:(?:\\s|\\t)*)(\\S+)\\{(.*)\\}(?:(?:\\s|\\t)*)Out\\{(.*)\\}(?:(?:\\s|\\t)*)");
if (!util::doIfNameMatch(regex, definition, [this,&definition](auto sm)
{
try
{
for (auto & index : util::split(sm.str(1), ' '))
focusedBuffer.emplace_back(std::stoi(index));
for (auto & index : util::split(sm.str(2), ' '))
focusedStack.emplace_back(std::stoi(index));
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]));
int outSize = std::stoi(sm.str(5));
if (subModuleType == "LSTM")
myModule = register_module("myModule", LSTM(1, outSize, options));
else if (subModuleType == "GRU")
myModule = register_module("myModule", GRU(1, outSize, options));
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 UppercaseRateModuleImpl::forward(torch::Tensor input)
{
auto context = input.narrow(1, firstInputIndex, getInputSize());
auto values = torch::from_blob(context.data_ptr(), context.sizes(), context.strides(), torch::TensorOptions(torch::kDouble).requires_grad(false).device(NeuralNetworkImpl::device)).to(torch::kFloat).unsqueeze(-1).clone();
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
return myModule->forward(values);
}
std::size_t UppercaseRateModuleImpl::getOutputSize()
{
return myModule->getOutputSize(getInputSize());
}
std::size_t UppercaseRateModuleImpl::getInputSize()
{
return focusedBuffer.size() + focusedStack.size();
}
void UppercaseRateModuleImpl::addToContext(std::vector<std::vector<long>> & context, const Config & config)
{
std::vector<long> focusedIndexes;
for (int index : focusedBuffer)
focusedIndexes.emplace_back(config.getRelativeWordIndex(index));
for (int index : focusedStack)
if (config.hasStack(index))
focusedIndexes.emplace_back(config.getStack(index));
else
focusedIndexes.emplace_back(-1);
for (auto & contextElement : context)
{
for (auto index : focusedIndexes)
{
double res = -1.0;
if (index >= 0)
{
auto word = util::splitAsUtf8(config.getAsFeature("FORM", index).get());
int nbUpper = 0;
for (auto & letter : word)
if (util::isUppercase(letter))
nbUpper++;
if (word.size() > 0)
res = 1.0*nbUpper/word.size();
}
contextElement.emplace_back(0);
std::memcpy(&contextElement.back(), &res, sizeof res);
}
}
}
void UppercaseRateModuleImpl::registerEmbeddings(std::filesystem::path)