blob: 760c9a0ccd99fe94a518fefc63081303e48b9b33 [file] [log] [blame]
//
// Copyright © 2017 Arm Ltd. All rights reserved.
// SPDX-License-Identifier: MIT
//
#include "Activation.hpp"
#include <boost/log/trivial.hpp>
#include <cmath>
namespace armnn
{
float Activation(float in,
ActivationFunction function,
float a,
float b)
{
float output;
// Compute the result of the activation function.
switch (function)
{
case ActivationFunction::Linear:
{
output = a * in + b;
break;
}
case ActivationFunction::Sigmoid:
{
output = 1.f / (1.f + expf(-in));
break;
}
case ActivationFunction::ReLu:
{
output = std::max(0.f, in);
break;
}
case ActivationFunction::BoundedReLu:
{
output = std::min(a, std::max(b, in));
break;
}
case ActivationFunction::SoftReLu:
{
output = logf(1.0f + expf(in));
break;
}
case ActivationFunction::LeakyReLu:
{
output = in > 0.0f ? in : (in * a);
break;
}
case ActivationFunction::Abs:
{
output = in < 0 ? -in : in;
break;
}
case ActivationFunction::Sqrt:
{
output = sqrtf(in);
break;
}
case ActivationFunction::Square:
{
output = in * in;
break;
}
case ActivationFunction::TanH:
{
output = a * tanhf(b * in);
break;
}
default:
{
throw InvalidArgumentException("Unsupported activation function");
}
}
return output;
}
void Activation(Decoder<float>& in,
Encoder<float>& out,
const TensorInfo& tensorInfo,
ActivationFunction function,
float a,
float b)
{
for (size_t i = 0; i<tensorInfo.GetNumElements(); i++)
{
out.Set(Activation(in.Get(), function, a, b));
++in;
++out;
}
}
void Activation(const float* in,
float* out,
const TensorInfo& tensorInfo,
ActivationFunction function,
float a,
float b)
{
for (size_t i = 0; i<tensorInfo.GetNumElements(); i++)
{
out[i] = Activation(in[i], function, a, b);
}
}
} //namespace armnn