你是對的,張量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如果您認爲這是正確的答案,我可以請您標記爲正確嗎? –