问题描述
我想将文本数据转换为相应的值(矢量化),但是最终数据不会一直填充,似乎如果存在一些延迟,它有时间填充结果数组,否则它只会完成一部分最终数据,尽管使用了 sync 和 eval 方法:
// input: ['0','9',' ','1','5','0','2','3','8','4']
void TextToByteFilter::transform(af::array& input) const
{
using namespace af;
const auto len = 2; // length of uniform values
auto data = input;
data(data >= 65 && data <= 70) -= 7; // convert hex (A to F) -> (10 to 15)
data(data >= 97 && data <= 102) -= 39; // convert hex (a to f) -> (10 to 15)
data = data(data != ' ') - 48; // remove spaces & convert to number
// base conversion
af::array target(data.elements() / len);
for (auto i = 0; i < len; i += 1)
target = target + data(seq(len - static_cast<double>(i) - 1.0,end,len)) * static_cast<unsigned>(pow(base_,i));
// if I use u8 type for target array on deFinition results are incorrect due to overflow I think although I have never exceeded the u8 range so I have to cast it from f32.
data = target.as(u8);
af_print(data);
auto v0 = af::toString("data",data);
// possible data values:
// 1. [9,11,15,2,3,0] - two last items are not filled
// 2. [9,8,14]- happens sometimes (and if I drag the next statement arrow to the top of the function)
input = data;
}
还有一件事是,如果我使用u8类型作为 target 变量,由于溢出,结果将变得不正确,尽管我从未超过u8范围,这就是为什么我必须将其强制转换为f32。
注意:如果我通过将下一个语句指示符移到该函数的顶部来重新运行此代码,则该
我该怎么办?
解决方法
@pardeep说的没错:
target
变量未初始化,最终会导致一些问题,因为您在随后的for循环中会累积到该变量中。我认为这是问题所在,请尝试array target = constant(0,data.elements()/len)