关于初始化Tensor时Shape顺序问题
收藏回复举报
关于初始化Tensor时Shape顺序问题
发表于2025-09-21 16:10:53
0 查看

今天初始化Tensor时发现一个问题,使用下面第一种Shape的顺序:B*C*H*W和第二种顺序B*H*W*3打印出来的Shape是一样的,这个初始化对Shape的顺序有什么要求吗

# B*C*H*W
std::vector<unsigned int> batchShape = {
    static_cast<unsigned int>(batchSize),
    3,
    static_cast<unsigned int>(imageHeight),
    static_cast<unsigned int>(imageWidth)
};
Tensor batchTensor(batchShape, TensorDType::FLOAT32, -1);
# p batchTensor.GetShape()[0]
# $1 = (__gnu_cxx::__alloc_traits<std::allocator<unsigned int>, unsigned int>::value_type &) @0xaaaaadb6b4e0: 5
# (gdb) p batchTensor.GetShape()[1]
# $2 = (__gnu_cxx::__alloc_traits<std::allocator<unsigned int>, unsigned int>::value_type &) @0xaaaaad8fc144: 512
# (gdb) p batchTensor.GetShape()[2]
# $3 = (__gnu_cxx::__alloc_traits<std::allocator<unsigned int>, unsigned int>::value_type &) @0xaaaaadb6b648: 640
# (gdb) p batchTensor.GetShape()[3]
# $4 = (__gnu_cxx::__alloc_traits<std::allocator<unsigned int>, unsigned int>::value_type &) @0xaaaaadb6b4ac: 3

# B*H*W*C
std::vector<unsigned int> batchShape = {
    static_cast<unsigned int>(batchSize),
    static_cast<unsigned int>(imageHeight),
    static_cast<unsigned int>(imageWidth),
    3
};
Tensor batchTensor(batchShape, TensorDType::FLOAT32, -1);
# p batchTensor.GetShape()[0]
# $1 = (__gnu_cxx::__alloc_traits<std::allocator<unsigned int>, unsigned int>::value_type &) @0xaaaaadb6b670: 5
# (gdb) p batchTensor.GetShape()[1]
# $2 = (__gnu_cxx::__alloc_traits<std::allocator<unsigned int>, unsigned int>::value_type &) @0xaaaaad8fc0e4: 512
# (gdb) p batchTensor.GetShape()[2]
# $3 = (__gnu_cxx::__alloc_traits<std::allocator<unsigned int>, unsigned int>::value_type &) @0xaaaaadb6bad8: 640
# (gdb) p batchTensor.GetShape()[3]
# $4 = (__gnu_cxx::__alloc_traits<std::allocator<unsigned int>, unsigned int>::value_type &) @0xaaaaadb6b11c: 3

我要发帖子