ceras
yet another deep learning engine
layer.hpp
Go to the documentation of this file.
1 #ifndef NLESIGQPSASUTOXPLGXCUHFGGUGYSWLQQFATNISJOSPUFHRORXBNXLSWTYRNSIWJKYFXIQXVN
2 #define NLESIGQPSASUTOXPLGXCUHFGGUGYSWLQQFATNISJOSPUFHRORXBNXLSWTYRNSIWJKYFXIQXVN
3 
4 #include "./operation.hpp"
5 #include "./activation.hpp"
6 #include "./loss.hpp"
7 #include "./optimizer.hpp"
8 #include "./utils/better_assert.hpp"
9 
10 // try to mimic classes defined in tensorflow.keras
11 
12 namespace ceras
13 {
14 
15  inline auto Input()
16  {
18  }
19 
44  inline auto Conv2D( unsigned long output_channels, std::vector<unsigned long> const& kernel_size,
45  std::vector<unsigned long> const& input_shape, std::string const& padding="valid",
46  std::vector<unsigned long> const& strides={1,1}, std::vector<unsigned long> const& dilations={1, 1}, bool use_bias=true,
47  float kernel_regularizer_l1=0.0f, float kernel_regularizer_l2=0.0f, float bias_regularizer_l1=0.0f, float bias_regularizer_l2=0.0f
48  )
49  {
50  better_assert( output_channels > 0, "Expecting output_channels larger than 0." );
51  better_assert( kernel_size.size() > 0, "Expecting kernel_size at least has 1 elements." );
52  better_assert( input_shape.size() ==3, "Expecting input_shape has 3 elements." );
53  better_assert( strides.size() > 0, "Expecting strides at least has 1 elements." );
54  return [=]<Expression Ex>( Ex const& ex )
55  {
56  unsigned long const kernel_size_x = kernel_size[0];
57  unsigned long const kernel_size_y = kernel_size.size() == 2 ? kernel_size[1] : kernel_size[0];
58  //unsigned long const kernel_size_y = kernel_size[1];
59  unsigned long const input_channels = input_shape[2];
60  unsigned long const input_x = input_shape[0];
61  unsigned long const input_y = input_shape[1];
62  unsigned long const stride_x = strides[0];
63  unsigned long const stride_y = strides.size() == 2 ? strides[1] : strides[0];
64  unsigned long const dilation_row = dilations[0];
65  unsigned long const dilation_col = dilations.size() == 2 ? dilations[1] : dilations[0];
66  //unsigned long const stride_y = strides[1];
67  auto w = variable<tensor<float>>{ glorot_uniform<float>({output_channels, kernel_size_x, kernel_size_y, input_channels}), kernel_regularizer_l1, kernel_regularizer_l2 };
68  auto b = variable<tensor<float>>{ zeros<float>({1, 1, output_channels}), bias_regularizer_l1, bias_regularizer_l2, use_bias };
69  return conv2d( input_x, input_y, stride_x, stride_y, dilation_row, dilation_col, padding )( ex, w ) + b;
70  };
71  }
72 
92  inline auto Dense( unsigned long output_size, unsigned long input_size, bool use_bias=true, float kernel_regularizer_l1=0.0f, float kernel_regularizer_l2=0.0f, float bias_regularizer_l1=0.0f, float bias_regularizer_l2=0.0f )
93  {
94  return [=]<Expression Ex>( Ex const& ex )
95  {
96  auto w = variable<tensor<float>>{ glorot_uniform<float>({input_size, output_size}), kernel_regularizer_l1, kernel_regularizer_l2 };
97  auto b = variable<tensor<float>>{ zeros<float>({1, output_size}), bias_regularizer_l1, bias_regularizer_l2, use_bias }; // if use_baias, then b is trainable; otherwise, non-trainable.
98  return ex * w + b;
99  };
100  }
101 
117  inline auto BatchNormalization( std::vector<unsigned long> const& shape, float threshold = 0.95f, float kernel_regularizer_l1=0.0f, float kernel_regularizer_l2=0.0f, float bias_regularizer_l1=0.0f, float bias_regularizer_l2=0.0f )
118  {
119  return [=]<Expression Ex>( Ex const& ex )
120  {
121  unsigned long const last_dim = *(shape.rbegin());
122  auto gamma = variable{ ones<float>( {last_dim, } ), kernel_regularizer_l1, kernel_regularizer_l2 };
123  auto beta = variable{ zeros<float>( {last_dim, } ), bias_regularizer_l1, bias_regularizer_l2 };
124  return batch_normalization( threshold )( ex, gamma, beta );
125  };
126  }
127 
128  inline auto BatchNormalization( float threshold, std::vector<unsigned long> const& shape, float kernel_regularizer_l1=0.0f, float kernel_regularizer_l2=0.0f, float bias_regularizer_l1=0.0f, float bias_regularizer_l2=0.0f )
129  {
130  return BatchNormalization( shape, threshold, kernel_regularizer_l1, kernel_regularizer_l2, bias_regularizer_l1, bias_regularizer_l2 );
131  }
132 
133 #if 0
134  // TODO: fix this layer
135  inline auto LayerNormalization( std::vector<unsigned long> const& shape )
136  {
137  return [=]<Expression Ex>( Ex const& ex )
138  {
139  unsigned long const last_dim = *(shape.rbegin());
140  auto gamma = variable<tensor<float>>{ ones<float>( {last_dim, } ) };
141  auto beta = variable<tensor<float>>{ zeros<float>( {last_dim, } ) };
142  return layer_normalization()( ex, gamma, beta );
143  };
144  }
145 #endif
146 
158  inline auto Concatenate(unsigned long axis = -1) noexcept
159  {
160  return [=]<Expression Lhs_Expression, Expression Rhs_Expression>( Lhs_Expression const& lhs_ex, Rhs_Expression const& rhs_ex ) noexcept
161  {
162  return concatenate( axis )( lhs_ex, rhs_ex );
163  };
164  }
165 
178  inline auto Add() noexcept
179  {
180  return []<Expression Lhs_Expression, Expression Rhs_Expression>( Lhs_Expression const& lhs_ex, Rhs_Expression const& rhs_ex ) noexcept
181  {
182  return lhs_ex + rhs_ex;
183  };
184  }
185 
186 
199  inline auto Subtract() noexcept
200  {
201  return []<Expression Lhs_Expression, Expression Rhs_Expression>( Lhs_Expression const& lhs_ex, Rhs_Expression const& rhs_ex ) noexcept
202  {
203  return lhs_ex - rhs_ex;
204  };
205  }
206 
219  inline auto Multiply() noexcept
220  {
221  return []<Expression Lhs_Expression, Expression Rhs_Expression>( Lhs_Expression const& lhs_ex, Rhs_Expression const& rhs_ex ) noexcept
222  {
223  return hadamard_product( lhs_ex, rhs_ex );
224  };
225  }
226 
230  template< Expression Ex >
231  inline auto ReLU( Ex const& ex ) noexcept
232  {
233  return relu( ex );
234  }
235 
239  inline auto Softmax() noexcept
240  {
241  return []< Expression Ex >( Ex const& ex ) noexcept
242  {
243  return softmax( ex );
244  };
245  }
246 
247 
251  template< typename T = float >
252  inline auto LeakyReLU( T const factor=0.2 ) noexcept
253  {
254  return leaky_relu( factor );
255  }
256 
260  template< typename T = float >
261  inline auto ELU( T const factor=0.2 ) noexcept
262  {
263  return elu( factor );
264  }
265 
266 
270  inline auto Reshape( std::vector<unsigned long> const& new_shape, bool include_batch_flag=true ) noexcept
271  {
272  return reshape( new_shape, include_batch_flag );
273  }
274 
278  inline auto Flatten() noexcept
279  {
280  return []<Expression Ex>( Ex const& ex ) noexcept
281  {
282  return flatten( ex );
283  };
284  }
285 
289  inline auto MaxPooling2D( unsigned long stride ) noexcept
290  {
291  return max_pooling_2d( stride );
292  }
293 
297  inline auto UpSampling2D( unsigned long stride ) noexcept
298  {
299  return up_sampling_2d( stride );
300  }
301 
305  template< typename T >
306  inline auto Dropout( T factor ) noexcept
307  {
308  return drop_out( factor );
309  }
310 
314  inline auto AveragePooling2D( unsigned long stride ) noexcept
315  {
316  return average_pooling_2d( stride );
317  }
318 
319  //
320  // TODO: PReLU
321  //
322 
323 
324 
325 
326 
327 
328 
329 
330 }//namespace f
331 
332 #endif//NLESIGQPSASUTOXPLGXCUHFGGUGYSWLQQFATNISJOSPUFHRORXBNXLSWTYRNSIWJKYFXIQXVN
333 
Definition: activation.hpp:12
auto relu(Ex const &ex) noexcept
Relu function, an unary operator. Returns x if positive, 0 otherwise.
Definition: activation.hpp:259
auto Reshape(std::vector< unsigned long > const &new_shape, bool include_batch_flag=true) noexcept
Definition: layer.hpp:270
auto BatchNormalization(std::vector< unsigned long > const &shape, float threshold=0.95f, float kernel_regularizer_l1=0.0f, float kernel_regularizer_l2=0.0f, float bias_regularizer_l1=0.0f, float bias_regularizer_l2=0.0f)
Applies a transformation that maintains the mean output close to 0 and the output standard deviation ...
Definition: layer.hpp:117
auto Dropout(T factor) noexcept
Definition: layer.hpp:306
Tsor concatenate(Tsor const &lhs, Tsor const &rhs, unsigned long axis=0) noexcept
Definition: tensor.hpp:914
auto Concatenate(unsigned long axis=-1) noexcept
Definition: layer.hpp:158
auto LeakyReLU(T const factor=0.2) noexcept
Definition: layer.hpp:252
auto Softmax() noexcept
Definition: layer.hpp:239
auto MaxPooling2D(unsigned long stride) noexcept
Definition: layer.hpp:289
auto ELU(T const factor=0.2) noexcept
Definition: layer.hpp:261
Tsor reshape(Tsor const &ts, std::vector< unsigned long > const &new_shape)
Definition: tensor.hpp:692
auto Add() noexcept
Definition: layer.hpp:178
requires std::floating_point< T > auto leaky_relu(T const factor=0.2) noexcept
Leaky Rectified Linear function, an unary operator. Returns x if positive, alpha x otherwise....
Definition: activation.hpp:331
concept Expression
A type that represents a unary operator, a binary operator, a variable, a place_holder,...
Definition: operation.hpp:169
auto AveragePooling2D(unsigned long stride) noexcept
Definition: layer.hpp:314
constexpr auto softmax(Ex const &ex) noexcept
Softmax activation function, an unary operator.
Definition: activation.hpp:26
auto Flatten() noexcept
Definition: layer.hpp:278
auto UpSampling2D(unsigned long stride) noexcept
Definition: layer.hpp:297
auto ReLU(Ex const &ex) noexcept
Definition: layer.hpp:231
constexpr auto hadamard_product(Lhs_Expression const &lhs_ex, Rhs_Expression const &rhs_ex) noexcept
Definition: operation.hpp:444
auto Dense(unsigned long output_size, unsigned long input_size, bool use_bias=true, float kernel_regularizer_l1=0.0f, float kernel_regularizer_l2=0.0f, float bias_regularizer_l1=0.0f, float bias_regularizer_l2=0.0f)
Densly-connected layer.
Definition: layer.hpp:92
auto Input()
Definition: layer.hpp:15
requires std::floating_point< T > auto elu(T const alpha=1.0) noexcept
Exponential Linear function, an unary operator. Returns x if positive, alpha* (exp(x)-1) otherwise....
Definition: activation.hpp:376
auto Multiply() noexcept
Definition: layer.hpp:219
auto Conv2D(unsigned long output_channels, std::vector< unsigned long > const &kernel_size, std::vector< unsigned long > const &input_shape, std::string const &padding="valid", std::vector< unsigned long > const &strides={1, 1}, std::vector< unsigned long > const &dilations={1, 1}, bool use_bias=true, float kernel_regularizer_l1=0.0f, float kernel_regularizer_l2=0.0f, float bias_regularizer_l1=0.0f, float bias_regularizer_l2=0.0f)
2D convolution layer.
Definition: layer.hpp:44
auto Subtract() noexcept
Definition: layer.hpp:199
requires std::floating_point< T > auto batch_normalization(T const momentum=0.98) noexcept
Definition: operation.hpp:1499
auto conv2d(unsigned long row_input, unsigned long col_input, unsigned long const row_stride=1, unsigned long const col_stride=1, unsigned long const row_dilation=1, unsigned long const col_dilation=1, std::string const &padding="valid") noexcept
Definition: operation.hpp:994
auto up_sampling_2d(unsigned long stride) noexcept
Definition: operation.hpp:1350
auto average_pooling_2d(unsigned long stride) noexcept
Definition: operation.hpp:1216
auto max_pooling_2d(unsigned long stride) noexcept
Definition: operation.hpp:1197
requires std::floating_point< T > auto drop_out(T const factor) noexcept
Definition: operation.hpp:1044
constexpr auto flatten(Ex const &ex) noexcept
Definition: operation.hpp:782
Definition: place_holder.hpp:24
Definition: variable.hpp:45