Eigen::ArrayXXf RepSupernmotif::repSupermotifComputeMatrixDissimCosS2NM(Eigen::ArrayXXf matS_supernmotifs,Eigen::ArrayXXf matNM_supernmotifs){

	//compute Cosine dissimilarity between matS_supernmotifs and matNM_supernmotifs//
		int n=matS_supernmotifs.rows();
		int m=matNM_supernmotifs.rows();
        Eigen::MatrixXf matDistS2NM = Eigen::MatrixXf::Zero(matS_supernmotifs.rows(),matNM_supernmotifs.rows());
        Eigen::VectorXf allNormMatS_supernmotifs,allNormMatNM_supernmotifs,vectTempi,vectTempj;
		allNormMatS_supernmotifs= matS_supernmotifs.matrix().rowwise().norm();
		allNormMatNM_supernmotifs= matNM_supernmotifs.matrix().rowwise().norm();
		for (int i=0; i<n;i++)
		{
			for (int j=0; j<m;j++)
			{
				vectTempi=matS_supernmotifs.row(i);
				vectTempj=matNM_supernmotifs.row(j);
				matDistS2NM(i,j)=1-(vectTempi.dot(vectTempj)/(allNormMatS_supernmotifs[i]*allNormMatNM_supernmotifs[j]));
				if(matDistS2NM(i,j)<0){matDistS2NM(i,j)=0;}
			}
		}

return matDistS2NM;
}
RepSupernmotif::RepSupernmotif(Eigen::ArrayXXf matALLMotifWeigthed,int nbSupNmotifs,int p_outputOption) {
	// TODO Auto-generated constructor stub

        if((p_outputOption==0)||(p_outputOption==1))//Compute the super n-motifs representation
        {
			int maxDim;
			if(matALLMotifWeigthed.rows()>matALLMotifWeigthed.cols())
			{maxDim=matALLMotifWeigthed.cols();}
			else{maxDim=matALLMotifWeigthed.rows();}

            if (nbSupNmotifs>maxDim)
			{
                cerr<<"warning: the number of super n-motifs selected (-k) is greater than the maximum number of computed super n-motifs. -k is set to the maximum number of computed super n-motifs."<<endl;
                nbSupNmotifs=maxDim;
			}


			//compute SVD//
            Eigen::JacobiSVD<Eigen::MatrixXf> svd(matALLMotifWeigthed.matrix(), Eigen::ComputeThinU);
            //Eigen::BDCSVD<Eigen::MatrixXf> svd(matALLMotifWeigthed.matrix(), Eigen::ComputeThinU | Eigen::ComputeThinV);
            singularVal=svd.singularValues();

            //Brocken stick model

            if(nbSupNmotifs==0)
            {
                Eigen::RowVectorXf singularVal_precent=singularVal/singularVal.sum();
                Eigen::RowVectorXf brModel=singularVal;
                float tempbrModel;

                for(int i=0; i<brModel.size(); i++)
                {
                    tempbrModel=0;
                    for(int j=i; j<brModel.size(); j++)
                    {
                        tempbrModel+=1/float(j+1);
                    }
                    brModel(i)=tempbrModel/brModel.size();
                }

                int nbSupNmotifs2=0;
                while(singularVal_precent(nbSupNmotifs2)>brModel(nbSupNmotifs2))
                {nbSupNmotifs2++;}

                nbSupNmotifs=nbSupNmotifs2;

                if (nbSupNmotifs<2)
                {
                    nbSupNmotifs=2;
                }

            }

            matS_supernmotifs=svd.matrixU().leftCols(nbSupNmotifs);
            //matS_supernmotifs=randomizedSVD.matrixU().leftCols(nbSupNmotifs);//Resvd Test

            for(int i=0;i<nbSupNmotifs;i++)
			{matS_supernmotifs.col(i)=matS_supernmotifs.col(i)*(singularVal(i));}

            //Compute the SS dissimilarities using the cosine dissimilarity
            //if (p_outputOption==0)
            //{matDissimS2S=repSupermotifComputeMatrixDissimCosS2S(matS_supernmotifs);}

		}
        else if(p_outputOption==2)
        {

            int maxDim;
            if(matALLMotifWeigthed.rows()>matALLMotifWeigthed.cols())
            {maxDim=matALLMotifWeigthed.cols();}
            else{maxDim=matALLMotifWeigthed.rows();}

            if (nbSupNmotifs>maxDim)
            {
                cerr<<"warning: the number of super n-motifs selected (-k) is greater than the maximum number of computed super n-motifs. -k is set to the maximum number of computed super n-motifs."<<endl;
                nbSupNmotifs=maxDim;
            }

            //*******compute SVD*************************//
            Eigen::JacobiSVD<Eigen::MatrixXf> svd(matALLMotifWeigthed.matrix(), Eigen::ComputeThinU | Eigen::ComputeThinV);
            singularValFull=svd.singularValues();

            //Brocken stick model

            if(nbSupNmotifs==0)
            {
                Eigen::RowVectorXf singularVal_precent=singularValFull/singularValFull.sum();
                Eigen::RowVectorXf brModel=singularValFull;
                float tempbrModel;

                for(int i=0; i<brModel.size(); i++)
                {
                    tempbrModel=0;
                    for(int j=i; j<brModel.size(); j++)
                    {
                        tempbrModel+=1/float(j+1);
                    }
                    brModel(i)=tempbrModel/brModel.size();
                }

                int nbSupNmotifs2=0;
                while(singularVal_precent(nbSupNmotifs2)>brModel(nbSupNmotifs2))
                {nbSupNmotifs2++;}

                nbSupNmotifs=nbSupNmotifs2;

                if (nbSupNmotifs<2)
                {
                    nbSupNmotifs=2;
                }

            }

            singularVal=singularValFull.head(nbSupNmotifs);
            matS_supernmotifs=svd.matrixU().leftCols(nbSupNmotifs);
            matNM_supernmotifs=svd.matrixV().leftCols(nbSupNmotifs);

            for(int i=0;i<nbSupNmotifs;i++)
            {
             matS_supernmotifs.col(i)=matS_supernmotifs.col(i)*(singularVal(i));
             matNM_supernmotifs.col(i)=matNM_supernmotifs.col(i)*(singularVal(i));
            }

            //Compute the SS dissimilarities using the cosine dissimilarity
            //matDissimS2S=repSupermotifComputeMatrixDissimCosS2S(matS_supernmotifs);
            matDissimS2NM=repSupermotifComputeMatrixDissimCosS2NM(matS_supernmotifs,matNM_supernmotifs);

        }
        else if(p_outputOption==3)//Compute the secondary structure dissimilarities based on the n-motifs representation
        {
         cout<<"No super n-motifs computed (-p 3) ..."<<endl;
         //matDissimS2S=repSupermotifComputeMatrixDissimCosS2S(matALLMotifWeigthed);
        }
        else if ((p_outputOption==4))
        {
        cout<<"No super n-motifs computed (-p 4) ..."<<endl;
        }
        else if (p_outputOption==5)
		{
			int maxDim;
			if(matALLMotifWeigthed.rows()>matALLMotifWeigthed.cols())
			{maxDim=matALLMotifWeigthed.cols();}
			else{maxDim=matALLMotifWeigthed.rows();}

			//compute SVD
            if (nbSupNmotifs>maxDim)
			{
                cerr<<"warning: the number of super n-motifs selected (-k) is greater than the maximum number of computed super n-motifs. -k is set to the maximum number of computed super n-motifs."<<endl;
                nbSupNmotifs=maxDim;
			}

			//compute SVD//
            Eigen::JacobiSVD<Eigen::MatrixXf> svd(matALLMotifWeigthed.matrix(), Eigen::ComputeThinU | Eigen::ComputeThinV);
            singularValFull=svd.singularValues();
            //Brocken stick model
            if(nbSupNmotifs==0)
            {
                Eigen::RowVectorXf singularVal_precent=singularValFull/singularValFull.sum();
                Eigen::RowVectorXf brModel=singularValFull;
                float tempbrModel;

                for(int i=0; i<brModel.size(); i++)
                {
                    tempbrModel=0;
                    for(int j=i; j<brModel.size(); j++)
                    {
                        tempbrModel+=1/float(j+1);
                    }
                    brModel(i)=tempbrModel/brModel.size();
                }

                int nbSupNmotifs2=0;
                while(singularVal_precent(nbSupNmotifs2)>brModel(nbSupNmotifs2))
                {nbSupNmotifs2++;}

                nbSupNmotifs=nbSupNmotifs2;

                if (nbSupNmotifs<2)
                {
                    nbSupNmotifs=2;
                }

            }


            matS_supernmotifsFull=svd.matrixU();
            matNM_supernmotifsFull=svd.matrixV();

            singularVal=singularValFull.head(nbSupNmotifs);
            matS_supernmotifs=matS_supernmotifsFull.leftCols(nbSupNmotifs);
            matNM_supernmotifs=matNM_supernmotifsFull.leftCols(nbSupNmotifs);;


            //Compute super n-motifs representation of n-motifs

            for(int i=0;i<nbSupNmotifs;i++)
			{
				matS_supernmotifs.col(i)=matS_supernmotifs.col(i)*(singularVal(i));
                matNM_supernmotifs.col(i)=matNM_supernmotifs.col(i)*(singularVal(i));
			}

            //Compute the dissimilarities between SS and n-motifs in the super n-motif space using the cosine dissimilarity
            matDissimS2NM=repSupermotifComputeMatrixDissimCosS2NM(matS_supernmotifs,matNM_supernmotifs);
            //Compute the SS dissimilarities using the cosine dissimilarity
            //matDissimS2S=repSupermotifComputeMatrixDissimCosS2S(matS_supernmotifs);
		}


}
Example #3
0
void Trainee::train(std::vector<std::pair<InputType, AnswerType>> minibatch, float learning_rate)
{
    Eigen::MatrixXf dweight3 = Eigen::MatrixXf::Zero(n_outputvec, n_hid2vec);
    Eigen::VectorXf dbias3 = Eigen::VectorXf::Zero(n_outputvec);
    Eigen::MatrixXf dweight2 = Eigen::MatrixXf::Zero(n_hid2vec, n_hid1vec);
    Eigen::VectorXf dbias2 = Eigen::VectorXf::Zero(n_hid2vec);
    Eigen::MatrixXf dweight1 = Eigen::MatrixXf::Zero(n_hid1vec, n_inputvec);
    Eigen::VectorXf dbias1 = Eigen::VectorXf::Zero(n_hid1vec);

    /* For AdaGrad */
    auto fn = [](float lhs, float rhs) -> float { return lhs != 0.0 ? lhs / rhs : 0.0; };

    for(auto sample: minibatch){
        Eigen::VectorXf inputvec = input2vec(sample.first);
        Eigen::VectorXf z1 = feedforward(inputvec, 1);
        Eigen::VectorXf z2 = feedforward(inputvec, 2);  // 後付けとはいえ。この計算、あからさまに無駄だな。z1からz2を計算すべき。

        // Calculate delta of output layer.
        Eigen::VectorXf delta3;
        delta3 = feedforward(inputvec, 3);
        delta3(sample.second) -= 1.0f;
        {
            Eigen::ArrayXXf e = delta3 * z2.transpose();
            gsq_w3 += e * e;
            gsq_b3 += delta3.array() * delta3.array();
            dweight3 += e.matrix();
            dbias3 += delta3;
        }

        // Calculate delta of 2nd hidden layer.
        Eigen::VectorXf delta2 = Eigen::VectorXf::Zero(n_hid2vec);
        for(int j=0;j<n_hid2vec;j++){
            for(int k=0;k<n_outputvec;k++) delta2(j) += delta3(k) * weight3(k, j) * (z2(j) >= 0.f ? 1.f : 0.f);
        }
        {
            Eigen::ArrayXXf e = delta2 * z1.transpose();
            gsq_w2 += e * e;
            gsq_b2 += delta2.array() * delta2.array();
            dweight2 += e.matrix();
            dbias2 += delta2;
        }

        // Calculate delta of 1st hidden layer.
        Eigen::VectorXf delta1 = Eigen::VectorXf::Zero(n_hid1vec);
        for(int j=0;j<n_hid1vec;j++){
            for(int k=0;k<n_hid2vec;k++) delta1(j) += delta2(k) * weight2(k, j) * (z1(j) >= 0.f ? 1.f : 0.f);
        }
        {
            Eigen::ArrayXXf e = delta1 * inputvec.transpose();
            gsq_w1 += e * e;
            gsq_b1 += delta1.array() * delta1.array();
            dweight1 += e.matrix();
            dbias1 += delta1;
        }
    }
    weight1 -= dweight1.binaryExpr(gsq_w1.sqrt().matrix(), fn) * learning_rate / minibatch.size();
    bias1 -= dbias1.binaryExpr(gsq_b1.sqrt().matrix(), fn) * learning_rate / minibatch.size();
    weight2 -= dweight2.binaryExpr(gsq_w2.sqrt().matrix(), fn) * learning_rate / minibatch.size();
    bias2 -= dbias2.binaryExpr(gsq_b2.sqrt().matrix(), fn) * learning_rate / minibatch.size();
    weight3 -= dweight3.binaryExpr(gsq_w3.sqrt().matrix(), fn) * learning_rate / minibatch.size();
    bias3 -= dbias3.binaryExpr(gsq_b3.sqrt().matrix(), fn) * learning_rate / minibatch.size();
}