Пример #1
0
MyMatrix MyQuat::convertToRotationMatrix(void) const
{
	MyMatrix result;
	GLfloat rotMatrix[16];

	float xx = this->w * this->w;
	float xy = this->w * this->v.x;
	float xz = this->w * this->v.y;
	float xw = this->w * this->v.z;
	float yy = this->v.x * this->v.x;
	float yz = this->v.x * this->v.y;
	float yw = this->v.x * this->v.z;
	float zz = this->v.y * this->v.y;
	float zw = this->v.y * this->v.z;

	rotMatrix[0] = 1 - 2 * (yy + zz);
	rotMatrix[1] = 2 * (xy - zw);
	rotMatrix[2] = 2 * (xz + yw);
	rotMatrix[4] = 2 * (xy + zw);
	rotMatrix[5] = 1 - 2 * (xx + zz);
	rotMatrix[6] = 2 * (yz - xw);
	rotMatrix[8] = 2 * (xz - yw);
	rotMatrix[9] = 2 * (yz + xw);
	rotMatrix[10] = 1 - 2 * (xx + yy);
	rotMatrix[3] = rotMatrix[7] = rotMatrix[11] = rotMatrix[12] = rotMatrix[13] = rotMatrix[14] = 0;
	rotMatrix[15] = 1;

	result.setMyMatrix(rotMatrix);
	return result;
}
Пример #2
0
void * Chi2Lib::computeDifferenceThread( void* ptr){
	PartitionDiff *part = (PartitionDiff*) ptr;
	MyMatrix<double> *img = part->img;
	MyMatrix<double> *grid_x = part->grid_x;
	MyMatrix<double> *grid_y = part->grid_y;
	double d = part->d;
	double w = part->w;
	MyMatrix<double> *diffout = part->diffout;

	MyLogger::log()->debug("[Chi2Lib][computeDifferenceThread] Computing Chi2 Difference");
	double chi2err = 0.0;
	for(unsigned int x=part->x1; x < part->x2; ++x){
		for(unsigned int y=part->y1; y < part->y2; ++y){
			double x2y2 = sqrt(1.0*grid_x->getValue(x,y)*grid_x->getValue(x,y) + 1.0*grid_y->getValue(x,y)*grid_y->getValue(x,y));
			double temp = ((1.0-tanh( (x2y2-d/2.0)/w )) - 2.0*img->getValue(x,y))/2.0;

			diffout->at(x,y) = temp;
			chi2err += temp*temp;
		}
	}
	MyLogger::log()->debug("[Chi2Lib][computeDifferenceThread] Chi2 Error: %f", chi2err);

	part->err = chi2err;
	return 0;
}
Пример #3
0
CellMatrix::CellMatrix(const MyMatrix& data): Cells(data.rows()), 
	Rows(data.rows()), Columns(data.columns())
{
	for (unsigned long i=0; i < data.rows(); ++i)
		for (unsigned long j=0; j < data.columns(); ++j)
			Cells[i].push_back(CellValue(Element(data,i,j)));
}
Пример #4
0
void MenuSlider::Draw(MyMatrix* pMatProj, MyMatrix* pMatView)
{
    float centerx = m_PosX;
    float top = m_PosY;
    // TODO: take more justifications and vertical/horizontal into account for top and left.
    //       should ideally be done once instead of every frame.
    //if( m_Justification & Justify_Left )
    //{
    //    centerx += m_BarThickness/2;
    //}

    float emptypos = (m_PosY - m_VisualRange);

    MyAssert( m_pSprite );
    //m_pSprite = g_pGame->m_pResources->m_pSprites[SL_WhiteSquareResizable];

    if( m_pSprite )
    {
        BufferManager* pBufferManager = m_pGameCore->GetManagers()->GetBufferManager();
        m_pSprite->Create( pBufferManager, "MenuSlider", m_BarThickness, m_VisualRange, 0, 1, 0, 1, Justify_CenterX|Justify_Top );
        MyMatrix world;
        world.SetIdentity();
        world.SetTranslation( centerx, top, 0 );
        //FIX m_pSprite->SetTint( m_Colors[MSCT_BarColor] );
        m_pSprite->Draw( pMatProj, pMatView, &world ); //&g_pGame->m_OrthoMatrix );

        m_pSprite->Create( pBufferManager, "MenuSlider", m_HandleWidth, m_HandleHeight, 0, 1, 0, 1, Justify_CenterX|Justify_Top );
        world.SetTranslation( centerx, emptypos + m_ValuePerc*m_VisualRange, 0 );
        //FIX m_pSprite->SetTint( m_Colors[MSCT_HandleColor] );
        m_pSprite->Draw( pMatProj, pMatView, &world ); //&g_pGame->m_OrthoMatrix );
    }
}
Пример #5
0
xlw::CellMatrix::CellMatrix(const MyMatrix& data): Cells(data.size1()),
    Rows(data.size1()), Columns(data.size2())
{
    for (size_t i=0; i < data.size1(); ++i)
        for (size_t j=0; j < data.size2(); ++j)
            Cells[i].push_back(CellValue(Element(data,i,j)));
}
Пример #6
0
MyMatrix MyMatrix::operator+(const MyMatrix& A)
{
    MyMatrix result;
    result.setN(fN);
    result.setM(fM);
    result.fMatrix = fMatrix+A.fMatrix;
    return result;
}
Пример #7
0
double BSDeltaWithParamsFD(const MyMatrix& parametersMatrix, double epsilon) {
	if (parametersMatrix.columns() != 6 && parametersMatrix.rows() != 1 ) {
	throw("Input matrix should be 1 x 5");}
	double Spot =  parametersMatrix(0,0);
	double Strike =  parametersMatrix(0,1);
	double r =  parametersMatrix(0,2);
	double d =  parametersMatrix(0,3);
	double vol =  parametersMatrix(0,4);
	double expiry = parametersMatrix(0,5); 
	return BlackScholesDeltaFD(Spot, Strike,r,d,vol,expiry, epsilon);
}
Пример #8
0
double BSZeroCouponBondWithParams(const MyMatrix& parametersMatrix){
	if (parametersMatrix.columns() != 6 && parametersMatrix.rows() != 1 ) {
	throw("Input matrix should be 1 x 5");}
	double Spot =  parametersMatrix(0,0);
	double Strike =  parametersMatrix(0,1);
	double r =  parametersMatrix(0,2);
	double d =  parametersMatrix(0,3);
	double vol =  parametersMatrix(0,4);
	double expiry = parametersMatrix(0,5); 
	return BlackScholesZeroCouponBond(Spot, Strike,r,d,vol,expiry);
}
Пример #9
0
		CellMatrix::CellMatrix(const MyMatrix& data):pimpl(new defaultCellMatrixImpl(data.rows(),data.columns()))
		{
			
			for(size_t i(0); i < data.rows(); ++i)
			{
				for(size_t j(0); j < data.columns(); ++j)
				{
				  (*pimpl)(i,j) = data(i,j);
				}
			}
			
		}
Пример #10
0
void MyMatrix::RotateZ(float angle)
{
	float sinAngle, cosAngle;
	sinAngle = sinf ( angle * PI / 180.0f );
	cosAngle = cosf ( angle * PI / 180.0f );
	MyMatrix temp;
	temp.LoadIndentity();
	temp.m[0][0] = temp.m[1][1] = cosAngle;
	temp.m[0][1] = sinAngle;
	temp.m[1][0] = -sinAngle;
	MatrixMultiply(&temp,this);
}
Пример #11
0
	void operator/(const MyMatrix &b){	// transpose
		for(int i = 0; i < _dim; i++){
			for(int j = 0; j < _dim; j++){
				vecvec[i][j] = b.get(j,i);
			}
		}
	}
Пример #12
0
	void operator-=(const MyMatrix &b){
		for(int i = 0; i < _dim; i++){
			for(int j = 0; j < _dim; j++){
				int tmp= (int) (vecvec[i][j]- b.get(i, j));
				vecvec[i][j]=tmp;
			}
		}
	}
Пример #13
0
CellMatrix //
BSGreeksFD(const MyMatrix& parametersMatrix,double epsilon) {
		if (parametersMatrix.columns() != 6 && parametersMatrix.rows() != 1 ) {
		throw("Input matrix should be 1 x 5");}
	double Spot =  parametersMatrix(0,0);
	double Strike =  parametersMatrix(0,1);
	double r =  parametersMatrix(0,2);
	double d =  parametersMatrix(0,3);
	double vol =  parametersMatrix(0,4);
	double expiry = parametersMatrix(0,5); 
	CellMatrix resultMatrix(1,5); 
	resultMatrix(0,0) = BlackScholesDeltaFD(Spot,Strike,r,d,vol,expiry,epsilon);
	resultMatrix(0,1) = BlackScholesGammaFD(Spot,Strike,r,d,vol,expiry,epsilon);
	resultMatrix(0,2) = BlackScholesVegaFD(Spot,Strike,r,d,vol,expiry,epsilon);
	resultMatrix(0,3) = BlackScholesRhoFD(Spot,Strike,r,d,vol,expiry,epsilon);
	resultMatrix(0,4) = BlackScholesThetaFD(Spot,Strike,r,d,vol,expiry,epsilon);
	return resultMatrix;
}
Пример #14
0
CellMatrix // returns delta, gamma, vega, rho, theta
BSGreeks(const MyMatrix& parametersMatrix) {
	if (parametersMatrix.columns() != 6 && parametersMatrix.rows() != 1 ) {
		throw("Input matrix should be 1 x 5");}
	double Spot =  parametersMatrix(0,0);
	double Strike =  parametersMatrix(0,1);
	double r =  parametersMatrix(0,2);
	double d =  parametersMatrix(0,3);
	double vol =  parametersMatrix(0,4);
	double expiry = parametersMatrix(0,5); 
	CellMatrix resultMatrix(1,5); 
	resultMatrix(0,0) = BlackScholesDelta(Spot,Strike,r,d,vol,expiry);
	resultMatrix(0,1) = BlackScholesGamma(Spot,Strike,r,d,vol,expiry);
	resultMatrix(0,2) = BlackScholesVega(Spot,Strike,r,d,vol,expiry);
	resultMatrix(0,3) = BlackScholesRho(Spot,Strike,r,d,vol,expiry);
	resultMatrix(0,4) = BlackScholesTheta(Spot,Strike,r,d,vol,expiry);
	return resultMatrix;
}
Пример #15
0
/**********************************************************
 * 
 * SetViewTransformation
 * 
 * parameters IN :
 *	MyMatrix & viewTransformation
 * 
 *********************************************************/
void ViewPointRec::SetViewTransformation (MyMatrix & viewTransformation) {
  	viewTransformation.Identity();

  	viewTransformation[0][0] = -sinTheta;
  	viewTransformation[0][1] = -cosTheta * cosPhi;
  	viewTransformation[0][2] = -cosTheta * sinPhi;
  	viewTransformation[1][0] =  cosTheta;
  	viewTransformation[1][1] = -sinTheta * cosPhi;
  	viewTransformation[1][2] = -sinTheta * sinPhi;
  	viewTransformation[2][1] =  sinPhi;
  	viewTransformation[2][2] = -cosPhi;
  	viewTransformation[3][2] =  rho;
}
Пример #16
0
MyVector operator*(const MyMatrix& lhs ,const MyVector& rhs) {
    MyVector y = MyVector(lhs.rows_);
    if (lhs.columns_ == rhs.size()) {
        for(long long int i = 1; i <= lhs.rows_; i++) {
            for(long long int j = 1; j <= lhs.columns_; j++) {
                y[i-1] +=  rhs[j-1] *lhs.at(i,j);
            }
        }
    } else {
        cout << "Matrix-Vektor-Multiplikation nicht möglich  Dimensionen stimmen nicht überein!" << endl;
    }
    return y;
}
Пример #17
0
MyMatrix MyMatrix::operator*(const MyMatrix& rhs) {
    MyMatrix lhs = *this;
    auto n=lhs.rows_;
    auto m = lhs.columns_;
    auto p = rhs.columns_;
    MyMatrix y = MyMatrix(n,p);
    if (m == rhs.rows_) {
        for(long long int i = 1; i <= n; i++) {
            for(long long int j = 1; j <= p; j++) {
                y.mval_[(i-1)*(p) + (j-1)]=0;
                for(long long int k = 1; k <= m; k++) {
                    y.mval_[(i-1)*(p) + (j-1)] +=  lhs.at(i,k)*rhs.at(k,j) ;
                }
            }
        }
    }

    else {
        cout << "Dimensionen stimmen nicht überein!" << endl;
    }
    return y;
}
Пример #18
0
	void operator*=(const MyMatrix &b){
		for(int i = 0; i < _dim; i++){
			std::vector<float> temporalVectorA((unsigned long) _dim,0);
			for (int h = 0; h < _dim; h++){
				temporalVectorA[h] = vecvec[i][h];
			}
			for(int k = 0; k < _dim; k++){
				std::vector<float> temporalVectorB((unsigned long) _dim,0);
				for(int l = 0; l < _dim; l++){
					temporalVectorB[l] = b.get(l, k);
				}
				vecvec[i][k] = vectorMultiply(temporalVectorA, temporalVectorB);
			}
		}
	}
Пример #19
0
void DACE::build_R(double **ax, MyMatrix& R)
{
    // takes the array of x vectors, theta, and p, and returns the correlation matrix R.
    for(int i=0;i<fCorrelationSize;i++)
    {
        for(int j=0;j<fCorrelationSize;j++)
        {
            if(Debug)
                fprintf(stdout,"%.5lf %.5lf %.5lf  %.5lf %d\n", ax[i][1],ax[j][1],gtheta[1], gp[1], daceSpace->fSearchSpaceDim);
            R.insert(i,j,correlation(ax[i+1],ax[j+1], gtheta, gp, fCorrelationSize));
            if(Debug)
                fprintf(stdout,"%.5lf\n", R(i,j));
        }
        if(Debug)
            fprintf(stdout,"\n");
    }
    
}
Пример #20
0
bool MyMatrix::operator==(const MyMatrix& a) const
{
  if (this->rows() != a.rows()) return false;
  if (this->columns() != a.columns()) return false;
  return(((EigenMatrix)(*this)-(EigenMatrix)a).isApproxToConstant(0.0));
}
Пример #21
0
int MyMeshText::CreateString(bool concat, float fontheight, float x, float y, float z, float rotz, unsigned char justificationflags, ColorByte color, Vector2 size, const char* text, ...)
{
    assert( m_pFont && m_pFont->m_pFont );

    if( strlen( text ) == 0 )
        return 0;

    const char* stringtodraw = text;
    if( g_pLanguageTable != 0 && text[0] == '.' )
        stringtodraw = g_pLanguageTable->LookUp( text );

    int numlines = 0;

    if( concat == false )
    {
        ClearText();
    }

    bool moretexttocome = true;
    const char* stringpos = stringtodraw;

    while( moretexttocome )
    {
        numlines++;

        char singlelinebuffer[300];
        singlelinebuffer[0] = 0;
        char* singlelinebufferpos = singlelinebuffer;

        // word wrap if width of text is not 0.
        if( size.x != 0 )
        {
            float linewidth = -1;// = GetStringSize( fontheight, Vector2(0,0), singlelinebuffer ).x;
            while( linewidth < size.x &&
                    *stringpos != 0 )
            {
                *singlelinebufferpos = *stringpos;
                singlelinebufferpos++;
                *singlelinebufferpos = 0;
                stringpos++;

                linewidth = GetStringSize( fontheight, Vector2(0,0), singlelinebuffer ).x;

                assert( singlelinebufferpos < singlelinebuffer + 300 );
            }

            int numcharswewentback = 0;
            while( ( *(singlelinebufferpos-1) != ' ' && *stringpos != 0 ) &&
                    singlelinebufferpos > singlelinebuffer )
            {
                singlelinebufferpos--;
                numcharswewentback++;
            }

            if( singlelinebufferpos != singlelinebuffer )
            {
                *singlelinebufferpos = 0;
                stringpos -= numcharswewentback;
            }

            if( *stringpos == 0 )
                moretexttocome = false;

            stringtodraw = singlelinebuffer;
        }
        else
        {
            moretexttocome = false;
        }
            
        //// don't bother drawing if fontheight is zero... still doing logic above so the currect number of lines will be returned.
        //if( g_pRTQGlobals->m_WordWrapCountLinesOnly )
        //    continue;

        Vertex_XYZUV_RGBA* pVertsToDraw = (Vertex_XYZUV_RGBA*)GetVerts( true );

        int newverts = (int)strlen( stringtodraw ) * 4;
#if _DEBUG
        m_MostLettersAttemptedToDrawThisFrame += newverts/4;
        if( m_MostLettersAttemptedToDrawThisFrame > m_MostLettersAttemptedToDrawEver )
            m_MostLettersAttemptedToDrawEver = m_MostLettersAttemptedToDrawThisFrame;
#endif

        if( m_NumVertsToDraw + newverts > GetNumVerts() )
        {
#if _DEBUG
            LOGInfo( LOGTag, "TextMesh buffer isn't big enough for string (%s) - %d of %d letters used - most letters needed (%d)\n", stringtodraw, m_NumVertsToDraw/4, GetNumVerts()/4, m_MostLettersAttemptedToDrawEver );
#endif
            //assert( false ); // drawing more than we have room for.
            return 0;
        }

        pVertsToDraw += m_NumVertsToDraw;

        unsigned int textstrlen = m_pFont->m_pFont->GenerateVerts( stringtodraw, true, pVertsToDraw, fontheight, GL_TRIANGLES, justificationflags, color );

        m_NumVertsToDraw += (unsigned short)(textstrlen * 4);
        m_NumIndicesToDraw += textstrlen * 6;

        MyMatrix position;
        position.SetIdentity();
        position.Rotate( rotz, 0, 0, 1 );
        position.SetPosition( x, y - (numlines-1)*fontheight, z );
        //position.SetPosition( x, y - (numlines-1)*g_pRTQGlobals->m_WordWrapLineIncSize, z );
        //position.SetPosition( x, y, z );

        for( unsigned int i=0; i<textstrlen*4; i++ )
        {
            Vector3 out = position.TransformVector3( *(Vector3*)&pVertsToDraw[i].x );
            pVertsToDraw[i].x = out.x;
            pVertsToDraw[i].y = out.y;
            pVertsToDraw[i].z = out.z;
        }
    }

    return numlines;
}
Пример #22
0
int main(int argc, char *argv[]){
  
	Params params;
  
	std::map<std::string, std::string> args;
	readArgs(argc, argv, args);
	if(args.find("algo")!=args.end()){
		params.algo = args["algo"];
	}else{
		params.algo = "qdMCNat";
	}

	if(args.find("inst_file")!=args.end())
		setParamsFromFile(args["inst_file"], args, params);
	else   
		setParams(params.algo, args, params);
  
	createLogDir(params.dir_path);
  
	gen.seed(params.seed);

	// Load the dataset
	MyMatrix X_train, X_valid;
	VectorXd Y_train, Y_valid;
	loadMnist(params.ratio_train, X_train, X_valid, Y_train, Y_valid);
	//loadCIFAR10(params.ratio_train, X_train, X_valid, Y_train, Y_valid);
	//loadLightCIFAR10(params.ratio_train, X_train, X_valid, Y_train, Y_valid);
  
	// ConvNet parameters
	std::vector<ConvLayerParams> conv_params;
	ConvLayerParams conv_params1;
	conv_params1.Hf = 5;
	conv_params1.stride = 1;
	conv_params1.n_filter = 20;
	conv_params1.padding = 0;
	conv_params.push_back(conv_params1);
  
	ConvLayerParams conv_params2;
	conv_params2.Hf = 5;
	conv_params2.stride = 1;
	conv_params2.n_filter = 50;
	conv_params2.padding = 0;
	conv_params.push_back(conv_params2);

	std::vector<PoolLayerParams> pool_params;
	PoolLayerParams pool_params1;
	pool_params1.Hf = 2;
	pool_params1.stride = 2;
	pool_params.push_back(pool_params1);

	PoolLayerParams pool_params2;
	pool_params2.Hf = 2;
	pool_params2.stride = 2;
	pool_params.push_back(pool_params2);
  
	const unsigned n_conv_layer = conv_params.size();
  
	for(unsigned l = 0; l < conv_params.size(); l++){

		if(l==0){
			conv_params[l].filter_size = conv_params[l].Hf * conv_params[l].Hf * params.img_depth;
			conv_params[l].N = (params.img_width - conv_params[l].Hf + 2*conv_params[l].padding)/conv_params[l].stride + 1;
		}
		else{
			conv_params[l].filter_size = conv_params[l].Hf * conv_params[l].Hf * conv_params[l-1].n_filter;
			conv_params[l].N = (pool_params[l-1].N - conv_params[l].Hf + 2*conv_params[l].padding)/conv_params[l].stride + 1;
		}
		pool_params[l].N = (conv_params[l].N - pool_params[l].Hf)/pool_params[l].stride + 1;
	}
  
	// Neural Network parameters
	const unsigned n_training = X_train.rows();
	const unsigned n_valid = X_valid.rows();
	const unsigned n_feature = X_train.cols();
	const unsigned n_label = Y_train.maxCoeff() + 1;
  
	params.nn_arch.insert(params.nn_arch.begin(),conv_params[n_conv_layer-1].n_filter * pool_params[n_conv_layer-1].N * pool_params[n_conv_layer-1].N);
	params.nn_arch.push_back(n_label);
	const unsigned n_layers = params.nn_arch.size();
  
	// Optimization parameter
	const int n_train_batch = ceil(n_training/(float)params.train_minibatch_size);
	const int n_valid_batch = ceil(n_valid/(float)params.valid_minibatch_size);
	double prev_loss = std::numeric_limits<double>::max();
	double eta = params.eta;

	// Create the convolutional layer
	std::vector<MyMatrix> conv_W(n_conv_layer);
	std::vector<MyMatrix> conv_W_T(n_conv_layer);
	std::vector<MyVector> conv_B(n_conv_layer);
  
	// Create the neural network
	MyMatrix W_out(params.nn_arch[n_layers-2],n_label);
	std::vector<MySpMatrix> W(n_layers-2);
	std::vector<MySpMatrix> Wt(n_layers-2);
	std::vector<MyVector> B(n_layers-1);

	double init_sigma = 0.;
	ActivationFunction act_func;
	ActivationFunction eval_act_func;
	if(params.act_func_name=="sigmoid"){
		init_sigma = 4.0;
		act_func = std::bind(logistic,true,_1,_2,_3);
		eval_act_func = std::bind(logistic,false,_1,_2,_3);
	}else if(params.act_func_name=="tanh"){
		init_sigma = 1.0;
		act_func = std::bind(my_tanh,true,_1,_2,_3);
		eval_act_func = std::bind(my_tanh,false,_1,_2,_3);
	}else if(params.act_func_name=="relu"){
		init_sigma = 1.0; // TODO: Find the good value
		act_func = std::bind(relu,true,_1,_2,_3);
		eval_act_func = std::bind(relu,false,_1,_2,_3);
	}else{
		std::cout << "Not implemented yet!" << std::endl;
		assert(false);
	}

	std::cout << "Initializing the network... ";
	params.n_params = initNetwork(params.nn_arch, params.act_func_name, params.sparsity, conv_params, pool_params, W_out, W, Wt, B, conv_W, conv_W_T, conv_B); // TODO: Init the conv bias

	// Deep copy of parameters for the adaptive rule
	std::vector<MyMatrix> mu_dW(n_layers-1);
	std::vector<MyVector> mu_dB(n_layers-1);

	MyMatrix pW_out = W_out;
	std::vector<MySpMatrix> pW = W;
	std::vector<MySpMatrix> pWt = Wt;
	std::vector<MyVector> pB = B;

	MyMatrix ppMii_out, ppM0i_out;
	MyVector ppM00_out;
  
	std::vector<MySpMatrix> ppMii,ppM0i;
	std::vector<MyVector> ppM00;

	MyMatrix pMii_out,pM0i_out;
	MyVector pM00_out;
  
	std::vector<MySpMatrix> pMii,pM0i;
	std::vector<MyVector> pM00;

	std::vector<MyMatrix> conv_ppMii, conv_ppM0i;
	std::vector<MyVector> conv_ppM00;

	std::vector<MyMatrix> conv_pMii, conv_pM0i;
	std::vector<MyVector> conv_pM00;
  
	// Convert the labels to one-hot vector
	MyMatrix one_hot = MyMatrix::Zero(n_training, n_label);
	labels2oneHot(Y_train,one_hot);
  
	// Configure the logger 
	std::ostream* logger;
	if(args.find("verbose")!=args.end()){
		getOutput("",logger);
	}else{
		getOutput(params.file_path,logger);
	}

	double cumul_time = 0.;
  
	printDesc(params, logger);
	printConvDesc(params, conv_params, pool_params, logger);
	std::cout << "Starting the learning phase... " << std::endl;
	*logger << "Epoch Time(s) train_loss train_accuracy valid_loss valid_accuracy eta" << std::endl;
  
	for(unsigned i = 0; i < params.n_epoch; i++){
		for(unsigned j = 0; j < n_train_batch; j++){
      
			// Mini-batch creation
			unsigned curr_batch_size = 0;
			MyMatrix X_batch, one_hot_batch;
			getMiniBatch(j, params.train_minibatch_size, X_train, one_hot, params, conv_params[0], curr_batch_size, X_batch, one_hot_batch);
      
			double prev_time = gettime();

			// Forward propagation for conv layer
			std::vector<std::vector<unsigned>> poolIdxX1(n_conv_layer);
			std::vector<std::vector<unsigned>> poolIdxY1(n_conv_layer);
      
			MyMatrix z0;
			std::vector<MyMatrix> conv_A(conv_W.size());
			std::vector<MyMatrix> conv_Ap(conv_W.size());
			convFprop(curr_batch_size, conv_params, pool_params, act_func, conv_W, conv_B, X_batch, conv_A, conv_Ap, z0, poolIdxX1, poolIdxY1);
            
			// Forward propagation
			std::vector<MyMatrix> Z(n_layers-1);
			std::vector<MyMatrix> A(n_layers-2);
			std::vector<MyMatrix> Ap(n_layers-2);
			fprop(params.dropout_flag, act_func, W, W_out, B, z0, Z, A, Ap);
      
			// Compute the output and the error
			MyMatrix out;
			softmax(Z[n_layers-2], out);
      
			std::vector<MyMatrix> gradB(n_layers-1);
			gradB[n_layers-2] = out - one_hot_batch;

			// Backpropagation
			bprop(Wt, W_out, Ap, gradB);

			// Backpropagation for conv layer
			std::vector<MyMatrix> conv_gradB(conv_W.size());
			MyMatrix layer_gradB = (gradB[0] * W[0].transpose());
			MyMatrix pool_gradB;
			layer2pool(curr_batch_size, pool_params[conv_W.size()-1].N, conv_params[conv_W.size()-1].n_filter, layer_gradB, pool_gradB);
      
			convBprop(curr_batch_size, conv_params, pool_params, conv_W_T, conv_Ap, pool_gradB, conv_gradB, poolIdxX1, poolIdxY1);
      
			if(params.algo == "bprop"){
				update(eta, gradB, A, z0, params.regularizer, params.lambda, W_out, W, Wt, B);
				convUpdate(curr_batch_size, eta, conv_params, conv_gradB, conv_A, X_batch, "", 0., conv_W, conv_W_T, conv_B);
	
			}else{

				// Compute the metric
				std::vector<MyMatrix> metric_gradB(n_layers-1);
				std::vector<MyMatrix> metric_conv_gradB(conv_params.size());

				if(params.algo=="qdMCNat"){

					// Monte-Carlo Approximation of the metric
					std::vector<MyMatrix> mc_gradB(n_layers-1);
					computeMcError(out, mc_gradB[n_layers-2]);

					// Backpropagation
					bprop(Wt, W_out, Ap, mc_gradB);

					for(unsigned k = 0; k < gradB.size(); k++){
						metric_gradB[k] = mc_gradB[k].array().square();
					}

					// Backpropagation for conv layer
					std::vector<MyMatrix> mc_conv_gradB(conv_W.size());
					MyMatrix mc_layer_gradB = (mc_gradB[0] * W[0].transpose());
					MyMatrix mc_pool_gradB;
					layer2pool(curr_batch_size, pool_params[conv_W.size()-1].N, conv_params[conv_W.size()-1].n_filter, mc_layer_gradB, mc_pool_gradB);
	  
					convBprop(curr_batch_size, conv_params, pool_params, conv_W_T, conv_Ap, mc_pool_gradB, mc_conv_gradB, poolIdxX1, poolIdxY1);
	  
					for(unsigned k = 0; k < conv_params.size(); k++){
						metric_conv_gradB[k] = mc_conv_gradB[k].array().square();
					}
				}	
				else if(params.algo=="qdop"){

					for(unsigned k = 0; k < conv_params.size(); k++){
						metric_conv_gradB[k] = conv_gradB[k].array().square();
					}
					for(unsigned k = 0; k < gradB.size(); k++){
						metric_gradB[k] = gradB[k].array().square();
					}
				}
				else if(params.algo=="qdNat"){
	  
					for(unsigned k = 0; k < conv_params.size(); k++){
						metric_conv_gradB[k] = conv_gradB[k].array().square();
					}

					for(unsigned k = 0; k < metric_gradB.size(); k++){
						metric_gradB[k] = MyMatrix::Zero(gradB[k].rows(),gradB[k].cols());
					}

					for(unsigned l = 0; l < n_label; l++){
						MyMatrix fisher_ohbatch = MyMatrix::Zero(curr_batch_size, n_label);
						fisher_ohbatch.col(l).setOnes();

						std::vector<MyMatrix> fgradB(n_layers-1);
						fgradB[n_layers-2] = out - fisher_ohbatch;
						bprop(Wt, W_out, Ap, fgradB);

						// Backpropagation for conv layer
						std::vector<MyMatrix> fisher_conv_gradB(conv_W.size());
						MyMatrix fisher_layer_gradB = (fgradB[0] * W[0].transpose());
						MyMatrix fisher_pool_gradB;
						layer2pool(curr_batch_size, pool_params[conv_W.size()-1].N, conv_params[conv_W.size()-1].n_filter, fisher_layer_gradB, fisher_pool_gradB);
	    
						convBprop(curr_batch_size, conv_params, pool_params, conv_W_T, conv_Ap, fisher_pool_gradB, fisher_conv_gradB, poolIdxX1, poolIdxY1);

						for(unsigned k = 0; k < conv_params.size(); k++){
							MyMatrix fisher_conv_gradB_sq = fisher_conv_gradB[k].array().square();
							for(unsigned m = 0; m < out.rows(); m++){
								for(unsigned f = 0; f < conv_params[k].n_filter; f++){
									for(unsigned n = 0; n < conv_params[k].N * conv_params[k].N; n++){
										fisher_conv_gradB_sq(f,m*conv_params[k].N*conv_params[k].N+n) *= out(m,l);
									}
								}
							}
							metric_conv_gradB[k] += fisher_conv_gradB_sq;
						}
	    
						for(unsigned k = 0; k < W.size(); k++){
							const unsigned rev_k = n_layers - k - 2;
							metric_gradB[rev_k] += (fgradB[rev_k].array().square().array().colwise() * out.array().col(l)).matrix();
						}
					}
				}
	
				bool init_flag = false;
				if(i == 0 && j == 0 && !params.init_metric_id){
					init_flag = true;
				}

				std::vector<MyMatrix> conv_Mii(conv_params.size());
				std::vector<MyMatrix> conv_M0i(conv_params.size());
				std::vector<MyVector> conv_M00(conv_params.size());
	
				buildConvQDMetric(curr_batch_size, metric_conv_gradB, conv_A, X_batch, conv_W, params.matrix_reg, conv_Mii, conv_M0i, conv_M00);

				updateConvMetric(init_flag, params.metric_gamma, conv_pMii, conv_pM0i, conv_pM00, conv_Mii, conv_M0i, conv_M00);

				MyMatrix Mii_out, M0i_out;
				MyVector M00_out;
				std::vector<MySpMatrix> Mii(W.size());
				std::vector<MySpMatrix> M0i(W.size());
				std::vector<MyVector> M00(W.size());

				buildQDMetric(metric_gradB, A, z0, W_out, W, params.matrix_reg, Mii_out, M0i_out, M00_out, Mii, M0i, M00);

				updateMetric(init_flag, params.metric_gamma, Mii_out, M0i_out, M00_out, Mii, M0i, M00, pMii_out, pM0i_out, pM00_out, pMii, pM0i, pM00);
				update(eta, gradB, A, z0, params.regularizer, params.lambda, W_out, W, Wt, B, Mii_out, M0i_out, M00_out, Mii, M0i, M00);
			}
      
			double curr_time = gettime();
			cumul_time += curr_time - prev_time;      
      
			if(params.minilog_flag){
	
				double train_loss = 0.;
				double train_accuracy = 0.;
				double valid_loss = 0.;
				double valid_accuracy = 0.;
				evalModel(eval_act_func, params, n_train_batch, n_training, X_train, Y_train, conv_params, pool_params, conv_W, conv_B, W_out, W, B, train_loss, train_accuracy);
				evalModel(eval_act_func, params, n_valid_batch, n_valid, X_valid, Y_valid, conv_params, pool_params, conv_W, conv_B, W_out, W, B, valid_loss, valid_accuracy);
	
				// Logging
				*logger << i + float(j)/n_train_batch << " " << cumul_time << " " << train_loss <<  " " << train_accuracy << " " << valid_loss <<  " " << valid_accuracy << " " << eta << std::endl;
	
			}
		}
		if(!params.minilog_flag || params.adaptive_flag){
			double train_loss = 0.;
			double train_accuracy = 0.;
			double valid_loss = 0.;
			double valid_accuracy = 0.;
			evalModel(eval_act_func, params, n_train_batch, n_training, X_train, Y_train, conv_params, pool_params, conv_W, conv_B, W_out, W, B, train_loss, train_accuracy);
			evalModel(eval_act_func, params, n_valid_batch, n_valid, X_valid, Y_valid, conv_params, pool_params, conv_W, conv_B, W_out, W, B, valid_loss, valid_accuracy);
      
			// if(params.adaptive_flag)
			// 	adaptiveRule(train_loss, prev_loss, eta, W, B, pMii, pM0i, pM00, pW, pB, ppMii, ppM0i, ppM00);
      
			// Logging
			if(!params.minilog_flag){
				*logger << i  << " " << cumul_time << " " << train_loss <<  " " << train_accuracy << " " << valid_loss <<  " " << valid_accuracy << " " << eta << std::endl;
			}
		}
	}
}
Пример #23
0
void createHamiltonian(Geometry const &geometry, DynVars const &dynVars,
                       MyMatrix<std::complex<double> >& matrix,Parameters const &ether,Aux &aux,int type)
// Modified by IS Nov-08-04

{
    int	col, volume, i = 0, p,dir;
    tpem_t hopping,hopping2,bandHop;
    double	 tmp,tmp2;
    int	j,iTmp;
    volume = ether.linSize;
    tpem_t S_ij;
    //static vector<double> phonon_q1(volume);
    //static vector<double> phonon_q2(volume);
    //static vector<double> phonon_q3(volume);


    Phonons<Parameters,Geometry> phonons(ether,geometry);
    for (p = 0; p < matrix.getRank(); p++)
        for (col = 0; col < matrix.getRank(); col++)
            matrix(p,col)=0;

    for (p = 0; p < volume; p++) {
        double phonon_q1=phonons.calcPhonon(p,dynVars,0);
        double phonon_q2=phonons.calcPhonon(p,dynVars,1);
        double phonon_q3=phonons.calcPhonon(p,dynVars,2);
        matrix(p,p) = ether.phononEjt[0]*phonon_q1+ether.phononEjt[2]*phonon_q3+ether.potential[p];
        matrix(p+volume,p+volume) = -ether.phononEjt[2]*phonon_q3+ether.phononEjt[0]*phonon_q1+ether.potential[p];
        matrix(p,p+volume) = (ether.phononEjt[1]*phonon_q2);
        matrix(p+volume,p) = conj(matrix(p,p+volume));

        for (j = 0; j < geometry.z(p); j++) {	/* hopping elements */
            iTmp=geometry.borderId(p,j);;
            if (iTmp>=0) hopping = ether.hoppings[iTmp];
            else hopping= -1.0;
            col = geometry.neighbor(p,j);
            tmp=cos(0.5*dynVars.theta[p])*cos(0.5*dynVars.theta[col]);
            tmp2=sin(0.5*dynVars.theta[p])*sin(0.5*dynVars.theta[col]);
            S_ij=tpem_t(tmp+tmp2*cos(dynVars.phi[p]-dynVars.phi[col]),
                        -tmp2*sin(dynVars.phi[p]-dynVars.phi[col]));

            if (p>col) hopping2 = conj(hopping);
            else hopping2=hopping;
            dir = int(j/2);

            bandHop=ether.bandHoppings[0+0*2+dir*4];
            hopping=hopping2 * bandHop ;
            matrix(p,col) = hopping * S_ij;
            matrix(col,p) = conj(hopping * S_ij);

            bandHop=ether.bandHoppings[0+1*2+dir*4];
            hopping= hopping2 * bandHop;
            matrix(p, col+volume)=hopping * S_ij;
            matrix(col+volume,p)=conj(matrix(p,col+volume));

            //
            bandHop=ether.bandHoppings[1+0*2+dir*4];
            hopping= hopping2 * bandHop;
            matrix(p+volume,col) =  hopping * S_ij;
            matrix(col,p+volume) =  conj(matrix(p+volume,col));

            bandHop=ether.bandHoppings[1+1*2+dir*4];
            hopping=hopping2 * bandHop;
            matrix(p+volume,col+volume) =  hopping * S_ij;
            matrix(col+volume,p+volume) = conj(matrix(p+volume,col+volume));
        }

        if (ether.isSet("tprime")) {
            for (j = 0; j < geometry.z(p,2); j++) {	/* hopping elements */
                iTmp=geometry.borderId(p,j,2);
                hopping= -1.0;
                if (iTmp>=0) hopping = ether.hoppings[iTmp];

                col = geometry.neighbor(p,j,2);
                tmp=cos(0.5*dynVars.theta[p])*cos(0.5*dynVars.theta[col]);
                tmp2=sin(0.5*dynVars.theta[p])*sin(0.5*dynVars.theta[col]);
                S_ij=tpem_t(tmp+tmp2*cos(dynVars.phi[p]-dynVars.phi[col]),
                            -tmp2*sin(dynVars.phi[p]-dynVars.phi[col]));

                if (p>col) hopping2 = conj(hopping);
                else hopping2=hopping;
                dir = int(j/2);

                bandHop=ether.bandHoppings[0+0*2+dir*4]*ether.tprime;
                hopping=hopping2 * bandHop ;
                matrix(p,col) = hopping * S_ij;
                matrix(col,p) = conj(hopping * S_ij);

                bandHop=ether.bandHoppings[0+1*2+dir*4]*ether.tprime;
                hopping= hopping2 * bandHop;
                matrix(p, col+volume)=hopping * S_ij;
                matrix(col+volume,p)=conj(matrix(p,col+volume));

                //
                bandHop=ether.bandHoppings[1+0*2+dir*4]*ether.tprime;
                hopping= hopping2 * bandHop;
                matrix(p+volume,col) =  hopping * S_ij;
                matrix(col,p+volume) =  conj(matrix(p+volume,col));

                bandHop=ether.bandHoppings[1+1*2+dir*4]*ether.tprime;
                hopping=hopping2 * bandHop;
                matrix(p+volume,col+volume) =  hopping * S_ij;
                matrix(col+volume,p+volume) = conj(matrix(p+volume,col+volume));
            }
        }
    }
    //if (!matrix.isHermitian()) throw std::runtime_error("I'm barking\n");


}