a logo for the course

Matrix Computations

David Gleich

Purdue University

Fall 2017

Course number CS-51500

Tuesday and Thursday, 10:30-11:45am

Location Forney B124


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 3rd, 2017.)

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: A statistical test

This problem will be more difficult if you haven't used Julia or Matlab before, so get started early! It's designed to give you some simple experience.

One way of viewing a rank-1 matrix is as any matrix that can be written as a single outer-product, that is, for some and some .

In this problem, we'll look at the expected value of the random matrix where is a random rank-1 matrix following a simple probability distribution.

  1. The first question is just to get you to document your initial impressions and is not worth any points. Suppose that and are length vectors with elements drawn from a standard normal distribution. Let . Then, let , where is the expected-value operator. What do you think the rank of is (guessing is fine!)? (Remember this isn't worth any points, so don't think about it too long or get hung up on small details.)

  2. Now, let's check your guess! We can approximate using what's called a Monte-Carlo approximation. Suppose that is a single matrix generated where and are instances of length- vectors with elements drawn from a random normal. Then the Monte Carlo approximation to is given by: We can generate and in Matlab/Numpy/Julia via

    Matlab

    x = randn(n,1); 
    y = randn(n,1);
    

    Numpy/Python

    import numpy
    x = numpy.random.randn(n,1) # Numpy
    y = numpy.random.randn(n,1)
    

    Julia

    x = randn(n); # Julia
    y = randn(n);
    

    Using your language of choice, evaluate where and and report the rank.

  3. Did you find the rank surprising given your initial guess? If so, comment on which you think is correct, your code (2) or your guess (1). (Hint: your code should be correct, but maybe you don't trust an approximation?)

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

    Load these images in Julia and convert them into matrices.

    using FileIO
    X1 = Float64.(load("image1.png"))
    X2 = Float64.(load("image2.png"))
    X3 = Float64.(load("image3.png"))
    

    Report the result of

    trace(X1+X2+X3)
    

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

    colorview(Gray, X1)
    
  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

    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 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>
        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 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"))))
    @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 .