Library · Pine Script v5 · Library

MLActivationFunctions

by ricardosantos79· code credited to RicardoSantos

MLActivationFunctions is a Pine Script v5 library that provides standard activation functions and their derivatives for neural network models built directly in Pine Script.

View on GitHub

GitHub stars261on the repository
Pine versionv5runs as is
Lines of code296
Settings0inputs
LicenceNone statedfile: MPL-2.0
Last update4 years ago13 Nov 2021

Add It to Your TradingView Chart

  1. 1Copy the scriptPress Copy script above, or the copy button on the code below.
  2. 2Open the Pine EditorOpen a TradingView chart and choose Pine Editor at the bottom of the screen.
  3. 3Paste over the templateCreate a new library, select all of the template text and paste. Press Save and give it a name.
  4. 4Publish or import itA library draws nothing. Publish it from the editor, then import its functions in your own scripts.

Source Code

296 lines of Pine Script v5 from beta/libs/ML/MLActivationFunctions.pinescript, published under MPL-2.0 (file header). Keep the author's credit and licence lines when you reuse it.

MLActivationFunctions.pinescript
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © RicardoSantos

//@version=5

// @description Activation functions for Neural networks.
library(title='MLActivationFunctions')

// reference: 
//      https://www.educba.com/perceptron-learning-algorithm/
//      https://blog.primen.dk/basic-neural-network-model-insights/
//      https://ml-cheatsheet.readthedocs.io/en/latest/activation_functions.html
//      https://en.wikipedia.org/wiki/Artificial_neural_network
//      https://blog.primen.dk/basic-neural-network-code-example-java/
//      https://mlfromscratch.com/activation-functions-explained/
//      https://www.analyticssteps.com/blogs/7-types-activation-functions-neural-network
//      http://binaryplanet.org/category/activation-function/
//      https://keras.io/api/layers/activations/

// @function Enumerate available activation function names.
// @param idx int, index of the function name, unbound range (names: 1 to 17).
// @returns string
export names_enum (int idx) => //{
    switch (idx)
        01 => 'binary step'
        02 => 'linear'
        03 => 'sigmoid'
        04 => 'sigmoid derivative'
        05 => 'tahn'
        06 => 'tahn derivative'
        07 => 'relu'
        08 => 'relu derivative'
        09 => 'leaky relu'
        10 => 'leaky relu derivative'
        11 => 'relu-6'
        12 => 'softmax'
        13 => 'softplus'
        14 => 'softsign'
        15 => 'elu'
        16 => 'selu'
        17 => 'exponential'
        => ''
//{ usage:
//{ remarks:
//}}}

// @function available activation function names in a array.
// @returns string array
export names_array () => //{
    array.from( 
     'binary step', 
     'linear', 
     'sigmoid', 
     'sigmoid derivative', 
     'tahn', 
     'tahn derivative', 
     'relu', 
     'relu derivative', 
     'leaky relu', 
     'leaky relu derivative', 
     'relu-6', 
     'softmax', 
     'softplus', 
     'softsign', 
     'elu', 
     'selu', 
     'exponential')
//{ usage:
//{ remarks:
//}}}

// @function Basic threshold output classifier to activate/deactivate neuron.
// @param value float, value to process.
// @returns float
export binary_step (float value) => //{
    value > 0.0 ? 1.0 : 0.0
//{ usage:
//{ remarks:
//}}}

// @function Input is the same as output.
// @param value float, value to process.
// @returns float
export linear (float value) => //{
    value
//{ usage:
//{ remarks:
//}}}

// @function Sigmoid or logistic function.
// @param value float, value to process.
// @returns float
export sigmoid (float value) => //{
    1.0 / (1.0 + math.exp(-value))
//{ usage:
//{ remarks:
//}}}

// @function Derivative of sigmoid function.
// @param value float, value to process.
// @returns float
export sigmoid_derivative (float value) => //{
    float _f = sigmoid(value)
    _f * (1.0 - _f)
//{ usage:
//{ remarks:
//}}}

// @function Hyperbolic tangent function.
// @param value float, value to process.
// @returns float
export tahn (float value) => //{
    float _ep = math.exp(value)
    float _en = math.exp(-value)
    (_ep - _en) / (_ep + _en)
//{ usage:
//{ remarks:
//}}}

// @function Hyperbolic tangent function derivative.
// @param value float, value to process.
// @returns float
export tahn_derivative (float value) => //{
    1.0 - math.pow(tahn(value), 2)
//{ usage:
//{ remarks:
//}}}

// @function Rectified linear unit (RELU) function.
// @param value float, value to process.
// @returns float
export relu (float value) => //{
    value <= 0.0 ? 0.0 : value
//{ usage:
//{ remarks:
//}}}

// @function RELU function derivative.
// @param value float, value to process.
// @returns float
export relu_derivative (float value) => //{
    value <= 0 ? 0 : 1.0
//{ usage:
//{ remarks:
//}}}

// @function Leaky RELU function.
// @param value float, value to process.
// @returns float
export leaky_relu (float value) => //{
    value < 0.0 ? 0.01 * value : value
//{ usage:
//{ remarks:
//}}}

// @function Leaky RELU function derivative.
// @param value float, value to process.
// @returns float
export leaky_relu_derivative (float value) => //{
    value < 0 ? 0.01 : 1.0
//{ usage:
//{ remarks:
//}}}

// @function RELU-6 function.
// @param value float, value to process.
// @returns float
export relu6 (float value) => //{
    switch
        (value < 0.0) => 0.01 * value
        (value > 6.0) => 6.0
        => value
//{ usage:
//{ remarks:
//}}}

// @function Softmax function.
// @param value float array, values to process.
// @returns float
export softmax (float[] values) => //{
    int _size_v = array.size(values)
    switch
        _size_v < 1 => runtime.error(str.format('MLActivationFunctions -> softmax(): "values" has the wrong size: {0}', _size_v))

    float[] _tmp = array.copy(values)
    for _i = 0 to (_size_v - 1)
        array.set(_tmp, _i, math.exp(array.get(values, _i)))
    float _tot = array.sum(_tmp)
    for _i = 0 to (_size_v - 1)
        array.set(_tmp, _i, array.get(_tmp, _i) / _tot)
    _tmp
//{ usage:
// var a = array.from(0.0, 1, 2, 3, 4, 5, 6, 7, 8, 9)
// var b = softmax(a)
// if barstate.islastconfirmedhistory
//     label.new(bar_index, 0.0, str.tostring(b))//ok
//{ remarks:
//}}}

// @function Softplus function.
// @param value float, value to process.
// @returns float
export softplus (float value) => //{
    math.log(math.exp(value) + 1.0)
//{ usage:
// var a = array.from(-20.0, -1, 0, 1, 20)
// if barstate.islastconfirmedhistory
//     b = array.new_float(5)
//     for _i = 0 to 4
//         array.set(b, _i, softplus(array.get(a, _i)))
//     label.new(bar_index, 0.0, str.tostring(b))//ok
//{ remarks:
//}}}

// @function Softsign function.
// @param value float, value to process.
// @returns float
export softsign (float value) => //{
    value / (math.abs(value) + 1.0)
//{ usage:
// var a = array.from(-1.0, 0, 1)
// if barstate.islastconfirmedhistory
//     b = array.new_float(3)
//     for _i = 0 to 2
//         array.set(b, _i, softsign(array.get(a, _i)))
//     label.new(bar_index, 0.0, str.tostring(b))//ok
//{ remarks:
//}}}

// @function Exponential Linear Unit (ELU) function.
// @param value float, value to process.
// @param alpha float, default=1.0, predefined constant, controls the value to which an ELU saturates for negative net inputs. .
// @returns float
export elu (float value, float alpha=1.0) => //{
    switch
        (value > 0.0) => value
        (value < 0.0) => alpha * (math.exp(value) - 1.0)
        => 0.0
//{ usage:
//{ remarks:
//}}}

// @function Scaled Exponential Linear Unit (SELU) function.
// @param value float, value to process.
// @param alpha float, default=1.67326324, predefined constant, controls the value to which an SELU saturates for negative net inputs. .
// @param scale float, default=1.05070098, predefined constant.
// @returns float
export selu (float value, float alpha=1.67326324, float scale=1.05070098) => //{
    switch
        (value > 0) => scale * value
        (value < 0) => scale * alpha * (math.exp(value) - 1.0)
        => 0.0
//{ usage:
//{ remarks:
//}}}

// @function Pointer to math.exp() function.
// @param value float, value to process.
// @returns float
export exponential (float value) => //{
    math.exp(value)
//{ usage:
//{ remarks:
//}}}

// @function Run a activation function from provided string name.
// @param name string, activation function name.
// @returns float
export function (float value, string name='sigmoid', float alpha=na, float scale=na) => //{
    switch (name)
        'binary step'           => binary_step(value)
        'linear'                => linear(value)
        'sigmoid'               => sigmoid(value)
        'sigmoid derivative'    => sigmoid_derivative(value)
        'tahn'                  => tahn(value)
        'tahn derivative'       => tahn_derivative(value)
        'relu'                  => relu(value)
        'relu derivative'       => relu_derivative(value)
        'leaky relu'            => leaky_relu(value)
        'leaky relu derivative' => leaky_relu_derivative(value)
        'relu-6'                => relu6(value)
        'softmax'               => runtime.error('MLActivationFunctions -> function(): softmax function is not implemented via selection due to signature incompatibility.'), float(na)
        'softplus'              => softplus(value)
        'softsign'              => softsign(value)
        'elu'                   => na(alpha) ? elu(value) : elu(value, alpha)
        'selu'                  => 
            switch
                (na(alpha) and na(scale))     => selu(value)
                (not na(alpha) and na(scale)) => selu(value, alpha=alpha)
                (na(alpha) and not na(scale)) => selu(value, scale=scale)
                =>                               selu(value, alpha, scale)
        'exponential'           => exponential(value)
        => runtime.error(str.format('MLActivationFunctions -> function(): "name" is not a valid function name. found: {0}', name)), float(na)
//{ usage:
//{ remarks:
//}}}

Overview

MLActivationFunctions exports mathematical functions including binary step, linear, sigmoid, hyperbolic tangent, rectified linear unit (ReLU), leaky ReLU, softmax, softplus, softsign, ELU, SELU, and exponential functions, alongside string enum and array helpers. Designed strictly as an exportable library, it does not generate visual plots or accept chart inputs.

Details

Published by

ricardosantos79 on GitHub

Code credit

RicardoSantos, as written in the script's header

Type

Library, Pine Script v5

Draws

Library (no plots)

Category

AI / machine learning

Markets

Any symbol

Timeframes

Any timeframe

Risk Notes

No repainting, lookahead or stacked entries were found in its code or README. That says nothing about how well it works: test it first.

Third-party code. ForexR did not write, test or endorse this script, and the summary above was generated automatically from its code and README. Indicators describe the past, and a strategy's backtest can look better than live trading, especially when a script repaints. Check it in the Strategy Tester and on a demo account before you rely on it.

Similar Pine Scripts

MLExtensions

by artificial-intelligence-edge
75 stars

MLExtensions is a Pine Script library providing utility and mathematical helper functions for machine learning indicators, specifically optimized for approximate nearest neighbors classification in Lorentzian space. It includes functions to normalize series de

Library v5 AI / machine learning
Updated 2 days ago · MIT GitHub repository of MLExtensions

Ta

by nersent
29 stars

This Pine Script v5 library exports thin wrappers around built-in technical analysis functions. It covers moving averages, momentum measures such as RSI and CCI, volatility tools including ATR and standard deviation, range extremes, cross detection, and runnin

Library v5
Updated 11 months ago · Custom licence GitHub repository of Ta

Bands

by heyphat
26 stars

This library exports a Band type and functions that build a simple-moving-average basis with upper and lower envelopes from standard deviation. It classifies a source as above, below, or inside those envelopes, measures band width, and scales an imported z-sco

Library v6 Volatility & bandsMoving averages
Updated 2 months ago · AGPL-3.0 GitHub repository of Bands

Mylib

by ferranbt
21 stars

This script serves as a minimal utility library written in Pine Script v6. It provides an exported helper function that calculates the sum of two numeric arguments. As a reusable code library, it produces no visual plots, charts, or trading signals, and it con

Library v6
Updated 7 weeks ago · MPL-2.0 GitHub repository of Mylib

HLevel

by stupenkov
14 stars

HLevel is a Pine Script v5 utility library designed to manage horizontal price levels for cryptocurrency charts. It defines custom types for pivots and levels, along with methods to initialize, add, track, and expire price levels based on pivot high and pivot

Library v5
Updated 3 years ago · None stated GitHub repository of HLevel

Zignaly2Beta

by ruckard
13 stars

This library provides helper functions to format and build JSON webhook alert payloads for the Zignaly automated trading platform. It constructs formatted messages containing parameters such as trading pair, exchange, account type, leverage, margin mode, hedge

Library v5
Updated 1 year ago · None stated GitHub repository of Zignaly2Beta

More libraries

Trade Your Charts With a Regulated Broker

Compare brokers that connect to TradingView, with spreads, minimum deposits and leverage side by side.

Compare brokers