19 #ifndef SINGA_MODEL_LAYER_H_ 20 #define SINGA_MODEL_LAYER_H_ 27 #include "singa/core/tensor.h" 28 #include "singa/proto/model.pb.h" 29 #include "singa/utils/factory.h" 33 typedef vector<size_t> Shape;
43 void Setup(
const Shape& in_shape,
const string& proto_str) {
45 conf.ParseFromString(proto_str);
46 this->
Setup(in_shape, conf);
50 void Setup(
const vector<Shape>& in_shapes,
const string& proto_str) {
52 conf.ParseFromString(proto_str);
53 this->
Setup(in_shapes, conf);
63 virtual const std::string
layer_type()
const {
return "Unknown"; }
72 virtual void Setup(
const Shape& in_sample,
const LayerConf& conf) {
78 virtual void Setup(
const vector<Shape>& in_samples,
const LayerConf& conf) {
85 LOG(FATAL) <<
"Pls override this function";
86 return vector<size_t>{};
91 LOG(FATAL) <<
"Pls override this function";
92 return vector<size_t>{};
104 LOG(FATAL) <<
"Not implemented";
114 virtual const vector<Tensor>
Forward(
int flag,
const vector<Tensor>& inputs) {
116 if (inputs.size() == 1) ret.push_back(
Forward(flag, inputs.at(0)));
118 LOG(FATAL) <<
"Not implemented";
132 virtual const std::pair<Tensor, vector<Tensor>>
Backward(
int flag,
134 LOG(FATAL) <<
"Not implemented!";
136 return std::make_pair(t, vector<Tensor>{});
141 virtual const std::pair<vector<Tensor>, vector<Tensor>>
Backward(
142 int flag,
const vector<Tensor>& grads) {
143 vector<Tensor> input_grad, param_grad;
144 if (grads.size() == 1u) {
145 auto ret =
Backward(flag, grads.at(0));
146 input_grad.push_back(ret.first);
147 param_grad = ret.second;
149 LOG(FATAL) <<
"Not implemented";
151 return std::make_pair(input_grad, param_grad);
159 virtual void ToDevice(std::shared_ptr<Device> device) {
184 conf.SerializeToString(&str);
193 CHECK_LT(i, param_specs_.size());
194 return param_specs_.at(i);
199 return vector<Tensor>{};
204 vector<string> pname;
205 for (
const auto& spec : param_specs_) pname.push_back(spec.name());
211 CHECK_LT(i, param_specs_.size());
212 return param_specs_.at(i).name();
217 const std::string
name()
const {
return name_; }
221 vector<ParamSpec> param_specs_;
235 #define RegisterLayerClass(Name, SubLayer) \ 236 static Registra<Layer, SubLayer> Name##SubLayer(#Name); 238 inline std::shared_ptr<Layer> CreateLayer(
const std::string type) {
243 inline const std::vector<std::string> GetRegisteredLayers() {
244 vector<std::string> ret;
246 auto layer = CreateLayer(type);
247 ret.push_back(
"Register type: " + type);
252 #endif // SINGA_MODEL_LAYER_H_ const string & param_name(size_t i)
Return the 'i'-th parameter name.
Definition: layer.h:210
const std::string name() const
Each layer instance would optionally have a name.
Definition: layer.h:217
const vector< string > param_names()
Return names of all parmaeters.
Definition: layer.h:203
virtual void ToProto(LayerConf *conf) const
Serialize the layer info (including params) into a LayerConf proto message.
Definition: layer.h:167
virtual const std::string layer_type() const
Each layer sub-class would optionaly have a type name.
Definition: layer.h:63
void Setup(const Shape &in_shape, const string &proto_str)
Set meta data fields from a string representing a proto message.
Definition: layer.h:43
const ParamSpec & param_specs(size_t i)
Return the i-th ParamSpec.
Definition: layer.h:192
virtual const Shape GetOutputSampleShape() const
Return the shape of the generated Tensor without the batchsize dimension.
Definition: layer.h:84
Factory template to generate class (or a sub-class) object based on id.
Definition: factory.h:45
virtual const std::pair< Tensor, vector< Tensor > > Backward(int flag, const Tensor &grad)
Compute gradients of this layer.
Definition: layer.h:132
std::string ToProtoStr() const
Serialize the layer info, including params_, into a string representing a LayerParameter message...
Definition: layer.h:180
virtual ~Layer()
Destruct objects created by this layer.
Definition: layer.h:59
virtual void ToDevice(std::shared_ptr< Device > device)
Clone the layer to the given device.
Definition: layer.h:159
virtual void Setup(const Shape &in_sample, const LayerConf &conf)
Set meta data fields configured in 'conf' (a proto message).
Definition: layer.h:72
virtual const Shape GetOutputSampleShape(int k)
Return the shape of the k-th generated tensor without the batchsize dimension.
Definition: layer.h:90
virtual const vector< Tensor > param_values()
Return pointers to parameter Tensor s.
Definition: layer.h:198
virtual void Setup(const vector< Shape > &in_samples, const LayerConf &conf)
Used for layers that have multiple input tensors, e.g., concatenate layer.
Definition: layer.h:78
const vector< ParamSpec > param_specs()
Return specs/configuration of all parameter instances of this layer.
Definition: layer.h:189
A Tensor instance is a multi-dimensional array resident on a Device (default device is the host CPU)...
Definition: tensor.h:56
virtual const vector< Tensor > Forward(int flag, const vector< Tensor > &inputs)
Do feature transformation for the given 'input' tensor (denoted as x).
Definition: layer.h:114
void Setup(const vector< Shape > &in_shapes, const string &proto_str)
'in_shapes' is the shape of the input feature for one sample
Definition: layer.h:50
virtual void AsType(DataType dtype)
Set the data type of Tensor in this layer.
Definition: layer.h:163
The base layer class.
Definition: layer.h:37
virtual const Tensor Forward(int flag, const Tensor &input)
Do feature transformation for the given 'input' tensor (denoted as x).
Definition: layer.h:103
Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements...
Definition: common.h:48
virtual const std::pair< vector< Tensor >, vector< Tensor > > Backward(int flag, const vector< Tensor > &grads)
Definition: layer.h:141