Beispiel #1
0
int Assembler::AddLabel(string& name, LabelVector& labels)
{
  int index = 0;
  LabelVector::iterator i;
  for (i = labels.begin(); i != labels.end(); i ++, index ++) {
    if(i->label == name)
      return index;
  }

  labels.push_back(Label(name));
  return index;
}
static void expandLabels(Bundle& bundle, size_t beamSize)
{
    auto& outputActivationsVector = bundle["outputActivations"].get<MatrixVector>();

    auto& outputActivations = outputActivationsVector.back();

    auto& labels = bundle["referenceLabels"].get<LabelVector>();
    auto& inputTimesteps = bundle["inputTimesteps"].get<IndexVector>();

    size_t characters    = outputActivations.size().front();
    size_t miniBatchSize = outputActivations.size()[outputActivations.size().size() - 2];
    size_t maxTimesteps  = outputActivations.size()[outputActivations.size().size() - 1];

    size_t originalMiniBatchSize = miniBatchSize / beamSize;

    LabelVector expandedLabels;
    IndexVector expandedTimesteps;

    for(size_t miniBatch = 0; miniBatch < originalMiniBatchSize; ++miniBatch)
    {
        for(size_t beam = 0; beam < beamSize; ++beam)
        {
            expandedLabels.push_back(labels[miniBatch]);
            expandedTimesteps.push_back(inputTimesteps[miniBatch]);
        }
    }

    labels = std::move(expandedLabels);
    inputTimesteps = std::move(expandedTimesteps);

    auto referenceActivations = matrix::zeros({characters, miniBatchSize, maxTimesteps},
        outputActivations.precision());

    for(size_t miniBatch = 0; miniBatch < miniBatchSize; ++miniBatch)
    {
        for(size_t timestep = 0; timestep < maxTimesteps; ++timestep)
        {
            if(timestep < labels[miniBatch].size())
            {
                size_t character = labels[miniBatch][timestep];

                assert(character < characters);

                referenceActivations(character, miniBatch, timestep) = 1.0;
            }
            else
            {
                referenceActivations(characters - 1, miniBatch, timestep) = 1.0;
            }
        }
    }

    if(util::isLogEnabled("CTCDecoderLayer::Detail"))
    {
        util::log("CTCDecoderLayer::Detail") << "  reference labels: \n";
        for(auto& label : labels)
        {
            util::log("CTCDecoderLayer::Detail") << "   reference label: "
                << util::toString(label) << "\n";

        }
        util::log("CTCDecoderLayer::Detail") << "  reference activations: "
            << referenceActivations.debugString();
    }
    else
    {
        util::log("CTCDecoderLayer::Detail") << "  reference labels size: "
            << labels.size() << "\n";
        util::log("CTCDecoderLayer") << "  reference activations size: "
            << outputActivations.shapeString() << "\n";
    }

    bundle["referenceActivations"] = MatrixVector({referenceActivations});
}