void Rnn::lineSearch() { // saving the model Model modelSave(model_); double wordEntropy = 0.0; double charEntropy = 0.0; for (int i=0; i<20; i++) { model_.update(0.001); computeEntropy(wordEntropy, charEntropy); printf("%8.3f ", model_.alpha_ * wordEntropy + (1.0 - model_.alpha_) * charEntropy); } printf("\n"); // returning to original model model_.copy(modelSave); }
bool importedModelSave(const char* fname, GLfloat* verticesBuffer, unsigned int verticesNumber) { std::vector<GLfloat> vertices; std::vector<unsigned int> indices; unsigned int usedIndices = 0; const GLfloat eps = 0.00005f; // real case: 1.0f and 0.999969f should be considered equal for(unsigned int vtx = 0; vtx < verticesNumber; ++vtx) { unsigned int foundIndex = 0; bool indexFound = false; for(unsigned int idx = 0; !indexFound && idx < usedIndices; ++idx) { indexFound = true; for(unsigned int k = 0; indexFound && k < floatsPerVertex; ++k) { // compare X, Y, Z, NX, NY, NZ, U and V if(fabs(verticesBuffer[vtx * floatsPerVertex + k] - vertices[idx * floatsPerVertex + k]) > eps) { indexFound = false; } } if(indexFound) foundIndex = idx; } if(!indexFound) { for(unsigned int k = 0; k < floatsPerVertex; ++k) { // save X, Y, Z, NX, NY, NZ, U and V vertices.push_back(verticesBuffer[vtx * floatsPerVertex + k]); } foundIndex = usedIndices; usedIndices++; } indices.push_back(foundIndex); } unsigned char indexSize = 1; if(verticesNumber > 255) indexSize *= 2; if(verticesNumber > 65535) indexSize *= 2; unsigned int modelSize = (unsigned int) (verticesNumber*floatsPerVertex*sizeof(GLfloat)); unsigned int indexedModelSize = (unsigned int) (usedIndices*floatsPerVertex*sizeof(GLfloat) + verticesNumber*indexSize); float ratio = (float)indexedModelSize*100.0f / (float)modelSize; std::cout << "importedModelSave - fname = " << fname << ", verticesNumber = " << verticesNumber << ", usedIndices = " << usedIndices << std::endl; std::cout << "importedModelSave - modelSize = " << modelSize << ", indexedModelSize = " << indexedModelSize << ", ratio = " << ratio << " %" << std::endl; return modelSave(fname, vertices.data(), usedIndices* floatsPerVertex *sizeof(GLfloat), indices.data(), verticesNumber); }
void Rnn::gradientCheck() { // saving the model Model modelSave(model_); // computing initial cost double initWordEntropy = 0.0; double initCharEntropy = 0.0; computeEntropy(initWordEntropy, initCharEntropy); double initEntropy = model_.alpha_ * initWordEntropy + (1.0 - model_.alpha_) * initCharEntropy; // printf("%8.3f\n", initEntropy); // storage for linearization and difference int nSteps = 30; double* linearization = new double[nSteps]; double* difference = new double[nSteps]; double maxPow = 2; double minPow = -7; double c = pow(10.0, (maxPow - minPow) / (nSteps-1)); double t = - minPow / log10(c); double wordEntropy = 0.0; double charEntropy = 0.0; // model_.pickDeltas(); for (int j=0; j<4; j++) { // pick random direction model_.copy(modelSave); model_.pickDeltas(); for (int i=0; i<nSteps; i++) { double gamma = pow(c, (double)i-t); model_.addDeltas(gamma); computeEntropy(wordEntropy, charEntropy); double entropy = model_.alpha_ * wordEntropy + (1.0 - model_.alpha_) * charEntropy; difference[i] = entropy - initEntropy; linearization[i] = gamma * model_.gradTDelta(); model_.addDeltas(-gamma); } for (int i=0; i<nSteps; i++) { printf("%+10.2e ", linearization[i]); } std::cout << std::endl; for (int i=0; i<nSteps; i++) { printf("%+10.2e ", difference[i]); } std::cout << std::endl; for (int i=0; i<nSteps; i++) { printf("%+10.3f ", difference[i] / linearization[i]); } // std::cout << std::endl; std::cout << std::endl; } delete[] linearization; delete[] difference; // returning to original model model_.copy(modelSave); computeEntropy(wordEntropy, charEntropy); // checking that the entropy is the same as at the beginning }