Caffe源码理解1:Blob存储结构与设计 (3)

值得一提的是,Blob维度索引支持负数,-1表示最后一个维度,与Python相同,实现代码如下,在需要访问某个维度时,先使用CanonicalAxisIndex获得真正维度,比如CanonicalAxisIndex(-1)。

// axis_index the axis index. // If 0 <= index < num_axes(), return index. // If -num_axes <= index <= -1, return (num_axes() - (-index)) inline int CanonicalAxisIndex(int axis_index) const { CHECK_GE(axis_index, -num_axes()) << "axis " << axis_index << " out of range for " << num_axes() << "-D Blob with shape " << shape_string(); CHECK_LT(axis_index, num_axes()) << "axis " << axis_index << " out of range for " << num_axes() << "-D Blob with shape " << shape_string(); if (axis_index < 0) { return axis_index + num_axes(); } return axis_index; }

其他函数,只取代表。

// set get // 省略基本的set和get函数,如上面提到的const和mutable函数 // 返回(n, c, h, w)处的数据,return cpu_data()[offset(n, c, h, w)] inline Dtype data_at(const int n, const int c, const int h, const int w) const; inline Dtype diff_at(const int n, const int c, const int h, const int w) const; void ShareData(const Blob& other); // 与另一Blob共享data,类似浅拷贝 void ShareDiff(const Blob& other); // 与另一Blob共享diff // 从另一Blob拷贝,类似深拷贝 void Blob<Dtype>::CopyFrom(const Blob& source, bool copy_diff, bool reshape); // 切片元素数量统计,count *= shape(i) inline int count(int start_axis, int end_axis) const; // proto序列化与反序列化 void FromProto(const BlobProto& proto, bool reshape = true); // 从proto导入 void ToProto(BlobProto* proto, bool write_diff = false) const; // 导出为proto // 运算 Dtype asum_data() const; // data L1 norm Dtype asum_diff() const; // diff L1 norm Dtype sumsq_data() const; // data L2 norm Dtype sumsq_diff() const; // diff L2 norm void scale_data(Dtype scale_factor); // data 数乘,in place void scale_diff(Dtype scale_factor); // diff 数乘,in place // 逻辑判断 bool ShapeEquals(const BlobProto& other); // 判断shape是否相同

以上。

参考

Blobs, Layers, and Nets: anatomy of a Caffe model

Row- and column-major order

Caffe: a fast open framework for deep learning

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/zywzgw.html