instantiatiatiatiatiating of std :: multi_array의 벡터 : CPP 전문가를위한 뇌를 녹는 것?

StackOverflow https://stackoverflow.com/questions/9023178

문제

나는 인스턴스를 인스턴스화하는 것과 혼동이라고 생각합니다. new 문을 컴파일 오류로 만드기 때문에 제대로 객체가 아닙니다.내 배경은 파이썬과 Java에 있으며 클래스가 아닌 객체를 만드는 C ++ 방식 앞에서 갇혀 있습니다.

C #에서 알고리즘을 번역하고 기계 학습을 위해 다차원 배열의 배열을 사용합니다.

C # 코드 :

public double Learn(int[][] observations, int symbols, int states, ...

    // ...

    double[][, ,] epsilon = new double[N][, ,]; // also referred as ksi or psi
    double[][,] gamma = new double[N][,];

    for (int i = 0; i < N; i++)
    {
        int T = observations[i].Length;
        epsilon[i] = new double[T, States, States];
        gamma[i] = new double[T, States];
    }
.

다차원 배열에 대한 부스트 라이브러리를 사용하기로 결정했으며 다음과 같습니다.

typedef boost::multi_array<double, 2> matrix2D;
typedef boost::multi_array<double, 3> matrix3D;

vector<matrix3D> epsilon;
vector<matrix2D> gamma;

cout << "HMM::learn >> before" << endl;
for (int i = 0; i < N; i++)
{
    int T = observations[i].size();
    epsilon[i] = matrix3D(boost::extents[T][states][symbols]);
    gamma[i] = matrix2D(boost::extents[T][states]);
}
.

및이 런타임 오류가 발생했습니다 :

흠 :: 배우기 >>

std :: bad_alloc 'what () : std :: bad_alloc

도움이 되었습니까?

해결책

The vectors have no space allocated (well it may be reserved already but you can't reference it with the array indexers). Change the lines:

epsilon[i] = matrix3D(boost::extents[T][states][symbols]);
gamma[i] = matrix2D(boost::extents[T][states]);

To:

epsilon.push_back(matrix3D(boost::extents[T][states][symbols]);
gamma.push_back(matrix2D(boost::extents[T][states]);

that should solve it. In your case since you know the array size you should reserve that much space in the vectors so that it reduces the reallocations needed:

epsilon.reserve(N);
gamma.reserve(N);
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top