0

在來自Caffe一個新的損失層上工作,如何遍歷Caffe中Blob變量的所有元素?

我在diff_.cpu_data()一些值,讓我們的名字,每個元素作爲Di

現在,我要計算這個功能對每個Di

enter image description here

,然後將結果到bottom[0]->mutable_cpu_diff()中的對應元素。

正如所看到的,對於第二項沒有必要遍歷輸入和輸出變量(diff_.cpu_data & bottom[0]->mutable_cpu_diff()分別地),而在第一項,我需要訪問的每個元素的值輸入變量,那麼當然我需要將函數的結果分配給輸出變量的相應元素,如果他們是2-d陣列,很明顯,我可以做這樣的事情:

enter image description here

但如你所知,那些變量是4維數組,我不清楚該怎麼做。

我應該使用Offset()函數或類似的東西循環所有這些變量的元素類似於this

有人可以請給我解釋一下,或者轉介給我一個有用的參考嗎?

謝謝,

回答

1

首先,應該存儲的第二項的結果(1/N^2 \ sum_i D_i)複製到本地變量。如您already know,這個總和可以使用caffe_cpu_dot來計算。

所以,你的代碼可能看起來像:

vector<Dtype> mult_data(diff_.count(), Dtype(1)); 
const Dtype* Di = diff_.cpu_data(); 
Dtype const_sum = caffe_cpu_dot(diff_.count(), &mult_data[0], Di); 
Dtype N = diff_.count(); 
const_sum /= N*N; // divide by N^2 

現在你可以遍歷所有條目(假設bottom[0]->count()==diff_.count()):

Dtype* bottom_diff = bottom[0]->mutable_cpu_diff(); 
for (int i=0; i<diff_.count(); i++) { 
    bottom_diff[i] = 2*Di[i]/N - const_sum; 
} 

或者,如果你想要更多的東西「BLAS 「-like:

caffe_copy(diff_.count(), Di, bottom_diff); // copy Di to bottom_diff 
caffe_scal(diff_.count(), Dtype(2.0/N), bottom_diff); // bottom_diff is now (2/N)*Di 
caffe_add_scalar(diff_.count(), -const_sum, bottom_diff); // subtract the second term from all the elements. 
+1

完美的解決方案,謝謝。讓我在caffe_scal(diff_.count(),2.0/N,bottom_diff)之前提一下。最好定義Dtype alpha = 2.0/N;然後像這樣調用它:caffe_scal(diff_.count(),alpha,bottom_diff);以防止任何類型不匹配。再次感謝你。 – Ali

+0

只是一個小問題。當我使用Dtype const_sum = caffe_cpu_dot(diff_.count(),&mult_data [0],Di);在Backward_cpu中效果很好,但是當我將它轉換爲GPU模式時:Dtype const_sum; caffe_gpu_dot(diff_.count(),&mult_data [0],Di,&const_sum);在Backward_gpu中,我得到了CUBLAS_STATUS_MAPPING_ERROR。你知道爲什麼嗎? – Ali

+0

@Ali當你轉換成GPU時,你把所有的代碼放在一個不同的.cu文件中嗎? – Shai