예제 #1
0
template <typename PointInT, typename IntensityT> void
pcl::tracking::PyramidalKLTTracker<PointInT, IntensityT>::mismatchVector (const Eigen::ArrayXXf& prev,
                                                                   const Eigen::ArrayXXf& prev_grad_x,
                                                                   const Eigen::ArrayXXf& prev_grad_y,
                                                                   const FloatImage& next,
                                                                   const Eigen::Array2i& location,
                                                                   const Eigen::Array4f& weight,
                                                                   Eigen::Array2f &b) const
{
  const int step = next.width;
  b.setZero ();
  for (int y = 0; y < track_height_; y++)
  {
    const float* next_ptr = &(next.points[0]) + (y + location[1])*step + location[0];
    const float* prev_ptr = prev.data () + y*prev.cols ();
    const float* prev_grad_x_ptr = prev_grad_x.data () + y*prev_grad_x.cols ();
    const float* prev_grad_y_ptr = prev_grad_y.data () + y*prev_grad_y.cols ();

    for (int x = 0; x < track_width_; ++x, ++prev_grad_y_ptr, ++prev_grad_x_ptr)
    {
      float diff = next_ptr[x]*weight[0] + next_ptr[x+1]*weight[1]
      + next_ptr[x+step]*weight[2] + next_ptr[x+step+1]*weight[3] - prev_ptr[x];
      b[0] += *prev_grad_x_ptr * diff;
      b[1] += *prev_grad_y_ptr * diff;
    }
  }
}
예제 #2
0
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;
}
예제 #3
0
template <typename PointInT, typename IntensityT> void
pcl::tracking::PyramidalKLTTracker<PointInT, IntensityT>::spatialGradient (const FloatImage& img,
                                                                    const FloatImage& grad_x,
                                                                    const FloatImage& grad_y,
                                                                    const Eigen::Array2i& location,
                                                                    const Eigen::Array4f& weight,
                                                                    Eigen::ArrayXXf& win,
                                                                    Eigen::ArrayXXf& grad_x_win,
                                                                    Eigen::ArrayXXf& grad_y_win,
                                                                    Eigen::Array3f &covariance) const
{
  const int step = img.width;
  covariance.setZero ();

  for (int y = 0; y < track_height_; y++)
  {
    const float* img_ptr = &(img.points[0]) + (y + location[1])*step + location[0];
    const float* grad_x_ptr = &(grad_x.points[0]) + (y + location[1])*step + location[0];
    const float* grad_y_ptr = &(grad_y.points[0]) + (y + location[1])*step + location[0];

    float* win_ptr = win.data () + y*win.cols ();
    float* grad_x_win_ptr = grad_x_win.data () + y*grad_x_win.cols ();
    float* grad_y_win_ptr = grad_y_win.data () + y*grad_y_win.cols ();

    for (int x =0; x < track_width_; ++x, ++grad_x_ptr, ++grad_y_ptr)
    {
      *win_ptr++  = img_ptr[x]*weight[0] + img_ptr[x+1]*weight[1] + img_ptr[x+step]*weight[2] + img_ptr[x+step+1]*weight[3];
      float ixval = grad_x_ptr[0]*weight[0] + grad_x_ptr[1]*weight[1] + grad_x_ptr[step]*weight[2] + grad_x_ptr[step+1]*weight[3];
      float iyval = grad_y_ptr[0]*weight[0] + grad_y_ptr[1]*weight[1] + grad_y_ptr[step]*weight[2] + grad_y_ptr[step+1]*weight[3];
      //!!! store those
      *grad_x_win_ptr++ = ixval;
      *grad_y_win_ptr++ = iyval;
      //covariance components
      covariance[0] += ixval*ixval;
      covariance[1] += ixval*iyval;
      covariance[2] += iyval*iyval;
    }
  }
}
예제 #4
0
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);
		}


}
예제 #5
0
파일: train.cpp 프로젝트: gyu-don/mnist_nn
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();
}