你是对的,张量LHS [0]是一维的,但要回答你的问题,首先让我展示什么是引擎盖下回事。 TensorContainer不覆盖[]操作,而是使用从父的一个(它是张量),更精确地following one叫做:
MSHADOW_XINLINE Tensor<Device, kSubdim, DType> operator[](index_t idx) const {
return Tensor<Device, kSubdim, DType>(dptr_ + this->MemSize<1>() * idx,
shape_.SubShape(), stride_, stream_);
}
如可以看到它创建一个堆栈上一个新的张量。而对于大多数情况下,它将创建通用N-dimensional Tensor,在这里为1维情况下它将创建一个特殊的1-dimensional Tensor。
现在,我们已经建立了究竟是由运营商[]返回,让我们来看看在该类领域:
DType *dptr_;
Shape<1> shape_;
index_t stride_;
可以看出该shape_这里只有1个尺寸!所以没有shape_ 1,而是通过调用shape_ 1它将返回stride_(或它的一部分)。下面是修改的张量构造函数,你可以尝试运行,看看什么是真正对那里发生的:
MSHADOW_XINLINE Tensor(DType *dptr, Shape<1> shape,
index_t stride, Stream<Device> *stream)
: dptr_(dptr), shape_(shape), stride_(stride), stream_(stream) {
std::cout << "shape[0]: " << shape[0] << std::endl; // 3
std::cout << "shape[1]: " << shape[1] << std::endl; // 0, as expected
std::cout << "_shape[0]: " << shape_[0] << std::endl; // 3, as expected
std::cout << "_shape[1]: " << shape_[1] << std::endl; // garbage (4)
std::cout << "address of _shape[1]: " << &(shape_[1]) << std::endl;
std::cout << "address of stride: " << &(stride_) << std::endl;
}
和输出:
shape[0]: 3
shape[1]: 0
_shape[0]: 3
_shape[1]: 4
address of _shape[1]: 0x7fffa28ec44c
address of stride: 0x7fffa28ec44c
_shape 1和步幅均具有相同的地址(0x7fffa28ec44c)。
绝妙的答案!非常感谢! –
@ROBOTAI如果您认为这是正确的答案,我可以请您标记为正确吗? –