内積層では、(top_diff * bottom_data) .* (2*weight)
を掛ける必要があります。まず、caffe_cpu_gemm
で行列乗算として(result = top_diff * bottom_data
)を計算し、次にweight
とresult
の間でdot product
を実行します。カフェのマトリックス間にドットプロダクトを行う方法は?
詳しい説明は以下のように定義されます。
const Dtype* weight = this->blobs_[0]->cpu_data();
if (this->param_propagate_down_[0]) {
const Dtype* top_diff = top[0]->cpu_diff();
const Dtype* bottom_data = bottom[0]->cpu_data();
caffe_cpu_gemm<Dtype>(CblasTrans, CblasNoTrans, N_, K_, M_, (Dtype)1.,
top_diff, bottom_data, (Dtype)1., this->blobs_[0]->mutable_cpu_diff());
}
はより理解するために、私はmath_function.c
をチェックします。次のように実装されています。
template<>
void caffe_cpu_gemm<float>(const CBLAS_TRANSPOSE TransA,
const CBLAS_TRANSPOSE TransB, const int M, const int N, const int K,
const float alpha, const float* A, const float* B, const float beta,
float* C) {
int lda = (TransA == CblasNoTrans) ? K : M;
int ldb = (TransB == CblasNoTrans) ? N : K;
cblas_sgemm(CblasRowMajor, TransA, TransB, M, N, K, alpha, A, lda, B,
ldb, beta, C, N);
}
私はcaffe_cpu_gemm()
に乗算(result = top_diff * bottom_data
)を行い、その後weight
でdot product
をやるべきだと思います。どうすればいいですか?
多くのありがとうございます!何かアドバイスをいただければ幸いです!