-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTestSequentialLayer.cpp
More file actions
115 lines (91 loc) · 2.96 KB
/
TestSequentialLayer.cpp
File metadata and controls
115 lines (91 loc) · 2.96 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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
#include <filesystem>
#include <memory>
#include <vector>
#include <Layers/SequentialLayer.h>
#include <gtest/gtest.h>
#include <stddef.h>
#include "AutoDiff/GraphNodes.hpp"
#include "DummyLayer.hpp"
#include "Layers/BaseLayer.h"
#include "LayersTestFixture.hpp"
#include "MLCore/BasicTensor.h"
#include "Utilities.hpp"
namespace
{
class TestSequentialLayer : public testUtilities::fixtures::LayersTestFixture
{
protected:
layers::BaseLayerPtr _createLayerInstance() override
{
return std::make_shared<layers::SequentialLayer>(
"TestSequentialLayer",
std::vector<layers::BaseLayerPtr>{
std::make_shared<testUtilities::fixtures::DummyLayer>("DummyLayer1", 10),
std::make_shared<testUtilities::fixtures::DummyLayer>("DummyLayer2", 10),
std::make_shared<testUtilities::fixtures::DummyLayer>("DummyLayer3", 10),
});
}
void _testBuildingLayer() override
{
const std::vector<mlCore::TensorShape> inputShapes = {mlCore::TensorShape({32, 10})};
_layer->build(inputShapes);
ASSERT_EQ(_layer->getOutputShape(), mlCore::TensorShape({32, 10}));
const auto weights = _layer->getTrainableWeights();
ASSERT_EQ(weights.size(), 3);
for(size_t i = 0; i < 3; ++i)
{
ASSERT_EQ(weights[i]->getOutputShape(), mlCore::TensorShape({10}));
}
}
void _testSavingAndLoadingWeights() override
{
const std::filesystem::path weightsPath = testUtilities::createTempFile();
_layer->build({mlCore::TensorShape({32, 10})});
const auto originalWeights = _layer->getTrainableWeights();
{
_layer->saveWeights(weightsPath);
ASSERT_TRUE(testUtilities::areSavedWeightsAsExpected(weightsPath / "DummyLayer1",
{originalWeights[0]->getValue()}));
ASSERT_TRUE(testUtilities::areSavedWeightsAsExpected(weightsPath / "DummyLayer2",
{originalWeights[1]->getValue()}));
ASSERT_TRUE(testUtilities::areSavedWeightsAsExpected(weightsPath / "DummyLayer3",
{originalWeights[2]->getValue()}));
}
_layer = _createLayerInstance();
_layer->build({mlCore::TensorShape({32, 10})});
{
_layer->loadWeights(weightsPath);
const auto newWeights = _layer->getTrainableWeights();
for(size_t i = 0; i < 3; ++i)
{
ASSERT_TRUE(testUtilities::areTensorsEqual(originalWeights[i]->getValue(),
newWeights[i]->getValue()));
}
}
}
void _testCalling() override
{
const auto input = std::make_shared<autoDiff::Constant>(mlCore::Tensor{mlCore::TensorShape{32, 10}});
_layer->build({input->getOutputShape()});
const auto output = _layer->call({input});
const mlCore::TensorShape expectedOutputShape{32, 10};
ASSERT_EQ(output->getOutputShape(), expectedOutputShape);
}
};
} // namespace
TEST_F(TestSequentialLayer, PreventsCallsOnNotBuiltLayer)
{
_testPreventingCallsOnNotBuiltLayer();
}
TEST_F(TestSequentialLayer, BuildsLayer)
{
_testBuildingLayer();
}
TEST_F(TestSequentialLayer, SavesAndLoadsWeights)
{
_testSavingAndLoadingWeights();
}
TEST_F(TestSequentialLayer, IsCalledCorrectly)
{
_testCalling();
}