#include "neural/network.h" #include "neural/column.h" #include "neural/row.h" #include "neural/neuron.h" #include #include #include #include using namespace Bu; #include #include #include int main( int argc, char *argv[] ) { Bu::Random::setGenerator(); Bu::Random::seed( time( NULL ) ); /* Neural::Column *c = new Neural::Column(); Neural::Row *r1 = new Neural::Row(); r1->addNode( new Neural::Neuron() ); r1->addNode( new Neural::Neuron() ); r1->addNode( new Neural::Neuron() ); r1->addNode( new Neural::Neuron() ); r1->addNode( new Neural::Neuron() ); c->addNode( r1 ); Neural::Row *r2 = new Neural::Row(); r2->addNode( new Neural::Neuron() ); r2->addNode( new Neural::Neuron() ); r2->addNode( new Neural::Neuron() ); r2->addNode( new Neural::Neuron() ); r2->addNode( new Neural::Neuron() ); r2->addNode( new Neural::Neuron() ); r2->addNode( new Neural::Neuron() ); c->addNode( r2 ); Neural::Row *r3 = new Neural::Row(); r3->addNode( new Neural::Neuron() ); r3->addNode( new Neural::Neuron() ); r3->addNode( new Neural::Neuron() ); c->addNode( r3 ); */ Neural::Network *pNet = Neural::Network::fromStr( "column(row(5),row(12),row(3))" ); Neural::Node *c = pNet->getRoot(); c->finalize( 2 ); sio << "Total weights: " << c->getNumWeights() << sio.nl; sio << "Total biases: " << c->getNumBiases() << sio.nl; sio << "Network inputs: " << c->getNumInputs() << sio.nl; sio << "Network outputs: " << c->getNumOutputs() << sio.nl; float *pWeights = new float[c->getNumWeights()]; float *pBiases = new float[c->getNumBiases()]; for( int j = 0; j < c->getNumWeights(); j++ ) pWeights[j] = (Bu::Random::randNorm()*2.0)-1.0; for( int j = 0; j < c->getNumBiases(); j++ ) pBiases[j] = (Bu::Random::randNorm()*2.0)-1.0; c->setWeights( pWeights ); c->setBiases( pBiases ); delete pWeights; delete pBiases; float *pIn = new float[c->getNumInputs()]; float *pOut = new float[c->getNumOutputs()]; FILE *fp = fopen("test.png", "wb"); if (!fp) return 1; png_structp png_ptr = png_create_write_struct (PNG_LIBPNG_VER_STRING, NULL, NULL, NULL ); if (!png_ptr) return 1; png_infop info_ptr = png_create_info_struct(png_ptr); if (!info_ptr) { png_destroy_write_struct(&png_ptr, (png_infopp)NULL); return 1; } png_set_IHDR(png_ptr, info_ptr, 500, 500, 8, PNG_COLOR_TYPE_RGB, PNG_INTERLACE_NONE, PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT ); /* Set the zlib compression level */ png_set_compression_level(png_ptr, Z_BEST_COMPRESSION); /* Set other zlib parameters for compressing IDAT */ png_set_compression_mem_level(png_ptr, 8); png_set_compression_strategy(png_ptr, Z_DEFAULT_STRATEGY); png_set_compression_window_bits(png_ptr, 15); png_set_compression_method(png_ptr, 8); png_set_compression_buffer_size(png_ptr, 8192); /* Set zlib parameters for text compression * If you don't call these, the parameters * fall back on those defined for IDAT chunks */ png_set_text_compression_mem_level(png_ptr, 8); png_set_text_compression_strategy(png_ptr, Z_DEFAULT_STRATEGY); png_set_text_compression_window_bits(png_ptr, 15); png_set_text_compression_method(png_ptr, 8); png_bytep *row_pointers; row_pointers = new png_bytep[500]; for( int y = 0; y < 500; y++ ) { row_pointers[y] = new png_byte[500*3]; for( int x = 0; x < 500; x++ ) { pIn[0] = (x/499.0)*2.0-1.0; pIn[1] = (y/499.0)*2.0-1.0; c->process( pIn, pOut ); row_pointers[y][x*3+0] = pOut[0]*127+127; row_pointers[y][x*3+1] = pOut[1]*127+127; row_pointers[y][x*3+2] = pOut[2]*127+127; } } png_init_io( png_ptr, fp ); png_set_rows( png_ptr, info_ptr, row_pointers ); png_write_png(png_ptr, info_ptr, PNG_TRANSFORM_IDENTITY, NULL); if (setjmp(png_jmpbuf(png_ptr))) { png_destroy_write_struct(&png_ptr, &info_ptr); fclose(fp); return 1; } png_destroy_write_struct( &png_ptr, &info_ptr ); fclose( fp ); delete pNet; return 0; }