Atul Vaish
Atul Vaish

Reputation: 11

Unable to figure out CTS tests for Android NNAPI for SOFTMAX inputs/outputs

Unable to figure out CTS tests for Android NNAPI for SOFTMAX inputs/outputs

Have been using Android P for our internal Product Ramp up and looked into following code where I am unable to figure out , how below softmax input and output could match like what the mathematical formula here ?...any one could help me to understand or any link for documentations around it ?

http://androidxref.com/9.0.0_r3/xref/frameworks/ml/nn/runtime/test/generated/examples/softmax_float_1.example.cpp

Upvotes: 0

Views: 159

Answers (1)

ShreeVidhya
ShreeVidhya

Reputation: 123

Softmax output is calculated using this formula (ref: 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)}

As per your test case, input tensor is defined as {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)

The actual test case is defined here - 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());
 }

input is {1.0f, 2.0f, 10.0f, 20.0f}
beta is {1e-06f} (constant value initialized as beta_init in the above code)
max of input array is 20.0f

Here is the python code (rough) for softmax function:

 # 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)

Output is [ 0.24550335 0.24611264 0.25104177 0.25734224] - This is the expected output (rounded off) {0.25f, 0.25f, 0.25f, 0.25f} // as per your test data - http://androidxref.com/9.0.0_r3/xref/frameworks/ml/nn/runtime/test/generated/examples/softmax_float_1.example.cpp

Hope it helps!

Upvotes: 1

Related Questions