|
| 1 | +/* |
| 2 | + * Copyright (c) 2014, Shogun Toolbox Foundation |
| 3 | + * All rights reserved. |
| 4 | + * |
| 5 | + * Redistribution and use in source and binary forms, with or without |
| 6 | + * modification, are permitted provided that the following conditions are met: |
| 7 | +
|
| 8 | + * 1. Redistributions of source code must retain the above copyright notice, |
| 9 | + * this list of conditions and the following disclaimer. |
| 10 | + * |
| 11 | + * 2. Redistributions in binary form must reproduce the above copyright notice, |
| 12 | + * this list of conditions and the following disclaimer in the documentation |
| 13 | + * and/or other materials provided with the distribution. |
| 14 | + * |
| 15 | + * 3. Neither the name of the copyright holder nor the names of its |
| 16 | + * contributors may be used to endorse or promote products derived from this |
| 17 | + * software without specific prior written permission. |
| 18 | +
|
| 19 | + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| 20 | + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 21 | + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 22 | + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE |
| 23 | + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 24 | + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 25 | + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 26 | + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 27 | + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 28 | + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
| 29 | + * POSSIBILITY OF SUCH DAMAGE. |
| 30 | + * |
| 31 | + * Written (W) 2014 Khaled Nasr |
| 32 | + */ |
| 33 | + |
| 34 | +#include <shogun/lib/config.h> |
| 35 | +#ifdef HAVE_JSON |
| 36 | + |
| 37 | +#include <shogun/io/NeuralNetworkFileReader.h> |
| 38 | +#include <shogun/neuralnets/NeuralNetwork.h> |
| 39 | +#include <shogun/neuralnets/NeuralLayer.h> |
| 40 | +#include <shogun/neuralnets/NeuralInputLayer.h> |
| 41 | +#include <shogun/neuralnets/NeuralLinearLayer.h> |
| 42 | +#include <shogun/neuralnets/NeuralLogisticLayer.h> |
| 43 | +#include <shogun/neuralnets/NeuralSoftmaxLayer.h> |
| 44 | +#include <shogun/neuralnets/NeuralRectifiedLinearLayer.h> |
| 45 | +#include <shogun/lib/DynamicObjectArray.h> |
| 46 | +#include <shogun/lib/SGVector.h> |
| 47 | + |
| 48 | +using namespace shogun; |
| 49 | + |
| 50 | +CNeuralNetwork* CNeuralNetworkFileReader::read_file(const char* file_path) |
| 51 | +{ |
| 52 | + json_object* json_network = json_object_from_file(file_path); |
| 53 | + |
| 54 | + if (is_error(json_network)) |
| 55 | + { |
| 56 | + SG_ERROR("Error while opening file: %s!\n", file_path); |
| 57 | + return NULL; |
| 58 | + } |
| 59 | + |
| 60 | + CNeuralNetwork* network = parse_network(json_network); |
| 61 | + |
| 62 | + json_object_put(json_network); |
| 63 | + |
| 64 | + return network; |
| 65 | +} |
| 66 | + |
| 67 | +CNeuralNetwork* CNeuralNetworkFileReader::read_string(const char* str) |
| 68 | +{ |
| 69 | + json_object* json_network = json_tokener_parse(str); |
| 70 | + |
| 71 | + if (is_error(json_network)) |
| 72 | + { |
| 73 | + SG_ERROR("Error while parsing the given string\n"); |
| 74 | + return NULL; |
| 75 | + } |
| 76 | + |
| 77 | + CNeuralNetwork* network = parse_network(json_network); |
| 78 | + |
| 79 | + json_object_put(json_network); |
| 80 | + |
| 81 | + return network; |
| 82 | +} |
| 83 | + |
| 84 | +CNeuralNetwork* CNeuralNetworkFileReader::parse_network(json_object* json_network) |
| 85 | +{ |
| 86 | + CNeuralNetwork* network = new CNeuralNetwork; |
| 87 | + |
| 88 | + // find the layers |
| 89 | + json_object_iter iter; |
| 90 | + json_object* json_layers = NULL; |
| 91 | + json_object_object_foreachC(json_network, iter) |
| 92 | + { |
| 93 | + if (string_equal(iter.key, "layers")) |
| 94 | + json_layers = iter.val; |
| 95 | + } |
| 96 | + |
| 97 | + if (json_layers) |
| 98 | + network->set_layers(parse_layers(iter.val)); |
| 99 | + else |
| 100 | + SG_ERROR("No layers found in file\n"); |
| 101 | + |
| 102 | + // set the connections |
| 103 | + json_object_iter layers_iter; |
| 104 | + json_object_object_foreachC(json_layers, layers_iter) |
| 105 | + { |
| 106 | + json_object_iter layer_iter; |
| 107 | + json_object_object_foreachC(layers_iter.val, layer_iter) |
| 108 | + { |
| 109 | + if (string_equal(layer_iter.key, "inputs")) |
| 110 | + { |
| 111 | + int32_t len = json_object_array_length(layer_iter.val); |
| 112 | + |
| 113 | + for (int32_t i=0; i<len; i++) |
| 114 | + { |
| 115 | + const char* input_key = json_object_get_string( |
| 116 | + json_object_array_get_idx(layer_iter.val, i)); |
| 117 | + |
| 118 | + int32_t from = find_layer_index(json_layers, input_key); |
| 119 | + int32_t to = find_layer_index(json_layers, layers_iter.key); |
| 120 | + |
| 121 | + if (from == -1) |
| 122 | + SG_ERROR("Invalid layer identifier (%s) in layer (%s)\n", |
| 123 | + input_key, layers_iter.key); |
| 124 | + |
| 125 | + network->connect(from, to); |
| 126 | + } |
| 127 | + } |
| 128 | + } |
| 129 | + } |
| 130 | + |
| 131 | + // set the training parameters |
| 132 | + float sigma = 0.01; |
| 133 | + json_object_object_foreachC(json_network, iter) |
| 134 | + { |
| 135 | + if (string_equal(iter.key, "sigma")) |
| 136 | + sigma = json_object_get_double(iter.val); |
| 137 | + else if (string_equal(iter.key, "optimization_method")) |
| 138 | + { |
| 139 | + const char* method = json_object_get_string(iter.val); |
| 140 | + if (string_equal(method, "NNOM_LBFGS")) |
| 141 | + network->optimization_method = NNOM_LBFGS; |
| 142 | + else if (string_equal(method, "NNOM_GRADIENT_DESCENT")) |
| 143 | + network->optimization_method = NNOM_GRADIENT_DESCENT; |
| 144 | + else |
| 145 | + SG_ERROR("Invalid optimization method (%s)\n", method); |
| 146 | + } |
| 147 | + else if (string_equal(iter.key, "l2_coefficient")) |
| 148 | + network->l2_coefficient = json_object_get_double(iter.val); |
| 149 | + else if (string_equal(iter.key, "l1_coefficient")) |
| 150 | + network->l1_coefficient = json_object_get_double(iter.val); |
| 151 | + else if (string_equal(iter.key, "dropout_hidden")) |
| 152 | + network->dropout_hidden = json_object_get_double(iter.val); |
| 153 | + else if (string_equal(iter.key, "dropout_input")) |
| 154 | + network->dropout_input = json_object_get_double(iter.val); |
| 155 | + else if (string_equal(iter.key, "max_norm")) |
| 156 | + network->max_norm = json_object_get_double(iter.val); |
| 157 | + else if (string_equal(iter.key, "epsilon")) |
| 158 | + network->epsilon = json_object_get_double(iter.val); |
| 159 | + else if (string_equal(iter.key, "max_num_epochs")) |
| 160 | + network->max_num_epochs = json_object_get_int(iter.val); |
| 161 | + else if (string_equal(iter.key, "gd_mini_batch_size")) |
| 162 | + network->gd_mini_batch_size = json_object_get_int(iter.val); |
| 163 | + else if (string_equal(iter.key, "gd_learning_rate")) |
| 164 | + network->gd_learning_rate = json_object_get_double(iter.val); |
| 165 | + else if (string_equal(iter.key, "gd_learning_rate_decay")) |
| 166 | + network->gd_learning_rate_decay = json_object_get_double(iter.val); |
| 167 | + else if (string_equal(iter.key, "gd_momentum")) |
| 168 | + network->gd_momentum = json_object_get_double(iter.val); |
| 169 | + else if (string_equal(iter.key, "gd_error_damping_coeff")) |
| 170 | + network->gd_error_damping_coeff = json_object_get_double(iter.val); |
| 171 | + |
| 172 | + else if (!string_equal(iter.key, "layers")) |
| 173 | + SG_ERROR("Invalid parameter (%s)\n", iter.key); |
| 174 | + } |
| 175 | + |
| 176 | + network->initialize(sigma); |
| 177 | + |
| 178 | + return network; |
| 179 | +} |
| 180 | + |
| 181 | +CDynamicObjectArray* CNeuralNetworkFileReader::parse_layers( |
| 182 | + json_object* json_layers) |
| 183 | +{ |
| 184 | + CDynamicObjectArray* layers = new CDynamicObjectArray(); |
| 185 | + |
| 186 | + json_object_iter iter; |
| 187 | + json_object_object_foreachC(json_layers, iter) |
| 188 | + { |
| 189 | + layers->append_element(parse_layer(iter.val)); |
| 190 | + } |
| 191 | + |
| 192 | + return layers; |
| 193 | +} |
| 194 | + |
| 195 | +CNeuralLayer* CNeuralNetworkFileReader::parse_layer(json_object* json_layer) |
| 196 | +{ |
| 197 | + json_object_iter iter; |
| 198 | + |
| 199 | + CNeuralLayer* layer = NULL; |
| 200 | + const char* type = NULL; |
| 201 | + |
| 202 | + // find the layer type and create a appropriate instance |
| 203 | + json_object_object_foreachC(json_layer, iter) |
| 204 | + { |
| 205 | + if (string_equal(iter.key, "type")) |
| 206 | + { |
| 207 | + type = json_object_get_string(iter.val); |
| 208 | + |
| 209 | + if (string_equal(type, "NeuralInputLayer")) |
| 210 | + layer = new CNeuralInputLayer(); |
| 211 | + else if (string_equal(type, "NeuralLinearLayer")) |
| 212 | + layer = new CNeuralLinearLayer(); |
| 213 | + else if (string_equal(type, "NeuralLogisticLayer")) |
| 214 | + layer = new CNeuralLogisticLayer(); |
| 215 | + else if (string_equal(type, "NeuralSoftmaxLayer")) |
| 216 | + layer = new CNeuralSoftmaxLayer(); |
| 217 | + else if (string_equal(type, "NeuralRectifiedLinearLayer")) |
| 218 | + layer = new CNeuralRectifiedLinearLayer(); |
| 219 | + else |
| 220 | + SG_ERROR("Unknown layer type: %s", type); |
| 221 | + } |
| 222 | + } |
| 223 | + |
| 224 | + // fill in the fields |
| 225 | + json_object_object_foreachC(json_layer, iter) |
| 226 | + { |
| 227 | + if(string_equal(iter.key, "num_neurons")) |
| 228 | + { |
| 229 | + layer->set_num_neurons(json_object_get_int(iter.val)); |
| 230 | + } |
| 231 | + else if(string_equal(type,"NeuralInputLayer") && |
| 232 | + string_equal(iter.key, "start_index")) |
| 233 | + { |
| 234 | + ((CNeuralInputLayer*)layer)->set_start_index( |
| 235 | + json_object_get_int(iter.val)); |
| 236 | + } |
| 237 | + } |
| 238 | + |
| 239 | + return layer; |
| 240 | +} |
| 241 | + |
| 242 | +int32_t CNeuralNetworkFileReader::find_layer_index(json_object* json_layers, |
| 243 | + const char* layer_key) |
| 244 | +{ |
| 245 | + int32_t index = 0; |
| 246 | + |
| 247 | + json_object_iter iter; |
| 248 | + json_object_object_foreachC(json_layers, iter) |
| 249 | + { |
| 250 | + if (string_equal(iter.key, layer_key)) |
| 251 | + return index; |
| 252 | + else |
| 253 | + index++; |
| 254 | + } |
| 255 | + |
| 256 | + return -1; |
| 257 | +} |
| 258 | + |
| 259 | +bool CNeuralNetworkFileReader::string_equal(const char* str1, const char* str2) |
| 260 | +{ |
| 261 | + return (strcmp(str1, str2) == 0); |
| 262 | +} |
| 263 | + |
| 264 | +#endif |
0 commit comments