0% found this document useful (0 votes)
27 views20 pages

Python Neural Network Activation Functions

The document outlines various practical exercises involving Python programming for neural networks, including plotting activation functions, implementing McCulloch-Pitts neurons, recognizing even and odd numbers with a perceptron, and creating a neural network for multi-class classification. Each practical includes code snippets demonstrating the concepts and algorithms used in neural network training and evaluation. The exercises cover topics such as forward propagation, back propagation, and decision regions visualization.
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)
27 views20 pages

Python Neural Network Activation Functions

The document outlines various practical exercises involving Python programming for neural networks, including plotting activation functions, implementing McCulloch-Pitts neurons, recognizing even and odd numbers with a perceptron, and creating a neural network for multi-class classification. Each practical includes code snippets demonstrating the concepts and algorithms used in neural network training and evaluation. The exercises cover topics such as forward propagation, back propagation, and decision regions visualization.
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

Practical no 1

Write a Python program to plot a few activation functions that are being used in neural networks.
import numpy as np

import [Link] as plt

def sigmoid(x):

return 1 / (1 + [Link](-x))

# Generate x values from -10 to 10

x = [Link](-10, 10, 100)

# Calculate sigmoid function values for x

y = sigmoid(x)

# Plot sigmoid function

[Link](x, y)

[Link]('Sigmoid Activation Function')

[Link]('x')

[Link]('sigmoid(x)')

[Link]()
Practical no 2

Generate ANDNOT function using McCulloch-Pitts neural net by a python program


[Link]

—----------------------------------------------------------------------------------------------------------------------
--------
import numpy as np

def mp_neuron(inputs, weights, threshold):

weighted_sum = [Link](inputs, weights)

output = 1 if weighted_sum >= threshold else 0

return output

def and_not(x1, x2):

weights = [1, -1]

threshold = 1

inputs = [Link]([x1, x2])

output = mp_neuron(inputs, weights, threshold)

return output

print(and_not(0, 0))

print(and_not(1, 0))

print(and_not(0, 1))

print(and_not(1, 1))

—----------------------------------------------------------------------------------------------------------------------------------

import numpy as np

# Define the input and output vectors for the AND NOT function

X = [Link]([[0, 0],

[1, 0],

[0, 1]])

Y = [Link]([1, 0, 1])

# Initialize the weights and bias


w = [Link]([0.0, 0.0], dtype=np.float64)

b = 0.0

# Set the learning rate and number of iterations

lr = 0.1

n_iter = 10

# Define the step function for the output

def step(x):

return 1 if x > 0 else 0

# Train the network using the perceptron learning algorithm

for i in range(n_iter):

for j in range(len(X)):

x = X[j]

y = Y[j]

z = [Link](x, w) + b

o = step(z)

dw = lr * (y - o) * x

db = lr * (y - o)

w += dw

b += db

# Test the network using the trained weights and bias

test_x = [Link]([1, 1])

test_z = [Link](test_x, w) + b

test_o = step(test_z)

# Print the results

print("Input: ", test_x)

print("Output: ", test_o)


Practical No 3

Write a Python Program using Perceptron Neural Network to recognize even and odd numbers.
Given numbers are in ASCII from 0 to 9

import numpy as np

j = int(input("Enter a Number (0-9): "))

step_function = lambda x: 1 if x >= 0 else 0

training_data = [

{'input': [0, 0, 0, 0, 0, 0], 'label': 1},

{'input': [0, 0, 0, 0, 0, 1], 'label': 0},

{'input': [0, 0, 0, 0, 1, 0], 'label': 1},

{'input': [0, 0, 0, 1, 1, 1], 'label': 0},

{'input': [0, 0, 0, 1, 0, 0], 'label': 1},

{'input': [0, 0, 0, 1, 0, 1], 'label': 0},

{'input': [0, 0, 0, 1, 1, 0], 'label': 1},

{'input': [0, 0, 0, 1, 1, 1], 'label': 0},

{'input': [0, 0, 1, 0, 0, 0], 'label': 1},

{'input': [0, 0, 1, 0, 0, 1], 'label': 0},

weights = [Link]([0, 0, 0, 0, 0, 1])

for data in training_data:

input = [Link](data['input'])

label = data['label']

output = step_function([Link](input, weights))

error = label - output

weights += input * error

input = [Link]([int(x) for x in list('{0:06b}'.format(j))])

output = "odd" if step_function([Link](input, weights)) == 0 else "even"

print(j, " is ", output)


Practical No 4

With a suitable example demonstrate the perceptron learning law with its decision regions using
python. Give the output in graphical form.

Ref:
[Link]
thon/

import numpy as np

import [Link] as plt

from sklearn.linear_model import Perceptron

# Generate random data

[Link](0)

X = [Link](100, 2)

y = [Link](X[:, 0] + X[:, 1] > 0, 1, -1)

# Create perceptron object

clf = Perceptron(random_state=0)

# Fit perceptron to the data

[Link](X, y)

# Plot decision regions

xmin, xmax = X[:, 0].min() - 1, X[:, 0].max() + 1

ymin, ymax = X[:, 1].min() - 1, X[:, 1].max() + 1

xx, yy = [Link]([Link](xmin, xmax, 0.1),

[Link](ymin, ymax, 0.1))

Z = [Link](np.c_[[Link](), [Link]()])

Z = [Link]([Link])

[Link](xx, yy, Z, alpha=0.4)

[Link](X[:, 0], X[:, 1], c=y, alpha=0.8)

[Link](xmin, xmax)

[Link](ymin, ymax)
[Link]('Feature 1')

[Link]('Feature 2')

[Link]()

—---------------------------------------------------------------------------------------------------------------------------------

import numpy as np

import [Link] as plt

X = [Link]([[0, 0], [1, 0], [0, 1], [1, 1]])

Y = [Link]([-1, -1, -1, 1])

w = [Link]([Link][1])

b=0

for _ in range(6):

for i in range([Link][0]):

y_pred = [Link]([Link](X[i], w) + b)

if y_pred != Y[i]:

w += 0.3 * Y[i] * X[i]

b += 0.3 * Y[i]
x_min, x_max = X[:, 0].min() - 1, X[:, 0].max() + 1

y_min, y_max = X[:, 1].min() - 1, X[:, 1].max() + 1

xx, yy = [Link]([Link](x_min, x_max, 0.01),

[Link](y_min, y_max, 0.01))

Z = [Link]([Link](np.c_[[Link](), [Link]()], w) + b)

Z = [Link]([Link])

[Link](xx, yy, Z, alpha=0.8)

[Link](X[:, 0], X[:, 1], c=Y)

[Link]('X1')

[Link]('X2')

[Link]('Perceptron Decision Regions')

[Link]()
Practical No- 5

Implement Artificial Neural Network training process in Python by using Forward Propagation,
Back Propagation

Program 1 :

from joblib.numpy_pickle_utils import xrange

from numpy import *

class NeuralNet(object):

def __init__(self):

# Generate random numbers

[Link](1)

# Assign random weights to a 3 x 1 matrix,

self.synaptic_weights = 2 * [Link]((3, 1)) - 1

# The Sigmoid function

def __sigmoid(self, x):

return 1 / (1 + exp(-x))

# The derivative of the Sigmoid function.

# This is the gradient of the Sigmoid curve.

def __sigmoid_derivative(self, x):

return x * (1 - x)

# Train the neural network and adjust the weights each time.

def train(self, inputs, outputs, training_iterations):

for iteration in xrange(training_iterations):

# Pass the training set through the network.

output = [Link](inputs)

# Calculate the error

error = outputs - output

# Adjust the weights by a factor

factor = dot(inputs.T, error * self.__sigmoid_derivative(output))


self.synaptic_weights += factor

# The neural network thinks.

def learn(self, inputs):

return self.__sigmoid(dot(inputs, self.synaptic_weights))

if __name__ == "__main__":

# Initialize

neural_network = NeuralNet()

# The training set.

inputs = array([[0, 1, 1], [1, 0, 0], [1, 0, 1]])

outputs = array([[1, 0, 1]]).T

# Train the neural network

neural_network.train(inputs, outputs, 10000)

# Test the neural network with a test example.

print(neural_network.learn(array([1, 0, 1])))

********************************************************************************************

Program 2:

import numpy as np

# Define the sigmoid activation function and its derivative

def sigmoid(x):

return 1 / (1 + [Link](-x))

def sigmoid_derivative(x):

return x * (1 - x)

# Define the input and target output data

X = [Link]([[0, 0], [0, 1], [1, 0], [1, 1]])

y = [Link]([[0], [1], [1], [0]])

# Define the neural network structure and initialize weights

input_size = 2
hidden_size = 2

output_size = 1

weights_input_hidden = [Link](size=(input_size, hidden_size))

weights_hidden_output = [Link](size=(hidden_size, output_size))

# Set the learning rate and number of epochs

learning_rate = 0.1

num_epochs = 10000

# Define the training loop

for i in range(num_epochs):

# Forward propagation

hidden_layer_input = [Link](X, weights_input_hidden)

hidden_layer_activation = sigmoid(hidden_layer_input)

output_layer_input = [Link](hidden_layer_activation, weights_hidden_output)

output_layer_activation = sigmoid(output_layer_input)

# Calculate the error and delta for backpropagation

error = y - output_layer_activation

output_delta = error * sigmoid_derivative(output_layer_activation)

hidden_delta = output_delta.dot(weights_hidden_output.T) * sigmoid_derivative(hidden_layer_activation)

# Update the weights

weights_hidden_output += hidden_layer_activation.[Link](output_delta) * learning_rate

weights_input_hidden += [Link](hidden_delta) * learning_rate

# Make predictions on the input data

hidden_layer_input = [Link](X, weights_input_hidden)

hidden_layer_activation = sigmoid(hidden_layer_input)

output_layer_input = [Link](hidden_layer_activation, weights_hidden_output)

output_layer_activation = sigmoid(output_layer_input)

print(output_layer_activation)
Practical No. 6

Create a Neural network architecture from scratch in Python and use it to do multi-class
classification on any data

import numpy as np

import pandas as pd

import [Link] as plt

from sklearn.model_selection import train_test_split

from [Link] import StandardScaler

# Add header names

headers = ['age', 'sex', 'chest_pain', 'resting_blood_pressure', 'serum_cholestoral',

'fasting_blood_sugar', 'resting_ecg_results', 'max_heart_rate_achieved',

'exercise_induced_angina', 'oldpeak', 'slope_of_the_peak', 'num_of_major_vessels',

'thal', 'heart_disease']

# Load the dataset

heart_df = pd.read_csv('/home/student/Downloads/[Link]', sep=',', names=headers)

# Convert input to numeric values (if any non-numeric values exist)

heart_df = heart_df.apply(pd.to_numeric, errors='coerce')

# Handle missing values (if any)

heart_df.fillna(heart_df.mean(), inplace=True)

# Convert input data to numpy arrays

X = heart_df.drop(columns=['heart_disease'])
# Replace target class with 0 and 1

heart_df['heart_disease'] = heart_df['heart_disease'].replace({1: 0, 2: 1})

y_label = heart_df['heart_disease'].[Link](-1, 1)

# Split data into train and test sets

Xtrain, Xtest, ytrain, ytest = train_test_split(X, y_label, test_size=0.2, random_state=2)

# Standardize the dataset

sc = StandardScaler()

Xtrain = sc.fit_transform(Xtrain)

Xtest = [Link](Xtest)

print(heart_df.dtypes)

print(f"\nShape of train set is {[Link]}")

print(f"\nShape of test set is {[Link]}")

print(f"\nShape of train label is {[Link]}")

print(f"\nShape of test labels is {[Link]}")

# Neural Network Class

class NeuralNet:

def _init_(self, layers=[13, 8, 1], learning_rate=0.001, iterations=1000):

[Link] = {}

self.learning_rate = learning_rate

[Link] = iterations

[Link] = []
[Link] = layers

self.X = None

self.y = None

def init_weights(self):

[Link](1)

[Link]["W1"] = [Link]([Link][0], [Link][1]) * 0.01

[Link]['b1'] = [Link]((1, [Link][1]))

[Link]['W2'] = [Link]([Link][1], [Link][2]) * 0.01

[Link]['b2'] = [Link]((1, [Link][2]))

def sigmoid(self, Z):

return 1 / (1 + [Link](-Z))

def dSigmoid(self, Z):

sig = [Link](Z)

return sig * (1 - sig)

def relu(self, Z):

return [Link](0, Z)

def dRelu(self, Z):

return (Z > 0).astype(float)

def eta(self, x):

return [Link](x, 1e-10)


def entropy_loss(self, y, yhat):

epsilon = 1e-10

yhat = [Link](yhat, epsilon, 1 - epsilon)

loss = -[Link](y * [Link](yhat) + (1 - y) * [Link](1 - yhat))

return loss

def forward_propagation(self):

Z1 = [Link]([Link]['W1']) + [Link]['b1']

A1 = [Link](Z1)

Z2 = [Link]([Link]['W2']) + [Link]['b2']

yhat = [Link](Z2)

loss = self.entropy_loss(self.y, yhat)

[Link]['Z1'] = Z1

[Link]['Z2'] = Z2

[Link]['A1'] = A1

return yhat, loss

def back_propagation(self, yhat):

m = [Link][0]

y_inv = 1 - self.y

yhat_inv = 1 - yhat

dl_wrt_yhat = (yhat - self.y) / m

dl_wrt_sig = [Link]([Link]['Z2'])

dl_wrt_z2 = dl_wrt_yhat * dl_wrt_sig


dl_wrt_A1 = dl_wrt_z2.dot([Link]['W2'].T)

dl_wrt_w2 = [Link]['A1'].[Link](dl_wrt_z2)

dl_wrt_b2 = [Link](dl_wrt_z2, axis=0, keepdims=True)

dl_wrt_z1 = dl_wrt_A1 * [Link]([Link]['Z1'])

dl_wrt_w1 = [Link](dl_wrt_z1)

dl_wrt_b1 = [Link](dl_wrt_z1, axis=0, keepdims=True)

[Link]['W1'] -= self.learning_rate * dl_wrt_w1

[Link]['W2'] -= self.learning_rate * dl_wrt_w2

[Link]['b1'] -= self.learning_rate * dl_wrt_b1

[Link]['b2'] -= self.learning_rate * dl_wrt_b2

def fit(self, X, y):

self.X = X

self.y = y

self.init_weights()

for i in range([Link]):

yhat, loss = self.forward_propagation()

self.back_propagation(yhat)

[Link](loss)

# Learning rate decay (optional)

if i % 100 == 0 and i != 0:

self.learning_rate *= 0.9 # Decay learning rate


if i % 100 == 0:

print(f"Iteration {i}, Loss: {loss}")

def predict(self, X):

Z1 = [Link]([Link]['W1']) + [Link]['b1']

A1 = [Link](Z1)

Z2 = [Link]([Link]['W2']) + [Link]['b2']

yhat = [Link](Z2)

return [Link](yhat)

def acc(self, y, yhat):

return [Link](y == yhat) * 100

def plot_loss(self):

[Link]([Link])

[Link]("Iterations")

[Link]("Loss")

[Link]("Loss curve during training")

[Link]()

# Train the model

nn = NeuralNet(layers=[13, 8, 1], learning_rate=0.001, iterations=1000)

[Link](Xtrain, ytrain)

# Evaluate the model

y_pred = [Link](Xtest)

accuracy = [Link](ytest, y_pred)


print(f"Test Accuracy: {accuracy}%")

# Plot the loss curve

nn.plot_loss()

Practical No. 7

Write a python program to illustrate ART neural network.

import numpy as np

def initialize_weights(input_dim, category):

weights = [Link](size=(input_dim,))

weights /= [Link](weights)

return weights

def calculate_similarity(input_pattern, weights):

return [Link](input_pattern, weights).sum()

def update_weights(input_pattern, weights, vigilance):

while True:

activation = calculate_similarity(input_pattern, weights)

if activation >= vigilance:

return weights

else:

weights[[Link](input_pattern)] += 1

weights /= [Link](weights)

def ART_neural_network(input_patterns, vigilance):

num_patterns, input_dim = input_patterns.shape


categories = []

for pattern in input_patterns:

matched_category = None

for category in categories:

if calculate_similarity(pattern, category["weights"]) >= vigilance:

matched_category = category

break

if matched_category is None:

weights = initialize_weights(input_dim, len(categories))

matched_category = {"weights": weights, "patterns": []}

[Link](matched_category)

matched_category["patterns"].append(pattern)

matched_category["weights"] = update_weights(pattern, matched_category["weights"], vigilance)

return categories

# Example usage

input_patterns = [Link]([[1, 0, 1, 0], [0, 1, 0, 1], [1, 1, 1, 0]])

vigilance = 0.5

categories = ART_neural_network(input_patterns, vigilance)

# Print the learned categories

for i, category in enumerate(categories):

print(f"Category {i+1}:")
print("Patterns:")

[print(pattern) for pattern in category["patterns"]]

print("Weights:")

print(category["weights"])

print()

Common questions

Powered by AI

The McCulloch-Pitts neuron is a simple model of a neuron in a neural network that operates using binary thresholds. It uses weighted summation of the inputs to determine whether a neuron should fire. In the generation of an ANDNOT function, it employs two inputs with weights [1, -1] and a threshold of 1. By using the rule `output = 1 if weighted_sum >= threshold else 0`, the neuron effectively implements the logical operation ANDNOT, which returns true only if the first input is true and the second input is false. This is demonstrated by the Python function `and_not(x1, x2)`, which uses the `mp_neuron` function to compute the correct binary output .

Forward propagation is the process in a neural network during which input data passes through each layer of the network, using weights and biases to compute intermediate values until reaching the output layer. In each layer, these calculations involve a linear combination of the inputs followed by an activation function, such as sigmoid, to introduce non-linearity. Forward propagation is essential for training as it allows the network to generate predictions that can be compared to actual output data to compute the loss. This loss value is then used in backpropagation to update the weights and biases of the network, forming a foundational component of the learning process .

Yes, the neural network architecture can be adjusted to improve classification accuracy on complex datasets by altering various factors. These include increasing the number of hidden layers and neurons to capture more complex patterns and introducing dropout layers for regularization to prevent overfitting. Additionally, selecting appropriate activation functions like ReLU can help in maintaining gradient flow during training. The depth and breadth of the network must align with dataset complexity; adding convolutional or recurrent layers can aid in tasks like image recognition and time series analysis, respectively. Hyperparameter tuning, including learning rate, batch size, and epoch number, also significantly impacts the performance and accuracy of the model .

A neural network determines the decision boundary through the weighted sum of its features and activation functions within its neurons, which collectively define regions of the input space categorized as different outputs. This boundary is influenced by the network's architecture, including layers, nodes, and type of activation functions. Factors affecting its accuracy include the quality and amount of training data, its feature representation, and hyperparameters such as learning rate and regularization methods. Overfitting and underfitting are critical challenges; the former occurs if the network is too complex for the data, while the latter happens if it is too simple. Proper regularization, training data augmentation, and cross-validation can help mitigate these issues .

The step function in a perceptron model is used as an activation function that helps decide the binary output of the perceptron. It processes the weighted sum of inputs plus bias and outputs either 0 or 1, depending on whether the result is below or above a certain threshold. Specifically, `step(x) = 1 if x > 0 else 0`. This function enables the perceptron to classify inputs into two categories, making it a fundamental component for tasks requiring binary classification. Its simplicity, however, limits the perceptron to linearly separable problems only .

Visualizing the decision regions of a perceptron involves plotting the feature space and indicating areas classified as each possible output. This can be accomplished using tools like matplotlib in Python, where the decision boundary is plotted by using meshgrid arrays that cover the feature space and applying the perceptron's prediction across this grid. The resulting contour plot or decision boundary gives insight into how the perceptron separates data into classes. It illustrates how changes in input feature values affect the classification and highlights potential limitations, such as the perceptron's inability to classify non-linearly separable data correctly. Such visualizations are crucial for understanding the nature of the dataset and the performance of the model .

The sigmoid activation function is used in neural networks to introduce non-linearity into the model, which allows the network to learn complex patterns. Its primary mathematical form is: sigmoid(x) = 1 / (1 + exp(-x)). This function maps any real-valued number into the range (0, 1), making it particularly suitable for binary classification tasks as it can act as a smooth threshold function. The sigmoid function is beneficial for compressing input data into a more manageable scale. However, it also suffers from the vanishing gradient problem during backpropagation, which can slow down learning in deeper networks .

The perceptron learning algorithm is a supervised learning technique used to train single-layer binary classifiers known as perceptrons. It iteratively adjusts the weights and bias of the network to minimize the error in classification. This is accomplished by iterating over the input data, calculating the output using a step function, and updating weights based on the difference between the predicted and actual outputs. The algorithm is particularly effective in linearly separable data, such as basic logical functions like AND, OR. For example, to learn an AND NOT function, the perceptron updates its weights using the perceptron rule: `w += lr * (y - o) * x`, converging to a solution over time if the data is linearly separable .

Backpropagation is an algorithm used to train neural networks, which involves calculating the gradient of the loss function with respect to each weight by the chain rule, updating weights to minimize this loss. During this process, each layer's error is computed, starting from the output layer and moving backward to the input layer. The weights are adjusted by: `weights += learning_rate * gradient`, which means they are incremented in the direction that reduces the loss. The sigmoid function's derivative plays a key role in calculating these gradients. The updated weights allow the network to better approximate the desired output, improving performance over successive iterations .

Activation functions like ReLU (Rectified Linear Unit) and sigmoid serve different roles in neural network training. The sigmoid function outputs values in the range (0, 1) and is used primarily in binary classification tasks due to its property of smooth gradient descent, though it suffers from vanishing gradients. ReLU, on the other hand, outputs the input directly if it is positive and zero otherwise, expressed as `ReLU(x) = max(0, x)`. This function is favored in deep networks because it mitigates the vanishing gradient issue, allowing for efficient learning by maintaining stronger gradient signals across multiple layers. While sigmoid helps with probabilities, ReLU enhances the ability to capture non-linearities without zeroing out gradients for the majority of its range .

You might also like