Domanda

Penso di essere confuso con gli oggetti istanziati .. Bene .. non correttamente oggetto perché le dichiarazioni new fanno un errore di compilazione.Il mio background è in Python e Java e sono bloccato davanti al modo di C ++ per creare oggetti che non sono lezioni.

Sto traducendo un algoritmo da C # e per l'apprendimento automatico e utilizza una serie di array multidimensionali.

C # Codice:

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];
    }
.

Ho deciso di utilizzare la Biblioteca Boost per l'array multidimensionale e ho:

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]);
}
.

e ottengo questo errore di runtime:

.

hmm :: Learn >> prima
STD :: Bad_alloc 'What (): std :: Bad_alloc

È stato utile?

Soluzione

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);
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top