diff --git a/src/basicnet.cpp b/src/basicnet.cpp index 02bd67d..67666ac 100644 --- a/src/basicnet.cpp +++ b/src/basicnet.cpp @@ -1,134 +1,135 @@ #include #include #include #include #include "basicnet.h" #define NB_CHNS 4 #define L2_K 0.1 #define L2_B 0.1 #define L2_A 0.0 #define DROPOUT_RATE 0.5 BasicNet::BasicNet() { using namespace eddl; /* ######################################################### ######################################################### ######################################################### ######################################################### - # REGULARIZERS ARE AVAILABLE, BUT AS SIMPLE LAYERS: NOT AS PARAMETERS KERNEL_REGULARIZER AND BIAS_REGULARIZER: WHERE DO I PUT THE LAYERS FOR THEM TO HAVE THE SAME EFFECT? - + # BIAS REGULARIZERS NOT AVAILABLE + # FOR LAYERS WHICH HAVE BATCHNORM AFTER, NO BIAS REGULARISER IS NEEDED: BIAS IS REMOVED IN THE OUTPUT BY BATCHNORM, WHATEVER THE BIAS VALUE + # FUNCTION TRAIN UNDERWENT MAJOR (?) CHANGES: fit function is much simpler than keras one # Maybe implemented in the futre with "fine-grained training" */ layer in_ = Input({1, NB_CHNS, 1280}); layer l = in_; // l = Conv(l, 16, {3, 5}, data_format="channels_last", kernel_initialiser='glorot_uniform', bias_initialiser='zeros', kernel_regularizer=L2(l2_k), bias_regularizer=L2(l2_b)) - l = Conv(l, 16, {3, 5}, {1,1}, "same"); + l = L2(GlorotUniform(Conv(l, 16, {3, 5}, {1,1}, "same")), L2_K); l = BatchNormalization(l, 0.99, 0.001); l = Activation(l, "relu"); l = MaxPool(l, {1,2}, {1,2}, "same"); // l = Conv(l, 32, {3, 3}, {1,1}, data_format="channels_last", kernel_initialiser='glorot_uniform', bias_initialiser='zeros', kernel_regularizer=L2(l2_k), bias_regularizer=L2(l2_b)); - l = Conv(l, 32, {3, 3}, {1,1}, "same"); + l = L2(GlorotUniform(Conv(l, 32, {3, 3}, {1,1}, "same")), L2_K); l = BatchNormalization(l, 0.99, 0.001); l = Activation(l, "relu"); l = MaxPool(l, {1,2}, {1,2}, "same"); // l = Conv(l, 32, {3, 3}, {2,2}, data_format="channels_last", kernel_initialiser='glorot_uniform', bias_initialiser='zeros', kernel_regularizer=L2(l2_k), bias_regularizer=L2(l2_b)); - l = Conv(l, 32, {3, 3}, {2,2}, "same"); + l = L2(GlorotUniform(Conv(l, 32, {3, 3}, {2,2}, "same")), L2_K); l = BatchNormalization(l, 0.99, 0.001); l = Activation(l, "relu"); l = Dropout(l, DROPOUT_RATE); l = Flatten(l); // l = Dense(l, 64, kernel_initialiser='glorot_uniform', bias_initialiser='zeros', kernel_regularizer=L2(l2_k), bias_regularizer=L2(l2_b)); - l = Dense(l, 64); + l = L2(GlorotUniform(Dense(l, 64)), L2_K); l = Activation(l, "relu"); l = Dropout(l, DROPOUT_RATE); // l = Dense(l, 1, kernel_initialiser='glorot_uniform', bias_initialiser='zeros'); - l = Dense(l, 1); + l = GlorotUniform(Dense(l, 1)); l = Activation(l, "sigmoid"); layer out_ = l; net = Model({in_}, {out_}); build(net, sgd(0.01, 0.9, 0.0, true), {"soft_cross_entropy"}, {"categorical_accuracy"}, CS_CPU(4, "full_mem")); summary(net); } void BasicNet::fit(Tensor* x_train, Tensor* y_train, Tensor* x_val, Tensor* y_val, int batch_size, int epochs, float learning_rate) { x_train = Tensor::moveaxis(x_train, 3, 1); eddl::setlr(net, {learning_rate}); // THIS DOES NOT WORK... WAITING FOR ISSUE TO BE SOLVED /* // Format: batch_size, n_channels, height, width std::vector x_shape = eddlT::getShape(x_train); int num_batches = x_shape[0] / batch_size; Tensor* x_batch = eddlT::create({batch_size, x_shape[1], x_shape[2], x_shape[3]}); Tensor* y_batch = eddlT::create({batch_size}); Loss* loss = eddl::getLoss("soft_cross_entropy"); Metric* metric = eddl::getMetric("categorical_accuracy"); for(int epoch=0; epoch