# Predominant Code Accomplished by 8/6/2024
import numpy as np
def initialize_params(options, hidden_size, output_size): # initializing weights
w1 = np.random.randn(hidden_size, options)
w2 = np.random.randn(output_size, hidden_size)
print(“w1: “, w1)
print(“nw2: “, w2)
return [w1, w2]
def sigmoid(Z): # first activation perform
return (1/(1 + np.exp(-Z)))
def relu(x): # second activation perform
x_array = np.array(x)
return np.most(0, x_array)
def forward_prop(params, x): # ahead propagation
w1, w2 = params
Z1 = np.dot(w1, x)
A1 = sigmoid(Z1)
A1 = relu(A1)
Z2 = np.dot(w2, A1)
A2 = sigmoid(Z2)
A2 = relu(A2)
return [Z1, A1, Z2, A2]
def binary_cross_entropy_loss(output, anticipated): # calculating loss perform
m = anticipated.form[1]
loss = -1/m * np.sum(anticipated * np.log(output) + (1-expected) * np.log(1 – output))
return loss
def backward_prop(m, params, forwardparams, y): # backward propagation perform
w1, w2 = params
_, A1, _, A2 = forwardparams
dZ2 = A2-y
dW2 = np.dot(dZ2,A1.T) / m
dZ1 = np.dot(w2.T,dZ2) * A1 * (1-A1)
dW1 = np.dot(dZ1,x.T) / m
dW1 = np.reshape(dW1,w1.form)
dW2 = np.reshape(dW2,w2.form)
return [dZ2, dW2, dZ1, dW1]
def predict(finalweights, check): # ahead propagation on inputs to obtain the outputs (“predicts” utilizing the ultimate weights)
_, _, _, A2 = forward_prop(finalweights, check)
output = np.squeeze(A2) # removes “single-element dimensions”
prediction = 1 if output >= 0.5 else 0
test_flat = check.flatten()
indices = [i for i in range(x.shape[1]) if np.all(x[:, i] == test_flat)]
if indices:
anticipated = y[0, indices[0]]
print(f”For enter {[i[0] for i in check]}, output is {prediction}, anticipated: {anticipated}, {prediction==anticipated}”)
return (prediction == anticipated)
def most important(training_type, training_set, hidden_size, learningrate=.37, iterations=10000):
# outline enter and output (x and y)
world x, y
losses = np.array([])
if training_type == ‘2’:
if training_set == ‘AND’:
# AND dataset
x = np.array([[0,1,0,1],
[0,0,1,1]])
y = np.array([[0,0,0,1]])
if training_set == ‘OR’:
# OR dataset
x = np.array([[0,1,0,1],
[0,0,1,1]])
y = np.array([[0,1,1,1]])
if training_set == ‘NAND’:
# NAND dataset
x = np.array([[0,1,0,1],
[0,0,1,1]])
y = np.array([[1,1,1,0]])
if training_set == ‘NOR’:
# NOR dataset
x = np.array([[0,1,0,1],
[0,0,1,1]])
y = np.array([[1,0,0,0]])
if training_set == ‘XOR’:
# XOR dataset
x = np.array([[0,1,0,1],
[0,0,1,1]])
y = np.array([[0,1,1,0]])
if training_set == ‘XNOR’:
# XNOR dataset
x = np.array([[0,1,0,1],
[0,0,1,1]])
y = np.array([[1,0,0,1]])
if training_type == ‘3’:
if training_set == ‘AND’:
x = np.array([[0,0,0,0,1,1,1,1],
[0,0,1,1,0,0,1,1],
[0,1,0,1,0,1,0,1]])
y = np.array([[0,0,0,0,0,0,0,1]])
if training_set == ‘OR’:
x = np.array([[0,0,0,0,1,1,1,1],
[0,0,1,1,0,0,1,1],
[0,1,0,1,0,1,0,1]])
y = np.array([[0,1,1,1,1,1,1,1]])
if training_set == ‘NAND’:
x = np.array([[0,0,0,0,1,1,1,1],
[0,0,1,1,0,0,1,1],
[0,1,0,1,0,1,0,1]])
y = np.array([[1,1,1,1,1,1,1,0]])
if training_set == ‘NOR’:
x = np.array([[0,0,0,0,1,1,1,1],
[0,0,1,1,0,0,1,1],
[0,1,0,1,0,1,0,1]])
y = np.array([[1,0,0,0,0,0,0,0]])
if training_set == ‘XOR’: # unique or, so simply if ONE, not two or three are 1
x = np.array([[0,0,0,0,1,1,1,1],
[0,0,1,1,0,0,1,1],
[0,1,0,1,0,1,0,1]])
y = np.array([[0,1,1,0,1,0,0,0]])
if training_set == ‘XNOR’:
x = np.array([[0,0,0,0,1,1,1,1],
[0,0,1,1,0,0,1,1],
[0,1,0,1,0,1,0,1]])
y = np.array([[1,0,0,1,0,1,1,0]])
if training_type == ‘4’:
if training_set == ‘AND’:
x = np.array([[0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,1],
[0,0,0,0,1,1,1,1,0,0,0,0,1,1,1,1],
[0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1],
[0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1]])
y = np.array([[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1]])
if training_set == ‘OR’:
x = np.array([[0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,1],
[0,0,0,0,1,1,1,1,0,0,0,0,1,1,1,1],
[0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1],
[0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1]])
y = np.array([[0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1]])
if training_set == ‘NAND’:
x = np.array([[0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,1],
[0,0,0,0,1,1,1,1,0,0,0,0,1,1,1,1],
[0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1],
[0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1]])
y = np.array([[1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0]])
if training_set == ‘NOR’:
x = np.array([[0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,1],
[0,0,0,0,1,1,1,1,0,0,0,0,1,1,1,1],
[0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1],
[0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1]])
y = np.array([[1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]])
if training_set == ‘XOR’:
x = np.array([[0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,1],
[0,0,0,0,1,1,1,1,0,0,0,0,1,1,1,1],
[0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1],
[0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1]])
y = np.array([[0,1,1,0,1,0,0,0,1,0,0,0,0,0,0,0]])
if training_set == ‘XNOR’:
x = np.array([[0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,1],
[0,0,0,0,1,1,1,1,0,0,0,0,1,1,1,1],
[0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1],
[0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1]])
y = np.array([[1,0,0,1,0,1,1,1,0,1,1,1,1,1,1,1]])
if training_type == ‘5’:
if training_set == ‘AND’:
x = np.array([[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1],
[0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,1,0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,1],
[0,0,0,0,1,1,1,1,0,0,0,0,1,1,1,1,0,0,0,0,1,1,1,1,0,0,0,0,1,1,1,1],
[0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1],
[0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1]])
y = np.array([[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,1]])
if training_set == ‘OR’:
x = np.array([[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1],
[0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,1,0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,1],
[0,0,0,0,1,1,1,1,0,0,0,0,1,1,1,1,0,0,0,0,1,1,1,1,0,0,0,0,1,1,1,1],
[0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1],
[0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1]])
y = np.array([[0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1]])
if training_set == ‘NAND’:
x = np.array([[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,1,1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,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1],
[0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1]])
y = np.array([[0,1,1,0,1,0,0,0,1,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]])
if training_set == ‘XNOR’:
x = np.array([[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1],
[0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,1,0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,1],
[0,0,0,0,1,1,1,1,0,0,0,0,1,1,1,1,0,0,0,0,1,1,1,1,0,0,0,0,1,1,1,1],
[0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1],
[0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1]])
y = np.array([[1,0,0,1,0,1,1,1,0,1,1,1,1,1,1,1,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1]])
m = x.form[1] # variety of columns (4)
options = x.form[0] #variety of rows (variety of inputs/options)
params = initialize_params(options, hidden_size, 1)
w1, w2 = params # extracting weights from the array of weights
starting_weights = np.append(w1, w2.reshape(-1,1), axis=1)
for i in vary(iterations): # coaching for the set variety of iterations
forwardparams = forward_prop(params,x)
Z1, A1, Z2, A2 = forwardparams
loss = binary_cross_entropy_loss(A2, y)
losses = np.append(losses, loss)
dZ2, dW2, dZ1, dW1 = backward_prop(m, params, forwardparams, y)
w2 -= learningrate * dW2
w1 -= learningrate * dW1
params = (w1, w2)
if ipercent500==0: # may be modified (the five hundred) if you wish to see the up to date loss kind of usually (say, each 1000 iterations vs each 100)
print(f”Iteration: {i}, Loss: {loss}”)
print(“nFinal weights after coaching:”)
print(params[0])
print(params[1])
ending_weights = np.append(params[0], params[1].reshape(-1,1), axis=1)
# Testing the predictions
if training_type == ‘2’:
print(“nTwo-Enter Logic Gates: “)
print(f”Testing predictions for {training_set}:”)
val = all([
predict(params, np.array([[0], [0]])),
predict(params, np.array([[1], [0]])),
predict(params, np.array([[0], [1]])),
predict(params, np.array([[1], [1]]))])
if training_type == ‘3’:
print(“nThree-Enter Logic Gates: “)
print(f”Testing predictions for {training_set}:”)
val = all([
predict(params, np.array([[0], [0], [0]])),
predict(params, np.array([[0], [0], [1]])),
predict(params, np.array([[0], [1], [0]])),
predict(params, np.array([[0], [1], [1]])),
predict(params, np.array([[1], [0], [0]])),
predict(params, np.array([[1], [0], [1]])),
predict(params, np.array([[1], [1], [0]])),
predict(params, np.array([[1], [1], [1]]))])
if training_type == ‘4’:
print(‘nFour-Enter Logic Gates: ‘)
print(f’Testing predictions for {training_set}: ‘)
val = all([
predict(params, np.array([[0],[0],[0],[0]])),
predict(params, np.array([[0],[0],[0],[1]])),
predict(params, np.array([[0],[0],[1],[0]])),
predict(params, np.array([[0],[0],[1],[1]])),
predict(params, np.array([[0],[1],[0],[0]])),
predict(params, np.array([[0],[1],[0],[1]])),
predict(params, np.array([[0],[1],[1],[0]])),
predict(params, np.array([[0],[1],[1],[1]])),
predict(params, np.array([[1],[0],[0],[0]])),
predict(params, np.array([[1],[0],[0],[1]])),
predict(params, np.array([[1],[0],[1],[0]])),
predict(params, np.array([[1],[0],[1],[1]])),
predict(params, np.array([[1],[1],[0],[0]])),
predict(params, np.array([[1],[1],[0],[1]])),
predict(params, np.array([[1],[1],[1],[0]])),
predict(params, np.array([[1],[1],[1],[1]]))])
if training_type == ‘5’:
print(‘nFive-Enter Logic Gates: ‘)
print(f’Testing predictions for {training_set}: ‘)
val = all([
predict(params, np.array([[0],[0],[0],[0],[0]])),
predict(params, np.array([[0],[0],[0],[0],[1]])),
predict(params, np.array([[0],[0],[0],[1],[0]])),
predict(params, np.array([[0],[0],[0],[1],[1]])),
predict(params, np.array([[0],[0],[1],[0],[0]])),
predict(params, np.array([[0],[0],[1],[0],[1]])),
predict(params, np.array([[0],[0],[1],[1],[0]])),
predict(params, np.array([[0],[0],[1],[1],[1]])),
predict(params, np.array([[0],[1],[0],[0],[0]])),
predict(params, np.array([[0],[1],[0],[0],[1]])),
predict(params, np.array([[0],[1],[0],[1],[0]])),
predict(params, np.array([[0],[1],[0],[1],[1]])),
predict(params, np.array([[0],[1],[1],[0],[0]])),
predict(params, np.array([[0],[1],[1],[0],[1]])),
predict(params, np.array([[0],[1],[1],[1],[0]])),
predict(params, np.array([[0],[1],[1],[1],[1]])),
predict(params, np.array([[1],[0],[0],[0],[0]])),
predict(params, np.array([[1],[0],[0],[0],[1]])),
predict(params, np.array([[1],[0],[0],[1],[0]])),
predict(params, np.array([[1],[0],[0],[1],[1]])),
predict(params, np.array([[1],[0],[1],[0],[0]])),
predict(params, np.array([[1],[0],[1],[0],[1]])),
predict(params, np.array([[1],[0],[1],[1],[0]])),
predict(params, np.array([[1],[0],[1],[1],[1]])),
predict(params, np.array([[1],[1],[0],[0],[0]])),
predict(params, np.array([[1],[1],[0],[0],[1]])),
predict(params, np.array([[1],[1],[0],[1],[0]])),
predict(params, np.array([[1],[1],[0],[1],[1]])),
predict(params, np.array([[1],[1],[1],[0],[0]])),
predict(params, np.array([[1],[1],[1],[0],[1]])),
predict(params, np.array([[1],[1],[1],[1],[0]])),
predict(params, np.array([[1],[1],[1],[1],[1]]))])
print(f’nAll Predictions: {val}’)
return val, np.array([[training_set], [training_type], [starting_weights], [ending_weights], [losses]], dtype=object)