EECE5644_2020Spring_TakeHome4Questions.pdf EECE5644 Spring 2020 – Take Home Exam 4 Submit: Monday, 2020-April-13 before 11:45ET Please submit your solutions on Blackboard in a single PDF file that...

1 answer below »
Subject: Machine Learning


EECE5644_2020Spring_TakeHome4Questions.pdf EECE5644 Spring 2020 – Take Home Exam 4 Submit: Monday, 2020-April-13 before 11:45ET Please submit your solutions on Blackboard in a single PDF file that includes all math and numerical results. Only the contents of this PDF will be graded. For control purposes, please also provide any code you write to solve the questions in one of the following ways: 1. Include all of your code in the PDF as an appendix. 2. In the PDF provide a link to your online code repo where the code resides. This is a graded assignment and the entirety of your submission must contain only your own work. Do not make your code repo public until after the submission deadline. If people find your code and use it to solve questions, we will consider it cheating for all parties involved. You may benefit from publicly available literature including software (that is not from classmates), as long as these sources are properly acknowledged in your submission. Copying math or code from each other is clearly not allowed and will be considered as aca- demic dishonesty. Discussing with the instructor and the teaching assistant to get clarification, or to eliminate doubts is acceptable. By submitting a PDF file in response to this take home assignment you are declaring that the contents of your submission, and the associated code is your own work, except as noted in your acknowledgement citations to resources. Start early and use the office periods well. The office periods are on gcal and contain video links for remote participation. You may call 1-617-3733021 to reach Deniz if you have difficulty using the video links for the office periods. It is your responsibility to provide the necessary and sufficient evidence in the form of expla- nations, math, visualizations, and numerical values to earn scores. Do not provide a minimal set of what you interpret is being precisely requested; you may be providing too little evidence of your understanding. On the other hand, also please do not dump in unnecessarily large amounts of things; you may be creating the perception that you do not know what you are doing. Be inten- tional in what you present and how you present it. You are trying to convince the reader (grader) that your mathematical design, code implementations, and generated results are legitimate and correct/accurate. 1 Question 1 (30%) The data generation script for this question is called exam4q1 generateData.m. Generate two- dimensional x = [x1,x2]T samples with this Matlab script; specifically, 1000 samples for training and 10000 samples for testing. Train and test a single hidden layer MLP function approximator to estimate the value of X2 from the value of X1 by minimizing the mean-squared-error (MSE) on the training set. (The first coordinate of each data vectors will go into the MLP, and the output will try to approximate the second coordinate of the data vectors.) Use a softplus (SmoothReLu) activation function as the nonlinearity for the perceptrons in the hidden layer. Useing 10-fold cross validation, select the the best number of perceptrons that your training set can justify using. Leave the output layer of the MLP as a linear unit linear (no nonlinearity). Once the best model structure is identified using cross-validation, train the selected model with the entire training set. Apply the trained MLP to the test set. Estimate the test performance. You may use existing software packages for all aspects of this solution. Make sure to clearly demonstrate that you are using the packages properly. Hint: logistic(z) = 1/(1+ e−z) & so f t plus(z) = ln(1+ ez) Note: The theoretical minimum-MSE estimator is the conditional expectation of X2 given X1, which can be analytically derived using the joint pdf, if it had been known, but in practice we do not know the true pdf, so in this exercise we try to approximate the theoretically optimal solution with a neural network model. Question 2 (35%) For this question use the same multiring data generation script from the third assignment. Gen- erate a two-class training set with 1000 and testing set with 10000 samples. The class priors should be equal. Train and evaluate a support vector machine classifier with a Gaussian kernel (radial- basis function (RBF) kernel) on these datasets. Specifically, us a spherically symmetric Gaussian/RBF kernel. Using 10-fold cross-validation, select the best box constraint hyperparameter C and the Gaussian kernel width parameter σ (nota- tion based on previously covered math in class). Train a final SVM using the best combination of hyperparameters with the entire training set. Classify the testing dataset samples with this trained SVM to assess performance. You may use existing software packages for all aspects of this solution. Make sure to clearly demonstrate that you are using the packages properly. Question 3 (35%) In this question, you will use GMM-based clustering to segment the color images 3096 color.jpg and 42049 color.jpg from the Berkeley Image Segmentation Dataset. We will refer to these images as the airplane and bird images, respectively. As preprocessing, for each pixel, generate a 5-dimensional feature vector as follows: (1) ap- pend row index, column index, red value, green value, blue value for each pixel into a raw feature vector; (2) normalize each feature entry individually to the interval [0,1], so that all of the fea- ture vectors representing every pixel in an image fit into the 5-dimensional unit-hypercube. All segmentation algorithms should operate on these normalized feature vectors. 1 For each image do the following: (1) Using maximum likelihood parameter estimation, fit a GMM with 2-components, use this GMM to segment the image into two parts; (2) Using 10-fold cross-validation, and maximum everage validation-log-likelihood as the objective, identify the best number of clusters, then fit a new GMM with this best number of components and use this GMM to segment the image into as many parts as there are number of Gaussians. For GMM-based clustering, use the GMM components as class/cluster-conditional pdfs and assign cluster labels using the MAP-classification rule. 2 MATLAB code for reference/ClusteringWithMeanShiftThenSpectral.m function [Y,C,labels] = ClusteringWithMeanShiftThenSpectral(N,M) % Sample call: % [Y,C,labels] = ClusteringWithMeanShiftThenSpectral(100,4); % This demo generates N random 2-dim samples from % an order M mixture of Gaussians with arbitrary % weights-means-covariances. Uses a fixed-width % spherically symmetric Gaussian kernel % based KDE to improve cluster spread using % mean shift, then uses a basic spectral % clustering method to assign cluster labels % selecting number of clusters automatically % based on eigenvalue sequence in latter. % Generate iid samples from a random M-GMM [Y,C] = randGMM(N,M); % Y data, C component labels % Perform a fixed number of mean shift iterations T = 6; % Number of mean-shift iterations to be performed X = meanshift(Y,T); % Perform basic spectral clustering on X labels = spectralclustering(X); nClusters = length(unique(labels)); figure(1), clf, for k = 1:nClusters plot(Y(1,find(labels==k)),Y(2,find(labels==k)),'.','Color',hsv2rgb([rand,1,1])); title(strcat({'Number of Clusters = '},num2str(nClusters))), axis equal, hold on, drawnow, end, %%%%%% function labels = spectralclustering(Y) [D,N] = size(Y); covY = cov(Y'); sigma2 = (1/D)*trace(covY)*(4/((2*D+1)*N))^(2/(D+4)); pd = pdist2(Y',Y'); S = exp(-0.5*pd.^2/sigma2); %Deg = diag(sum(S,2));%^(-1/2); %Lsym = Deg^(-1/2)*(Deg-S)*Deg^(-1/2); %Graph Laplacian %[V,D] = eig(eye(N)-Lsym); [d,ind]=sort(diag(D),'descend'); [V,D] = eig(S); [d,ind]=sort(diag(D),'descend'); D = diag(d); V = V(:,ind); %figure(2), clf, stem(d,'r.'), hold on, stem(diff(d),'b.'); drawnow, dGYY = sort(abs(eig(S)),'descend'); nClusters = min(find(abs(cumsum(dGYY)>=0.99*N))); % the sume of evals for GYY will be N % the smallest number of clusters that total almost N in e.val. %[~,nClusters]=min(diff(d)); % crude method for deciding on number of clusters X = V(:,1:nClusters)'; % map data to relevant subspace of eigenfunction injection % the mapped data X will be clustered by angular separation [~,labels] = max(abs(X),[],1); % crude final label assignment method %figure(3), clf, %if nClusters == 2 % plot(X(1,find(labels==1)),X(2,find(labels==1)),'r.'); hold on, % plot(X(1,find(labels==2)),X(2,find(labels==2)),'g.'); % drawnow, %elseif nClusters == 3 % plot3(X(1,find(labels==1)),X(2,find(labels==1)),X(3,find(labels==1)),'r.'); hold on, % plot3(X(1,find(labels==2)),X(2,find(labels==2)),X(3,find(labels==2)),'g.'); % plot3(X(1,find(labels==3)),X(2,find(labels==3)),X(3,find(labels==3)),'b.'); % drawnow, %end, %%%%%% function X = meanshift(Y,T) [D,N] = size(Y); covY = cov(Y'); sigma2 = (1/D)*trace(covY)*(4/((2*D+1)*N))^(2/(D+4)); % sigma2*identity is the covariance used for KDE Gaussians X = Y; % initialize mean shift iterations to data points for t = 1:T % you can substitute this with a better stopping criterion figure(1), clf, plot(Y(1,:),Y(2,:),'r.');drawnow, axis equal, hold on, plot(X(1,:),X(2,:),'b.'), title(strcat({'Iteration '},num2str(t),{' of '},num2str(T))); %pause(3), pd = pdist2(X',Y'); GXY = exp(-0.5*pd.^2/sigma2)/((2*pi)^(D/2)*sqrt(sigma2)^D); W = inv(diag(sum(GXY,2)))*GXY; X = Y*W'; end figure(1), clf, plot(Y(1,:),Y(2,:),'r.'); axis equal, hold on, plot(X(1,:),X(2,:),'b.'), title(strcat({'Iteration '},num2str(t),{' of '},num2str(T))), %keyboard, %%%%%% function [Y,C] = randGMM(N,M) % Generates N 2-dim samples if 0 % random GMM parameters temp = rand(1,M); a = temp/sum(temp); % probability for each Gaussian component mu = 10*(rand(2,M)-0.5); % means for each Gaussian component S = randn(2,2,M); % scale matrices for each Gaussian component end if 1 % fixed GMM parameters a = ones(1,M)/M; mu = [linspace(-M,M,M);zeros(1,M)]; tempS = 0.25*M/(M-1)*eye(2,2); S = repmat(tempS,1,1,M); end Z = randn(2,N); % 0-mean I-scale Gaussian samples Nc = zeros(1,M); temp = rand(1,N); ca = [0,cumsum(a)]; for m = 1:M, Nc(1,m) = length(find(ca(m)<=temp &="">
Answered Same DayMar 31, 2021

Answer To: EECE5644_2020Spring_TakeHome4Questions.pdf EECE5644 Spring 2020 – Take Home Exam 4 Submit: Monday,...

Abr Writing answered on Apr 03 2021
150 Votes
EECE5644_2020Spring_TakeHome4Solution.m
%% Question 1
% Generating the training data
data = exam4q1_generateData(1000);
% Getting the variable X1 or predictor from the generated data
X_train = data(1,:);
% Extracting the response variable from the generated data
y_train = data(2,:);
% Generating the testing data
data = exam4q1_generateData(10000);
% Getting the variable X1 or predictor from the generated data
X_test = data(1,:);
% Extracting the response variable from the generated data
y_test = data(2,:);
% Creating the network with single layer with 10 ndoes
net = feedforwardnet([10]);
% number of h
idden layer neurons
net.layers{1}.size = 5;
% hidden layer transfer function
net.layers{1}.transferFcn = 'poslin';
% Declaring the number of folds
k = 10;
% Getting the indexes for cross validation
cv = cvpartition(length(X),'k', k);
for i = 1:k
% Splitting the data based on cross validation
trainIdxs = find(training(cv,i));
testIdxs = find(test(cv,i));
trainMatrix = data(trainIdxs, :);
validMatrix = data(testIdxs, :);
net = configure(net,X_train,y_train);
% initial network response without training
initial_output = net(X_train);
% network training
net.trainFcn = 'trainlm';
net.performFcn = 'mse';
net = train(net,X_train,y_train);
% Plotting performance
% Plotting Training state
% Plotting Error Histogram
% Plotting Regression Results
plotregression(net);
% network response after training
final_output = net(X_val);
end
%% Question 2
% Generating the data
[X_train,y_train] = generateMultiringDataset(2,1000);
[X_test,y_test] = generateMultiringDataset(2,10000);
X_train = X_train';
X_test = X_test';
y_train = num2cell(num2str(y_train'));
y_test = num2cell(num2str(y_test'));
rng(1); % For reproducibility
% 10 fold cross validation
c = cvpartition(length(y_train),'KFold',10);
opts = struct('Optimizer','bayesopt', ...
'ShowPlots',true, ...
'CVPartition',c,...
'AcquisitionFunctionName','expected-improvement-plus');
% Train an SVM classifier.
SVMModel = fitcsvm(X_train,y_train, ...
'Standardize',true, ...
'ClassNames',{'1', '2'}, ...
'KernelFunction','RBF', ...
'OptimizeHyperparameters','auto', ...
'HyperparameterOptimizationOptions',opts);
% Cross-validate the classifier
CVSVMModel = crossval(SVMModel,'Kfold', 10);
% Estimate the generalization error.
kfoldLoss(CVSVMModel)
svInd = SVMModel.IsSupportVector;
h = 1; % Mesh grid step size
[X1,X2] = meshgrid(min(X_train(:,1)):h:max(X_train(:,1)),...
min(X_train(:,2)):h:max(X_train(:,2)));
[~,score] = predict(SVMModel,[X1(:),X2(:)]);
scoreGrid = reshape(score(:,2),size(X1,1),size(X2,2));
figure
plot(X_train(:,1),X_train(:,2),'k.')
hold on
plot(X_train(svInd,1),X_train(svInd,2),'ro','MarkerSize',10)
contour(X1,X2,scoreGrid)
colorbar;
title('{\bfOutlier Detection via SVM with RBF kernel}')
legend('Observation','Support Vector')
hold off
% Prediction
y_pred = predict(SVMModel, X_test);
y_pred = cell2mat(y_pred);
y_test = cell2mat(y_test);
confusionmat(y_test, y_pred)
%% Question 3
% Reading the file
airplane = imread('3096_color.jpg');
bird = imread('42049_color.jpg');
% Generating 5 dimensional array as described in problem
airplane_temp = zeros(size(airplane,1)*size(airplane,2), 5);
for i = 1:size(airplane,1)
for j = 1:size(airplane,2)
airplane_temp((i-1)*size(airplane, 1)+j, :) = [i, ...
j ...
airplane(i, j, 1), ...
airplane(i, j, 2), ...
airplane(i, j, 3)];
end
end
airplane = airplane_temp;
bird_temp = zeros(size(bird,1)*size(bird,2), 5);
for i = 1:size(bird,1)
for j = 1:size(bird,2)
bird_temp((i-1)*size(bird, 1)+j, :) = [i, ...
j ...
bird(i, j, 1), ...
bird(i, j, 2), ...
bird(i, j, 3)];
end
end
bird = bird_temp;
airplane_plot = airplane(:,1:2);
bird_plot = bird(:,1:2);
% Normalizing the daya
airplane(:,1) = standardize(airplane(:,1));
airplane(:,2) = standardize(airplane(:,2));
airplane(:,3) = standardize(airplane(:,3));
airplane(:,4) = standardize(airplane(:,4));
airplane(:,5) = standardize(airplane(:,5));
bird(:,1) = standardize(bird(:,1));
bird(:,2) = standardize(bird(:,2));
bird(:,3) = standardize(bird(:,3));
bird(:,4) = standardize(bird(:,4));
bird(:,5) = standardize(bird(:,5));
% Fitting the model
GMModel_airplane = fitgmdist(airplane,2,'RegularizationValue',0.1);
GMModel_bird = fitgmdist(bird,2,'RegularizationValue',0.1);
GMModels_airplane = cell(3,1); % Preallocation
options = statset('MaxIter',1000);
for j = 1:3
GMModels_airplane{j} = fitgmdist(airplane,j,'Options',options,'RegularizationValue',0.1);
fprintf('\n GM Mean for %i Component(s)\n',j)
Mu = GMModels_airplane{j}.mu
end
figure
for j = 1:3
idx = cluster(GMModels_airplane{j},airplane);
subplot(2,2,j)
gscatter(airplane_plot(:,1),airplane_plot(:,2),idx)
title(sprintf('GM Model - %i Component(s)',j));
xlabel('');
ylabel('');
legend off;
hold off
end
% Fitting the model
GMModel_bird = fitgmdist(bird,2,'RegularizationValue',0.1);
GMModel_bird = fitgmdist(bird,2,'RegularizationValue',0.1);
GMModels_bird = cell(3,1); % Preallocation
options = statset('MaxIter',1000);
for j = 1:3
GMModels_bird{j} = fitgmdist(bird,j,'Options',options,'RegularizationValue',0.1);
fprintf('\n GM Mean for %i Component(s)\n',j)
Mu = GMModels_bird{j}.mu
end
figure
for j = 1:3
idx = cluster(GMModels_bird{j},bird);
subplot(2,2,j)
gscatter(bird_plot(:,1),bird_plot(:,2),idx)
title(sprintf('GM Model - %i Component(s)',j));
xlabel('');
ylabel('');
legend off;
hold off
end
standardize.m
function X_std = standardize(X)
X_std = (X - min(X)) / ( max(X) - min(X) );
end
exam4q1_generateData.m
function x = exam4q1_generateData(N)
close all,
m(:,1) = [-9;-4]; Sigma(:,:,1) = 4*[1,0.8;0.8,1]; % mean and covariance of data pdf conditioned on label 3
m(:,2) = [0;0]; Sigma(:,:,2) = 3*[3,0;0,0.3]; % mean and covariance of data pdf conditioned on label 2
m(:,3) = [8;-3]; Sigma(:,:,3) =...
SOLUTION.PDF

Answer To This Question Is Available To Download

Related Questions & Answers

More Questions »

Submit New Assignment

Copy and Paste Your Assignment Here