a logo for the course

Matrix Computations

David Gleich

Purdue University

Fall 2018

Course number CS-51500

Tuesday and Thursday, 3:00-4:15pm

Location Forney G124

Homework 1

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

Problem 1: Operations

Compute the following by hand or using Julia. The vector (i.e. the all ones vector).

  1. ?

  2. ones(1000,1) 1:1000 ?

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

    Numpy users ones((1000,1)) arange(1.,1001.)[:, newaxis]'

  3. . (Assume is .)

  4. . (Assume is .) ?

Problem 2: A proof

Let and be invertible matrices. We'll prove that the inverse of is easy to determine!

  1. Show that the inverse of is

  2. Now, show that the inverse of is

  3. Recall that for general and , not those in the problem!, . Use this fact, and the result of problem 2.2 to determine the inverse to when and are invertible. Alternatively, give the inverse of . 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 is a square, non-negative matrix and (which is a diagonal matrix where is on the diagonal). We needed to show that: for some matrix that depends on . Your goal in this problem is to work out an expression for .

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 and are square diagonal matrices, then .

  2. Show that . (Note that you need to use some properties of diagonal matrices in order to show this.)

  3. Write an expression of in terms of 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 represent the input to the neural net. Then a deep net computes a function such as: where is a non-linear function and is a weight matrix where has fewer rows that columns. (So the output of is a smaller vector than the input.) These are called deep neural networks because the number of layers is usually large (hundreds). A popular choice for is the function: which is called the ReLU (rectified linear unit). (These units model a "neuron" that "activates" whenever is positive and is non-active when 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 pixel image, where each pixel is a real-valued number between and , we want to simulate the following neural network architecture: where

So the challenge here is to build and . 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 image. Suppose this input is represented by a matrix: What we want to do is output a result: where This particular formula comes from applying a computational stencil: to each 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 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 and . Let and . Write down the matrix such that .

  2. We will want to run this on real images. Download

    (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
    X1 = Float64.(load("image1.png"))
    X2 = Float64.(load("image2.png"))
    X3 = Float64.(load("image3.png"))

    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
    heatmap(X1,yflip=true, color=:gray)
  3. In what follows, we'll talk about two different types of indices. The image index of a pixel is a pair 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 . Also, pixel (1,4) has index . 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


    return the appropriate entry.

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

  4. Now we need to construct the matrix 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 and 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>
      return W
    W1 = crude_edge_detector(32,16)
    W2 = crude_edge_detector(16,8)

    Show the non-zero entries in the first row of 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>

    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"))))
    @show net(vec(Float64.(load("image2.png"))))
    @show net(vec(Float64.(load("image3.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 and .

  7. The matrices and 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
      return sparse(I,J,V,nout^2,nin^2)

    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.