0% found this document useful (0 votes)
16 views6 pages

Neural Network Code Examples in Python

assy

Uploaded by

kritbarnwal5004
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)
16 views6 pages

Neural Network Code Examples in Python

assy

Uploaded by

kritbarnwal5004
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

Submitted by:- Kirti Singhal

BCA V M2

09413702022

ASSIGNMENT – 4

SUB : MACHINE LEARNING WITH PYTHON

1. What is an Artificial Neural Network (ANN), and how does it draw inspiration
from biological neural networks?

Solution: An Artificial Neural Network (ANN) is a computational model inspired by the way
biological neural networks in the human brain process information. In biological neural
networks, neurons are connected through synapses, which strengthen or weaken based on
learning experiences. Similarly, ANNs consist of layers of artificial neurons or "nodes"
connected by "weights." These weights adjust during training to make the network learn patterns
in data.

 Input Layer: Receives the input data.


 Hidden Layers: Perform computations and extract features.
 Output Layer: Produces the final prediction.

Example Code (Simple ANN using Python's Keras library):

from [Link] import Sequential

from [Link] import Dense

# Initialize the ANN

model = Sequential()

# Adding input layer and the first hidden layer

[Link](Dense(units=6, activation='relu', input_dim=4))

# Adding the second hidden layer

[Link](Dense(units=6, activation='relu'))
# Adding the output layer

[Link](Dense(units=1, activation='sigmoid'))

# Compiling the ANN

[Link](optimizer='adam', loss='binary_crossentropy', metrics=['accuracy'])

# Fitting the ANN to the training set (sample training data required)

# [Link](X_train, y_train, epochs=50, batch_size=10)

2. Explain the Hebbian learning rule and its significance in neural


networks.
Solution: The Hebbian learning rule, often summarized as "cells that fire together wire
together," is based on the idea that if two neurons are activated simultaneously, their connection
strengthens. This rule is essential for unsupervised learning, as it allows networks to self-
organize and discover patterns in data without explicit output labels.

Mathematical Formulation: Δwij=η⋅xi⋅yj\Delta w_{ij} = \eta \cdot x_i \cdot y_jΔwij=η⋅xi⋅yj


where:

 Δwij\Delta w_{ij}Δwij is the change in the weight between neurons iii and jjj,
 η\etaη is the learning rate,
 xix_ixi is the input from neuron iii,
 yjy_jyj is the output from neuron jjj.

Example Code:

import numpy as np

# Sample Hebbian Learning Rule Implementation

def hebbian_learning(weights, inputs, outputs, learning_rate=0.01):

delta_w = learning_rate * [Link](inputs, outputs)

return weights + delta_w

# Example data
inputs = [Link]([1, 0, -1])

outputs = [Link]([1, -1, 0])

weights = [Link]((3, 3))

# Update weights

weights = hebbian_learning(weights, inputs, outputs)

print("Updated Weights:\n", weights)

3. What is the perceptron learning rule, and how does it adjust


weights during training?
Solution: The perceptron learning rule updates weights based on errors between predicted and
actual outputs. It iteratively adjusts the weights to minimize these errors, making it suitable for
binary classification tasks.

Perceptron Learning Rule Formula: w=w+η⋅(y−y^)⋅xw = w + \eta \cdot (y - \hat{y}) \cdot


xw=w+η⋅(y−y^)⋅x where:

 www is the weight,


 η\etaη is the learning rate,
 yyy is the true label,
 y^\hat{y}y^ is the predicted label.

Example Code (Perceptron Training Algorithm):

import numpy as np

# Perceptron Learning Algorithm

def perceptron_learning(X, y, learning_rate=0.1, epochs=100):

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

for epoch in range(epochs):

for i, x_i in enumerate(X):

y_pred = [Link](x_i, weights) > 0

weights += learning_rate * (y[i] - y_pred) * x_i

return weights
# Sample data

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

y = [Link]([1, 0, 0, 0]) # AND gate example

# Train the perceptron

weights = perceptron_learning(X, y)

print("Trained Weights:", weights)

4. Explain the concept of adaptive weights in Adaline.


Solution: Adaline (Adaptive Linear Neuron) is a type of neural network model that adjusts
weights continuously, unlike the perceptron, which updates only when errors occur. Adaline’s
weights are updated using the mean squared error between predicted and actual outputs, making
it suitable for regression tasks.

Weight Update Formula: w=w+η⋅(y−y^)⋅xw = w + \eta \cdot (y - \hat{y}) \cdot


xw=w+η⋅(y−y^)⋅x

Example Code (Adaline using Stochastic Gradient Descent):

import numpy as np

def adaline_sgd(X, y, learning_rate=0.01, epochs=10):

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

for epoch in range(epochs):

for i, x_i in enumerate(X):

y_pred = [Link](x_i, weights)

error = y[i] - y_pred

weights += learning_rate * error * x_i

return weights

# Sample data for Adaline


X = [Link]([[1, 2], [2, 3], [3, 4]])

y = [Link]([1, 2, 3])

# Train Adaline model

weights = adaline_sgd(X, y)

print("Trained Weights:", weights)

5. Compare and contrast linear and nonlinear activation functions.


Solution:

 Linear Activation Function: The output is a weighted sum of inputs. Linear functions
are simple but lack the capacity to capture complex relationships.
o Formula: f(x)=xf(x) = xf(x)=x
o Limitation: Cannot introduce non-linearity, making it inadequate for deep neural
networks.
 Nonlinear Activation Functions: These functions introduce non-linearity, which helps
networks capture complex patterns.
o Sigmoid: f(x)=11+e−xf(x) = \frac{1}{1 + e^{-x}}f(x)=1+e−x1 (Used for binary
classification)
o ReLU: f(x)=max⁡(0,x)f(x) = \max(0, x)f(x)=max(0,x) (Efficient for deep
networks, avoiding the vanishing gradient problem)
o Tanh: f(x)=ex−e−xex+e−xf(x) = \frac{e^x - e^{-x}}{e^x + e^{-
x}}f(x)=ex+e−xex−e−x (Output ranges from -1 to 1, preserving the sign of the
input)

Example Code:

import numpy as np

# Activation functions

def sigmoid(x):

return 1 / (1 + [Link](-x))
def relu(x):

return [Link](0, x)

def tanh(x):

return [Link](x)

# Example input

x = [Link]([-2.0, -1.0, 0.0, 1.0, 2.0])

print("Sigmoid:", sigmoid(x))

print("ReLU:", relu(x))

print("Tanh:", tanh(x))

Common questions

Powered by AI

The perceptron learning algorithm utilizes linear classifications by adjusting weights based on the predicted boolean outcome of activation (y_pred = np.dot(x_i, weights) > 0). It modifies the weights to better align the predicted output with the actual binary labels, refining the decision boundary iteratively to improve classification over multiple training epochs .

The concept 'cells that fire together wire together' relates to ANN learning as it forms the basis of the Hebbian learning rule, where simultaneous activation of neurons leads to strengthening their connection. This principle guides how artificial neurons in an ANN adjust their weights, enhancing the network's ability to identify and encode patterns based on input data correlations .

ReLU (Rectified Linear Unit) contributes to solving the vanishing gradient problem by maintaining a constant gradient for positive input values, allowing efficient training of deep networks. It avoids the issue of gradients shrinking towards zero, as occurs with some other activation functions like Sigmoid or Tanh, which ensures the learning process remains robust in deeper layers .

The Hebbian learning rule is significant in neural networks as it facilitates unsupervised learning. It allows networks to self-organize and discover patterns in data without explicit output labels, based on the principle that neurons that fire together strengthen their connectivity. This rule enables the network to develop complex ideas of data structures in an unsupervised manner .

In an ANN, the input layer receives input data, serving as the entry point where raw data is fed into the network. Hidden layers perform computations and extract features by applying activation functions to input data, processing it further. The output layer produces the final prediction, translating the learned patterns into output comprehensible by users .

Adaline differs from the perceptron because it continuously adjusts weights using the mean squared error between predicted and actual outputs rather than updating weights only when errors occur. This makes Adaline suitable for regression tasks, as it focuses on minimizing a continuous error function through weight adaptation .

Nonlinear activation functions are essential for modern neural networks because they enable the learning of complex, non-linear data patterns. By introducing non-linearity, functions like Sigmoid, Tanh, and ReLU allow networks to model complex relationships and hierarchies in data, necessary for handling diverse tasks such as image and speech recognition .

Linear activation functions output a simple weighted sum of inputs, which is straightforward but lacks the ability to capture complex relationships and thus inadequate for deep networks. Nonlinear activation functions, such as Sigmoid, ReLU, and Tanh, introduce non-linearity to help networks learn complex patterns, making them essential for modern neural networks .

An Artificial Neural Network (ANN) is a computational model inspired by the way biological neural networks in the human brain process information. In biological systems, neurons connect via synapses, with connections that strengthen or weaken based on learning experiences. Similarly, ANNs consist of artificial neurons or "nodes" connected by "weights," which adjust during training to learn patterns in data .

The perceptron learning rule updates weights by minimizing errors between predicted and actual outputs. It involves iterative adjustments to the weights to align predictions with true labels, particularly effective for binary classification tasks. The rule uses the formula: w = w + η ⋅ (y - ŷ) ⋅ x, where η is the learning rate, y is the true label, and ŷ is the predicted label .

You might also like