Ann - Lab - Ipynb - Colaboratory
Ann - Lab - Ipynb - Colaboratory
ipynb - Colaboratory
v2 = -2.0
bias: 2.0
Logistic regression
import numpy as np
from sklearn.linear_model import LogisticRegression
from sklearn.model_selection import train_test_split
from sklearn.metrics import accuracy_score
X = np.array([[1.2, 2.3],
[2.8, 3.9],
[0.4, 1.8],
[3.0, 2.5],
[2.5, 1.0]])
y = np.array([0, 1, 0, 1, 0])
y_pred = logistic_regression_model.predict(X_test)
Accuracy: 100.00%
Back-propagation
import numpy as np
def derivatives_sigmoid(x):
return x * (1 - x)
epoch=5
lr=0.1
inputlayer_neurons = 2
hiddenlayer_neurons = 3
output_neurons = 1
wh=np.random.uniform(size=(inputlayer_neurons,hiddenlayer_neurons))
bh=np.random.uniform(size=(1,hiddenlayer_neurons))
wout=np.random.uniform(size=(hiddenlayer_neurons,output_neurons))
bout=np.random.uniform(size=(1,output_neurons))
for i in range(epoch):
hinp1=np.dot(X,wh)
hinp=hinp1 + bh
hlayer_act = sigmoid(hinp)
outinp1=np.dot(hlayer_act,wout)
outinp= outinp1+bout
output = sigmoid(outinp)
EO = y-output
outgrad = derivatives_sigmoid(output)
d_output = EO * outgrad
EH = d_output.dot(wout.T)
hiddengrad = derivatives_sigmoid(hlayer_act)
d_hiddenlayer = EH * hiddengrad
https://colab.research.google.com/drive/1Gy7kAVTwCwKl5DFIctIgmlYxPWhphozs#scrollTo=gTfkTYsqYYk3&printMode=true 1/13
11/1/23, 9:37 PM ann_lab.ipynb - Colaboratory
-----------Epoch- 1 Starts----------
Input:
[[0.66666667 1. ]
[0.33333333 0.55555556]
[1. 0.66666667]]
Actual Output:
[[0.92]
[0.86]
[0.89]]
Predicted Output:
[[0.76779577]
[0.75723532]
[0.76896431]]
-----------Epoch- 1 Ends----------
-----------Epoch- 2 Starts----------
Input:
[[0.66666667 1. ]
[0.33333333 0.55555556]
[1. 0.66666667]]
Actual Output:
[[0.92]
[0.86]
[0.89]]
Predicted Output:
[[0.76986431]
[0.75919163]
[0.77101865]]
-----------Epoch- 2 Ends----------
-----------Epoch- 3 Starts----------
Input:
[[0.66666667 1. ]
[0.33333333 0.55555556]
[1. 0.66666667]]
Actual Output:
[[0.92]
[0.86]
[0.89]]
Predicted Output:
[[0.77187568]
[0.76109505]
[0.77301617]]
-----------Epoch- 3 Ends----------
-----------Epoch- 4 Starts----------
Input:
[[0.66666667 1. ]
[0.33333333 0.55555556]
[1. 0.66666667]]
Actual Output:
[[0.92]
[0.86]
[0.89]]
Predicted Output:
[[0.77383217]
[0.76294764]
[0.77495914]]
Activation functions
import numpy as np
def linear_activation(x):
return x
def sigmoid_activation(x):
return 1 / (1 + np.exp(-x))
def tanh_activation(x):
return np.tanh(x)
https://colab.research.google.com/drive/1Gy7kAVTwCwKl5DFIctIgmlYxPWhphozs#scrollTo=gTfkTYsqYYk3&printMode=true 2/13
11/1/23, 9:37 PM ann_lab.ipynb - Colaboratory
def relu_activation(x):
return max(0, x)
input_value = 2.0
threshold = 0.5
import numpy as np
from sklearn import datasets
from sklearn import svm
import matplotlib.pyplot as plt
clf = svm.SVC(kernel='linear')
clf.fit(X, y)
logistic regression
import numpy as np
import matplotlib.pyplot as plt
https://colab.research.google.com/drive/1Gy7kAVTwCwKl5DFIctIgmlYxPWhphozs#scrollTo=gTfkTYsqYYk3&printMode=true 3/13
11/1/23, 9:37 PM ann_lab.ipynb - Colaboratory
np.random.seed(0)
X = np.random.randn(100, 2)
y = (X[:, 0] + X[:, 1] > 0).astype(int)
X_train, X_test = X[:80], X[80:]
y_train, y_test = y[:80], y[80:]
def sigmoid(z):
return 1 / (1 + np.exp(-z))
theta = np.zeros(3)
X_train = np.hstack((np.ones((X_train.shape[0], 1)), X_train))
X_test = np.hstack((np.ones((X_test.shape[0], 1)), X_test))
learning_rate = 0.1
epochs = 1000
for epoch in range(epochs):
z = np.dot(X_train, theta)
h = sigmoid(z)
gradient = np.dot(X_train.T, (h - y_train)) / y_train.size
theta -= learning_rate * gradient
predicted_probabilities = sigmoid(np.dot(X_test, theta))
predicted_labels = (predicted_probabilities > 0.5).astype(int)
accuracy = np.mean(predicted_labels == y_test)
print(f"Accuracy: {accuracy}")
x_values = np.array([np.min(X[:, 0]) - 1, np.max(X[:, 0]) + 1])
y_values = - (theta[0] + theta[1] * x_values) / theta[2]
plt.scatter(X[:, 0], X[:, 1], c=y)
plt.plot(x_values, y_values, label='Decision Boundary')
plt.xlabel('Feature 1')
plt.ylabel('Feature 2')
plt.legend()
plt.show()
Accuracy: 1.0
Auto-Associative memory
import numpy as np
source = [-1, 1, 1, 1]
target = source
sourcet = np.transpose(source)
weight = [[0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]]
for i in range(len(source)):
for j in range(len(source)):
weight[i][j] = source[i] * target[j]
print("Weight Matrix:")
for row in weight:
print(row)
for i in range(len(source)):
for j in range(len(source)):
output[i] += source[j] * weight[j][i]
https://colab.research.google.com/drive/1Gy7kAVTwCwKl5DFIctIgmlYxPWhphozs#scrollTo=gTfkTYsqYYk3&printMode=true 4/13
11/1/23, 9:37 PM ann_lab.ipynb - Colaboratory
print("Output Matrix:", output)
if is_auto_associative:
print("It is auto-associative")
else:
print("It is not auto-associative")
Weight Matrix:
[1, -1, -1, -1]
[-1, 1, 1, 1]
[-1, 1, 1, 1]
[-1, 1, 1, 1]
Output Matrix: [-4, 4, 4, 4]
It is auto-associative
Hetero-Associative memory
import numpy as np
s1=[[1,1,0,0]]
s2=[[1,1,1,0]]
s3=[[0,0,1,1]]
s4=[[0,1,0,0]]
t1=[[1,0]]
t2=[[0,1]]
t3=[[1,0]]
t4=[[1,0]]
y11=[0,0,0,0]
a=np.transpose(s1)
b=np.transpose(s2)
c=np.transpose(s3)
d=np.transpose(s4)
S1=np.dot(a,t1)
S2=np.dot(b,t2)
S3=np.dot(c,t3)
S4=np.dot(d,t4)
x=S1+S2+S3+S4
print(x)
print()
def fun(j,x):
y=np.dot(j,x)
y1=y.reshape(-1)
y11=[None for i in range(len(y1))]
for i in range(len(y1)):
y11[i]=1 if y1[i]>1 else 0
print(y11)
print()
fun(s1,x)
fun(s2,x)
fun(s3,x)
fun(s4,x)
[[1 1]
[2 1]
[1 1]
[1 0]]
[1, 1]
[1, 1]
[1, 0]
[1, 0]
import numpy as np
x1 = np.array([1, 1, 1, 1, 1, 1]).reshape(6, 1)
x2 = np.array([-1, -1, -1, -1, -1, -1]).reshape(6, 1)
x3 = np.array([1, 1, -1, -1, 1, 1]).reshape(6, 1)
x4 = np.array([-1, -1, 1, 1, -1, -1]).reshape(6, 1)
y1 = np.array([1, 1, 1]).reshape(3, 1)
y2 = np.array([-1, -1, -1]).reshape(3, 1)
y3 = np.array([1, -1, 1]).reshape(3, 1)
y4 = np.array([-1, 1, -1]).reshape(3, 1)
https://colab.research.google.com/drive/1Gy7kAVTwCwKl5DFIctIgmlYxPWhphozs#scrollTo=gTfkTYsqYYk3&printMode=true 5/13
11/1/23, 9:37 PM ann_lab.ipynb - Colaboratory
inputSet = np.concatenate((x1, x2, x3, x4), axis=1)
targetSet = np.concatenate((y1.T, y2.T, y3.T, y4.T), axis=0)
print("\nWeight matrix:")
weight = np.dot(inputSet, targetSet)
print(weight)
print("\n-------------------------------------------------")
print("\nTesting for input patterns: Set A")
def testInputs(x, weight):
y = np.dot(weight.T, x)
y[y < 0] = -1
y[y >= 0] = 1
return np.array(y)
[[ 1 -1 1 -1]
[ 1 -1 1 -1]
[ 1 -1 -1 1]
[ 1 -1 -1 1]
[ 1 -1 1 -1]
[ 1 -1 1 -1]]
Weight matrix:
[[4 0 4]
[4 0 4]
[0 4 0]
[0 4 0]
[4 0 4]
[4 0 4]]
https://colab.research.google.com/drive/1Gy7kAVTwCwKl5DFIctIgmlYxPWhphozs#scrollTo=gTfkTYsqYYk3&printMode=true 6/13
11/1/23, 9:37 PM ann_lab.ipynb - Colaboratory
Output of input pattern 4
[[-1]
[ 1]
[-1]]
https://colab.research.google.com/drive/1Gy7kAVTwCwKl5DFIctIgmlYxPWhphozs#scrollTo=gTfkTYsqYYk3&printMode=true 7/13