Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

I just need answers for all TODO. I do not need any explanation in detail. Just only answer for all TODO Please. TODO 15 Complete

I just need answers for all TODO. I do not need any explanation in detail. Just only answer for all TODO Please.

TODO 15

Complete the TODO by calling the get_mini_batches() and observing its output.

Call the get_mini_batches() function and pass the length of the X_trn data and a batch size of 32. Store the output into batches.

# TODO 15.1 batches = display(batches)

TODO 16 (Important!)

Tips:

There are many different ways of implementing this algorithm correctly. The instructions will try to guide you towards one potential implementation!

Programming implementations don't always correspond 1-1 with the mathematical equations! Sometimes terms are changed around or transposed!

Check the shapes of all your NumPy arrays. You will most likely run into shape mismatch, matmul, and broadcasting issues when implementing this code! Refer to the print statements which help you to debug your shape issues in code! The goal here is to practice troubleshooting these errors.

Please also refer back to Module 1's lab to review shape mismatch, matmul, and broadcasting errors and how to debug them!

Making all your arrays 2D will cause you less headaches when it comes to broadcasting, shape mismatch, or matmul errors. Remember a 2D means it has 2 dimensions. Take the example of a random array that has 5 elements. It can be represented either as a 1D or 2D array in NumPy as seen below:

1D array has the shape (5,)

2D array has the shape (5, 1) or (1, 5)

A column vector has the shape (5, 1)

A row vector has the shape (1, 5)

Notice this is the same as the 1D array but we explicitly defined a 1 either in the 1st or 2nd dimension. This is what we mean when we say a 2D vector (a 2D array where one a dimension is equal to 1, either in the row or column dimension)!

To make a 1D array into a 2D array you can use the .reshape() method. For instance, if our weights self.w had the shape (,)(,) we could use the.reshape() method add a dimension by doing the following: self.w = self.w.reshape(-1, 1) (post on meaning for (-1, 1)) giving us the new shape (,1)(,1).

To switch the dimensions of an array we can also use the .reshape() method. For instance, if our weights self.w had the shape (1,)(1,) we could use the.reshape() method switch the row and column dimensions by doing the following: self.w = self.w.reshape(-1, 1) giving us the new shape (,1)(,1).

TODO 16.1 for predict()

First, complete the self.predict() method to which returns the prediction between our input data X and weights self.w. We'll be using this method to complete our fit() method as well!

Hint: Recall, we use the dot product to between the weights self.w and the passed data X to make a prediction. You might have to adjust the order of whether the weights or data comes first to avoid shape mismatch or matmul errors!

TODO 16.2 for fit(): Setting the weights

Initialize the weights self.w randomly using our instance variable rng of NumPy's np.random.RandomState class and use rng.rand() (docs) method to create a column vector of shape (,1)(,1) where is the number of features/columns in our data X (1 weight for each feature including the bias term). Store the output back into our class variable self.w. Pass the arguments that correspond to the following descriptions:

The first argument ALWAYS corresponds to size of the 1st dimension (i.e., the number of rows). Set this equal to the number of features/columns in our data X.

The second argument ALWAYS corresponds to size of the 2nd dimension (i.e., the number of columns). Set this equal to 1 since we want a 2D column vector!

TODOs 16.3-16.4 for fit(): Getting mini-batches and predictions

Pre-compute the mini-batch indexes by using the get_mini_batches()function. Store the output in batch_indexes. Pass the arguments that correspond to the following descriptions:

Number of data samples in X (try finding the length of X).

The size of each mini-batch (refer to the LeastMeanSquares class variables).

Compute the predictions for the CURRENT mini-batch by indexing X by mb and calling the self.predict() method. Store the output into y_hat.

TODOs 16.5-16.7 for fit(): Computing the average mini-batch gradient

Compute the L2 norm derivative . Store the output into l2_norm.

Hint: Recall refers to our class variable self.lamb.

Compute the gradient and store the output into gradient. Use the below equation to compute the gradient:

()=mb(mbmb)+=mb( mbmb)+()=mb(mbmb)+=mb(^mbmb)+

Hint: Recall mb= mbmb=^mb. This means we can just use the variable y_hat which already holds the output of mbmb.

Hint: Recall refers to the l2_norm variable we just set!

Compute the average gradient by dividing by the number of data samples in the mini-batch (i.e., the length of the mini-batch). Store the output into avg_gradient.

()=1mb()()=1mb()

TODO 16.8 for fit(): Updating the weights

Update the weights using the average gradient as shown in the below equation. Store the output into self.w.

+1=()+1=()

Hint: Recall ()() refers to our gradient in this case avg_gradient.

Hint: Recall refers to our learning rate stored in the class variable self.alpha.

import IPython.display as ipd # for display and clear_output

class LeastMeanSquares(): """ Performs regression using least mean squares (gradient descent) attributes: w (np.ndarray): weight matrix alpha (float): learning rate or step size lamb (float): Regularization parameter for controlling L2 regularization. batch_size(int): Size of mini-batches for mini-batch gradient descent. epochs (int): Number of epochs to run for mini-batch gradient descent seed (int): Seed to be used for NumPy's RandomState class or universal seed np.random.seed() function. """

def __init__( self, alpha: float = .01, lamb: float = 0, batch_size: int = 32, epochs: int = 1, seed: int = None ): self.w = None self.alpha = alpha self.lamb = lamb self.epochs = epochs self.seed = seed self.batch_size = batch_size self.fig = plt.figure(figsize=(10,5)) self.trn_err_hist = [] def predict(self, X: np.ndarray): # TODO 16.1 pass # Replace this line with your code

def fit(self, X: np.ndarray, y: np.ndarray) -> np.ndarray: # Set global seed for mini-batch reproducibility np.random.seed(self.seed) # Set rnd with seed for weight reproducibility rng = np.random.RandomState(self.seed) # TODO 16.2 self.w = print("Initial Shape check...") print(f"\t X shape: {X.shape}") print(f"\t y shape: {y.shape}") print(f"\t w shape: {self.w.shape}") for e in range(self.epochs): # TODO 16.3 batch_indexes = for mb in batch_indexes: print(f"\ty[mb] shape: {y[mb].shape}") print(f"\tX[mb, :] shape: {X[mb].shape}") print(f"\tself.w: {self.w.shape}") # TODO 16.4 y_hat = print(f"\ty_hat shape: {y_hat.shape}") # TODO 16.5 l2_norm = print(f"\tl2_norm shape: {l2_norm.shape}") # TODO 16.6 gradient = print(f"\tgradient shape: {gradient.shape}")

# TODO 16.7 avg_gradient = print(f"\tavg_gradient shape: {avg_gradient.shape}")

# TODO 16.8 self.w = print(f"\tself.w shape AFTER update: {self.w.shape}") # Checking predictions and RMSE performance # of training data. preds = self.predict(X) print(f"\tpreds: {preds.shape}")

trn_rmse = rmse(preds, y) self.trn_err_hist.append(trn_rmse) # Plot error every minibatch self._plot_rmse_hist()

def _plot_rmse_hist(self): # Training RMSE tracking (left graph) plt.subplot(1, 2, 1) plt.plot(self.trn_err_hist) plt.ylabel(f"Training RMSE") plt.xlabel("Mini-Batches Trained") plt.title(f"RMSE Learning Curve - Current RMSE: {self.trn_err_hist[-1]}") # clear display ipd.clear_output(wait=True) ipd.display(self.fig) plt.clf()

TODO 17

Call the data_prep() function to get our cleaned and transformed data. Store the output into data. Make sure to pass forestfire_df and the arguments corresponding to the following descriptions:

Return all data as NumPy arrays.

Hint: You can specify this list of strings manually, or automatically generate it by using Pandas drop() method on the forestfire_df and dropping the 'area', 'month', and 'day' columns. Once dropped, you can then call the columns class variable to get the column names of the remaining numerical features.

Transform the specified features using a degree 3 polynomial.

Create an instance of the LeastMeanSquares and pass the following arguments as specified below. Store the output into lms.

Pass the lambda or $$\lambda = .2$

Pass a learning rate or =.1=.1

Pass 12 epochs

Pass a batch size of 128

Pass a seed of 42

# TODO 17.1 data = X_trn, y_trn, X_vld, y_vld, _, _, feature_names = data

# TODO 17.2 lms =

lms.fit(X_trn, y_trn)

y_hat_trn = lms.predict(X_trn)

_, trn_sse, trn_mse, trn_rmse = analyze( y=y_trn, y_hat=y_hat_trn, title="Training Predictions Log Transform", dataset="Training", xlabel="Data Sample Index", ylabel="Predicted Log Area" )

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

Data Science For Dummies

Authors: Lillian Pierson ,Jake Porway

2nd Edition

1119327636, 978-1119327639

More Books

Students also viewed these Databases questions

Question

How to solve maths problems with examples

Answered: 1 week ago