Boost :: Multi_ArrayのSTD :: Bectorのインスタンス化:: CPP Guruの融解脳?
-
14-11-2019 - |
質問
私は私がインスタンス化されているオブジェクトのインスタンス化と混同していると思います。私の背景はPythonと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]);
}
.
とこのランタイムエラー:
HMM :: Learn >>
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);
所属していません StackOverflow