Skip to content

Commit 885a556

Browse files
AndreySorokin7AndreySorokin7
andauthored
Connect layers implementation with graph model (#130)
Closes #33 --------- Co-authored-by: AndreySorokin7 <andrey_sorokin_nn@mail,ru>
1 parent c9d9033 commit 885a556

12 files changed

Lines changed: 171 additions & 146 deletions

File tree

include/graph/graph.hpp

Lines changed: 25 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -5,49 +5,18 @@
55
#include <string>
66
#include <vector>
77

8-
namespace itlab_2023 {
9-
10-
enum LayerType {
11-
kInput,
12-
kPooling,
13-
kNormalization,
14-
kDropout,
15-
kElementWise,
16-
kConvolution,
17-
kFullyConnected,
18-
kOutput
19-
};
20-
21-
class LayerExample {
22-
private:
23-
int id_;
24-
std::string name_;
25-
LayerType type_;
26-
std::string version_;
27-
int numInputs_;
28-
int numNeurons_;
29-
std::vector<int> primer_;
8+
#include "layers/Layer.hpp"
309

31-
public:
32-
LayerExample(LayerType type1) : type_(type1) {}
33-
LayerType getType() { return type_; }
34-
int getNumInputs() const { return numInputs_; }
35-
int getNumNeurons() const { return numNeurons_; }
36-
int checkID() const { return id_; }
37-
void giveID(int id1) { id_ = id1; }
38-
void In(const std::vector<int>& a) { primer_ = a; }
39-
void Work() {}
40-
std::vector<int> Out() { return primer_; }
41-
};
10+
namespace itlab_2023 {
4211

4312
class Graph {
4413
int BiggestSize_;
4514
int V_;
46-
std::vector<LayerExample> layers_;
15+
std::vector<Layer*> layers_;
4716
std::vector<int> arrayV_;
4817
std::vector<int> arrayE_;
49-
std::vector<int> startvec_;
50-
std::vector<int>* outvector_;
18+
Tensor inten_;
19+
Tensor* outten_;
5120
int start_;
5221
int end_;
5322

@@ -59,35 +28,34 @@ class Graph {
5928
arrayV_.push_back(0);
6029
V_ = 0;
6130
}
62-
void setInput(LayerExample& lay, const std::vector<int>& vec) {
63-
lay.giveID(0);
64-
layers_.push_back(lay);
31+
void setInput(Layer& lay, Tensor& vec) {
32+
lay.setID(0);
33+
layers_.push_back(&lay);
6534
arrayV_.push_back(0);
66-
startvec_ = vec;
67-
start_ = lay.checkID();
35+
inten_ = vec;
36+
start_ = lay.getID();
6837
V_++;
6938
}
70-
void makeConnection(const LayerExample& layPrev, LayerExample& layNext) {
71-
layNext.giveID(V_);
72-
layers_.push_back(layNext);
39+
void makeConnection(const Layer& layPrev, Layer& layNext) {
40+
layNext.setID(V_);
41+
layers_.push_back(&layNext);
7342
arrayV_[V_] = arrayV_[V_ - 1];
7443
arrayV_.push_back(static_cast<int>(arrayE_.size()));
75-
if (layPrev.checkID() == layNext.checkID()) {
44+
if (layPrev.getID() == layNext.getID()) {
7645
throw std::out_of_range("i=j cant add edge");
7746
}
7847
for (int ind = 1; ind < static_cast<int>(arrayV_.size()) -
79-
static_cast<int>(layPrev.checkID()) - 1;
48+
static_cast<int>(layPrev.getID()) - 1;
8049
ind++)
81-
arrayV_[layPrev.checkID() + ind]++;
82-
arrayE_.insert(arrayE_.begin() + arrayV_[layPrev.checkID()],
83-
layNext.checkID());
50+
arrayV_[layPrev.getID() + ind]++;
51+
arrayE_.insert(arrayE_.begin() + arrayV_[layPrev.getID()], layNext.getID());
8452
V_++;
8553
arrayV_[V_] = static_cast<int>(arrayE_.size());
8654
}
87-
bool areLayerNext(const LayerExample& layPrev, const LayerExample& layNext) {
88-
for (int i = arrayV_[layPrev.checkID()]; i < arrayV_[layPrev.checkID() + 1];
55+
bool areLayerNext(const Layer& layPrev, const Layer& layNext) {
56+
for (int i = arrayV_[layPrev.getID()]; i < arrayV_[layPrev.getID() + 1];
8957
i++) {
90-
if (arrayE_[i] == layNext.checkID()) {
58+
if (arrayE_[i] == layNext.getID()) {
9159
return true;
9260
}
9361
}
@@ -122,15 +90,13 @@ class Graph {
12290
}
12391
}
12492
for (int i : traversal) {
125-
layers_[i].In(startvec_);
126-
layers_[i].Work();
127-
startvec_ = layers_[i].Out();
93+
layers_[i]->run(inten_, *outten_);
94+
inten_ = *outten_;
12895
}
129-
outvector_->assign(startvec_.begin(), startvec_.end());
13096
}
131-
void setOutput(const LayerExample& lay, std::vector<int>& vec) {
132-
end_ = lay.checkID();
133-
outvector_ = &vec;
97+
void setOutput(const Layer& lay, Tensor& vec) {
98+
end_ = lay.getID();
99+
outten_ = &vec;
134100
}
135101
};
136102
} // namespace itlab_2023

include/layers/ConvLayer.hpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,18 @@ class ConvolutionalLayer : public Layer {
1212
size_t stride_;
1313
size_t pads_;
1414
size_t dilations_;
15+
Tensor kernel_;
1516

1617
public:
1718
ConvolutionalLayer() = default;
18-
ConvolutionalLayer(size_t step, size_t pads, size_t dilations) {
19+
ConvolutionalLayer(size_t step, size_t pads, size_t dilations,
20+
const Tensor& kernel) {
1921
stride_ = step;
2022
pads_ = pads;
2123
dilations_ = dilations;
24+
kernel_ = kernel;
2225
}
23-
void run(const Tensor& input, Tensor& output, const Tensor& kernel_) const;
26+
void run(const Tensor& input, Tensor& output) override;
2427
};
2528

2629
template <typename ValueType>

include/layers/FCLayer.hpp

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,18 @@
88
namespace itlab_2023 {
99

1010
class FCLayer : public Layer {
11+
private:
12+
Tensor weights_;
13+
Tensor bias_;
14+
1115
public:
1216
FCLayer() = default;
17+
FCLayer(const Tensor& weights, const Tensor& bias) {
18+
weights_ = weights;
19+
bias_ = bias;
20+
}
1321
static std::string get_name() { return "Fully-connected layer"; }
14-
static void run(const Tensor& input, Tensor& output, const Tensor& weights,
15-
const Tensor& bias);
22+
void run(const Tensor& input, Tensor& output) override;
1623
};
1724

1825
template <typename ValueType>

include/layers/InputLayer.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ class InputLayer : public Layer {
2626
mean_ = mean;
2727
std_ = std;
2828
} // layout = kNchw(0), kNhwc(1)
29-
void run(Tensor& input, Tensor& output) const {
29+
void run(const Tensor& input, Tensor& output) override {
3030
switch (input.get_type()) {
3131
case Type::kInt: {
3232
std::vector<int> in = *input.as<int>();

include/layers/Layer.hpp

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,29 @@
1010

1111
namespace itlab_2023 {
1212

13+
enum LayerType {
14+
kInput,
15+
kPooling,
16+
kNormalization,
17+
kDropout,
18+
kElementWise,
19+
kConvolution,
20+
kFullyConnected,
21+
kOutput,
22+
};
23+
1324
class Layer {
1425
public:
1526
Layer() = default;
27+
int getID() const { return id_; }
28+
void setID(int id) { id_ = id; }
29+
LayerType getName() const { return type_; }
30+
void setName(LayerType type) { type_ = type; }
31+
virtual void run(const Tensor& input, Tensor& output) = 0;
32+
33+
private:
34+
int id_;
35+
LayerType type_;
1636
};
1737

1838
template <typename ValueType>

include/layers/OutputLayer.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ class OutputLayer : public Layer {
1111
OutputLayer() = default;
1212
OutputLayer(const std::vector<std::string>& labels) : labels_(labels) {}
1313
static std::string get_name() { return "Output layer"; }
14-
static void run(const Tensor& input, Tensor& output) { output = input; }
14+
void run(const Tensor& input, Tensor& output) override { output = input; }
1515
std::vector<std::string> get_labels() const { return labels_; }
1616
std::pair<std::vector<std::string>, Tensor> top_k(const Tensor& input,
1717
size_t k) const;

src/layers/ConvLayer.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
#include "layers/ConvLayer.hpp"
22
namespace itlab_2023 {
33

4-
void ConvolutionalLayer::run(const Tensor& input, Tensor& output,
5-
const Tensor& kernel_) const {
4+
void ConvolutionalLayer::run(const Tensor& input, Tensor& output) {
65
switch (input.get_type()) {
76
case Type::kInt: {
87
ConvImpl<int> used_impl(

src/layers/FCLayer.cpp

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,25 +2,24 @@
22

33
namespace itlab_2023 {
44

5-
void FCLayer::run(const Tensor& input, Tensor& output, const Tensor& weights,
6-
const Tensor& bias) {
7-
if (input.get_type() != weights.get_type()) {
5+
void FCLayer::run(const Tensor& input, Tensor& output) {
6+
if (input.get_type() != weights_.get_type()) {
87
throw std::invalid_argument("Input and weights data type aren't same");
98
}
10-
if (bias.get_type() != weights.get_type()) {
9+
if (bias_.get_type() != weights_.get_type()) {
1110
throw std::invalid_argument("Bias and weights data type aren't same");
1211
}
1312
switch (input.get_type()) {
1413
case Type::kInt: {
15-
FCLayerImpl<int> used_impl(*weights.as<int>(), weights.get_shape(),
16-
*bias.as<int>());
14+
FCLayerImpl<int> used_impl(*weights_.as<int>(), weights_.get_shape(),
15+
*bias_.as<int>());
1716
output = make_tensor(used_impl.run(*input.as<int>()),
1817
used_impl.get_output_shape());
1918
break;
2019
}
2120
case Type::kFloat: {
22-
FCLayerImpl<float> used_impl(*weights.as<float>(), weights.get_shape(),
23-
*bias.as<float>());
21+
FCLayerImpl<float> used_impl(*weights_.as<float>(), weights_.get_shape(),
22+
*bias_.as<float>());
2423
output = make_tensor(used_impl.run(*input.as<float>()),
2524
used_impl.get_output_shape());
2625
break;

0 commit comments

Comments
 (0)