Instantáneando un STD :: Vector de Boost :: Multi_Array. ¿Cerebro de fusión para el gurú de CPP?
-
14-11-2019 - |
Pregunta
Creo que estoy confundido con los objetos instantáneos ... bueno .. no se opone correctamente porque las declaraciones de new
hacen un error de compilación.Mi fondo está en Python y Java y estoy atrapado frente a la forma de crear objetos que no son clases.
Estoy traduciendo un algoritmo de C # y para el aprendizaje automático y utiliza una matriz de matrices multidimensionales.
c # Código:
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];
}
He decidido usar la biblioteca de BOOST para la matriz multidimensional, y tengo:
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]);
}
y obtengo este error de ejecución:
hmm :: aprender >> antes de la sociedad std :: bad_alloc 'qué (): std :: bad_alloc
Solución
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);