#include <math.h>
// Fungsi aktivasi sigmoid biner
float sigmoidBinary(float x) {
return 1.0 / (1.0 + exp(-x));
}
// Fungsi aktivasi sigmoid bipolar
float sigmoidBipolar(float x) {
return 2.0 / (1.0 + exp(-x)) - 1.0;
}
// Fungsi sigmoid parametrik
float parametricSigmoid(float x, float a) {
return 1.0 / (1.0 + exp(-a * x));
}
// Fungsi aktivasi hiperbolik tangen (tanh)
float tanhActivation(float x) {
return tanh(x);
}
// Fungsi Exponential Linear Unit (ELU)
float eluActivation(float x, float alpha) {
return (x > 0) ? x : alpha * (exp(x) - 1);
}
// Fungsi ReLU (Rectified Linear Unit)
float reluActivation(float x) {
return max(0.0f, x);
}
// Fungsi Leaky ReLU
float leakyReluActivation(float x, float alpha) {
return (x > 0) ? x : alpha * x;
}
// Fungsi sigmoid softplus
float softplusActivation(float x) {
return log(1.0 + exp(x));
}
// Fungsi Softmax - untuk lapisan keluaran
void softmaxActivation(float* x, int length) {
float sum = 0.0;
for (int i = 0; i < length; i++) {
x[i] = exp(x[i]);
sum += x[i];
}
for (int i = 0; i < length; i++) {
x[i] /= sum;
}
}
void hasil(String b, float a) {
Serial.print(b); Serial.print(" = "); Serial.println(a);
}
float a = -1.11;
void setup() {
Serial.begin(115200);
// Setup di sini
}
void loop() {
Serial.print("\n a = ");
Serial.println(a);
hasil("sigmoidBinary", sigmoidBinary(a));
hasil("sigmoidBipolar", sigmoidBipolar(a));
float aParametric = 2.0; // Parameter a dapat disesuaikan
hasil("parametricSigmoid", parametricSigmoid(a, aParametric));
hasil("tanh", tanhActivation(a));
float alphaELU = 1.0;
hasil("elu", eluActivation(a, alphaELU));
hasil("relu", reluActivation(a));
float alphaLeakyRelu = 0.01; // Parameter alpha dapat disesuaikan
hasil("leakyRelu", leakyReluActivation(a, alphaLeakyRelu));
hasil("softplus", softplusActivation(a));
const int lengthSoftmax = 3;
float inputSoftmax[lengthSoftmax] = {a, random(-3, 3), random(-3, 3)};
// Tampilkan input sebelum softmax
Serial.print("Input sebelum softmax: ");
for (int i = 0; i < lengthSoftmax; i++) {
Serial.print(inputSoftmax[i], 4); // Menampilkan 4 digit desimal
Serial.print(" ");
}
Serial.println();
softmaxActivation(inputSoftmax, lengthSoftmax);
// Tampilkan output setelah softmax
Serial.print("Output setelah softmax: ");
for (int i = 0; i < lengthSoftmax; i++) {
Serial.print(inputSoftmax[i], 4); // Menampilkan 4 digit desimal
Serial.print(" ");
}
Serial.println();
a = (a > 2.11) ? -1.01 : a + 0.01;
delay(1000); // Delay untuk kestabilan
}