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 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426
|
// Copyright (C) 2002 Ronan Collobert (collober@iro.umontreal.ca)
//
//
// This file is part of Torch. Release II.
// [The Ultimate Machine Learning Library]
//
// Torch is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// Torch 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 General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Torch; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
#include "IOTorch.h"
namespace Torch {
#ifdef USEDOUBLE
#define REAL_FORMAT "%lf"
#else
#define REAL_FORMAT "%f"
#endif
IOTorch::IOTorch()
{
blocks = NULL;
n_blocks = 0;
}
int IOTorch::addBlkMem(void *internal_alloc, void **data, void **targets, int n_examples, bool data_is_internal, bool targets_is_internal)
{
blocks = (BlkMem *)xrealloc((void *)blocks, (n_blocks+1)*sizeof(BlkMem));
blocks[n_blocks].internal_alloc = internal_alloc;
blocks[n_blocks].data = data;
blocks[n_blocks].targets = targets;
blocks[n_blocks].n_examples = n_examples;
blocks[n_blocks].data_is_internal = data_is_internal;
blocks[n_blocks].targets_is_internal = targets_is_internal;
n_blocks++;
return((n_blocks-1));
}
void IOTorch::destroyBlkMem(int number)
{
free(blocks[number].internal_alloc);
if(!blocks[number].data_is_internal)
{
void **ptr = blocks[number].data;
for(int t = 0; t < blocks[number].n_examples; t++)
{
free(*ptr);
ptr++;
}
}
free(blocks[number].data);
if(!blocks[number].targets_is_internal)
{
void **ptr = blocks[number].targets;
for(int t = 0; t < blocks[number].n_examples; t++)
{
free(*ptr);
ptr ++;
}
}
free(blocks[number].targets);
if(number != (n_blocks-1) )
memmove(&blocks[number], &blocks[number+1], (n_blocks-number-1)*sizeof(BlkMem));
blocks = (BlkMem *)xrealloc((void *)blocks, (n_blocks-1)*sizeof(BlkMem));
n_blocks--;
if(n_blocks == 0)
blocks = NULL;
}
int IOTorch::loadData(const char *file, real ***data_, real ***y_,
int n_inputs, int n_targets, int *n_examples, bool bin, int max_load)
{
real **data = NULL;
real **y = NULL;
real *internal_alloc = NULL;
int l, w;
FILE *f;
f = fopen(file, "r");
if(!f)
error("IOTorch: cannot open the file <%s> for reading", file);
if(bin)
{
xfread(&l, sizeof(int), 1, f);
xfread(&w, sizeof(int), 1, f);
}
else
{
fscanf(f, "%d", &l);
fscanf(f, "%d", &w);
}
if( (max_load > 0) && (max_load < l) )
{
l = max_load;
message("IOTorch: loading only %d examples", l);
}
if( (n_inputs+n_targets) != w )
error("IOTorch: %d inputs, %d targets != %d columns in the file", n_inputs, n_targets, w);
internal_alloc = (real *)xalloc(w*l*sizeof(real));
if(n_inputs)
{
data = (real **)xalloc(l*sizeof(real *));
for(int i = 0; i < l; i++)
data[i] = &internal_alloc[n_inputs*i];
}
if(n_targets)
{
y = (real **)xalloc(l*sizeof(real *));
for(int i = 0; i < l; i++)
y[i] = &internal_alloc[n_inputs*l+n_targets*i];
}
if(bin)
{
for(int i = 0; i < l; i++)
{
if(n_inputs)
xfread(data[i], sizeof(real), n_inputs, f);
if(n_targets)
xfread(y[i], sizeof(real), n_targets, f);
}
}
else
{
for(int i = 0; i < l; i++)
{
for(int j = 0; j < n_inputs; j++)
fscanf(f, REAL_FORMAT, &data[i][j]);
for(int j = 0; j < n_targets; j++)
fscanf(f, REAL_FORMAT, &y[i][j]);
}
}
fclose(f);
*n_examples = l;
if(n_inputs)
*data_ = data;
if(n_targets)
*y_ = y;
return(addBlkMem(internal_alloc, (void **)data, (void **)y, l, true, true));
}
int IOTorch::loadData(const char *file, sreal ***data_, real ***y_,
int n_inputs, int n_targets, int *n_examples, bool bin, int max_load)
{
sreal **data = NULL;
real **y = NULL;
real *internal_alloc = NULL;
int l, w;
FILE *f;
f = fopen(file, "r");
if(!f)
error("IOTorch: cannot open the file <%s> for reading", file);
if(bin)
{
xfread(&l, sizeof(int), 1, f);
xfread(&w, sizeof(int), 1, f);
}
else
{
fscanf(f, "%d", &l);
fscanf(f, "%d", &w);
}
if( (max_load > 0) && (max_load < l) )
{
l = max_load;
message("IOTorch: loading only %d examples", l);
}
if( (n_inputs+n_targets) != w )
error("IOTorch: %d inputs, %d targets != %d columns in the file", n_inputs, n_targets, w);
if(n_targets)
internal_alloc = (real *)xalloc(n_targets*l*sizeof(real));
if(n_inputs)
data = (sreal **)xalloc(l*sizeof(sreal *));
if(n_targets)
{
y = (real **)xalloc(l*sizeof(real *));
for(int t = 0; t < l; t++)
y[t] = &internal_alloc[n_targets*t];
}
sreal *temp = (sreal *)xalloc(w*sizeof(sreal));
int nt_in;
int w_on_line;
for(int i = 0; i < l; i++)
{
if(bin)
{
xfread(&w_on_line, sizeof(int), 1, f);
for(int j = 0; j < w_on_line; j++)
{
xfread(&temp[j].index, sizeof(int), 1, f);
xfread(&temp[j].value, sizeof(real), 1, f);
}
}
else
{
fscanf(f, "%d", &w_on_line);
for(int j = 0; j < w_on_line; j++)
{
fscanf(f, "%d", &temp[j].index);
fscanf(f, REAL_FORMAT, &temp[j].value);
}
}
nt_in = 0;
for(int j = 0; j < w_on_line; j++)
{
if(temp[j].index < n_inputs)
nt_in++;
else
break;
}
if(n_inputs)
{
data[i] = (sreal *)xalloc((nt_in+1)*sizeof(sreal));
data[i][nt_in].index = -1;
}
for(int j = 0; j < nt_in; j++)
{
data[i][j].index = temp[j].index;
data[i][j].value = temp[j].value;
}
for(int j = 0; j < n_targets; j++)
y[i][j] = 0;
for(int j = nt_in; j < w_on_line; j++)
y[i][temp[j].index-n_inputs] = temp[j].value;
}
free(temp);
fclose(f);
*n_examples = l;
if(n_inputs)
*data_ = data;
if(n_targets)
*y_ = y;
return(addBlkMem(internal_alloc, (void **)data, (void **)y, l, false, true));
}
//////////////// Save
void IOTorch::saveData(const char *file, real **data, real **y,
int l, int n_inputs, int n_targets, bool bin, int max_save)
{
FILE *f;
f = fopen(file, "w");
if(!f)
error("IOTorch: cannot open the file <%s> for writing", file);
int w = n_inputs + n_targets;
if(bin)
{
xfwrite(&l, sizeof(int), 1, f);
xfwrite(&w, sizeof(int), 1, f);
}
else
fprintf(f, "%d %d\n", l, w);
if( (max_save > 0) && (max_save < l) )
{
l = max_save;
message("IOTorch: saving only %d examples", l);
}
if(bin)
{
for(int i = 0; i < l; i++)
{
if(n_inputs)
xfwrite(data[i], sizeof(real), n_inputs, f);
if(n_targets)
xfwrite(y[i], sizeof(real), n_targets, f);
}
}
else
{
for(int i = 0; i < l; i++)
{
for(int j = 0; j < n_inputs; j++)
fprintf(f, "%g ", data[i][j]);
for(int j = 0; j < n_targets; j++)
fprintf(f, "%g ", y[i][j]);
fprintf(f, "\n");
}
}
fclose(f);
}
void IOTorch::saveData(const char *file, sreal **data, real **y,
int l, int n_inputs, int n_targets, bool bin, int max_save)
{
FILE *f;
f = fopen(file, "w");
if(!f)
error("IOTorch: cannot open the file <%s> for writing", file);
int w = n_inputs+n_targets;
if(bin)
{
xfwrite(&l, sizeof(int), 1, f);
xfwrite(&w, sizeof(int), 1, f);
}
else
fprintf(f, "%d %d\n", l, w);
if( (max_save > 0) && (max_save < l) )
{
l = max_save;
message("IOTorch: saving only %d examples", l);
}
sreal *temp = NULL;
if(n_targets)
temp = (sreal *)xalloc(n_targets*sizeof(sreal));
int w_on_line;
int nn = 0;
for(int i = 0; i < l; i++)
{
w_on_line = sparseVectorLength(data[i]);
if(n_targets)
{
nn = 0;
for(int j = 0; j < n_targets; j++)
{
real z = y[i][j];
if(z != 0)
{
temp[nn].index = n_inputs+j;
temp[nn].value = z;
nn++;
}
}
}
w_on_line += nn;
if(bin)
{
xfwrite(&w_on_line, sizeof(int), 1, f);
for(int j = 0; j < w_on_line-nn; j++)
{
xfwrite(&data[i][j].index, sizeof(int), 1, f);
xfwrite(&data[i][j].value, sizeof(real), 1, f);
}
for(int j = 0; j < nn; j++)
{
xfwrite(&temp[j].index, sizeof(int), 1, f);
xfwrite(&temp[j].value, sizeof(real), 1, f);
}
}
else
{
fprintf(f, "%d ", w_on_line);
for(int j = 0; j < w_on_line-nn; j++)
fprintf(f, "%d %g ", data[i][j].index, data[i][j].value);
for(int j = 0; j < nn; j++)
fprintf(f, "%d %g ", temp[j].index, temp[j].value);
fprintf(f, "\n");
}
}
fclose(f);
}
IOTorch::~IOTorch()
{
for(int i = 0; i < n_blocks; i++)
destroyBlkMem(i);
}
}
|