无法确定针对SOFTMAX输入/输出的Android NNAPI的CTS测试
一直使用安卓P为我们的内部产品坡道,并查看了以下代码,我无法搞清楚,如何以下softmax输入和输出可以匹配喜欢这里的数学公式?...any one可以帮助我理解或周围的任何文件的链接?
http://androidxref.com/9.0.0_r3/xref/frameworks/ml/nn/runtime/test/generated/examples/softmax_float_1.example.cpp
发布于 2020-01-11 21:38:01
Softmax输出使用以下公式计算(参考:https://android.googlesource.com/platform/frameworks/ml/+/android-p-preview-4/nn/runtime/include/NeuralNetworks.h)
output[batch, i] =
exp((input[batch, i] - max(input[batch, :])) * beta) /
sum_{k}{exp((input[batch, k] - max(input[batch, :])) * beta)}根据您的测试用例,输入张量定义为{1.0f,2.0f,10.0f,20.0f} (http://androidxref.com/9.0.0_r3/xref/frameworks/ml/nn/runtime/test/generated/examples/softmax_float_1.example.cpp)
实际的测试用例在这里定义- http://androidxref.com/9.0.0_r3/xref/frameworks/ml/nn/runtime/test/generated/models/softmax_float_1.model.cpp -
void CreateModel(Model *model) {
OperandType type1(Type::FLOAT32, {});
OperandType type0(Type::TENSOR_FLOAT32, {1, 4});
// Phase 1, operands
auto input = model->addOperand(&type0);
auto beta = model->addOperand(&type1);
auto output = model->addOperand(&type0);
// Phase 2, operations
static float beta_init[] = {1e-06f};
model->setOperandValue(beta, beta_init, sizeof(float) * 1);
model->addOperation(ANEURALNETWORKS_SOFTMAX, {input, beta}, {output});
// Phase 3, inputs and outputs
model->identifyInputsAndOutputs(
{input},
{output});
assert(model->isValid());
}输入为{1.0f,2.0f,10.0f,20.0f}
测试版为{1e-06f} (常量值在上面的代码中初始化为beta_init )
输入数组的最大为20.0f
这是softmax函数的python代码(粗略):
# input array
x = numpy.array([1.0,2.0, 10.0,20.0])
#operand value (constant)
beta = numpy.exp(-6)
# max of input array is 20 which is hardcoded here
y = numpy.exp((x - 20.0)*beta)/sum(numpy.exp((x - 20.0)*beta))
print(y)输出为 0.24550335 0.24611264 0.25104177 0.25734224 -这是预期输出(四舍五入) {0.25f,0.25f,0.25f,0.25f} //根据测试数据- http://androidxref.com/9.0.0_r3/xref/frameworks/ml/nn/runtime/test/generated/examples/softmax_float_1.example.cpp
希望它能帮上忙!
https://stackoverflow.com/questions/54247284
复制相似问题