Cover Image for Adaline Program in C
65 views

Adaline Program in C

The Adaline (Adaptive Linear Neuron) is a type of artificial neural network that learns to perform binary classification. It’s similar to the Perceptron but uses a different learning algorithm. Below is a simple C program implementing the Adaline algorithm.

C
#include <stdio.h>
#include <stdlib.h>
#include <math.h>

#define LEARNING_RATE 0.1
#define MAX_EPOCHS 1000
#define NUM_SAMPLES 4
#define NUM_FEATURES 3

// Training data (including bias)
int inputs[NUM_SAMPLES][NUM_FEATURES] = {
    {1, 0, 0}, // Input 1 with bias
    {1, 0, 1}, // Input 2 with bias
    {1, 1, 0}, // Input 3 with bias
    {1, 1, 1}  // Input 4 with bias
};

int target_outputs[NUM_SAMPLES] = {0, 0, 0, 1}; // Desired outputs

float weights[NUM_FEATURES] = {0.5, 0.5, 0.5}; // Initial weights (including bias)

// Activation function (identity function for Adaline)
float activation(float input) {
    return input;
}

// Update weights using Adaline learning rule
void updateWeights(float error, int *input) {
    for (int i = 0; i < NUM_FEATURES; i++) {
        weights[i] += LEARNING_RATE * error * input[i];
    }
}

int main() {
    int epoch, i, j;
    float net_input, output, error;

    for (epoch = 0; epoch < MAX_EPOCHS; epoch++) {
        for (i = 0; i < NUM_SAMPLES; i++) {
            // Calculate net input
            net_input = 0;
            for (j = 0; j < NUM_FEATURES; j++) {
                net_input += weights[j] * inputs[i][j];
            }

            // Apply activation function
            output = activation(net_input);

            // Calculate error
            error = target_outputs[i] - output;

            // Update weights
            updateWeights(error, inputs[i]);
        }
    }

    // Print final weights
    printf("Final Weights:\n");
    for (i = 0; i < NUM_FEATURES; i++) {
        printf("w%d = %.2f\n", i, weights[i]);
    }

    // Test the trained Adaline
    printf("\nTesting the trained Adaline:\n");
    for (i = 0; i < NUM_SAMPLES; i++) {
        net_input = 0;
        for (j = 0; j < NUM_FEATURES; j++) {
            net_input += weights[j] * inputs[i][j];
        }

        output = activation(net_input);

        printf("Input: ");
        for (j = 0; j < NUM_FEATURES; j++) {
            printf("%d ", inputs[i][j]);
        }
        printf("Output: %.2f\n", output);
    }

    return 0;
}

This C program implements a simple Adaline that learns to perform the logical OR operation on two binary inputs. The program first defines some constants and arrays for training data, target outputs, and initial weights. It then implements the activation function (which is the identity function for Adaline) and the weight updating function based on the Adaline learning rule.

The program iterates through epochs and samples to train the Adaline. After training, it prints out the final weights. Finally, it tests the trained Adaline using the same inputs to see the predicted outputs.

Compile and run this program to see the Adaline in action. Keep in mind that this is a simplified example for educational purposes and may not cover all aspects of a real-world application.

YOU MAY ALSO LIKE...

The Tech Thunder

The Tech Thunder

The Tech Thunder


COMMENTS