COMP-377Week7 v1.1
COMP-377Week7 v1.1
Artificial Neural
Networks – Part 1
Lecture 6 Review
❑ Logistic Regression ❑ The standard logistic function
➢ linear regression is not a (also known as the sigmoid
𝟏
good solution for predicting function), 𝒇 𝒙 = 𝟏+𝒆−𝒙 , where e is
binary-valued labels the base of the natural logarithm, is
(𝑦 (𝑖) ∈ {0,1}). the function that can be used to
model the binary response.
➢ Logistic regression can be
❑ If we solve 𝑃 𝑦 = 1 𝑥 =
used for predicting binary- 𝟏
valued labels – acts as a −(𝛽 + 𝛽 ∙𝑥)
for 𝛽0 + 𝛽1 ∙ 𝑥 + ϵ on
𝟏+𝒆 0 1
❑ An artificial neuron:
➢ takes a number of inputs
➢ calculates a simple weighted sum
➢ adds a bias
➢ decides whether it should be
“fired” or not
❑ A neuron maintains a
set (a vector) of weights.
❑ Each input to the neuron
is multiplied by its
corresponding weight that determines how important a specific
input signal is to this neuron.
8 7/18/2021 AI for Software Developers
https://en.wikipedia.org/wiki/Artificial_neuron
Artificial Neuron Model
❑ Each neuron also has a bias that’s added to the sum of the
weighted inputs before making the decision to “fire” or not.
➢ The bias can be seen as a modifier to the threshold of the
neuron's activation.
❑ We use an activation function to decide whether the neuron
should activate (“fire”) or not.
❑ Rosenblatt proposed a simple rule to compute the output.
❑ The neuron's output, 0 or 1, is determined by whether the weighted
sum is less than or greater than some threshold value.
0 𝑖𝑓 𝑤𝑖 𝑥𝑖 ≤ threshold
𝑜𝑢𝑡𝑝𝑢𝑡 =
1 𝑖𝑓 𝑤𝑖 𝑥𝑖 > threshold
0 𝑖𝑓 w ⋅ x + b ≤ 0
𝑜𝑢𝑡𝑝𝑢𝑡 = ቊ
1 𝑖𝑓 w ⋅ x + b > 0
import numpy as np
class PerceptronModel:
def __init__(self, no_of_featues):
self.weights = np.zeros(no_of_featues)
self.bias = 0.
def predict(self,features):
#calculate the weighted sum
weighted_sum = np.dot(features, self.weights) + self.bias
# use step function as activation function
if weighted_sum > 0:
return 1
else:
return 0
self.bias += gradient;
import matplotlib.pyplot as pl
%matplotlib inline
x = np.linspace(0,1);
# function to compute the line y=a*x+b
f = lambda x: a * x + b;
fig =pl.figure()
figa = pl.gca();
❑ For each instance in the training set, we classify that instance (that
is, prediction mode) and compare the predicted output to the
desired output.
❑ A good metric to use for comparing desired versus actual results
is mean squared error (MSE):
➢ It is the sum, over all the data points, of the square of the
difference between the predicted output, 𝒇(𝒙(𝒊) , and desired
output,𝒚(𝒊) , divided by the number of data points, n:
𝒏
𝟏 (𝒊) (𝒊) 𝟐
𝑬= 𝒚 − 𝒇(𝒙 )
𝒏
𝒊=𝟏
➢ This is the cost function we already know from the linear
regression model!
❑ Adaline Test -
https://sebastianraschka.com/Articles/2015_singlelayer_neurons.html
❑ See the entire code on eCentennial
import numpy as np
class AdalineGD(object):
for i in range(self.epochs):
output = self.net_input(X)
errors = (y - output)
self.w_[1:] += self.eta * X.T.dot(errors)
self.w_[0] += self.eta * errors.sum()
cost = (errors**2).sum() / 2.0
self.cost_.append(cost)
return self
❑ Textbook
❑ Vahid Mirjalili; Sebastian Raschka, Python Machine Learning -
Third Edition, by, Published by Packt Publishing, 2019
❑ Andriy Burkov The Hundred-Page Machine Learning Book.
❑ Single-Layer Neural Networks and Gradient Descent - Sebastian
Raschka
❑ https://www.learnpython.org/