-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathActivation.cpp
More file actions
67 lines (61 loc) · 1.46 KB
/
Activation.cpp
File metadata and controls
67 lines (61 loc) · 1.46 KB
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
//
// Created by anna_seli on 13/06/2021.
//
#include "Activation.h"
#define INVALID_ACT_FUNC "invalid activation function"
#define EXIT_FAILURE 1
/*
* Activation constructor
*/
Activation::Activation(ActivationType act_type)
: activation_type(act_type)
{
if ((act_type != RELU) && (act_type != SOFTMAX))
{
std::cerr << INVALID_ACT_FUNC << std::endl;
exit(EXIT_FAILURE);
}
}
/*
* activation type getter
* @return activation type
*/
ActivationType Activation::get_activation_type() const
{
return activation_type;
}
/*
* parenthesis operator that activates the right activation function
* @return reference to output of activation function
*/
Matrix Activation::operator()(const Matrix &input)
{
Matrix output_vector = input;
if (activation_type == RELU)
{
for (auto i = 0; i < input.get_rows() * input.get_cols(); i++)
{
if (output_vector[i] < 0.0)
{
output_vector[i] = 0.0;
}
}
}
else if (activation_type == SOFTMAX)
{
float sum = 0;
for (auto i = 0; i < input.get_rows() * input.get_cols(); i++)
{
float temp = std::exp(input.matrix_data[i]);
sum += temp;
output_vector.matrix_data[i] = temp;
}
output_vector = (1 / sum) * output_vector;
}
else
{
std::cerr << INVALID_ACT_FUNC << std::endl;
exit(EXIT_FAILURE);
}
return output_vector;
}