50 CNeuralNetwork* CNeuralNetworkFileReader::read_file(
const char* file_path)
52 json_object* json_network = json_object_from_file(file_path);
54 if (is_error(json_network))
56 SG_ERROR(
"Error while opening file: %s!\n", file_path);
62 json_object_put(json_network);
67 CNeuralNetwork* CNeuralNetworkFileReader::read_string(
const char* str)
69 json_object* json_network = json_tokener_parse(str);
71 if (is_error(json_network))
73 SG_ERROR(
"Error while parsing the given string\n");
79 json_object_put(json_network);
84 CNeuralNetwork* CNeuralNetworkFileReader::parse_network(json_object* json_network)
89 json_object_iter iter;
90 json_object* json_layers = NULL;
91 json_object_object_foreachC(json_network, iter)
93 if (string_equal(iter.key,
"layers"))
94 json_layers = iter.val;
100 SG_ERROR(
"No layers found in file\n");
103 json_object_iter layers_iter;
104 json_object_object_foreachC(json_layers, layers_iter)
106 json_object_iter layer_iter;
107 json_object_object_foreachC(layers_iter.val, layer_iter)
109 if (string_equal(layer_iter.key,
"inputs"))
111 int32_t len = json_object_array_length(layer_iter.val);
113 for (int32_t i=0; i<len; i++)
115 const char* input_key = json_object_get_string(
116 json_object_array_get_idx(layer_iter.val, i));
118 int32_t from = find_layer_index(json_layers, input_key);
119 int32_t to = find_layer_index(json_layers, layers_iter.key);
122 SG_ERROR(
"Invalid layer identifier (%s) in layer (%s)\n",
123 input_key, layers_iter.key);
133 json_object_object_foreachC(json_network, iter)
135 if (string_equal(iter.key,
"sigma"))
136 sigma = json_object_get_double(iter.val);
137 else if (string_equal(iter.key,
"optimization_method"))
139 const char* method = json_object_get_string(iter.val);
140 if (string_equal(method,
"NNOM_LBFGS"))
142 else if (string_equal(method,
"NNOM_GRADIENT_DESCENT"))
145 SG_ERROR(
"Invalid optimization method (%s)\n", method);
147 else if (string_equal(iter.key,
"l2_coefficient"))
149 else if (string_equal(iter.key,
"l1_coefficient"))
151 else if (string_equal(iter.key,
"dropout_hidden"))
153 else if (string_equal(iter.key,
"dropout_input"))
155 else if (string_equal(iter.key,
"max_norm"))
156 network->
set_max_norm(json_object_get_double(iter.val));
157 else if (string_equal(iter.key,
"epsilon"))
158 network->
set_epsilon(json_object_get_double(iter.val));
159 else if (string_equal(iter.key,
"max_num_epochs"))
161 else if (string_equal(iter.key,
"gd_mini_batch_size"))
163 else if (string_equal(iter.key,
"gd_learning_rate"))
165 else if (string_equal(iter.key,
"gd_learning_rate_decay"))
167 else if (string_equal(iter.key,
"gd_momentum"))
169 else if (string_equal(iter.key,
"gd_error_damping_coeff"))
172 else if (!string_equal(iter.key,
"layers"))
173 SG_ERROR(
"Invalid parameter (%s)\n", iter.key);
182 json_object* json_layers)
186 json_object_iter iter;
187 json_object_object_foreachC(json_layers, iter)
195 CNeuralLayer* CNeuralNetworkFileReader::parse_layer(json_object* json_layer)
197 json_object_iter iter;
200 const char* type = NULL;
203 json_object_object_foreachC(json_layer, iter)
205 if (string_equal(iter.key,
"type"))
207 type = json_object_get_string(iter.val);
209 if (string_equal(type,
"NeuralInputLayer"))
211 else if (string_equal(type,
"NeuralLinearLayer"))
213 else if (string_equal(type,
"NeuralLogisticLayer"))
215 else if (string_equal(type,
"NeuralSoftmaxLayer"))
217 else if (string_equal(type,
"NeuralRectifiedLinearLayer"))
220 SG_ERROR(
"Unknown layer type: %s", type);
225 json_object_object_foreachC(json_layer, iter)
227 if(string_equal(iter.key,
"num_neurons"))
231 else if(string_equal(type,
"NeuralInputLayer") &&
232 string_equal(iter.key,
"start_index"))
235 json_object_get_int(iter.val));
242 int32_t CNeuralNetworkFileReader::find_layer_index(json_object* json_layers,
243 const char* layer_key)
247 json_object_iter iter;
248 json_object_object_foreachC(json_layers, iter)
250 if (string_equal(iter.key, layer_key))
259 bool CNeuralNetworkFileReader::string_equal(
const char* str1,
const char* str2)
261 return (strcmp(str1, str2) == 0);
void set_gd_learning_rate(float64_t gd_learning_rate)
void set_gd_momentum(float64_t gd_momentum)
virtual void initialize_neural_network(float64_t sigma=0.01f)
void set_max_norm(float64_t max_norm)
void set_gd_mini_batch_size(int32_t gd_mini_batch_size)
void set_dropout_hidden(float64_t dropout_hidden)
A generic multi-layer neural network.
Base class for neural network layers.
void set_max_num_epochs(int32_t max_num_epochs)
void set_epsilon(float64_t epsilon)
virtual void set_num_neurons(int32_t num_neurons)
Neural layer with linear neurons, with a softmax activation function. can be only be used as an outpu...
virtual void connect(int32_t i, int32_t j)
void set_gd_error_damping_coeff(float64_t gd_error_damping_coeff)
void set_l2_coefficient(float64_t l2_coefficient)
Dynamic array class for CSGObject pointers that creates an array that can be used like a list or an a...
Neural layer with linear neurons, with an identity activation function. can be used as a hidden layer...
Neural layer with linear neurons, with a logistic activation function. can be used as a hidden layer ...
all of classes and functions are contained in the shogun namespace
void set_l1_coefficient(float64_t l1_coefficient)
void set_gd_learning_rate_decay(float64_t gd_learning_rate_decay)
virtual void set_layers(CDynamicObjectArray *layers)
void set_optimization_method(ENNOptimizationMethod optimization_method)
Neural layer with rectified linear neurons.
bool append_element(CSGObject *e)
void set_dropout_input(float64_t dropout_input)