Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Machine learning question, use jupyter lab, no AI plz: Import this: import sys from packaging import version import sklearn import matplotlib.pyplot as plt import numpy

Machine learning question, use jupyter lab, no AI plz:
Import this:
import sys
from packaging import version
import sklearn
import matplotlib.pyplot as plt
import numpy as np
import tensorflow as tf
from sklearn.preprocessing import add_dummy_feature
from sklearn.datasets import make_blobs
from sklearn import metrics #Import scikit-learn metrics module for accuracy calculation
from sklearn.model_selection import train_test_split
from sklearn.metrics import confusion_matrix, ConfusionMatrixDisplay
print("Sklearn package",sys.version_info)
print("Sklearn package",sklearn.__version__)
print("TensorFlow version:", tf.__version__)
assert sys.version_info >=(3,7)
assert version.parse(sklearn.__version__)>= version.parse("1.0.1")
plt.rc('font', size=12)
plt.rc('axes', labelsize=14, titlesize=14)
plt.rc('legend', fontsize=14)
plt.rc('xtick', labelsize=10)
plt.rc('ytick', labelsize=10)
----
Questions:
4. Q2
4.1. Fine-tune (learning rate, and number of hidden units) using the training set.
4.1.1. Use a two layer model with one hidden layer.
4.1.2. Store your best trained model in best_model,
4.1.3. Store the best number of hidden units in best_hu
4.1.4. Store the best number of hidden units in best_lr
4.1.5. For learning rate, try some values from 0.0001 to 0.1
4.1.6. For hidden units, try some values from 8,16,32,64,128
4.1.7. Other parameters should be: epochs=10, batch_size=32 and 'adam' as the optimizer
4.1.8. Print the accuracy of the model on the testing dataset, x_test
4.2. Hints:
4.2.1. You have to use two for loops for the two parameters you are trying to fine-tune.
4.2.2. You can get a testing accuracy above 97.5%
#Q2
best_model = None
best_accuracy =0
best_lr =0
best_hu =0
###Write your code here#####
#uncomment these two line to test your best_model
#acc = best_model.evaluate(x_test, y_test, verbose=2)[1]
#print("learning rate", best_lr, "hidden units", best_hu, "Testing Accuracy", acc)
(SEE Q5 PIC)
Q3
5.0.1. Calculate the gradient of f with respect to x and y using the backpropagation
algorithm
5.0.2. Calculate the gradient of f with respect to x and y using the computation graph in
tensorflow
f(x,y)=x3+y4(x+y)2
Declare intermediate variables, q1=x3,q2=y4,q=q1+q2 and so on. Remember to use is needed when x is used in
multiple paths of the computation), similarly dfdy.
x =2
y =5
#Forward pass
#Backward pass
#print(f)
#print(dfdx,dfdy)
#f =12.918367346938776
#dfdx =-3.4460641399416914
#dfdy =6.513119533527696
#using computation graph from tensorflow
x = tf.Variable(2.)
y = tf.Variable(5.)
#print(f)
#print(grad_x,grad_y)
(See Q6 pic)
6.0.1. Calculate the gradient of f with respect to x and y using the backpropagation
algorithm
f(x,y)=i=1i=m(xi**yi2)=(x1**y12+x2**y22+cdots+xm**ym2)
q=y2
z=x**q
f=i=1j=mi(zi)
# Using backpropagation, construct some intermediate
# variables for $q=y^2$ and z = x*q, then finally f = np.sum(z)
x = np.array([1,2,3,4])
y = np.array([1.5,2.2,2.4,4.1])
m = x.shape[0]
print(x)
print(y)
#Forward Pass
#Backward Pass
#print("f",f)
#print("dfdx",dfdx)
#print("dfdy",dfdy)
#Sample output
#f 96.44999999999999
#dfdq [1.1.1.1.]
#dfdx [2.254.845.7616.81]
#dfdy [3.8.814.432.8]
(see q6.0.2 pic)
6.0.2. Calculate the gradient of f with respect to x and y using the computation graph in
tensorflow
dfdx=
dfdy=
#using tensorflow
x = tf.Variable([1.,2.,3.,4.], trainable = True, name='x')
y = tf.Variable([1.5,2.2,2.4,4.1], trainable = True, name='y')
#tf.print("f",f)
#grad_x, grad_y = tp.gradient(f,[x,y])
#tf.print("grad_x",grad_x)
#tf.print("grad_y",grad_y)
#Sample output
#f 96.45
#grad_x [2.254.845.7616.81]
#grad_y [38.814.400000632.8]
image text in transcribed

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access with AI-Powered 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

Students also viewed these Databases questions