[regression] updated exercise infrastructure
This commit is contained in:
@@ -1,34 +1,4 @@
|
||||
TEXFILES=$(wildcard exercises??.tex)
|
||||
EXERCISES=$(TEXFILES:.tex=.pdf)
|
||||
SOLUTIONS=$(EXERCISES:exercises%=solutions%)
|
||||
TEXFILES=$(wildcard gradientdescent-?.tex)
|
||||
|
||||
.PHONY: pdf exercises solutions watch watchexercises watchsolutions clean
|
||||
include ../../exercises.mk
|
||||
|
||||
pdf : $(SOLUTIONS) $(EXERCISES)
|
||||
|
||||
exercises : $(EXERCISES)
|
||||
|
||||
solutions : $(SOLUTIONS)
|
||||
|
||||
$(SOLUTIONS) : solutions%.pdf : exercises%.tex instructions.tex
|
||||
{ echo "\\documentclass[answers,12pt,a4paper,pdftex]{exam}"; sed -e '1d' $<; } > $(patsubst %.pdf,%.tex,$@)
|
||||
pdflatex -interaction=scrollmode $(patsubst %.pdf,%.tex,$@) | tee /dev/stderr | fgrep -q "Rerun to get cross-references right" && pdflatex -interaction=scrollmode $(patsubst %.pdf,%.tex,$@) || true
|
||||
rm $(patsubst %.pdf,%,$@).[!p]*
|
||||
|
||||
$(EXERCISES) : %.pdf : %.tex instructions.tex
|
||||
pdflatex -interaction=scrollmode $< | tee /dev/stderr | fgrep -q "Rerun to get cross-references right" && pdflatex -interaction=scrollmode $< || true
|
||||
|
||||
watch :
|
||||
while true; do ! make -q pdf && make pdf; sleep 0.5; done
|
||||
|
||||
watchexercises :
|
||||
while true; do ! make -q exercises && make exercises; sleep 0.5; done
|
||||
|
||||
watchsolutions :
|
||||
while true; do ! make -q solutions && make solutions; sleep 0.5; done
|
||||
|
||||
clean :
|
||||
rm -f *~ *.aux *.log *.out
|
||||
|
||||
cleanup : clean
|
||||
rm -f $(SOLUTIONS) $(EXERCISES)
|
||||
|
||||
25
regression/exercises/checkdescent.m
Normal file
25
regression/exercises/checkdescent.m
Normal file
@@ -0,0 +1,25 @@
|
||||
% data:
|
||||
load('lin_regression.mat')
|
||||
|
||||
% compute mean squared error for a range of slopes and intercepts:
|
||||
slopes = -5:0.25:5;
|
||||
intercepts = -30:1:30;
|
||||
errors = zeros(length(slopes), length(intercepts));
|
||||
for i = 1:length(slopes)
|
||||
for j = 1:length(intercepts)
|
||||
errors(i,j) = lsqError([slopes(i), intercepts(j)], x, y);
|
||||
end
|
||||
end
|
||||
|
||||
% minimum of error surface:
|
||||
[me, mi] = min(errors(:));
|
||||
[ia, ib] = ind2sub(size(errors), mi);
|
||||
eparams = [errors(ia), errors(ib)];
|
||||
|
||||
% gradient descent:
|
||||
pstart = [-2. 10.];
|
||||
[params, errors] = descent(x, y, pstart);
|
||||
|
||||
% comparison:
|
||||
fprintf('descent: %6.3f %6.3f\n', params(1), params(2));
|
||||
fprintf('errors: %6.3f %6.3f\n', eparams(1), eparams(2));
|
||||
15
regression/exercises/descent.m
Normal file
15
regression/exercises/descent.m
Normal file
@@ -0,0 +1,15 @@
|
||||
function [params, errors] = descent(xdata, ydata, pstart)
|
||||
mingradient = 0.1;
|
||||
eps = 0.01;
|
||||
|
||||
errors = [];
|
||||
params = pstart;
|
||||
count = 1;
|
||||
gradient = [100.0, 100.0];
|
||||
while norm(gradient) > mingradient
|
||||
gradient = lsqGradient(params, xdata, ydata);
|
||||
errors(count) = lsqError(params, xdata, ydata);
|
||||
params = params - eps .* gradient;
|
||||
count = count + 1;
|
||||
end
|
||||
end
|
||||
22
regression/exercises/descentfit.m
Normal file
22
regression/exercises/descentfit.m
Normal file
@@ -0,0 +1,22 @@
|
||||
clear
|
||||
close all
|
||||
load('lin_regression.mat')
|
||||
|
||||
pstart = [-2. 10.];
|
||||
[params, errors] = descent(x, y, pstart);
|
||||
|
||||
figure()
|
||||
subplot(2,1,1)
|
||||
hold on
|
||||
scatter(x, y, 'displayname', 'data')
|
||||
xx = min(x):0.01:max(x);
|
||||
fx = params(1)*xx + params(2);
|
||||
plot(xx, fx, 'displayname', 'fit')
|
||||
xlabel('Input')
|
||||
ylabel('Output')
|
||||
grid on
|
||||
legend show
|
||||
subplot(2,1,2)
|
||||
plot(errors)
|
||||
xlabel('optimization steps')
|
||||
ylabel('error')
|
||||
@@ -1,60 +1,17 @@
|
||||
\documentclass[12pt,a4paper,pdftex]{exam}
|
||||
|
||||
\usepackage[german]{babel}
|
||||
\usepackage{natbib}
|
||||
\usepackage{xcolor}
|
||||
\usepackage{graphicx}
|
||||
\usepackage[small]{caption}
|
||||
\usepackage{sidecap}
|
||||
\usepackage{pslatex}
|
||||
\usepackage{amsmath}
|
||||
\usepackage{amssymb}
|
||||
\setlength{\marginparwidth}{2cm}
|
||||
\usepackage[breaklinks=true,bookmarks=true,bookmarksopen=true,pdfpagemode=UseNone,pdfstartview=FitH,colorlinks=true,citecolor=blue]{hyperref}
|
||||
\newcommand{\exercisetopic}{Resampling}
|
||||
\newcommand{\exercisenum}{9}
|
||||
\newcommand{\exercisedate}{December 21th, 2020}
|
||||
|
||||
%%%%% text size %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
||||
\usepackage[left=20mm,right=20mm,top=25mm,bottom=25mm]{geometry}
|
||||
\pagestyle{headandfoot}
|
||||
\ifprintanswers
|
||||
\newcommand{\stitle}{: Solutions}
|
||||
\else
|
||||
\newcommand{\stitle}{}
|
||||
\fi
|
||||
\header{{\bfseries\large Exercise 10\stitle}}{{\bfseries\large Gradient descent}}{{\bfseries\large December 16th, 2019}}
|
||||
\firstpagefooter{Dr. Jan Grewe}{Phone: 29 74588}{Email:
|
||||
jan.grewe@uni-tuebingen.de}
|
||||
\runningfooter{}{\thepage}{}
|
||||
\input{../../exercisesheader}
|
||||
|
||||
\setlength{\baselineskip}{15pt}
|
||||
\setlength{\parindent}{0.0cm}
|
||||
\setlength{\parskip}{0.3cm}
|
||||
\renewcommand{\baselinestretch}{1.15}
|
||||
\firstpagefooter{Prof. Dr. Jan Benda}{}{jan.benda@uni-tuebingen.de}
|
||||
|
||||
\newcommand{\code}[1]{\texttt{#1}}
|
||||
\renewcommand{\solutiontitle}{\noindent\textbf{Solution:}\par\noindent}
|
||||
%%%%% listings %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
||||
\usepackage{listings}
|
||||
\lstset{
|
||||
language=Matlab,
|
||||
basicstyle=\ttfamily\footnotesize,
|
||||
numbers=left,
|
||||
numberstyle=\tiny,
|
||||
title=\lstname,
|
||||
showstringspaces=false,
|
||||
commentstyle=\itshape\color{darkgray},
|
||||
breaklines=true,
|
||||
breakautoindent=true,
|
||||
columns=flexible,
|
||||
frame=single,
|
||||
xleftmargin=1em,
|
||||
xrightmargin=1em,
|
||||
aboveskip=10pt
|
||||
}
|
||||
|
||||
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
||||
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
||||
\begin{document}
|
||||
|
||||
\input{instructions}
|
||||
\input{../../exercisestitle}
|
||||
|
||||
\begin{questions}
|
||||
|
||||
@@ -96,7 +53,7 @@
|
||||
the parameter values at the minimum of the cost function and a vector
|
||||
with the value of the cost function at each step of the algorithm.
|
||||
\begin{solution}
|
||||
\lstinputlisting{../code/descent.m}
|
||||
\lstinputlisting{descent.m}
|
||||
\end{solution}
|
||||
|
||||
\part Plot the data and the straight line with the parameter
|
||||
@@ -105,7 +62,7 @@
|
||||
\part Plot the development of the costs as a function of the
|
||||
iteration step.
|
||||
\begin{solution}
|
||||
\lstinputlisting{../code/descentfit.m}
|
||||
\lstinputlisting{descentfit.m}
|
||||
\end{solution}
|
||||
|
||||
\part For checking the gradient descend method from (a) compare
|
||||
@@ -116,7 +73,7 @@
|
||||
minimum gradient. What are good values such that the gradient
|
||||
descent gets closest to the true minimum of the cost function?
|
||||
\begin{solution}
|
||||
\lstinputlisting{../code/checkdescent.m}
|
||||
\lstinputlisting{checkdescent.m}
|
||||
\end{solution}
|
||||
|
||||
\part Use the functions \code{polyfit()} and \code{lsqcurvefit()}
|
||||
@@ -124,7 +81,7 @@
|
||||
line that fits the data. Compare the resulting fit parameters of
|
||||
those functions with the ones of your gradient descent algorithm.
|
||||
\begin{solution}
|
||||
\lstinputlisting{../code/linefit.m}
|
||||
\lstinputlisting{linefit.m}
|
||||
\end{solution}
|
||||
|
||||
\end{parts}
|
||||
18
regression/exercises/linefit.m
Normal file
18
regression/exercises/linefit.m
Normal file
@@ -0,0 +1,18 @@
|
||||
% data:
|
||||
load('lin_regression.mat')
|
||||
|
||||
% gradient descent:
|
||||
pstart = [-2. 10.];
|
||||
[params, errors] = descent(x, y, pstart);
|
||||
|
||||
% lsqcurvefit:
|
||||
line = @(p, x) x.* p(1) + p(2);
|
||||
cparams = lsqcurvefit(line, pstart, x, y);
|
||||
|
||||
% polyfit:
|
||||
pparams = polyfit(x, y, 1);
|
||||
|
||||
% comparison:
|
||||
fprintf('descent: %6.3f %6.3f\n', params(1), params(2));
|
||||
fprintf('lsqcurvefit: %6.3f %6.3f\n', cparams(1), cparams(2));
|
||||
fprintf('polyfit: %6.3f %6.3f\n', pparams(1), pparams(2));
|
||||
Reference in New Issue
Block a user