Sometimes we define variable in caffe using Blob, which is convenient, but how do we initialize it as zero or other value?

In my project I want to init it as zero, I write the code using cudaMemset

1
2
3
Blob<Dtype> v;
v.Reshape(num,1,1,1);
cudaMemset(v, Dtype(0), v.count());

Which is not working, because I’m too stupid and forget to multiply the size of each element.

1
cudaMemset(v.mutable_gpu_data(), Dtype(0), sizeof(Dtype)&v.count());

Then it’s working.

But if we want to set it as other fixed value (say 1), we can use caffe_gpu_set instead:

1
2
Dtype Alpha = 1.0;
caffe_gpu_set(v.count(), Alpha, v.mutable_gpu_data());


Comments