1 #ifndef CAFFE2_FILLER_H_ 2 #define CAFFE2_FILLER_H_ 6 #include "caffe2/core/logging.h" 7 #include "caffe2/core/tensor.h" 8 #include "caffe2/utils/math.h" 13 enum FillerDistribution { FD_UNIFORM, FD_FIXEDSUM, FD_SYNTHETIC };
17 template <
class Type,
class Context>
18 void Fill(
Tensor* tensor, Context* context)
const {
19 CAFFE_ENFORCE(context,
"context is null");
20 CAFFE_ENFORCE(tensor,
"tensor is null");
21 auto min = (min_ < std::numeric_limits<Type>::min())
22 ? std::numeric_limits<Type>::min()
23 :
static_cast<Type>(min_);
24 auto max = (max_ > std::numeric_limits<Type>::max())
25 ? std::numeric_limits<Type>::max()
26 :
static_cast<Type>(max_);
27 CAFFE_ENFORCE_LE(min, max);
29 Tensor temp_tensor(shape_, Context::GetDeviceType());
30 std::swap(*tensor, temp_tensor);
31 Type* data = tensor->template mutable_data<Type>();
36 math::RandUniform<Type, Context>(
37 tensor->numel(), min, max, data, context);
41 auto fixed_sum =
static_cast<Type>(fixed_sum_);
42 CAFFE_ENFORCE_LE(min * tensor->numel(), fixed_sum);
43 CAFFE_ENFORCE_GE(max * tensor->numel(), fixed_sum);
44 math::RandFixedSum<Type, Context>(
45 tensor->numel(), min, max, fixed_sum_, data, context);
49 math::RandSyntheticData<Type, Context>(
50 tensor->numel(), min, max, data, context);
76 fixed_sum_ = (double)fixed_sum;
84 return FixedSum(total_length)
85 .Min(std::min(static_cast<Type>(1), total_length))
92 CAFFE_ENFORCE(dist_ != FD_FIXEDSUM);
93 return Min(0).Max(max_segment).Dist(FD_SYNTHETIC);
101 template <
class Type>
103 : shape_(shape), dist_(FD_FIXEDSUM), fixed_sum_((
double)fixed_sum) {}
106 : shape_(shape), dist_(FD_UNIFORM), fixed_sum_(0) {}
110 std::string DebugString()
const {
111 std::stringstream stream;
112 stream <<
"shape = [" << shape_ <<
"]; min = " << min_
113 <<
"; max = " << max_;
116 stream <<
"; dist = FD_FIXEDSUM";
119 stream <<
"; dist = FD_SYNTHETIC";
122 stream <<
"; dist = FD_UNIFORM";
129 std::vector<int64_t> shape_;
134 FillerDistribution dist_;
140 #endif // CAFFE2_FILLER_H_
A global dictionary that holds information about what Caffe2 modules have been loaded in the current ...