0% found this document useful (0 votes)
281 views12 pages

Fuzzy Set Operations and Activation Functions

The document contains 13 C programming code examples related to fuzzy logic and neural networks concepts: 1) Code to calculate the union, intersection, and complement of fuzzy sets. 2) Code implementing the lambda cut of a fuzzy set. 3) Code extending the lambda cut to matrices. 4) Code calculating the cartesian product of two fuzzy sets. 5) Code implementing common neural network activation functions like sigmoid, gaussian, bipolar, and binary. 6) Code for a basic feedforward neural network. 7) Code for linear regression using gradient descent. 8) Code implementing Hebb's learning rule. 9) Code for max-product composition of fuzzy relations.

Uploaded by

Mayuri Bapat
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
281 views12 pages

Fuzzy Set Operations and Activation Functions

The document contains 13 C programming code examples related to fuzzy logic and neural networks concepts: 1) Code to calculate the union, intersection, and complement of fuzzy sets. 2) Code implementing the lambda cut of a fuzzy set. 3) Code extending the lambda cut to matrices. 4) Code calculating the cartesian product of two fuzzy sets. 5) Code implementing common neural network activation functions like sigmoid, gaussian, bipolar, and binary. 6) Code for a basic feedforward neural network. 7) Code for linear regression using gradient descent. 8) Code implementing Hebb's learning rule. 9) Code for max-product composition of fuzzy relations.

Uploaded by

Mayuri Bapat
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

1.

//Union, Intersection and Complement of Fuzzy set


#include <stdio.h>
int main()
{
float a[10],b[10],max[10],min[10];
int n,i,j;
printf("Enter no. of elements=");
scanf("%d",&n);
printf("Enter fuzzy set A=");
for(i=0;i<n;i++)
scanf("%f",&a[i]);
printf("Enter fuzzy set B=");
for(i=0;i<n;i++)
scanf("%f",&b[i]);
for(i=0;i<n;i++)
{
if(a[i]>b[i])
max[i]=a[i];
else
max[i]=b[i];
}
printf("Union of Fuzzy sets A & B is=");
for(i=0;i<n;i++)
printf("%f\t",max[i]);
printf("\n************************************************************
*************\n");
for(i=0;i<n;i++)
{
if(a[i]<b[i])
min[i]=a[i];
else
min[i]=b[i];
}
printf("\nIntersection of Fuzzy sets A & B is=");
for(i=0;i<n;i++)
printf("%f\t",min[i]);
printf("\n************************************************************
*************\n");
printf("\nComplement of Fuzzy sets A is=");
for(i=0;i<n;i++)
printf("%f\t",1-a[i]);
printf("\n************************************************************
*************\n");
printf("\nComplement of Fuzzy sets B is=");
for(i=0;i<n;i++)
printf("%f\t",1-b[i]);
}
2// A program to implement lambda cut
#include<stdio.h>
int main()
{
float a[10],lambda;
int n,i;
printf("Enter no. of elements=");
scanf("%d",&n);
printf("Enter fuzzy set A=");
for(i=0;i<n;i++)
scanf("%f",&a[i]);
printf("\n Enter value of lambda:-");
scanf("%f",&lambda);
printf("Lambda Cut Set is given by=");
for(i=0;i<n;i++)
{
if(a[i]>=lambda)
printf("\t %f",a[i]);
}
}
3// A program to implement lambda cut for matrix
#include<stdio.h>
int main()
{
float a[10][10],lambda;
int r,c,i,j;
printf("Enter no. of rows & Columns=");
scanf("%d%d",&r,&c);
printf("Enter fuzzy set A=");
for(i=0;i<r;i++)
for(j=0;j<c;j++)
scanf("%f",&a[i][j]);

printf("\n Enter value of lambda:-");


scanf("%f",&lambda);
printf("Lambda Cut Set is given by=\n");
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
if(a[i][j]>=lambda)
printf("\t 1");
else
printf("\t 0");
}
printf("\n");
}
}
4// Program for cartesian product
#include <stdio.h>
int main()
{
float a[10],b[10],cartesian[10][10];
int r,c,i,j;
printf("Enter no. of elements in A & B=");
scanf("%d%d",&r, &c);
printf("Enter fuzzy set A=");
for(i=0;i<r;i++)
scanf("%f",&a[i]);
printf("Enter fuzzy set B=");
for(i=0;i<c;i++)
scanf("%f",&b[i]);
printf("\n cartesian product of AXB is=\n");
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
if(a[i]<b[j])
cartesian[i][j]=a[i];
else
cartesian[i][j]=b[j];
}
}
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
printf("\t%f",cartesian[i][j]);
}
printf("\n");
}
}

5
6Write a program to implement sigmoid Activation Function
#include <stdio.h>
#include <math.h>
double sigmoid(double x) {
return 1.0 / (1.0 + exp(-x));
}
int main() {
double input, output;
// Read the input value from user
printf("Enter the input value: ");
scanf("%lf", &input);
// Compute the sigmoid activation function
output = sigmoid(input);
// Print the output value to the console
printf("Output: %lf\n", output);
return 0;
}
7. Write a program to implement Gaussian Activation Function
#include <stdio.h>
#include <math.h>

double gaussian(double x, double mu, double sigma) {


double exponent = -1.0 * ((x - mu) * (x - mu)) / (2.0 * sigma * sigma);
return exp(exponent);
}

int main() {
// Test the Gaussian Activation Function
double x = 0.5;
double mu = 0.0;
double sigma = 1.0;
double output = gaussian(x, mu, sigma);

// Print the output value


printf("Output: %lf\n", output);

return 0;
}
8. Write a program to implement Bipolar Activation Function

#include <stdio.h>
double bipolar(double x) {
if (x >= 0) {
return 1.0;
} else {
return -1.0;
}
}
int main() {
// Test the bipolar Activation Function
double x = 0.5;
double output = bipolar(x);

// Print the output value


printf("Output: %lf\n", output);

return 0;
}
9. Write a program to implement binary Activation Function
#include <stdio.h>
int binary(double x) {
if (x >= 0) {
return 1;
} else {
return 0;
}
}
int main() {
// Test the binary Activation Function
double x = -0.5;
int output = binary(x);

// Print the output value


printf("Output: %d\n", output);

return 0;
}
[Link] a program to implement Feed Forward Network
#include <stdio.h>
#include <stdlib.h>
#include <math.h>

#define NUM_INPUTS 2
#define NUM_HIDDEN 3
#define NUM_OUTPUTS 1

double sigmoid(double x) {
return 1.0 / (1.0 + exp(-x));
}

int main() {
// Initialize the input values
double input[NUM_INPUTS] = {0.05, 0.10};

// Initialize the weights and biases for the hidden layer


double hidden_weights[NUM_INPUTS][NUM_HIDDEN] = {{0.15, 0.25, 0.35},
{0.20, 0.30, 0.40}};
double hidden_bias[NUM_HIDDEN] = {0.35, 0.35, 0.60};

// Initialize the weights and biases for the output layer


double output_weights[NUM_HIDDEN][NUM_OUTPUTS] = {{0.45},
{0.50},
{0.55}};
double output_bias[NUM_OUTPUTS] = {0.60};

// Forward pass through the network


double hidden_output[NUM_HIDDEN];
double output[NUM_OUTPUTS];
for (int i = 0; i < NUM_HIDDEN; i++) {
double node_input = 0.0;
for (int j = 0; j < NUM_INPUTS; j++) {
node_input += input[j] * hidden_weights[j][i];
}
node_input += hidden_bias[i];
hidden_output[i] = sigmoid(node_input);
}
for (int i = 0; i < NUM_OUTPUTS; i++) {
double node_input = 0.0;
for (int j = 0; j < NUM_HIDDEN; j++) {
node_input += hidden_output[j] * output_weights[j][i];
}
node_input += output_bias[i];
output[i] = sigmoid(node_input);
}
// Print the output values
printf("Output: %lf\n", output[0]);
return 0;
}

11Write a C program for Linear Regression Supervised Learning Algorithm


#include <stdio.h>
#define LEARNING_RATE 0.01
#define MAX_ITERATIONS 1000
double dot_product(double x[], double w[], int n) {
double result = 0.0;
for (int i = 0; i < n; i++) {
result += x[i] * w[i];
}
return result;
}
int main() {
// Training dataset for the linear regression algorithm
double X[5][2] = {{1, 1}, {2, 3}, {3, 5}, {4, 7}, {5, 9}};
double y[5] = {3, 7, 11, 15, 19};
// Initialize the weights and bias
double w[2] = {0.0, 0.0};
double b = 0.0;
// Train the linear regression model using the training dataset
int iterations = 0;
double mse = 0.0;
while (iterations < MAX_ITERATIONS) {
mse = 0.0;
for (int i = 0; i < 5; i++) {
double z = dot_product(X[i], w, 2) + b;
double error = y[i] - z;
w[0] += LEARNING_RATE * error * X[i][0];
w[1] += LEARNING_RATE * error * X[i][1];
b += LEARNING_RATE * error;
mse += error * error;
}
mse /= 5.0;
iterations++;
}
// Print the trained weights and bias
printf("Trained weights: w1 = %lf, w2 = %lf\n", w[0], w[1]);
printf("Trained bias: b = %lf\n", b);
// Test the linear regression model on new input
double input[2];
printf("Enter the input values: ");
scanf("%lf %lf", &input[0], &input[1]);
double output = dot_product(input, w, 2) + b;
printf("Output: %lf\n", output);
return 0;
}
12. //Write a program to implement Hebb’s Rule
#include <stdio.h>
#define N 3 // Number of inputs
#define M 2 // Number of outputs
int main() {
// Define the input and output arrays
double input[N][M] = {
{1.0, 0.0},
{0.0, 1.0},
{1.0, 1.0}
};
double output[N] = {1.0, 0.5, 0.0};
// Define the weight matrix
double weights[M] = {1.0, 0.0};
// Train the weight matrix using Hebb's Rule
for (int i = 0; i < N; i++) {
for (int j = 0; j < M; j++) {
weights[j] += input[i][j] * output[i];
}
}
// Print the trained weight matrix
printf("Trained Weight Matrix:\n");
for (int i = 0; i < M; i++) {
printf("%lf ", weights[i]);
}
printf("\n");
return 0;
}
13 C program for max product composition
#include <stdio.h>
#define N 3 // Number of elements in the input sets
// Define the fuzzy relations
double R1[N][N] = {
{0.6, 0.2, 0.1},
{0.3, 0.7, 0.4},
{0.1, 0.5, 0.8}
};
double R2[N][N] = {
{0.5, 0.3, 0.2},
{0.2, 0.6, 0.5},
{0.1, 0.4, 0.7}
};
// Define the result relation
double R3[N][N];
int main() {
// Compute the max product composition of R1 and R2
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
double max_product = 0.0;
for (int k = 0; k < N; k++) {
double product = R1[i][k] * R2[k][j];
if (product > max_product) {
max_product = product;
}
}
R3[i][j] = max_product;
}
}
// Print the result relation
printf("Result Relation:\n");
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
printf("%lf ", R3[i][j]);
}
printf("\n");
}
return 0;
}
Output:

Common questions

Powered by AI

Sigmoid activation functions map any input value to a range between 0 and 1, which is useful for probabilities and layers needing smooth transitions . Gaussian functions, which depend on mean and variance, are used in Radial Basis Function networks for clustering or as a measure of closeness . Bipolar functions produce outputs in the range [-1, 1], often used in neural networks requiring symmetric behavior around zero . Binary functions return outputs of only 0 or 1, commonly used in networks requiring strict binary decisions, such as classification tasks . Each function's choice depends on the specific application requirements, like non-linearity or binary decision boundaries.

Hebb's Rule states that synaptic connections strengthen when there is simultaneous activation of pre and post-synaptic neurons . In the neural network model, this is applied by adjusting weights with the product of input and corresponding outputs . This principle mimics biological learning processes, underpinning unsupervised learning models where patterns emerge from data without explicit error corrections. It's especially utilized in associative and self-organizing networks for pattern storage and recognition.

The iterative optimization process refines linear regression by progressively minimizing the mean squared error (MSE) between predicted and actual outputs . This involves adjusting weight parameters and biases in small, incremental steps dictated by the learning rate . Over iterations, the adjustments align predictions more closely with actual values, improving the model's predictive capability by learning the underlying data distributions, which ensures better generalization on unseen data.

Biases in a feedforward neural network adjust the threshold at which neurons activate, effectively shifting the input signal requirements for activation . Their inclusion is critical as they allow models to change the output range, enabling the modeling of complex patterns and data shifts that pure linear transformations cannot capture . By fine-tuning activations, biases enhance the network's ability to learn diverse data distributions, contributing to more accurate model outputs and improved performance in tasks requiring non-linear separations.

In programming environments, fuzzy set operations are represented through logical conditionals to compute union, intersection, and complement based on membership values . For union, the operation uses maximum values between corresponding elements. Intersection employs minimum values, while complement computes the negation of membership values . These operations enable the modeling of vague and imprecise information, applied in control systems, decision making, and knowledge-based systems where classical binary logic is insufficient due to uncertainty.

The input dimension in feedforward neural networks directly affects complexity and computational requirements as it determines the size of the input layer and influences the number of weights and biases throughout the network . Higher dimensionality implies more connections between layers, thus necessitating more computational resources to handle increased operations. This dimensionality demands more memory for parameter storage and longer training times. Optimal design balances dimensionality with computation, ensuring the model isn't overfitted or unnecessarily resource-intensive.

The sigmoid function's mathematical form, given by 1/(1 + exp(-x)), contributes to its normalization capability by mapping any real-valued number into the range (0, 1). This property helps in modeling outputs required to be interpreted as probabilities. The function's gentle slope stabilizes gradient descent by preventing drastic shifts in weight updates, curbing the effects of derivative fluctuations, which is vital for stable learning in neural networks.

In a single-dimensional array, the lambda-cut is applied by thresholding the elements with the lambda value, and elements meeting or exceeding this threshold are retained . This can be interpreted as filtering elements with sufficient degree of membership. For matrices, lambda-cut operates similarly but on a per-element basis, transforming each entry to a binary output indicating whether it meets the lambda threshold or not, with transformations into binary matrices . Implications include varying applications in fuzzy logic systems such as pattern recognition and decision support systems where element selection or filtering is critical.

The max-product composition method is significant as it combines two fuzzy relations by calculating the maximum value of the products of corresponding membership values . This provides a robust measure of composite relation strength, capturing the maximum impact across relations. Unlike min-based compositions or sums, it emphasizes highest compatibility levels, ensuring that maximum contributions in any pairwise set are accounted for. This is particularly valuable in decision-making processes and modeling complex interactions where highest reliability is desired.

The Cartesian product for fuzzy sets is implemented by comparing elements of sets A and B and selecting the minimum for each pair in the product . This implementation creates a matrix representing all possible combinations of set elements, showing the compatibility or interaction between elements of different sets. It reveals how combinations may jointly contribute to a fuzzy relation or complex systems designed to model multi-input scenarios like expert systems or control mechanisms.

You might also like