チームメイトと私は勾配降下法の実装をコード化しようとしていますが、かなり近づいていると思います
この質問に対する最初の回答に関連する手順、つまり次の手順に従うことを (試みました) 。
1.仮説 h = X * theta を計算します
2.損失 = h - y を計算し、コストの 2 乗 (損失^2)/2m を計算します。
3.勾配 = X' * 損失 / m を計算します。
4.パラメータ theta = theta - alpha * gradient を更新します。
しかし、欠落しているコードからわかるように、勾配を計算する方法について少し迷っています。正しく設定しましたか?
その計算を実行する方法は?
X' と X はどう違いますか?
double loss, cost, hypothesis;
int p, iteration;
iteration = 0;
do
{
iteration++;
cost = 0.0;
//loop through all instances (complete one epoch)
for (p = 0; p < number_of_files__train; p++)
{
hypothesis = calculateHypothesis( weights, feature_matrix__train, p, globo_dict_size );
loss = outputs__train[p] - hypothesis;
for (int i = 0; i < globo_dict_size; i++)
{
weights[i] += LEARNING_RATE * loss * feature_matrix__train[p][i] * calculateGradent( weights, i, number_of_files__train, loss );
}
//summation of squared error (error value for all instances)
cost += (loss*loss);
}
cost = cost/(2 * number_of_files__train);
}
while(cost != 0 && iteration<=MAX_ITER);
}
static double calculateHypothesis( double weights[], double[][] feature_matrix, int file_index, int globo_dict_size )
{
//# m denotes the number of examples here, not the number of features
double sum = 0.0;
for (int i = 0; i < globo_dict_size; i++)
{
sum += ( weights[i] * feature_matrix[file_index][i] );
}
//bias
sum += weights[ globo_dict_size ];
return sigmoid(sum);
}
private static double sigmoid(double x)
{
return 1 / (1 + Math.exp(-x));
}
static double calculateGradent( double weights[], int i, int number_of_files__train, double loss )
{
return weights[i] * loss / number_of_files__train;
}