$\newcommand{\eps}{\varepsilon} \newcommand{\kron}{\otimes} \DeclareMathOperator{\diag}{diag} \DeclareMathOperator{\trace}{trace} \DeclareMathOperator{\rank}{rank} \DeclareMathOperator*{\minimize}{minimize} \DeclareMathOperator*{\maximize}{maximize} \DeclareMathOperator{\subjectto}{subject to} \newcommand{\mat}[1]{\boldsymbol{#1}} \renewcommand{\vec}[1]{\boldsymbol{\mathrm{#1}}} \newcommand{\vecalt}[1]{\boldsymbol{#1}} \newcommand{\conj}[1]{\overline{#1}} \newcommand{\normof}[1]{\|#1\|} \newcommand{\onormof}[2]{\|#1\|_{#2}} \newcommand{\MIN}[2]{\begin{array}{ll} \minimize_{#1} & {#2} \end{array}} \newcommand{\MINone}[3]{\begin{array}{ll} \minimize_{#1} & {#2} \\ \subjectto & {#3} \end{array}} \newcommand{\MINthree}[5]{\begin{array}{ll} \minimize_{#1} & {#2} \\ \subjectto & {#3} \\ & {#4} \\ & {#5} \end{array}} \newcommand{\MAX}[2]{\begin{array}{ll} \maximize_{#1} & {#2} \end{array}} \newcommand{\MAXone}[3]{\begin{array}{ll} \maximize_{#1} & {#2} \\ \subjectto & {#3} \end{array}} \newcommand{\itr}[2]{#1^{(#2)}} \newcommand{\itn}[1]{^{(#1)}} \newcommand{\prob}{\mathbb{P}} \newcommand{\probof}[1]{\prob\left\{ #1 \right\}} \newcommand{\pmat}[1]{\begin{pmatrix} #1 \end{pmatrix}} \newcommand{\bmat}[1]{\begin{bmatrix} #1 \end{bmatrix}} \newcommand{\spmat}[1]{\left(\begin{smallmatrix} #1 \end{smallmatrix}\right)} \newcommand{\sbmat}[1]{\left[\begin{smallmatrix} #1 \end{smallmatrix}\right]} \newcommand{\RR}{\mathbb{R}} \newcommand{\CC}{\mathbb{C}} \newcommand{\eye}{\mat{I}} \newcommand{\mA}{\mat{A}} \newcommand{\mB}{\mat{B}} \newcommand{\mC}{\mat{C}} \newcommand{\mD}{\mat{D}} \newcommand{\mE}{\mat{E}} \newcommand{\mF}{\mat{F}} \newcommand{\mG}{\mat{G}} \newcommand{\mH}{\mat{H}} \newcommand{\mI}{\mat{I}} \newcommand{\mJ}{\mat{J}} \newcommand{\mK}{\mat{K}} \newcommand{\mL}{\mat{L}} \newcommand{\mM}{\mat{M}} \newcommand{\mN}{\mat{N}} \newcommand{\mO}{\mat{O}} \newcommand{\mP}{\mat{P}} \newcommand{\mQ}{\mat{Q}} \newcommand{\mR}{\mat{R}} \newcommand{\mS}{\mat{S}} \newcommand{\mT}{\mat{T}} \newcommand{\mU}{\mat{U}} \newcommand{\mV}{\mat{V}} \newcommand{\mW}{\mat{W}} \newcommand{\mX}{\mat{X}} \newcommand{\mY}{\mat{Y}} \newcommand{\mZ}{\mat{Z}} \newcommand{\mLambda}{\mat{\Lambda}} \newcommand{\mSigma}{\ensuremath{\mat{\Sigma}}} \newcommand{\mPbar}{\bar{\mP}} \newcommand{\ones}{\vec{e}} \newcommand{\va}{\vec{a}} \newcommand{\vb}{\vec{b}} \newcommand{\vc}{\vec{c}} \newcommand{\vd}{\vec{d}} \newcommand{\ve}{\vec{e}} \newcommand{\vf}{\vec{f}} \newcommand{\vg}{\vec{g}} \newcommand{\vh}{\vec{h}} \newcommand{\vi}{\vec{i}} \newcommand{\vj}{\vec{j}} \newcommand{\vk}{\vec{k}} \newcommand{\vl}{\vec{l}} \newcommand{\vm}{\vec{l}} \newcommand{\vn}{\vec{n}} \newcommand{\vo}{\vec{o}} \newcommand{\vp}{\vec{p}} \newcommand{\vq}{\vec{q}} \newcommand{\vr}{\vec{r}} \newcommand{\vs}{\vec{s}} \newcommand{\vt}{\vec{t}} \newcommand{\vu}{\vec{u}} \newcommand{\vv}{\vec{v}} \newcommand{\vw}{\vec{w}} \newcommand{\vx}{\vec{x}} \newcommand{\vy}{\vec{y}} \newcommand{\vz}{\vec{z}} \newcommand{\vpi}{\vecalt{\pi}} \newcommand{\vlambda}{\vecalt{\lambda}}$

# Homework 1

Please answer the following questions in complete sentences in a clearly prepared manuscript and submit the solution by the due date on Blackboard (around Sunday, September 2nd, 2018.)

Remember that this is a graduate class. There may be elements of the problem statements that require you to fill in appropriate assumptions. You are also responsible for determining what evidence to include. An answer alone is rarely sufficient, but neither is an overly verbose description required. Use your judgement to focus your discussion on the most interesting pieces. The answer to "should I include 'something' in my solution?" will almost always be: Yes, if you think it helps support your answer.

## Problem 0: Homework checklist

• Please identify anyone, whether or not they are in the class, with whom you discussed your homework. This problem is worth 1 point, but on a multiplicative scale.

• Make sure you have included your source-code and prepared your solution according to the most recent Piazza note on homework submissions.

## Problem 1: Operations

Compute the following by hand or using Julia. The vector $\ve = \bmat{1 & \ldots & 1}^T$ (i.e. the all ones vector).

1. $\bmat{ 1 & 1 & 2 \\ 3 & 5 & 8 \\ 13 & 21 & 34 } \bmat{ 1 & -2 & 3 \\ -4 & 5 & -6 \\ 7 & -8 & 9} =$ ?

2. $\vx =$ ones(1000,1) $\vy =$ 1:1000 $\vx^T \vy =$ ?

(Optional extra question -- worth no points -- who is always credited with discovering a very efficient way to compute this as a child?)

Numpy users $\vx =$ ones((1000,1)) $\vy =$ arange(1.,1001.)[:, newaxis]'

3. $\vx = \bmat{ 1.5 & 2 & -3}^T$. (Assume $\ve$ is $4 \times 1$.)
$\ve \vx^T =$?
$\vx \ve^T =$?

4. $\vx = \bmat{-5 & 4 & 2}^T$. (Assume $\ve_i$ is $3 \times 1$.) $\ve_1 \vx^T =$?
$\vx \ve_3^T =$?

## Problem 2: A proof

Let $\mA$ and $\mC$ be invertible matrices. We'll prove that the inverse of $\bmat{ \mA & \mB \\ 0 & \mC }$ is easy to determine!

1. Show that the inverse of is

2. Now, show that the inverse of is

3. Recall that for general $\mA$ and $\mB$, not those in the problem!, $(\mA \mB)^{-1} = \mB^{-1} \mA^{-1}$. Use this fact, and the result of problem 2.2 to determine the inverse to $\bmat{ \mA & \mB \\ 0 & \mC }$ when $\mA$ and $\mC$ are invertible. Alternatively, give the inverse of $\bmat{ \mA & \mB \\ 0 & \mC }$. Hint: think about diagonal matrices!

## Problem 3: Simplifying a matrix expression

The following derivation occured when I was working on a proof with a student that we recently used in a research paper. We have an expression: where $\mH$ is a square, non-negative matrix and (which is a diagonal matrix where $\vz$ is on the diagonal). We needed to show that: for some matrix $\mC$ that depends on $\vy$. Your goal in this problem is to work out an expression for $\mC$.

This entire problem can be done element-wise, but the proof and results are fairly simple if you embrace matrix notations.

1. (Very easy.) As a warm-up, show that if $\mF$ and $\mG$ are square diagonal matrices, then $\mF \mG = \mG \mF$.

2. Show that $\vq(\vx) = \text{Diag}(\vx) \vx - \text{Diag}(\mH \vx) \mH \vx$. (Note that you need to use some properties of diagonal matrices in order to show this.)

3. Write an expression of $\mC$ in terms of $\vy$ using the simplified version from part 2.

## Problem 4: Deep neural nets

This problem will be more difficult if you haven't used Julia or Matlab before, so get started early! It's designed to teach you about writing for loops to construct a matrix operation for a particular task.

Deep neural networks for image recognition are a new technology that demonstrates impressive results. In this problem, we will build matrices that let us simulate what would happen for a random deep neural net.

A deep neural network is a sequence of matrix-vector products and non-linear functions. Let $\vx$ represent the input to the neural net. Then a deep net computes a function such as: where $f$ is a non-linear function and $\mW_i$ is a weight matrix where $\mW_i$ has fewer rows that columns. (So the output of $\mW_i \vx$ is a smaller vector than the input.) These are called deep neural networks because the number of layers $k$ is usually large (hundreds). A popular choice for $f$ is the function: which is called the ReLU (rectified linear unit). (These units model a "neuron" that "activates" whenever $x$ is positive and is non-active when $x$ is non-positive.) Also, the function could vary with the layer. Other choices involved in deep neural net architecture are: how many layers (or how many functions and weight matrices)? what is the shape of a weight matrix? We don't wish to get into too many of the details here. We are going to have you evaluate a simple neural network architecture.

Given an input image as a $32 \times 32$ pixel image, where each pixel is a real-valued number between $0$ and $1$, we want to simulate the following neural network architecture: where

• $\mW_1$ is a $256 \times 1024$ matrix that is a rudimentary edge-detector, the result of $f(\mW_1 \vx)$ should be large if there is an edge (see below for how to do this);
• $f$ is a ReLU unit;
• $\mW_2$ is another $64 \times 256$ matrix which is the same type of edge detector.
• $\mW_3$ is a $1 \times 64$ matrix that simply sums the output of all the inputs. (Hence, $\mW_3 = \ve^T$ where $\ve \in \RR^{64}$.)

So the challenge here is to build $\mW_1$ and $\mW_2$. As mentioned, these are extremely crude edge-detectors. (There are much better things you can do, but I wanted to keep this fairly simple.)

We'll illustrate the construction of our edge detector with a $4 \times 4$ image. Suppose this input is represented by a matrix: What we want to do is output a $2 \times 2$ result: where This particular formula comes from applying a $2 \times 2$ computational stencil: to each $2 \times 2$ region of the input image to reduce it to a single number. (As mentioned a few times, if this number is positive, it means there is an edge or high-contrast region somewhere inside that $2 \times 2$ block, but there are much better ways to solve this problem! This is really just a simple example.)

Now, I described the input and output in terms of matrices above. However, the deep neural network architecture works with a vector input and produces a vector output. So we have to rework this a little bit.

1. Note that the input and output matrices have a linear ordering $x_1, \ldots, x_{16}$ and $y_1, \ldots, y_4$. Let $\vx \in \RR^{16}$ and $\vy \in \RR^{4}$. Write down the matrix $\mA$ such that $\vy = \mA \vx$.

(Before you do the next step, you may have to add the following packages)

using Pkg; Pkg.add(["Images","FileIO","ImageMagick"]) # on Linux
using Pkg; Pkg.add(["Images","FileIO","QuartzImageIO"]) # on OSX


Load these images in Julia and convert them into matrices.

using FileIO, Images


Report the result of

using LinearAlgebra
tr(X1+X2+X3) # this computed the trace


If you wish to look at the images (not required) then run

colorview(Gray, X1)


Alternatively, you can use

using Plots
pyplot()
heatmap(X1,yflip=true, color=:gray)
gui()

3. In what follows, we'll talk about two different types of indices. The image index of a pixel is a pair $(i,j)$ that identifies a row and column for the pixel in the image. The vector index of a pixel is the index of that pixel in a linear ordering of the image elements. For instance, in the sample from part 1, pixel (3,2) has linear index $7$. Also, pixel (1,4) has index $13$. Julia can help us built a map between pixel indices and linear or vector indices:

N = reshape(1:(4*4), 4, 4)


This creates the pixel index to linear index for the problem above because

N[1,4]
N[3,2]


return the appropriate entry.

In your own words, explain what the reshape operation does.

4. Now we need to construct the matrix $\mW_i$ in order to apply the edge detector that we've build.
I'm giving you the following template, that I hope you can fill in. Feel free to construct $\mW_{1}$ and $\mW_{2}$ any way you choose, but the following should provide some guidance.

function crude_edge_detector(nin,nout)
Nx = <fill in> # build a map using reshape that takes X indices to x
Ny = <fill in>
W = zeros(nout^2,nin^2)
for i=1:nin
for j=1:nin
xi = <fill in>
yj = <fill in>
W[yj, xi] = <fill in>
end
end
return W
end

W1 = crude_edge_detector(32,16)
W2 = crude_edge_detector(16,8)


Show the non-zero entries in the first row of $W2$ as well as the corresponding indices.

5. Now write a function to evaluate the neural net output on an image that we explained above. Note, your code should not recompute the edge detectors W1 and W2 each time, doing so will lose 1/3 the points on this question.

function net(x)
<fill in multiple lines>
end


To call net, we need convert an image into a vector. You can use the reshape command to do this, or in Julia, you can use the vec command too.

Show the results of the following commands

@show net(vec(Float64.(load("image1.png"))))


(Hint, I get net(vec(Float64.(load("image1.png")))) = 0.08235294117647171) The original images can be accessed from

• https://c1.staticflickr.com/8/7015/6554001581_3370ca8802_b.jpg
• https://c1.staticflickr.com/3/2880/33053003793_5840a879fb_b.jpg
• https://c1.staticflickr.com/5/4138/4814209463_10a00d0b2d_b.jpg

Do these results make sense?

6. Now suppose we change the edge detector to use the stencil instead. Show the output from the same neural net architecture using the new matrices $W_1$ and $W_2$.

7. The matrices $W1$ and $W2$ have very few entries compared to the number of zeros. This is a case where we could consider using sparse matrices instead of dense matrices. One efficient way of creating a sparse matrix in Julia is to produce a list of the elements that are non-zero, and then to use the matrix. Write the following function

using SparseArrays
function sparse_crude_edge_detector(nin,nout)
Nx = <fill in> # build a map using reshape that takes X indices to x
Ny = <fill in>
nnz = <fill in> # this is the number of non-zeros
I = zeros(Int, nnz) # the row index
J = zeros(Int, nnz) # the column index
V = zeros(nnz) # the value
index = 1
for i=1:nin
for j=1:nin
I[index] = <fill in>
J[index] = <fill in>
V[index] = <fill in>
index += 1
end
end
return sparse(I,J,V,nout^2,nin^2)
end


Write this function and make sure that the result is equivalent to what you got with your original function.

sW1 = sparse_crude_edge_detector(32,16)
W1 = crude_edge_detector(32,16)
sW1 == W1 # test for equality

8. Let x = vec(Float64.(load("image1.png"))). Then show the time to compute W1*x vs. sW1*x. (In Julia, this is @time). Repeat this a few times to make sure you have the correct time. Report the fastest time for each.