Question: Use python jupyter lab, NO AI PLZZZZZZ: Question ( check the pic ) : Declare intermediate variables, q 1 = x 3 , q 2

Use python jupyter lab, NO AI PLZZZZZZ:
Question (check the pic):
Declare intermediate variables, q1=x3,q2=y4,q=q1+q2 and so on. Remember to use dfdx is needed when x is used in
multiple paths of the computation), similarly dfdy.
Code:
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
from sklearn.datasets import make_classification
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)
##
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)
-----
Question (check pic):
Q4(20 points)
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=1i=m(zi)
Code:
# 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]
-----
Question (check pic):
6.0.2. Calculate the gradient of f with respect to x and y using the computation graph in
tensorflow
dfdx=
dfdy=
Code:
#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]
 Use python jupyter lab, NO AI PLZZZZZZ: Question (check the pic):

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!