-
Notifications
You must be signed in to change notification settings - Fork 0
/
TrainingData.cpp
225 lines (182 loc) · 5.64 KB
/
TrainingData.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
/*
*
* Fast Artificial Neural Network (fann) C# Wrapper
* Copyright (C) 2010 created by james (at) jamesbates.net
*
* On LinkedIn here http://uk.linkedin.com/in/alexanderjamesbates
*
* This wrapper is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This wrapper is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
*/
#include "StdAfx.h"
#include "TrainingData.h"
#include <vcclr.h>
#include <vector>
#include "Conversion.hpp"
#include <boost/multi_array.hpp>
#include <boost/scoped_array.hpp>
namespace FANN
{
namespace Net
{
TrainingData::TrainingData(void)
: ProxyImpl<FANN::training_data>(new FANN::training_data)
{
m_Instances[(unsigned int)&Raw()] = this;
}
TrainingData::TrainingData(FANN::training_data *data)
: ProxyImpl<FANN::training_data>(data)
{
m_Instances[(unsigned int)&Raw()] = this;
}
TrainingData::TrainingData(FANN::training_data *data,bool owner)
: ProxyImpl<FANN::training_data>(data,owner)
{
m_Instances[(unsigned int)&Raw()] = this;
}
TrainingData^ TrainingData::Instance(FANN::training_data* data)
{
unsigned int idata = (unsigned int)data;
if(!m_Instances->ContainsKey(idata))
{
m_Instances[idata] = gcnew TrainingData(data,false);
}
return m_Instances[idata];
}
TrainingData::~TrainingData(void)
{
m_Instances->Remove((unsigned int)&Raw());
}
bool TrainingData::ReadTrainFromFile(System::String^ filename)
{
return Raw().read_train_from_file(toNative(filename));
}
bool TrainingData::SaveTrain(System::String^ filename)
{
return Raw().save_train(toNative(filename));
}
bool TrainingData::SaveTrainToFixed(System::String^ filename, unsigned int decimalPoint)
{
return Raw().save_train_to_fixed(toNative(filename),decimalPoint);
}
void TrainingData::ShuffleTrainData()
{
Raw().shuffle_train_data();
}
void TrainingData::Merge(TrainingData^ data)
{
Raw().merge_train_data(data->Raw());
}
unsigned int TrainingData::TrainingDataLength::get()
{
return Raw().length_train_data();
}
int TrainingData::NumInputTrainData::get()
{
return Raw().num_input_train_data();
}
int TrainingData::NumOutputTrainData::get()
{
return Raw().num_output_train_data();
}
array<array<fann_type,1>^,1> ^TrainingData::Input::get()
{
return toManaged<fann_type>(Raw().get_input(),Raw().length_train_data(),Raw().num_input_train_data());
}
array<array<fann_type,1>^,1> ^TrainingData::Output::get()
{
return toManaged(Raw().get_output(),Raw().length_train_data(),Raw().num_output_train_data());
}
template<typename T>
class ArrayDeleter
{
public:
void operator () (T* d) const
{
delete [] d;
}
};
void TrainingData::SetTrainData(unsigned int numData,
unsigned int numInput,array<array<fann_type,1>^,1> ^input,
unsigned int numOutput,array<array<fann_type,1>^,1> ^output)
{
typedef boost::multi_array<double, 2> array_type;
//typedef array_type::index input;
array_type inputsMulti(boost::extents[numData][numInput]);
array_type outputsMulti(boost::extents[numData][numOutput]);
boost::scoped_array<fann_type*> fannInput(new fann_type*[numData]);
boost::scoped_array<fann_type*> fannOutput(new fann_type*[numData]);
for(size_t i=0;i<numData;++i)
{
fannInput[i] = &inputsMulti[i][0];
for(size_t j=0;j<numInput;++j)
{
inputsMulti[i][j] = input[i][j];
}
fannOutput[i] = &outputsMulti[i][0];
for(size_t j=0;j<numOutput;++j)
{
outputsMulti[i][j] = output[i][j];
}
}
Raw().set_train_data(numData,numInput,&fannInput[0],numOutput,&fannOutput[0]);
}
class CallTranslator
{
public:
CallTranslator(TrainingData::CallbackType^ fun)
{
current_callback = fun;
}
static void FANN_API translate_callback(
unsigned int a,
unsigned int b,
unsigned int c,
fann_type * d,
fann_type * e)
{
current_callback->Invoke(a,b,c,toManaged(d,b),toManaged(e,c));
}
private:
static gcroot<TrainingData::CallbackType^> current_callback;
};
gcroot<TrainingData::CallbackType^> CallTranslator::current_callback;
typedef void (FANN_API *TrainingCallback)( unsigned int,unsigned int,unsigned int,fann_type * ,fann_type * );
void TrainingData::CreateTrainFromCallback(unsigned int numData,
unsigned int numInput,
unsigned int numOutput,
TrainingData::CallbackType^ fun)
{
CallTranslator translator(fun);
Raw().create_train_from_callback(numData,numInput,numOutput,CallTranslator::translate_callback);
}
void TrainingData::ScaleInputTrainData(fann_type newMin, fann_type newMax)
{
Raw().scale_input_train_data(newMin,newMax);
}
void TrainingData::ScaleOutputTrainData(fann_type newMin, fann_type newMax)
{
Raw().scale_output_train_data(newMin,newMax);
}
void TrainingData::ScaleTrainData(fann_type newMin, fann_type newMax)
{
Raw().scale_train_data(newMin,newMax);
}
void TrainingData::SubsetTrainData(unsigned int pos, unsigned int length)
{
Raw().subset_train_data(pos,length);
}
}
}