Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Please, write an addition to the following python code that will generate a 2d weight space contour plot: import matplotlib.pyplot as plt import numpy as

Please, write an addition to the following python code that will generate a 2d weight space contour plot:

import matplotlib.pyplot as plt

import numpy as np

from sklearn.model_selection import train_test_split

def sigmoid(sop):

return 1.0/(1 + np.exp(-sop))

def error(predicted, target):

return np.mean(np.power(predicted - target, 2))

def error_predicted_deriv(predicted, target):

return 2 * (predicted - target)

def activation_sop_deriv(sop):

return sigmoid(sop) * (1.0 - sigmoid(sop))

def update_weights(weights, grads, learning_rate):

return weights - learning_rate * grads

# Generate data

mean = np.array([-0.4, -0.8])

mean1 = np.array([0.4, 0.8])

cov = np.array([[0.84, 0], [0, 0.036]])

x1 = np.random.multivariate_normal(mean, cov, 50)

x2 = np.random.multivariate_normal(mean1, cov, 50)

data = np.concatenate((x1, x2))

# Generate desired output

d = np.concatenate((np.zeros(50), np.ones(50)))

# Split data into train and test sets

X_train, X_test, d_train, d_test = train_test_split(

data, d, test_size=0.2, random_state=0

)

# Assign labels to the data points

labels = np.zeros(100)

labels[:50] = -1 # class 1

labels[50:] = 1 # class 2

plt.scatter(data[labels==-1, 0], data[labels==-1, 1], c='r', label='Class 1')

plt.scatter(data[labels==1, 0], data[labels==1, 1], c='b', label='Class 2')

plt.legend()

plt.show()

# Initialize weights

weights = np.random.rand(2)

# Train the network

max_iterations = 10

learning_rate = 1.5

MSE = []

train_pred = []

test_pred = []

for iteration in range(max_iterations):

# Forward Pass

sop = np.dot(X_train, weights)

predicted = sigmoid(sop)

err = error(predicted, d_train)

# Backward Pass

grad_predicted = error_predicted_deriv(predicted, d_train)

grad_sop = activation_sop_deriv(sop)

grad_weights = np.dot(X_train.T, grad_predicted * grad_sop)

# Update weights

weights = update_weights(weights, grad_weights, learning_rate)

print("Updated weights:", weights)

err = error(predicted, d_train)

MSE.append(np.mean(err))

if iteration % 100 == 0:

print(f"Iteration {iteration}: Error = {err}")

log_MSE = np.log10(MSE)

print("Final weights:", weights)

plt.plot(range(10), log_MSE, label='Log MSE (dB)')

plt.xlabel('Iteration')

plt.ylabel('Log MSE (dB)')

plt.legend()

plt.show()

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Processing

Authors: David M. Kroenke, David Auer

11th Edition

B003Y7CIBU, 978-0132302678

More Books

Students also viewed these Databases questions

Question

Explain the basic differences between a share of stock and a bond.

Answered: 1 week ago