Question
1. Use Keras Model with three layers of neural network to classify the dataset in HW#2 (for both questions). The first layer (input layer) has
1. Use Keras Model with three layers of neural network to classify the dataset in HW#2 (for both questions). The first layer (input layer) has four neurons, the second layer has three neurons, and the third layer (output layer) has only one neuron. You should use at least two different activation function and two different optimization functions.
Explain your experience with this network in compare with the ones you did in HW#2 and also the effect of different activation and optimization functions.
Hint: Go over the following video
https://www.youtube.com/watch?v=bemDFpNooA8&ab_channel=TensorFlow
***********HW#2*************
HW#2 question 1 code :
import numpy as np
import random
# In[116]:
xin=[[0,0,1],
[0,1,1],
[1,0,1],
[1,1,1]]
t= [1, 1, 1, 0]
for i in range(0,4):
print(xin[i][0], xin[i][1])
print(t)
# In[117]:
w1 = [[random.uniform(-2.0,2) for i in range(13)] ]
print(w1[0][11])
print(w1)
# In[118]:
w2 = [[random.uniform(-2.0,2) for i in range(4)] ]
print(w2)
print(w2[0][2])
lr= 0.1
# In[119]:
def sigmoid(x):
return (1/(1+np.exp(-x)))
# In[120]:
while True:
total_error = 0
for i in range(0,4):
v1= (xin[i][0]*w1[0][0] + xin[i][1]*w1[0][1] + w1[0][2])
v2= (xin[i][0]*w1[0][3] + xin[i][1]*w1[0][4] + w1[0][5])
v3= (xin[i][0]*w1[0][6] + xin[i][1]*w1[0][7] + w1[0][8])
v4 = (xin[i][0]*w1[0][9] + xin[i][1]*w1[0][10] + w1[0][11])
y1=sigmoid(v1)
y2=sigmoid(v2)
y3=sigmoid(v3)
y4=sigmoid(v4)
#print(y1,y2,y3)
u = y1 * w1[0][12] + y2 * w2[0][0] + y3 * w2[0][1] + y4 * w2[0][2] + w2[0][3]
y = sigmoid(u)
print(y)
error = np.square(t[i]- y)
d_error = (t[i]-y)
dw112 = lr * d_error * y * (1-y) * y1
dw20 = lr * d_error * y * (1-y) * y2
dw21 = lr * d_error * y * (1-y) * y3
dw22 = lr * d_error * y * (1-y) * y4
dw23 = lr * d_error * y * (1-y)
w1[0][12] = w1[0][12] + dw112
w2[0][0] = w2[0][0] + dw20
w2[0][1] = w2[0][1] + dw21
w2[0][2] = w2[0][2] + dw22
w2[0][3] = w2[0][3] + dw23
sum = w1[0][12]*y*(1-y)*d_error + w2[0][0]*y*(1-y)*d_error + w2[0][1]*y*(1-y)*d_error + w2[0][2]*y*(1-y)*d_error
dw1 = y1 * (1-y1) * xin[i][0] * sum
dw2 = y1 * (1-y1) * xin[i][1] * sum
dw3 = y1 * (1-y1) * sum
dw4 = y2 * (1-y2) * xin[i][0] * sum
dw5 = y2 * (1-y2) * xin[i][1] * sum
dw6 = y2 * (1-y2) * sum
dw7 = y3 * (1-y3) * xin[i][0] * sum
dw8 = y3 * (1-y3) * xin[i][1] * sum
dw9 = y3 * (1-y3) * sum
dw10 = y4 * (1-y4) * xin[i][0] * sum
dw11 = y4 * (1-y4) * xin[i][1] * sum
dw12 = y4 * (1-y4) * sum
w1[0][0] = w1[0][1] +dw1
w1[0][1] = w1[0][1] +dw2
w1[0][2] = w1[0][2] +dw3
w1[0][3] = w1[0][3] +dw4
w1[0][4] = w1[0][4] +dw5
w1[0][5] = w1[0][5] +dw6
w1[0][6] = w1[0][6] +dw7
w1[0][7] = w1[0][7] +dw8
w1[0][8] = w1[0][8] +dw9
w1[0][9] = w1[0][9] + dw10
w1[0][10] = w1[0][10] + dw11
w1[0][11] = w1[0][11] + dw12
total_error = total_error + error
#print(total_error)
if (total_error<0.01):
print("The Last 4 will be the Output")
break
print(" First layer weights: ")
for i in range(0,13):
print(w1[0][i])
print(" Second layer weights: ")
for i in range(0,4):
print(w2[0][i])
print(" The target outputs are: ")
print(t)
HW#2 question 2 code :
import numpy as np
import random
# In[59]:
x1 = np.random.uniform(0,3,size=(50,2))
x2 = np.random.uniform(6,9,size=(50,2))
t = [0 for i in range(50)]
t += [1 for i in range(50)]
x = np.append(x1,x2,axis=0)
print(x)
# In[60]:
w1 = [[random.uniform(-2.0,2) for i in range(13)]]
w2 = [[random.uniform(-2.0,2) for i in range(4)]]
lr = 0.1
print(w1)
# In[61]:
def sigmoid(x):
y = 1/(1 + np.exp(-x))
return y
# In[62]:
while True:
total_error = 0
output = []
for i in range(0,100):
v1= (x[i][0]*w1[0][0] + x[i][1]*w1[0][1] + w1[0][2])
v2= (x[i][0]*w1[0][3] + x[i][1]*w1[0][4] + w1[0][5])
v3= (x[i][0]*w1[0][6] + x[i][1]*w1[0][7] + w1[0][8])
v4 = (x[i][0]*w1[0][9] + x[i][1]*w1[0][10] + w1[0][11])
y1=sigmoid(v1)
y2=sigmoid(v2)
y3=sigmoid(v3)
y4=sigmoid(v4)
u = y1 * w1[0][12] + y2 * w2[0][0] + y3 * w2[0][1] + y4 * w2[0][2] + w2[0][3]
y = sigmoid(u)
print(y)
output.insert(i,y)
error = np.square(t[i]- y)
d_error = (t[i]-y)
dw112 = lr * d_error * y * (1-y) * y1
dw20 = lr * d_error * y * (1-y) * y2
dw21 = lr * d_error * y * (1-y) * y3
dw22 = lr * d_error * y * (1-y) * y4
dw23 = lr * d_error * y * (1-y)
w1[0][12] = w1[0][12] + dw112
w2[0][0] = w2[0][0] + dw20
w2[0][1] = w2[0][1] + dw21
w2[0][2] = w2[0][2] + dw22
w2[0][3] = w2[0][3] + dw23
sum = w1[0][12]*y*(1-y)*d_error + w2[0][0]*y*(1-y)*d_error + w2[0][1]*y*(1-y)*d_error + w2[0][2]*y*(1-y)*d_error
dw1 = y1 * (1-y1) * x[i][0] * sum
dw2 = y1 * (1-y1) * x[i][1] * sum
dw3 = y1 * (1-y1) * sum
dw4 = y2 * (1-y2) * x[i][0] * sum
dw5 = y2 * (1-y2) * x[i][1] * sum
dw6 = y2 * (1-y2) * sum
dw7 = y3 * (1-y3) * x[i][0] * sum
dw8 = y3 * (1-y3) * x[i][1] * sum
dw9 = y3 * (1-y3) * sum
dw10 = y4 * (1-y4) * x[i][0] * sum
dw11 = y4 * (1-y4) * x[i][1] * sum
dw12 = y4 * (1-y4) * sum
w1[0][0] = w1[0][1] +dw1
w1[0][1] = w1[0][1] +dw2
w1[0][2] = w1[0][2] +dw3
w1[0][3] = w1[0][3] +dw4
w1[0][4] = w1[0][4] +dw5
w1[0][5] = w1[0][5] +dw6
w1[0][6] = w1[0][6] +dw7
w1[0][7] = w1[0][7] +dw8
w1[0][8] = w1[0][8] +dw9
w1[0][9] = w1[0][9] + dw10
w1[0][10] = w1[0][10] + dw11
w1[0][11] = w1[0][11] + dw12
total_error = total_error + error
#print(total_error)
if (total_error < 0.01):
print(" The Output ")
break
print (" Target output: ", t)
print (" Predicted Output y: ", output)
print(" First layer weights: ")
for i in range(0,13):
print(w1[0][i])
print(" Second layer weights: ")
for i in range(0,4):
print(w2[0][i])
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started