Beatmup
Beatmup::NNets::ActivationFunctionMixin Class Reference

A mixin implementing activation functions in GLSL. More...

#include <operation.h>

Inheritance diagram for Beatmup::NNets::ActivationFunctionMixin:
Beatmup::NNets::Conv2D

Protected Member Functions

 ActivationFunctionMixin (const ActivationFunction activationFunc)
 
void apply (StringBuilder &code, const char *inputVariable)
 Renders a GLSL code applying activation function to a specific variable and writing the result to gl_FragColor shader output variable. More...
 

Protected Attributes

const ActivationFunction activationFunc
 

Detailed Description

A mixin implementing activation functions in GLSL.

The result of any operation must fit into 0..1 range to be stored as a color texture. The activation function introduces non-linear behavior in the model and determines how the computed values are mapped to the valid output range.

Definition at line 414 of file operation.h.

Constructor & Destructor Documentation

◆ ActivationFunctionMixin()

Beatmup::NNets::ActivationFunctionMixin::ActivationFunctionMixin ( const ActivationFunction  activationFunc)
inlineprotected

Definition at line 419 of file operation.h.

const ActivationFunction activationFunc
Definition: operation.h:417

Member Function Documentation

◆ apply()

void ActivationFunctionMixin::apply ( StringBuilder code,
const char *  inputVariable 
)
protected

Renders a GLSL code applying activation function to a specific variable and writing the result to gl_FragColor shader output variable.

Parameters
[in,out]codeA GLSL source code to be appended by the activation function code
[in]inputVariableName of the variable to apply the activation function to

Definition at line 282 of file operation.cpp.

282  {
283  switch (activationFunc) {
285  code.printf("gl_FragColor = %s;", inputVariable);
286  return;
288  code.printf("gl_FragColor = 0.167 * %s;", inputVariable);
289  return;
291  code.printf("gl_FragColor = clamp(0.1*(%s), -0.05, 0.05) + clamp(0.05*(%s), -0.125, 0.125) + 0.05*(%s) + 0.5;", inputVariable, inputVariable, inputVariable);
292  return;
293  default: Insanity::insanity("Invalid activation function");
294  }
295 }
static void insanity(const char *message)
Definition: exception.h:136
StringBuilder & printf(const char *format,...)
@ SIGMOID_LIKE
piecewise-linear sigmoid approximation
@ DEFAULT
default activation: 0..1 bounded ReLU (identity clipped to 0..1 range)
@ BRELU6
0.167 times identity clipped to 0..1 range

Member Data Documentation

◆ activationFunc

const ActivationFunction Beatmup::NNets::ActivationFunctionMixin::activationFunc
protected

Definition at line 417 of file operation.h.


The documentation for this class was generated from the following files: