Edge-Learning-Machine / Micro-LM

A plain C library for machine learning on edge devices
MIT License
13 stars 4 forks source link

No return output for decisionTree_classification(float X[]) #2

Closed nivetaiyer closed 3 years ago

nivetaiyer commented 3 years ago

In the classification code:

int decisionTree_classification(float X[])
{
    int currentNode = 0;
    while (1)
    {
        if (feature[currentNode] >= 0)
        {
            if (X[feature[currentNode]] <= threshold[currentNode])
            {
#ifdef DEBUG
                printf("\ncurrent node: %d, X:%f <= %f\n", currentNode, X[feature[currentNode]], threshold[currentNode]);
                fflush(stdout);
#endif
                currentNode = children_left[currentNode];
            }
            else
            {
#ifdef DEBUG
                printf("\ncurrent node: %d, X:%f > %f\n", currentNode, X[feature[currentNode]], threshold[currentNode]);
                fflush(stdout);
#endif
                currentNode = children_right[currentNode];
            }
        }
        else
        { // Leaf node
            /*{
                int j;
                int maxClass;
                int maxValue = 0;
                for (j = 0; j < N_CLASS; j++)
                {
                    if (values[currentNode][j] >= maxValue)
                    {
                        maxValue = values[currentNode][j];
                        maxClass = target_classes[j];
                    }
                }
                return maxClass;
            }
            break;*/
            int j;
            for (j = 0; j < N_LEAVES; j++) {
                if (leaf_nodes[j][0] == currentNode) {
                    int maxIdx = leaf_nodes[j][1];
                    int maxClass = target_classes[maxIdx];
                    printf("\ncurrent node: %d, decision: %d\n", currentNode, maxClass);
                    fflush(stdout);
                    return maxClass;
                }
            }
        }
    }
}

There is no return output in the if block, only in the else block. This results in a segfault. Is this intentional?

nivetaiyer commented 3 years ago

It's a recursive loop until the condition for return is true. Thank you!