Added TANH op and test to micro

This commit is contained in:
Yair Ehrenwald 2020-05-05 13:24:53 +03:00
parent 6062300fef
commit c9dc17cfdf
4 changed files with 54 additions and 14 deletions

View File

@ -76,6 +76,7 @@ AllOpsResolver::AllOpsResolver() {
/* min_version = */ 1,
/* max_version = */ 2);
AddBuiltin(BuiltinOperator_L2_NORMALIZATION, Register_L2_NORMALIZATION());
AddBuiltin(BuiltinOperator_TANH, Register_TANH());
}
} // namespace micro

View File

@ -106,6 +106,10 @@ TfLiteStatus LogicalNotEval(TfLiteContext* context, TfLiteNode* node) {
return EvalLogical(context, node, [](bool v) { return !v; });
}
TfLiteStatus TANHEval(TfLiteContext* context, TfLiteNode* node) {
return EvalNumeric(context, node, std::tanh);
}
} // namespace
} // namespace elementwise
@ -221,6 +225,21 @@ TfLiteRegistration* Register_LOGICAL_NOT() {
return &r;
}
TfLiteRegistration* Register_TANH() {
static TfLiteRegistration r = {
/*init=*/nullptr,
/*free=*/nullptr,
/*prepare=*/
elementwise::GenericPrepare<elementwise::IsNumericSupportedType>,
/*invoke=*/elementwise::TANHEval,
/*profiling_string=*/nullptr,
/*builtin_code=*/0,
/*custom_name=*/nullptr,
/*version=*/0 };
return &r;
}
} // namespace micro
} // namespace ops
} // namespace tflite

View File

@ -13,6 +13,9 @@ See the License for the specific language governing permissions and
limitations under the License.
==============================================================================*/
#include "tensorflow/lite/c/common.h"
#include "tensorflow/lite/micro/debug_log.h"
#include "tensorflow/lite/micro/kernels/all_ops_resolver.h"
@ -160,9 +163,9 @@ TF_LITE_MICRO_TEST(Abs) {
tflite::testing::TestElementwiseFloat(
tflite::BuiltinOperator_ABS, // ABS operator
{2, 2, 2}, // Input shape
{0.01, -0.01, 10, -10}, // Input values
{0.01f, -0.01f, 10.0f, -10.0f}, // Input values
{2, 2, 2}, // Output shape
{0.01, 0.01, 10, 10}, // Output values
{0.01f, 0.01f, 10.0f, 10.0f}, // Output values
output_data);
}
@ -172,9 +175,9 @@ TF_LITE_MICRO_TEST(Sin) {
tflite::testing::TestElementwiseFloat(
tflite::BuiltinOperator_SIN, // SIN operator
{2, 2, 2}, // Input shape
{0, 3.1415926, -3.1415926, 1}, // Input values
{0.0f, 3.1415926f, -3.1415926f, 1.0f}, // Input values
{2, 2, 2}, // Output shape
{0, 0, 0, 0.84147}, // Output values
{0.0f, 0.0f, 0.0f, 0.84147f}, // Output values
output_data);
}
@ -184,9 +187,9 @@ TF_LITE_MICRO_TEST(Cos) {
tflite::testing::TestElementwiseFloat(
tflite::BuiltinOperator_COS, // COS operator
{2, 2, 2}, // Input shape
{0, 3.1415926, -3.1415926, 1}, // Input values
{0.0f, 3.1415926f, -3.1415926f, 1.0f}, // Input values
{2, 2, 2}, // Output shape
{1, -1, -1, 0.54030}, // Output values
{1.0f, -1.0f, -1.0f, 0.54030f}, // Output values
output_data);
}
@ -196,9 +199,9 @@ TF_LITE_MICRO_TEST(Log) {
tflite::testing::TestElementwiseFloat(
tflite::BuiltinOperator_LOG, // LOG operator
{2, 2, 2}, // Input shape
{1, 2.7182818, 0.5, 2}, // Input values
{1.0f, 2.7182818f, 0.5f, 2.0f}, // Input values
{2, 2, 2}, // Output shape
{0, 1, -0.6931472, 0.6931472}, // Output values
{0.0f, 1.0f, -0.6931472f, 0.6931472f}, // Output values
output_data);
}
@ -208,9 +211,9 @@ TF_LITE_MICRO_TEST(Sqrt) {
tflite::testing::TestElementwiseFloat(
tflite::BuiltinOperator_SQRT, // SQRT operator
{2, 2, 2}, // Input shape
{0, 1, 2, 4}, // Input values
{0.0f, 1.0f, 2.0f, 4.0f}, // Input values
{2, 2, 2}, // Output shape
{0, 1, 1.41421, 2}, // Output values
{0.0f, 1.0f, 1.41421f, 2.0f}, // Output values
output_data);
}
@ -220,9 +223,9 @@ TF_LITE_MICRO_TEST(Rsqrt) {
tflite::testing::TestElementwiseFloat(
tflite::BuiltinOperator_RSQRT, // RSQRT operator
{2, 2, 2}, // Input shape
{1, 2, 4, 9}, // Input values
{1.0f, 2.0f, 4.0f, 9.0f}, // Input values
{2, 2, 2}, // Output shape
{1, 0.7071, 0.5, 0.33333}, // Output values
{1.0f, 0.7071f, 0.5f, 0.33333f}, // Output values
output_data);
}
@ -232,9 +235,9 @@ TF_LITE_MICRO_TEST(Square) {
tflite::testing::TestElementwiseFloat(
tflite::BuiltinOperator_SQUARE, // SQARE operator
{2, 2, 2}, // Input shape
{1, 2, 0.5, -3.0}, // Input values
{1.0f, 2.0f, 0.5f, -3.0f}, // Input values
{2, 2, 2}, // Output shape
{1, 4.0, 0.25, 9.0}, // Output values
{1.0f, 4.0f, 0.25f, 9.0f}, // Output values
output_data);
}
@ -250,4 +253,20 @@ TF_LITE_MICRO_TEST(LogicalNot) {
output_data);
}
TF_LITE_MICRO_TEST(TANH) {
constexpr int output_dims_count = 4;
float output_data[output_dims_count];
tflite::testing::TestElementwiseFloat(
tflite::BuiltinOperator_TANH, // TANH operator
{ 2, 2, 2 }, // Input shape
{ 0.0f, 50.0f, 0.5f, -50.0f }, // Input values
{ 2, 2, 2 }, // Output shape
{ 0.0f, 1.0f, 0.462117f, -1.0f }, // Output values
output_data);
}
TF_LITE_MICRO_TESTS_END

View File

@ -81,6 +81,7 @@ TfLiteRegistration* Register_SUB();
TfLiteRegistration* Register_SVDF();
TfLiteRegistration* Register_UNPACK();
TfLiteRegistration* Register_L2_NORMALIZATION();
TfLiteRegistration* Register_TANH();
} // namespace micro
} // namespace ops