Ann 1
Ann 1
Ann 1
Assignment 1
In [1]:
In [8]:
def sigmoid(x):
return 1 / (1 + np.exp(-0.5*x))
y = np.linspace(-10, 10, 100)
x = sigmoid(y)
plt.plot(y, x)
plt.xlabel("Input")
plt.ylabel("Output")
plt.title("Sigmoid Activation Function")
Out[8]:
In [9]:
Out[9]:
Assignment 2
In [10]:
import numpy as np
a=np.array([[1,1],[1,0],[0,1],[0,0]])
w=[1,-1]
T=1
result=[]
In [11]:
def AND_NOT():
print("Aggregation of inputs and weights is: ")
for i in range(0,4):
x=a[i]@w
print(x)
if x<T:
result.append(0)
else:
result.append(1)
print("Output Array: ")
return result
In [12]:
AND_NOT()
Out[12]:
[0, 1, 0, 0]
Assignment 3
In [13]:
import numpy as np
num=int(input("Enter the number:-"))
In [14]:
x=[]
def dec(num):
if num >= 1:
dec(num // 2)
x.append(num%2)
return x
In [17]:
dec(num)
Out[17]:
[1, 0, 1, 1, 0, 1]
In [18]:
w1=w2=1
b=1
def fun():
a=(x[-1]*w1) and (b*w2)
if a==1:
print("The number is odd")
return 1
else:
print("The number is even")
return 0
In [19]:
fun()
Out[19]:
Assignment 4
In [33]:
import numpy as np
import matplotlib.pyplot as plt
In [34]:
X1 = np.array([0, 0, 1, 1])
X2 = np.array([0, 1, 0, 1])
X = [X1, X2]
W = np.array([[4,5]])
In [35]:
In [36]:
X_ = aggregation(X,W)
print(X_)
[0 5 4 9]
In [37]:
def sigmoid(X):
result=[]
for i in range(len(X)):
result.append(1/(1+np.exp(-X[i])))
return np.array(result)
In [38]:
Y = sigmoid(X_)
print(Y)
In [39]:
In [40]:
In [41]:
value,result
Out[41]:
In [44]:
In [45]:
plt.plot(cos_alpha)
plt.title('Cosine Similarity')
plt.xlabel('Data Points')
plt.ylabel('Cosine Similarity')
plt.show()
Assignment 5
In [46]:
import numpy as np
In [47]:
Y1 = np.array([[1 ,0 ,1]])
Y2 = np.array([[1, 1, 1]])
Y3 = np.array([[0, 1, 1]])
Y4 = np.array([[1, 1, 0]])
Y= [Y1, Y2, Y3, Y4]
In [48]:
In [49]:
def weights(X,Y):
weis=[]
for i in range(4):
weis.append(calcWeight(X[i], Y[i]))
return weis
In [50]:
weis=weights(X,Y)
In [51]:
def forward_aggregation(X,W):
result=[]
for i in range(len(X)):
result.append(X[i]@W[i])
return np.squeeze(result)
In [52]:
def activation(X):
X = np.array(X)
X[X > 0] = 1
X[X == 0] = 0
X[X < 0] = -1
return X
In [53]:
In [54]:
for_aggri
Out[54]:
array([[4, 0, 4],
[4, 4, 4],
[0, 4, 4],
[4, 4, 0]])
In [55]:
print(activation(for_aggri))
[[1 0 1]
[1 1 1]
[0 1 1]
[1 1 0]]
In [56]:
def backward_aggregation(X,W):
result=[]
for i in range(len(X)):
result.append(W[i]@X[i].T)
return np.squeeze(result)
In [57]:
In [58]:
back_aggri
Out[58]:
In [59]:
print(activation(back_aggri))
[[ 1 -1 1 -1]
[-1 -1 1 1]
[ 1 1 -1 -1]
[ 1 1 1 1]]
Assignment 7
In [60]:
import numpy as np
from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split
W = np.random.randn(input_size, output_size)
In [61]:
accuracy
Out[61]:
0.7
In [62]:
y_pred
Out[62]:
array([2, 0, 2, 2, 2, 0, 2, 2, 2, 2, 2, 0, 0, 0, 0, 2, 2, 2, 2, 2,
0, 2,
0, 2, 2, 2, 2, 2, 0, 0])
In [63]:
import numpy as np
import pandas as pd
from sklearn.model_selection import train_test_split
In [68]:
data
Out[68]:
In [72]:
data = pd.read_csv('Iris.csv')
groups=data.groupby('Species')
setosa=groups.get_group("Iris-setosa")
veriscolor=groups.get_group("Iris-versicolor")
virginica=groups.get_group("Iris-virginica")
def test(data):
feature=["SepalLengthCm", "SepalWidthCm","PetalLengthCm", "PetalWidthCm"]
target=["Species"]
X=data[feature].values
y=data[target].values
X_train,X_test,y_train,y_test = train_test_split(X,y, test_size=0.30)
return X_train,X_test,y_train,y_test
SX_train,SX_test,Sy_train,Sy_test = test(setosa)
VX_train,VX_test,Vy_train,Vy_test = test(virginica)
vX_train,vX_test,vy_train,vy_test = test(veriscolor)
In [73]:
val = SX_test[10]
print(val)
val=np.array(val)
In [74]:
# Make prediction
def predict(X):
y_hat = forward_propagation(X)
return np.where(y_hat[0] == np.amax(y_hat[0]))[0][0]
class_name = iris.target_names[predict(new_input)]
print('Class name:', class_name)
Assignment 11
In [75]:
import tensorflow as tf
from tensorflow import keras
import matplotlib.pyplot as plt
%matplotlib inline
import numpy as np
In [76]:
In [77]:
plt.matshow(X_train[5])
Out[77]:
<matplotlib.image.AxesImage at 0x7fc4eed70490>
In [78]:
y_train[5]
Out[78]:
In [79]:
In [80]:
X_train[0]
[0. , 0. , 0. , 0. , 0.
,
0. , 0. , 0. , 0. , 0.
,
0. , 0. , 0. , 0. , 0.
,
0. , 0. , 0. , 0. , 0.
,
0. , 0. , 0. , 0. , 0.
,
0. , 0. , 0. ],
[0. , 0. , 0. , 0. , 0.
,
0. , 0. , 0. , 0. , 0.
,
0. , 0. , 0. , 0. , 0.
,
0. , 0. , 0. , 0. , 0.
,
0. , 0. , 0. , 0. , 0.
In [81]:
In [87]:
model = keras.Sequential([
keras.layers.Dense(100, input_shape=(784,), activation='relu'),
keras.layers.Dense(10, activation='sigmoid')
])
model.compile(optimizer='adam',
loss='sparse_categorical_crossentropy',
metrics=['accuracy'])
Epoch 1/20
Out[87]:
In [89]:
<keras.callbacks.History
y_predicted at 0x7fc4be7259d0>
= model.predict(X_test_flattened)
y_predicted_labels = [np.argmax(i) for i in y_predicted]
cm = tf.math.confusion_matrix(labels=y_test,predictions=y_predicted_labels)
cm
Out[89]:
In [90]:
plt.matshow(X_test[9])
Out[90]:
<matplotlib.image.AxesImage at 0x7fc4bdf524c0>
In [91]:
np.argmax(y_predicted[9])
Out[91]:
Assignment 8
In [92]:
import numpy as np
In [93]:
X = [
[0,0,0,1],
[0,1,0,1],
[0,0,1,1],
[1,0,0,0]
]
In [94]:
In [95]:
bij
Out[95]:
[[0.2, 0.2, 0.2], [0.2, 0.2, 0.2], [0.2, 0.2, 0.2], [0.2, 0.2, 0.
2]]
In [96]:
def buttom_up(inputs,global_bij):
norm_s = sum(inputs)
norm_x = sum(inputs)
temp2 = np.array(global_bij)
x = np.array(inputs)
y_list=[]
for l in range(len(temp2.T)):
a = (x @ temp2.T[l])
y_list.append(a)
print(y_list)
j = y_list.index(max(y_list))
print('WINNER IS ',j)
tension,t1 = top_down(inputs,j,norm_s)
return tension,t1
In [97]:
def top_down(si,j,norm_s):
si = np.array(si)
xi = si * tji[j]
norm_x = sum(xi)
answer = norm_x/norm_s
tension2,t2 = vigilance_check(si,answer,norm_x,j)
return tension2,t2
In [98]:
def vigilance_check(inputs,answer,norm_x,j):
if answer > rho:
tension3,t3 = weight_adjust(inputs,norm_x,j)
return tension3,t3
else:
pass
return 0
In [99]:
def weight_adjust(inputs,norm_x,j):
new_bij = [ ( ( alpha * i ) / ( ( alpha - 1 ) + norm_x ) ) for i in inputs ]
temp.T[j] = new_bij
bij = temp
tji[j] = new_bij
print("this is bij = ", bij)
print("this is tji = ", np.array(tji))
tension4 = bij
t4 = tji
flg = stop(old_bij,old_tji,bij,tji)
return tension4,t4
In [100]:
def stop(old_bij,old_tij,bij,tji):
bij=np.array(bij)
tji= np.array(tji)
if(bij.all() == old_bij.all() and tji.all() == old_tji.all()):
flg = 1
else:
pass
In [101]:
epochs = 10000
cnt = 0
for i in range(epochs):
print("this is epoch = ", i )
for j in X:
global_bij,global_tji = buttom_up(j,global_bij)
if( flg == 1):
break
else:
pass
[1. 0. 0. 0.]
[1. 1. 1. 1.]]
[1.0, 0.0, 0.4]
WINNER IS 0
this is bij = [[0. 1. 0.2]
[0. 0. 0.2]
[1. 0. 0.2]
[1. 0. 0.2]]
this is tji = [[0. 0. 1. 1.]
[1. 0. 0. 0.]
[1. 1. 1. 1.]]
[0.0, 1.0, 0.2]
WINNER IS 1
this is bij = [[0. 1. 0.2]
[0. 0. 0.2]
[1. 0. 0.2]
[1. 0. 0.2]]
this is tji = [[0. 0. 1. 1.]
[1. 0. 0. 0.]
[1. 1. 1. 1.]]
In [102]:
def clustering(inputs,global_bij,y):
norm_s = sum(inputs)
norm_x = sum(inputs)
temp2 = np.array(global_bij)
x = np.array(inputs)
y_list=[]
for l in range(len(temp2.T)):
a = (x @ temp2.T[l])
y_list.append(a)
print(y_list)
j = y_list.index(max(y_list))
print('WINNER IS ',j)
y[j].append(inputs)
return y
In [103]:
y = [[],[],[]]
for i in X:
a = clustering(i,global_bij,y)
print(a)
In [ ]: