void BackProp::createLayers(const Matrix& features, Matrix& labels) { assert(labels.cols() == 1); // create the layers std::vector<size_t> layerConfig; size_t nInputs = features.cols(); layerConfig.push_back(nInputs); size_t firstLayer = nInputs; layerConfig.push_back(firstLayer); //size_t hiddenLayer = firstLayer * 2; //size_t hiddenLayer = 16384; size_t hiddenLayer = _nHidden; layerConfig.push_back(hiddenLayer); size_t outputUnits = 0; for(size_t i = 0; i < labels.cols(); i++) outputUnits += (labels.valueCount(i) > 0 ? labels.valueCount(i) : 1); layerConfig.push_back(outputUnits); if(_loggingOn) std::cout << "Creating network of layers..." << std::endl; if(_layers != NULL) delete _layers; assert(layerConfig.size() > 1); _nLayers = layerConfig.size() - 1; _layers = new BackPropLayer[_nLayers]; for(size_t i = 1; i < layerConfig.size(); i++) { assert(layerConfig[i] > 0); size_t layerIndex = i - 1; _layers[layerIndex] = BackPropLayer(&_rand, layerConfig[i], layerIndex, _learningRate, _momentum, _loggingOn); if(layerIndex == 0) { size_t inputLayerConfig = 0; _layers[layerIndex].setNumInputs(layerConfig[inputLayerConfig]); } } connectLayers(_layers, _nLayers); for(size_t i = 0; i < _nLayers; i++) _layers[i].setRandomWeights(); assert(_nLayers > 0); assert(features.cols() == _layers[0].getNumUnits()); }
NetworkImpl(const vector<unsigned> &layerSizes) { assert(layerSizes.size() >= 2); layers.reserve(layerSizes.size()); // Create the layers. for (unsigned i = 0; i < layerSizes.size(); i++) { NeuronType layerType = (i == 0) ? NeuronType::INPUT : (i == layerSizes.size() - 1) ? NeuronType::OUTPUT : NeuronType::INTERNAL; layers.emplace_back(layerType, layerSizes[i]); } // Connect the layers. for (unsigned i = 0; i < layers.size()-1; i++) { connectLayers(layers[i], layers[i+1]); } }
void BackProp::copyLayers(const BackPropLayer src[], size_t srcLen, BackPropLayer** dest, size_t& destLen) { assert(src != NULL); assert(srcLen > 0); if(*dest != NULL) delete [] *dest; *dest = NULL; assert(srcLen > 0); *dest = new BackPropLayer[srcLen]; destLen = srcLen; for(size_t i = 0; i < srcLen; i++) { (*dest)[i].setNumUnits(src[i].getNumUnits()); (*dest)[i].setLayerId(i); } connectLayers(*dest, destLen); for(size_t i = 0; i < srcLen; i++) (*dest)[i].copyLayerUnits(src[i]); }