28 #ifndef UNSUPERVISED_RBM_PROBLEMS_MNIST_H 29 #define UNSUPERVISED_RBM_PROBLEMS_MNIST_H 50 std::string m_filename;
52 std::size_t m_batchSize;
54 int readInt (
unsigned char *memblock)
const{
55 return ((
int)memblock[0] << 24) + ((int)memblock[1] << 16) + ((int)memblock[2] << 8) + memblock[3];
59 std::ifstream infile(m_filename.c_str(), std::ios::binary);
61 std::stringstream str;
62 str<<
"cannot open mnist-file: " << m_filename << std::endl;
67 infile.seekg(0,std::ios::end);
68 std::ifstream::pos_type inputSize = infile.tellg();
71 unsigned char *memblock =
new unsigned char [inputSize];
72 infile.seekg (0, std::ios::beg);
73 infile.read ((
char *) memblock, inputSize);
75 if (readInt(memblock) != 2051){
76 std::stringstream str;
77 str<<
"magic number for mnist wrong: " << readInt(memblock) <<
" != 2051";
80 std::size_t numImages = readInt(memblock + 4);
81 std::size_t numRows = readInt(memblock + 8);
82 std::size_t numColumns = readInt(memblock + 12);
83 std::size_t sizeOfVis = numRows * numColumns;
85 std::vector<RealVector>
data(numImages,RealVector(sizeOfVis));
86 for (std::size_t i = 0; i != numImages; ++i){
87 RealVector imgVec(sizeOfVis);
89 for (
size_t j = 0; j != sizeOfVis; ++j){
90 char pixel = memblock[ 16 + i * sizeOfVis + j ] > m_threshold;
95 for (
size_t j = 0; j != sizeOfVis; ++j){
96 data[i](j) = memblock[ 16 + i * sizeOfVis + j ];
110 : m_filename(filename), m_threshold(threshold), m_batchSize(
batchSize){