57 REQUIRE(layers,
"Layers should not be NULL")
77 REQUIRE(
"i<j",
"i(%i) must be less that j(%i)\n", i, j);
104 int32_t num_inputs = 0;
115 input_indices[k] = i;
144 get_section(m_param_regularizable, i);
147 layer_param_regularizable, sigma);
167 if (output_activations[i]>0.5) labels->
set_label(i, 1);
170 labels->
set_value(output_activations[i], i);
175 float64_t v2 = output_activations[2*i+1];
193 labels_vec[i] = output_activations[i];
267 "Gradient descent momentum (%f) must be >= 0\n",
m_gd_momentum);
269 int32_t training_set_size = inputs.
num_cols;
278 param_updates.
zero();
280 float64_t error_last_time = -1.0, error = -1.0;
286 bool continue_training =
true;
289 for (int32_t i=0; continue_training; i++)
305 m_num_inputs, m_gd_mini_batch_size,
false);
307 for (int32_t k=0; k<n_param; k++)
316 if (layer_gradients.
vlen > 0)
328 error = (1.0-c) * error + c*e;
330 for (int32_t k=0; k<n_param; k++)
338 if (error_last_time!=-1.0)
340 float64_t error_change = (error_last_time-error)/error;
341 if (error_change< m_epsilon && error_change>=0)
343 SG_INFO(
"Gradient Descent Optimization Converged\n");
344 continue_training =
false;
348 SG_INFO(
"Epoch %i: Error = %f\n",i, error);
350 error_last_time = error;
360 int32_t training_set_size = inputs.
num_cols;
367 lbfgs_param.
past = 1;
370 m_lbfgs_temp_inputs = &inputs;
371 m_lbfgs_temp_targets = &targets;
376 &CNeuralNetwork::lbfgs_evaluate,
377 &CNeuralNetwork::lbfgs_progress,
381 m_lbfgs_temp_inputs = NULL;
382 m_lbfgs_temp_targets = NULL;
386 SG_INFO(
"L-BFGS Optimization Converged\n");
390 SG_INFO(
"L-BFGS Max Number of Epochs reached\n");
394 SG_INFO(
"L-BFGS optimization ended with return code %i\n",result);
399 float64_t CNeuralNetwork::lbfgs_evaluate(
void* userdata,
410 *network->m_lbfgs_temp_targets, grad_vector);
413 int CNeuralNetwork::lbfgs_progress(
void* instance,
420 int n,
int k,
int ls)
422 SG_SINFO(
"Epoch %i: Error = %f\n",k, fx);
429 if (layer_gradients.
vlen > 0)
452 for (int32_t i=0; i<=j; i++)
475 for (int32_t i=m_num_layers-1; i>=0; i--)
477 if (i==m_num_layers-1)
479 m_layers, get_section(gradients,i));
560 for (int32_t i=0; i<x.
num_rows; i++)
565 for (int32_t i=0; i<y.
num_rows; i++)
569 for (int32_t i=0; i<y.
num_rows; i++)
588 gradients_numerical[i] = (error_plus-error_minus)/(2*c);
598 sum +=
CMath::abs(gradients_backprop[i]-gradients_numerical[i]);
616 REQUIRE(features != NULL,
"Invalid (NULL) feature pointer\n");
618 "Feature type must be F_DREAL\n");
620 "Feature class must be C_DENSE\n");
624 "Number of features (%i) must match the network's number of inputs "
627 return inputs->get_feature_matrix();
632 REQUIRE(labs != NULL,
"Invalid (NULL) labels pointer\n");
641 "Number of classes (%i) must match the network's number of "
654 targets[i] = (labels_bin->
get_label(i)==1);
660 targets[i*2] = (labels_bin->
get_label(i)==1);
661 targets[i*2+1] = (labels_bin->
get_label(i)==-1);
701 "with more that 2 output neurons\n", lab->
get_name());
706 "with more that 1 output neuron\n", lab->
get_name());
714 REQUIRE(i<m_num_layers && i >= 0,
"Layer index (%i) out of range\n", i);
750 void CNeuralNetwork::init()
770 m_lbfgs_temp_inputs = NULL;
771 m_lbfgs_temp_targets = NULL;
815 "DynamicObjectArray of NeuralNetwork objects",
virtual const char * get_name() const =0
void allocate_confidences_for(int32_t n_classes)
virtual float64_t compute_error(SGMatrix< float64_t > targets)
SGVector< int32_t > m_index_offsets
virtual CBinaryLabels * apply_binary(CFeatures *data)
virtual ELabelType get_label_type() const =0
Real Labels are real-valued labels.
virtual int32_t get_num_parameters()
virtual void initialize_neural_network(float64_t sigma=0.01f)
int32_t lbfgs(int32_t n, float64_t *x, float64_t *ptr_fx, lbfgs_evaluate_t proc_evaluate, lbfgs_progress_t proc_progress, void *instance, lbfgs_parameter_t *_param, lbfgs_adjust_step_t proc_adjust_step)
static int32_t arg_max(T *vec, int32_t inc, int32_t len, T *maxv_ptr=NULL)
int32_t get_num_parameters()
virtual const char * get_name() const
virtual int32_t get_num_labels() const
The class Labels models labels, i.e. class assignments of objects.
float64_t m_l1_coefficient
virtual int32_t get_num_labels() const =0
real valued labels (e.g. for regression, classifier outputs)
float64_t m_gd_error_damping_coeff
multi-class labels 0,1,...
virtual bool train_machine(CFeatures *data=NULL)
SGVector< float64_t > m_params
virtual void compute_gradients(SGVector< float64_t > parameters, SGMatrix< float64_t > targets, CDynamicObjectArray *layers, SGVector< float64_t > parameter_gradients)
virtual int32_t get_num_vectors() const =0
virtual SGMatrix< float64_t > get_activation_gradients()
A generic multi-layer neural network.
virtual void initialize_neural_layer(CDynamicObjectArray *layers, SGVector< int32_t > input_indices)
virtual int32_t get_num_neurons()
SGMatrix< bool > m_adj_matrix
int32_t get_num_elements() const
float64_t get_label(int32_t idx)
SGMatrix< float64_t > features_to_matrix(CFeatures *features)
virtual void disconnect(int32_t i, int32_t j)
Base class for neural network layers.
virtual bool train_gradient_descent(SGMatrix< float64_t > inputs, SGMatrix< float64_t > targets)
virtual void enforce_max_norm(SGVector< float64_t > parameters, float64_t max_norm)
virtual void quick_connect()
virtual SGMatrix< float64_t > get_activations()
virtual float64_t compute_error(SGMatrix< float64_t > inputs, SGMatrix< float64_t > targets)
A generic learning machine interface.
int32_t get_num_classes()
bool set_label(int32_t idx, float64_t label)
float64_t m_dropout_hidden
virtual const char * get_name() const
SGVector< bool > m_param_regularizable
float64_t m_dropout_input
virtual CMulticlassLabels * apply_multiclass(CFeatures *data)
Multiclass Labels for multi-class classification.
float64_t m_l2_coefficient
virtual void initialize_parameters(SGVector< float64_t > parameters, SGVector< bool > parameter_regularizable, float64_t sigma)
Class SGObject is the base class of all shogun objects.
CDynamicObjectArray * m_layers
CSGObject * element(int32_t idx1, int32_t idx2=0, int32_t idx3=0)
virtual void connect(int32_t i, int32_t j)
virtual void set_batch_size(int32_t batch_size)
virtual void disconnect_all()
virtual ~CNeuralNetwork()
virtual void set_value(float64_t value, int32_t idx)
int32_t m_total_num_parameters
virtual CRegressionLabels * apply_regression(CFeatures *data)
static T sum(T *vec, int32_t len)
Return sum(vec)
virtual EFeatureClass get_feature_class() const =0
Dynamic array class for CSGObject pointers that creates an array that can be used like a list or an a...
virtual void dropout_activations()
ENNOptimizationMethod m_optimization_method
float64_t m_gd_learning_rate_decay
CDynamicObjectArray * get_layers()
int32_t m_gd_mini_batch_size
virtual float64_t check_gradients(float64_t approx_epsilon=1.0e-3, float64_t s=1.0e-9)
CNeuralLayer * get_layer(int32_t i)
void set_multiclass_confidences(int32_t i, SGVector< float64_t > confidences)
virtual bool is_label_valid(CLabels *lab) const
virtual CDenseFeatures< float64_t > * transform(CDenseFeatures< float64_t > *data)
all of classes and functions are contained in the shogun namespace
virtual void set_labels(CLabels *lab)
virtual void set_batch_size(int32_t batch_size)
virtual bool train_lbfgs(SGMatrix< float64_t > inputs, SGMatrix< float64_t > targets)
The class Features is the base class of all feature objects.
virtual void compute_activations(SGMatrix< float64_t > inputs)
SGMatrix< float64_t > labels_to_matrix(CLabels *labs)
virtual SGMatrix< float64_t > forward_propagate(CFeatures *data, int32_t j=-1)
int32_t get_num_outputs()
virtual EProblemType get_machine_problem_type() const
Binary Labels for binary classification.
virtual void set_layers(CDynamicObjectArray *layers)
SGVector< float64_t > * get_layer_parameters(int32_t i)
virtual void set_labels(CLabels *lab)
float64_t m_gd_learning_rate
void set_const(T const_elem)
virtual EFeatureType get_feature_type() const =0
virtual float64_t compute_gradients(SGMatrix< float64_t > inputs, SGMatrix< float64_t > targets, SGVector< float64_t > gradients)
void lbfgs_parameter_init(lbfgs_parameter_t *param)