1 /*
2 * Copyright (C) 2017 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #include "common/embedding-network.h"
18 #include "common/embedding-network-params-from-proto.h"
19 #include "common/embedding-network.pb.h"
20 #include "common/simple-adder.h"
21
22 #include "gmock/gmock.h"
23 #include "gtest/gtest.h"
24
25 namespace libtextclassifier {
26 namespace nlp_core {
27 namespace {
28
29 using testing::ElementsAreArray;
30
31 class TestingEmbeddingNetwork : public EmbeddingNetwork {
32 public:
33 using EmbeddingNetwork::EmbeddingNetwork;
34 using EmbeddingNetwork::FinishComputeFinalScoresInternal;
35 };
36
DiagonalAndBias3x3(int diagonal_value,int bias_value,MatrixParams * weights,MatrixParams * bias)37 void DiagonalAndBias3x3(int diagonal_value, int bias_value,
38 MatrixParams* weights, MatrixParams* bias) {
39 weights->set_rows(3);
40 weights->set_cols(3);
41 weights->add_value(diagonal_value);
42 weights->add_value(0);
43 weights->add_value(0);
44 weights->add_value(0);
45 weights->add_value(diagonal_value);
46 weights->add_value(0);
47 weights->add_value(0);
48 weights->add_value(0);
49 weights->add_value(diagonal_value);
50
51 bias->set_rows(3);
52 bias->set_cols(1);
53 bias->add_value(bias_value);
54 bias->add_value(bias_value);
55 bias->add_value(bias_value);
56 }
57
TEST(EmbeddingNetworkTest,IdentityThroughMultipleLayers)58 TEST(EmbeddingNetworkTest, IdentityThroughMultipleLayers) {
59 std::unique_ptr<EmbeddingNetworkProto> proto;
60 proto.reset(new EmbeddingNetworkProto);
61
62 // These layers should be an identity with bias.
63 DiagonalAndBias3x3(/*diagonal_value=*/1, /*bias_value=*/1,
64 proto->add_hidden(), proto->add_hidden_bias());
65 DiagonalAndBias3x3(/*diagonal_value=*/1, /*bias_value=*/2,
66 proto->add_hidden(), proto->add_hidden_bias());
67 DiagonalAndBias3x3(/*diagonal_value=*/1, /*bias_value=*/3,
68 proto->add_hidden(), proto->add_hidden_bias());
69 DiagonalAndBias3x3(/*diagonal_value=*/1, /*bias_value=*/4,
70 proto->add_hidden(), proto->add_hidden_bias());
71 DiagonalAndBias3x3(/*diagonal_value=*/1, /*bias_value=*/5,
72 proto->mutable_softmax(), proto->mutable_softmax_bias());
73
74 EmbeddingNetworkParamsFromProto params(std::move(proto));
75 TestingEmbeddingNetwork network(¶ms);
76
77 std::vector<float> input({-2, -1, 0});
78 std::vector<float> output;
79 network.FinishComputeFinalScoresInternal<SimpleAdder>(
80 VectorSpan<float>(input), &output);
81
82 EXPECT_THAT(output, ElementsAreArray({14, 14, 15}));
83 }
84
85 } // namespace
86 } // namespace nlp_core
87 } // namespace libtextclassifier
88