pt_leaky_relu_layer.cpp 937 Bytes
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 43 44 45 46 47 48 49
/*
 * pocket-tensor (c) 2018 Gustavo Valiente gustavo.valiente.m@gmail.com
 * Kerasify (c) 2016 Robert W. Rose
 *
 * MIT License, see LICENSE file.
 */

#include "pt_leaky_relu_layer.h"

#include "pt_parser.h"
#include "pt_layer_data.h"

namespace pt
{

std::unique_ptr<LeakyReluLayer> LeakyReluLayer::create(std::istream& stream)
{
    float alpha = 0;

    if(! Parser::parse(stream, alpha))
    {
        PT_LOG_ERROR << "Alpha parse failed" << std::endl;
        return std::unique_ptr<LeakyReluLayer>();
    }

    return std::unique_ptr<LeakyReluLayer>(new LeakyReluLayer(FloatType(alpha)));
}

bool LeakyReluLayer::apply(LayerData& layerData) const
{
    layerData.out = std::move(layerData.in);

    for(FloatType& value : layerData.out)
    {
        if(value < 0)
        {
            value *= _alpha;
        }
    }

    return true;
}

LeakyReluLayer::LeakyReluLayer(FloatType alpha) noexcept :
    _alpha(alpha)
{
}

}