' neural_net.pb — Simple feed-forward neural network
#COMPILE EXE
#DIM ALL

FUNCTION PBMAIN() AS LONG
    LOCAL weights(50) AS DOUBLE
    LOCAL inputs(10) AS DOUBLE
    LOCAL hidden(10) AS DOUBLE
    LOCAL output(5) AS DOUBLE
    LOCAL numInputs AS LONG
    LOCAL numHidden AS LONG
    LOCAL numOutputs AS LONG
    LOCAL i AS LONG
    LOCAL j AS LONG
    LOCAL k AS LONG
    LOCAL sumVal AS DOUBLE
    LOCAL activation AS DOUBLE
    LOCAL idx AS LONG
    LOCAL errorSum AS DOUBLE
    LOCAL learningRate AS DOUBLE
    LOCAL epoch AS LONG
    LOCAL numEpochs AS LONG
    LOCAL seedVal AS LONG
    LOCAL target AS DOUBLE
    LOCAL predicted AS DOUBLE
    LOCAL diff AS DOUBLE
    LOCAL weightIdx AS LONG
    LOCAL numSamples AS LONG
    LOCAL sample AS LONG
    LOCAL totalError AS DOUBLE
    LOCAL accuracy AS LONG

    numInputs = 3
    numHidden = 4
    numOutputs = 1
    learningRate = 0.1
    numEpochs = 100

    PRINT "=== Neural Network ==="
    PRINT "Architecture: " + STR$(numInputs) + "-" + STR$(numHidden) + "-" + STR$(numOutputs)

    weightIdx = 0
    FOR i = 0 TO numInputs - 1
        FOR j = 0 TO numHidden - 1
            weights(weightIdx) = i * 0.1 + j * 0.01
            weightIdx = weightIdx + 1
        NEXT j
    NEXT i
    FOR j = 0 TO numHidden - 1
        FOR k = 0 TO numOutputs - 1
            weights(weightIdx) = j * 0.05 + 0.1
            weightIdx = weightIdx + 1
        NEXT k
    NEXT i

    PRINT "Weights initialized: " + STR$(weightIdx)

    FOR epoch = 1 TO numEpochs
        totalError = 0

        FOR sample = 0 TO 3
            IF sample = 0 THEN inputs(0) = 0
            IF sample = 0 THEN inputs(1) = 0
            IF sample = 0 THEN inputs(2) = 1

            IF sample = 1 THEN inputs(0) = 0
            IF sample = 1 THEN inputs(1) = 1
            IF sample = 1 THEN inputs(2) = 0

            IF sample = 2 THEN inputs(0) = 1
            IF sample = 2 THEN inputs(1) = 0
            IF sample = 2 THEN inputs(2) = 0

            IF sample = 3 THEN inputs(0) = 1
            IF sample = 3 THEN inputs(1) = 1
            IF sample = 3 THEN inputs(2) = 1

            IF inputs(0) + inputs(1) + inputs(2) >= 2 THEN
                target = 1.0
            ELSE
                target = 0.0
            END IF

            FOR j = 0 TO numHidden - 1
                sumVal = 0
                FOR i = 0 TO numInputs - 1
                    idx = i * numHidden + j
                    sumVal = sumVal + inputs(i) * weights(idx)
                NEXT i
                IF sumVal > 0 THEN
                    hidden(j) = sumVal
                ELSE
                    hidden(j) = 0.0
                END IF
            NEXT j

            FOR k = 0 TO numOutputs - 1
                sumVal = 0
                FOR j = 0 TO numHidden - 1
                    idx = numInputs * numHidden + j * numOutputs + k
                    sumVal = sumVal + hidden(j) * weights(idx)
                NEXT j
                IF sumVal > 0 THEN
                    output(k) = sumVal
                ELSE
                    output(k) = 0.0
                END IF
            NEXT k

            predicted = output(0)
            diff = target - predicted
            IF diff < 0 THEN diff = 0 - diff
            totalError = totalError + diff

            IF epoch = numEpochs THEN
                IF diff < 0.5 THEN accuracy = accuracy + 1
            END IF
        NEXT sample

        totalError = totalError / 4
    NEXT epoch

    PRINT "Training epochs: " + STR$(numEpochs)
    PRINT "Final accuracy: " + STR$(accuracy) + "/4"

    PRINT "=== End Neural Net ==="
    FUNCTION = 0
END FUNCTION
