Skip to content

Commit 3a1b46b

Browse files
author
Soeren Sonnenburg
committed
Merge branch 'release/4.0.0'
2 parents 3cfe2d6 + abcf0ef commit 3a1b46b

File tree

11 files changed

+653
-8
lines changed

11 files changed

+653
-8
lines changed

NEWS

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
2015-01-18 Soeren Sonnenburg <[email protected]>
1+
2015-01-26 Soeren Sonnenburg <[email protected]>
22

3-
* SHOGUN Release version 3.2.1 (libshogun 16.1, data 0.8, parameter 1)
3+
* SHOGUN Release version 4.0.0 (libshogun 17.0, data 0.9, parameter 1)
44
* This release features the work of our 8 GSoC 2014 students [student; mentors]:
55
- OpenCV Integration and Computer Vision Applications [Abhijeet Kislay; Kevin Hughes]
66
- Large-Scale Multi-Label Classification [Abinash Panda; Thoralf Klein]

src/interfaces/modular/IO.i

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
%rename(SerializableHdf5File) CSerializableHdf5File;
2727
%rename(SerializableJsonFile) CSerializableJsonFile;
2828
%rename(SerializableXmlFile) CSerializableXmlFile;
29+
%rename(NeuralNetworkFileReader) CNeuralNetworkFileReader;
2930
%rename(SimpleFile) CSimpleFile;
3031
%rename(MemoryMappedFile) CMemoryMappedFile;
3132
%rename(VwParser) CVwParser;
@@ -131,6 +132,7 @@ namespace shogun
131132
%include <shogun/io/SerializableHdf5File.h>
132133
%include <shogun/io/SerializableJsonFile.h>
133134
%include <shogun/io/SerializableXmlFile.h>
135+
%include <shogun/io/NeuralNetworkFileReader.h>
134136

135137
%include <shogun/io/SimpleFile.h>
136138
%include <shogun/io/MemoryMappedFile.h>

src/interfaces/modular/IO_includes.i

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#include <shogun/io/SerializableHdf5File.h>
2121
#include <shogun/io/SerializableJsonFile.h>
2222
#include <shogun/io/SerializableXmlFile.h>
23+
#include <shogun/io/NeuralNetworkFileReader.h>
2324
#include <shogun/io/SimpleFile.h>
2425
#include <shogun/io/MemoryMappedFile.h>
2526
%}

src/interfaces/octave_static/OctaveInterface.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -633,8 +633,8 @@ void COctaveInterface::clear_octave_globals()
633633

634634
void COctaveInterface::run_octave_init()
635635
{
636-
char* name=strdup("octave");
637-
char* opts=strdup("-q");
636+
char* name=get_strdup("octave");
637+
char* opts=get_strdup("-q");
638638
char* argv[2]={name, opts};
639639
octave_main(2,argv,1);
640640
free(opts);

src/interfaces/r_static/RInterface.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ extern "C" {
1818
#include <shogun/lib/ShogunException.h>
1919
#include <shogun/io/SGIO.h>
2020
#include <shogun/base/init.h>
21+
#include <shogun/lib/memory.h>
2122

2223
#ifdef HAVE_PYTHON
2324
#include "../python_static/PythonInterface.h"
@@ -552,8 +553,8 @@ void CRInterface::run_r_init()
552553
#ifdef R_HOME_ENV
553554
setenv("R_HOME", R_HOME_ENV, 0);
554555
#endif
555-
char* name=strdup("R");
556-
char* opts=strdup("-q");
556+
char* name=get_strdup("R");
557+
char* opts=get_strdup("-q");
557558
char* argv[2]={name, opts};
558559
Rf_initEmbeddedR(2, argv);
559560
free(opts);

src/shogun/io/File.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -185,12 +185,12 @@ CFile::~CFile()
185185
void CFile::set_variable_name(const char* name)
186186
{
187187
SG_FREE(variable_name);
188-
variable_name=strdup(name);
188+
variable_name=get_strdup(name);
189189
}
190190

191191
char* CFile::get_variable_name()
192192
{
193-
return strdup(variable_name);
193+
return get_strdup(variable_name);
194194
}
195195

196196
#define SPARSE_VECTOR_GETTER(type) \
Lines changed: 264 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,264 @@
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

Comments
 (0)