19#include <unordered_map>
27float sigmoid(
const float x) {
return 1.0f / (1.0f + std::exp(-x)); }
29float tanh(
const float x) {
return std::tanh(x); }
31float linear(
const float x) {
return x; }
34 const float z = 0.2f * x + 0.5f;
35 return z <= 0.0f ? 0.0f : (z <= 1.0f ? z : 1.0f);
38float relu(
const float x) {
return (x > 0.0f) ? x : 0.0f; }
41 CHECK_GT(matrix.rows(), 0);
42 CHECK_GT(matrix.cols(), 0);
43 int output_size =
static_cast<int>(matrix.rows() * matrix.cols());
44 Eigen::MatrixXf output_matrix;
45 output_matrix.resize(1, output_size);
47 for (
int i = 0; i < matrix.rows(); ++i) {
48 for (
int j = 0; j < matrix.cols(); ++j) {
49 output_matrix(0, output_index) = matrix(i, j);
57 static const std::unordered_map<std::string, std::function<float(
float)>>
58 func_map({{
"linear",
linear},
63 return func_map.at(str);
67 if (tensor_pb.
data().empty() || tensor_pb.
shape().empty()) {
68 AERROR <<
"Fail to load the necessary fields!";
71 if (tensor_pb.shape_size() < 2) {
72 ADEBUG <<
"Load tensor size: (1, " << tensor_pb.
shape(0) <<
")";
73 matrix->resize(1, tensor_pb.
shape(0));
74 for (
int i = 0; i < tensor_pb.
shape(0); ++i) {
75 (*matrix)(0, i) =
static_cast<float>(tensor_pb.
data(i));
79 ADEBUG <<
"Load tensor size: (" << tensor_pb.
shape(0) <<
", "
80 << tensor_pb.
shape(1) <<
")";
81 CHECK_EQ(tensor_pb.shape_size(), 2);
82 matrix->resize(tensor_pb.
shape(0), tensor_pb.
shape(1));
83 for (
int i = 0; i < tensor_pb.
shape(0); ++i) {
84 for (
int j = 0; j < tensor_pb.
shape(1); ++j) {
86 static_cast<float>(tensor_pb.
data(i * tensor_pb.
shape(1) + j));
93 if (tensor_pb.
data().empty() || tensor_pb.
shape().empty()) {
94 AERROR <<
"Fail to load the necessary fields!";
97 ADEBUG <<
"Load tensor size: (" << tensor_pb.
shape(0) <<
", 1)";
98 CHECK_EQ(tensor_pb.shape_size(), 1);
99 if (tensor_pb.shape_size() == 1) {
100 vector->resize(tensor_pb.
shape(0));
101 for (
int i = 0; i < tensor_pb.
shape(0); ++i) {
102 (*vector)(i) =
static_cast<float>(tensor_pb.
data(i));
109 std::vector<Eigen::MatrixXf>*
const tensor3d) {
110 if (tensor_pb.
data().empty() || tensor_pb.shape_size() != 3) {
111 AERROR <<
"Fail to load the necessary fields!";
114 int num_depth = tensor_pb.
shape(0);
115 int num_row = tensor_pb.
shape(1);
116 int num_col = tensor_pb.
shape(2);
117 CHECK_EQ(tensor_pb.data_size(), num_depth * num_row * num_col);
118 int tensor_pb_index = 0;
119 for (
int k = 0; k < num_depth; ++k) {
120 Eigen::MatrixXf matrix = Eigen::MatrixXf::Zero(num_row, num_col);
121 for (
int i = 0; i < num_row; ++i) {
122 for (
int j = 0; j < num_col; ++j) {
123 matrix(i, j) = tensor_pb.
data(tensor_pb_index);
127 tensor3d->push_back(matrix);
129 CHECK_EQ(tensor_pb_index, num_depth * num_row * num_col);
float linear(const float x)
linear function: f(x) = x
std::function< float(float)> serialize_to_function(const std::string &str)
translate a string into a network activation function
float tanh(const float x)
hyperbolic tangent function: f(x) = (1 + exp(-2x)) / (1 - exp(-2x))
float hard_sigmoid(const float x)
"hard" sigmoid function: | 0.0 x in (-oo, 0) f(x) = | 0.2x + 0.5 x in [0, 2.5] | 1....
Eigen::MatrixXf FlattenMatrix(const Eigen::MatrixXf &matrix)
flatten a matrix to a row vector
float sigmoid(const float x)
sigmoid function: f(x) = 1 / (1 + exp(-x))
bool LoadTensor(const TensorParameter &tensor_pb, Eigen::MatrixXf *matrix)
load matrix value from a protobuf message
float relu(const float x)
relu function: | 0.0 x in (-oo, 0.0) f(x) = | | x x in [0.0, +oo)